From 1b9f9fb4651c9af36ca0bf21746c211afd90a916 Mon Sep 17 00:00:00 2001 From: Andree Buschmann Date: Thu, 11 Aug 2011 21:06:16 +0000 Subject: 5th part of FS#12176. Further fixed point migration. Only two emulators (ym2413, ym2612) still use floating point. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30281 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/libgme/hes_apu_adpcm.c | 8 ++++---- apps/codecs/libgme/hes_apu_adpcm.h | 2 +- apps/codecs/libgme/nes_apu.c | 2 +- apps/codecs/libgme/nes_vrc7_apu.c | 2 +- apps/codecs/libgme/nes_vrc7_apu.h | 2 +- apps/codecs/libgme/nsf_emu.c | 8 ++++---- apps/codecs/libgme/nsf_emu.h | 1 - apps/codecs/libgme/resampler.c | 18 ++++++++---------- apps/codecs/libgme/resampler.h | 4 ++-- apps/codecs/libgme/sms_fm_apu.c | 4 ++-- apps/codecs/libgme/sms_fm_apu.h | 2 +- apps/codecs/libgme/vgm_emu.c | 14 ++++++-------- apps/codecs/libgme/vgm_emu.h | 2 +- apps/codecs/libgme/ym2413_emu.c | 2 +- apps/codecs/libgme/ym2413_emu.h | 2 +- apps/codecs/libgme/ym2612_emu.c | 6 +++--- apps/codecs/libgme/ym2612_emu.h | 2 +- 17 files changed, 38 insertions(+), 43 deletions(-) diff --git a/apps/codecs/libgme/hes_apu_adpcm.c b/apps/codecs/libgme/hes_apu_adpcm.c index b2f78ff71f..66512dc702 100644 --- a/apps/codecs/libgme/hes_apu_adpcm.c +++ b/apps/codecs/libgme/hes_apu_adpcm.c @@ -107,7 +107,7 @@ static void Adpcm_run_until( struct Hes_Apu_Adpcm* this, blip_time_t end_time ) int fadetimer = state->fadetimer; int fadecount = state->fadecount; int last_time = this->last_time; - double next_timer = this->next_timer; + int next_timer = this->next_timer; int last_amp = this->last_amp; struct Blip_Buffer* output = this->output; // cache often-used values @@ -129,7 +129,7 @@ static void Adpcm_run_until( struct Hes_Apu_Adpcm* this, blip_time_t end_time ) volume = 0xFF - ( 0xFF * fadecount / fadetimer ); } } - next_timer += 7159.091; + next_timer += 7159; // 7159091/1000; } int amp; if ( state->ad_low_nibble ) @@ -160,7 +160,7 @@ static void Adpcm_run_until( struct Hes_Apu_Adpcm* this, blip_time_t end_time ) if ( !state->playflag ) { - while ( next_timer <= end_time ) next_timer += 7159.091; + while ( next_timer <= end_time ) next_timer += 7159; // 7159091/1000 last_time = end_time; } @@ -290,7 +290,7 @@ void Adpcm_end_frame( struct Hes_Apu_Adpcm* this, blip_time_t end_time ) { Adpcm_run_until( this, end_time ); this->last_time -= end_time; - this->next_timer -= (double)end_time; + this->next_timer -= end_time; check( last_time >= 0 ); if ( this->output ) Blip_set_modified( this->output ); diff --git a/apps/codecs/libgme/hes_apu_adpcm.h b/apps/codecs/libgme/hes_apu_adpcm.h index 38b8839276..d8d024ee34 100644 --- a/apps/codecs/libgme/hes_apu_adpcm.h +++ b/apps/codecs/libgme/hes_apu_adpcm.h @@ -43,7 +43,7 @@ struct Hes_Apu_Adpcm { struct Blip_Buffer* output; blip_time_t last_time; - double next_timer; + int next_timer; int last_amp; }; diff --git a/apps/codecs/libgme/nes_apu.c b/apps/codecs/libgme/nes_apu.c index fae799d8d6..d4e6e8df4d 100644 --- a/apps/codecs/libgme/nes_apu.c +++ b/apps/codecs/libgme/nes_apu.c @@ -46,7 +46,7 @@ void Apu_init( struct Nes_Apu* this ) void Apu_enable_nonlinear( struct Nes_Apu* this, int v ) { this->dmc.nonlinear = true; - Synth_volume( &this->square_synth, (int)((1.3 * 0.25751258 / 0.742467605 * 0.25 * FP_ONE_VOLUME) / amp_range * v) ); + Synth_volume( &this->square_synth, (int)((long long)(1.3 * 0.25751258 / 0.742467605 * 0.25 * FP_ONE_VOLUME) / amp_range * v) ); const int tnd = (int)(0.48 / 202 * 0.75 * FP_ONE_VOLUME); Synth_volume( &this->triangle.synth, 3 * tnd ); diff --git a/apps/codecs/libgme/nes_vrc7_apu.c b/apps/codecs/libgme/nes_vrc7_apu.c index 6148a6b90f..c9e638e3d8 100644 --- a/apps/codecs/libgme/nes_vrc7_apu.c +++ b/apps/codecs/libgme/nes_vrc7_apu.c @@ -29,7 +29,7 @@ void Vrc7_reset( struct Nes_Vrc7_Apu* this ) OPLL_setMask(&this->opll, this->mask); } -void Vrc7_set_rate( struct Nes_Vrc7_Apu* this, double r ) +void Vrc7_set_rate( struct Nes_Vrc7_Apu* this, int r ) { OPLL_set_quality( &this->opll, r < 44100 ? 0 : 1 ); OPLL_set_rate( &this->opll, (e_uint32)r ); diff --git a/apps/codecs/libgme/nes_vrc7_apu.h b/apps/codecs/libgme/nes_vrc7_apu.h index aa8bb7648c..b37dfd63b0 100644 --- a/apps/codecs/libgme/nes_vrc7_apu.h +++ b/apps/codecs/libgme/nes_vrc7_apu.h @@ -27,7 +27,7 @@ struct Nes_Vrc7_Apu { // See Nes_Apu.h for reference void Vrc7_init( struct Nes_Vrc7_Apu* this ); void Vrc7_reset( struct Nes_Vrc7_Apu* this ); -void Vrc7_set_rate( struct Nes_Vrc7_Apu* this, double r ); +void Vrc7_set_rate( struct Nes_Vrc7_Apu* this, int r ); void Vrc7_end_frame( struct Nes_Vrc7_Apu* this, blip_time_t ) ICODE_ATTR; void Vrc7_write_reg( struct Nes_Vrc7_Apu* this, int reg ) ICODE_ATTR; diff --git a/apps/codecs/libgme/nsf_emu.c b/apps/codecs/libgme/nsf_emu.c index 1b8d98e51a..79a15e21f2 100644 --- a/apps/codecs/libgme/nsf_emu.c +++ b/apps/codecs/libgme/nsf_emu.c @@ -179,9 +179,9 @@ static bool pal_only( struct header_t* this ) return (this->speed_flags & 3) == 1; } -static double clock_rate( struct header_t* this ) +static long clock_rate( struct header_t* this ) { - return pal_only( this ) ? 1662607.125 : 1789772.727272727; + return pal_only( this ) ? (long)1662607.125 : (long)1789772.727272727; } static int play_period( struct header_t* this ) @@ -206,7 +206,7 @@ static int play_period( struct header_t* this ) // Custom rate if ( rate != value ) - clocks = (int) (rate * clock_rate( this ) * (1.0/1000000.0)); + clocks = (int) ((1LL * rate * clock_rate( this )) / 1000000); return clocks; } @@ -285,7 +285,7 @@ blargg_err_t Nsf_post_load( struct Nsf_Emu* this ) this->track_count = this->header.track_count; // Change clock rate & setup buffer - this->clock_rate__ = (long) (clock_rate( &this->header ) + 0.5); + this->clock_rate__ = (long) clock_rate( &this->header ); Buffer_clock_rate( &this->stereo_buf, this->clock_rate__ ); this->buf_changed_count = Buffer_channels_changed_count( &this->stereo_buf ); return 0; diff --git a/apps/codecs/libgme/nsf_emu.h b/apps/codecs/libgme/nsf_emu.h index 05b5e5a920..513443226c 100644 --- a/apps/codecs/libgme/nsf_emu.h +++ b/apps/codecs/libgme/nsf_emu.h @@ -112,7 +112,6 @@ struct Nsf_Emu { long silence_count; // number of samples of silence to play before using buf long buf_remain; // number of samples left in silence buffer - double clock_rate_; long clock_rate__; unsigned buf_changed_count; diff --git a/apps/codecs/libgme/resampler.c b/apps/codecs/libgme/resampler.c index d9dcea02c6..a69a92cc53 100644 --- a/apps/codecs/libgme/resampler.c +++ b/apps/codecs/libgme/resampler.c @@ -24,15 +24,13 @@ unsigned const resampler_extra = 34; enum { shift = 14 }; int const unit = 1 << shift; -blargg_err_t Resampler_setup( struct Resampler* this, double oversample, double rolloff, double gain ) -{ - (void) rolloff; - - this->gain_ = (int)((1 << gain_bits) * gain); - this->step = (int) ( oversample * unit + 0.5); - this->rate_ = 1.0 / unit * this->step; - return 0; -} +blargg_err_t Resampler_setup( struct Resampler* this, int fm_rate, int fm_gain, int rate, int gain ) + { + this->gain_ = (int)( ((1LL << gain_bits) * fm_gain * gain) / FP_ONE_GAIN ); + this->step = (int)( ((1LL << shift) * fm_rate) / rate + 1); + this->rate_ = this->step; + return 0; + } blargg_err_t Resampler_reset( struct Resampler* this, int pairs ) { @@ -52,7 +50,7 @@ void Resampler_resize( struct Resampler* this, int pairs ) if ( this->sample_buf_size != new_sample_buf_size ) { this->sample_buf_size = new_sample_buf_size; - this->oversamples_per_frame = (int) (pairs * this->rate_) * 2 + 2; + this->oversamples_per_frame = (int) ((pairs * this->rate_ * 2LL) / unit) + 2; Resampler_clear( this ); } } diff --git a/apps/codecs/libgme/resampler.h b/apps/codecs/libgme/resampler.h index f5e8c55119..741bdb50a4 100644 --- a/apps/codecs/libgme/resampler.h +++ b/apps/codecs/libgme/resampler.h @@ -31,7 +31,7 @@ struct Resampler { int buffer_size; int write_pos; - double rate_; + int rate_; int pos; int step; @@ -55,7 +55,7 @@ static inline void Resampler_set_callback(struct Resampler* this, int (*func)( v this->callback_data = user_data; } -blargg_err_t Resampler_setup( struct Resampler* this, double oversample, double rolloff, double gain ); +blargg_err_t Resampler_setup( struct Resampler* this, int fm_rate, int fm_gain, int rate, int gain ); static inline void Resampler_clear( struct Resampler* this ) { diff --git a/apps/codecs/libgme/sms_fm_apu.c b/apps/codecs/libgme/sms_fm_apu.c index 93689cd811..5240405ad1 100644 --- a/apps/codecs/libgme/sms_fm_apu.c +++ b/apps/codecs/libgme/sms_fm_apu.c @@ -8,9 +8,9 @@ void Fm_apu_create( struct Sms_Fm_Apu* this ) Ym2413_init( &this->apu ); } -blargg_err_t Fm_apu_init( struct Sms_Fm_Apu* this, double clock_rate, double sample_rate ) +blargg_err_t Fm_apu_init( struct Sms_Fm_Apu* this, int clock_rate, int sample_rate ) { - this->period_ = (blip_time_t) (clock_rate / sample_rate + 0.5); + this->period_ = (blip_time_t) (clock_rate / sample_rate); CHECK_ALLOC( !Ym2413_set_rate( &this->apu, sample_rate, clock_rate ) ); Fm_apu_set_output( this, 0 ); diff --git a/apps/codecs/libgme/sms_fm_apu.h b/apps/codecs/libgme/sms_fm_apu.h index f78af3065e..0dd882b0f4 100644 --- a/apps/codecs/libgme/sms_fm_apu.h +++ b/apps/codecs/libgme/sms_fm_apu.h @@ -24,7 +24,7 @@ struct Sms_Fm_Apu { void Fm_apu_create( struct Sms_Fm_Apu* this ); static inline bool Fm_apu_supported( void ) { return Ym2413_supported(); } -blargg_err_t Fm_apu_init( struct Sms_Fm_Apu* this, double clock_rate, double sample_rate ); +blargg_err_t Fm_apu_init( struct Sms_Fm_Apu* this, int clock_rate, int sample_rate ); static inline void Fm_apu_set_output( struct Sms_Fm_Apu* this, struct Blip_Buffer* b ) { diff --git a/apps/codecs/libgme/vgm_emu.c b/apps/codecs/libgme/vgm_emu.c index 82e30c0b0f..b442b3e84b 100644 --- a/apps/codecs/libgme/vgm_emu.c +++ b/apps/codecs/libgme/vgm_emu.c @@ -22,8 +22,6 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ const char* const gme_wrong_file_type = "Wrong file type for this emulator"; int const fm_gain = 3; // FM emulators are internally quieter to avoid 16-bit overflow -double const rolloff = 0.990; -double const oversample_factor = 1.5; int const silence_max = 6; // seconds int const silence_threshold = 0x10; @@ -310,7 +308,7 @@ blargg_err_t Vgm_load_mem( struct Vgm_Emu* this, byte const* new_data, long new_ } void update_fm_rates( struct Vgm_Emu* this, int* ym2413_rate, int* ym2612_rate ); -static blargg_err_t init_fm( struct Vgm_Emu* this, double* rate ) +static blargg_err_t init_fm( struct Vgm_Emu* this, int* rate ) { int ym2612_rate = get_le32( header( this )->ym2612_rate ); int ym2413_rate = get_le32( header( this )->ym2413_rate ); @@ -320,14 +318,14 @@ static blargg_err_t init_fm( struct Vgm_Emu* this, double* rate ) if ( ym2612_rate ) { if ( !*rate ) - *rate = ym2612_rate / 144.0; + *rate = ym2612_rate / 144; RETURN_ERR( Ym2612_set_rate( &this->ym2612, *rate, ym2612_rate ) ); Ym2612_enable( &this->ym2612, true ); } else if ( ym2413_rate ) { if ( !*rate ) - *rate = ym2413_rate / 72.0; + *rate = ym2413_rate / 72; int result = Ym2413_set_rate( &this->ym2413, *rate, ym2413_rate ); if ( result == 2 ) return "YM2413 FM sound not supported"; @@ -342,15 +340,15 @@ static blargg_err_t init_fm( struct Vgm_Emu* this, double* rate ) blargg_err_t setup_fm( struct Vgm_Emu* this ) { - double fm_rate = 0.0; + int fm_rate = 0; if ( !this->disable_oversampling ) - this->fm_rate = this->sample_rate * oversample_factor; + this->fm_rate = (this->sample_rate * 3) / 2; // oversample factor = 1.5 RETURN_ERR( init_fm( this, &fm_rate ) ); if ( uses_fm( this ) ) { this->voice_count = 8; - RETURN_ERR( Resampler_setup( &this->resampler, fm_rate / this->sample_rate, rolloff, fm_gain * (double)(this->gain)/FP_ONE_GAIN ) ); + RETURN_ERR( Resampler_setup( &this->resampler, fm_rate, fm_gain, this->sample_rate, this->gain ) ); RETURN_ERR( Resampler_reset( &this->resampler, Buffer_length( &this->stereo_buf ) * this->sample_rate / 1000 ) ); Sms_apu_volume( &this->psg, ((this->gain/5)-(this->gain*5)/1000) * fm_gain ); } diff --git a/apps/codecs/libgme/vgm_emu.h b/apps/codecs/libgme/vgm_emu.h index 5a0f1e5fa5..65993dbd5d 100644 --- a/apps/codecs/libgme/vgm_emu.h +++ b/apps/codecs/libgme/vgm_emu.h @@ -63,7 +63,7 @@ struct track_info_t // aliasing on high notes. Currently YM2413 support requires that you supply a // YM2413 sound chip emulator. I can provide one I've modified to work with the library. struct Vgm_Emu { - double fm_rate; + int fm_rate; long psg_rate; long vgm_rate; bool disable_oversampling; diff --git a/apps/codecs/libgme/ym2413_emu.c b/apps/codecs/libgme/ym2413_emu.c index 67870f31dc..9efd3dcc3d 100644 --- a/apps/codecs/libgme/ym2413_emu.c +++ b/apps/codecs/libgme/ym2413_emu.c @@ -7,7 +7,7 @@ void Ym2413_init( struct Ym2413_Emu* this ) this->last_time = disabled_time; this->out = 0; } -int Ym2413_set_rate( struct Ym2413_Emu* this, double sample_rate, double clock_rate ) +int Ym2413_set_rate( struct Ym2413_Emu* this, int sample_rate, int clock_rate ) { OPLL_new ( &this->opll, clock_rate, sample_rate ); OPLL_reset_patch( &this->opll, OPLL_2413_TONE ); diff --git a/apps/codecs/libgme/ym2413_emu.h b/apps/codecs/libgme/ym2413_emu.h index 71369e9c88..7efb8e2fab 100644 --- a/apps/codecs/libgme/ym2413_emu.h +++ b/apps/codecs/libgme/ym2413_emu.h @@ -25,7 +25,7 @@ static inline bool Ym2413_supported( void ) { return true; } // Sets output sample rate and chip clock rates, in Hz. Returns non-zero // if error. -int Ym2413_set_rate( struct Ym2413_Emu* this, double sample_rate, double clock_rate ); +int Ym2413_set_rate( struct Ym2413_Emu* this, int sample_rate, int clock_rate ); // Resets to power-up state void Ym2413_reset( struct Ym2413_Emu* this ); diff --git a/apps/codecs/libgme/ym2612_emu.c b/apps/codecs/libgme/ym2612_emu.c index 54930298c9..2162d73850 100644 --- a/apps/codecs/libgme/ym2612_emu.c +++ b/apps/codecs/libgme/ym2612_emu.c @@ -682,15 +682,15 @@ static void impl_set_rate( struct Ym2612_Impl* impl, double sample_rate, double impl_reset( impl ); } -const char* Ym2612_set_rate( struct Ym2612_Emu* this, double sample_rate, double clock_rate ) +const char* Ym2612_set_rate( struct Ym2612_Emu* this, int sample_rate, int clock_rate ) { // Only set rates if necessary #if defined(ROCKBOX) - static double last_sample_rate = 0.0, last_clock_rate = 0.0; + static int last_sample_rate = 0, last_clock_rate = 0; if (last_sample_rate == sample_rate && last_clock_rate == clock_rate) return 0; #endif memset( &this->impl.YM2612, 0, sizeof this->impl.YM2612 ); - impl_set_rate( &this->impl, sample_rate, clock_rate ); + impl_set_rate( &this->impl, (double)sample_rate, (double)clock_rate ); return 0; } diff --git a/apps/codecs/libgme/ym2612_emu.h b/apps/codecs/libgme/ym2612_emu.h index 19f0903d61..9ca1989ced 100644 --- a/apps/codecs/libgme/ym2612_emu.h +++ b/apps/codecs/libgme/ym2612_emu.h @@ -199,7 +199,7 @@ static inline void Ym2612_init( struct Ym2612_Emu* this_ ) // Sets sample rate and chip clock rate, in Hz. Returns non-zero // if error. If clock_rate=0, uses sample_rate*144 -const char* Ym2612_set_rate( struct Ym2612_Emu* this_, double sample_rate, double clock_rate ); +const char* Ym2612_set_rate( struct Ym2612_Emu* this_, int sample_rate, int clock_rate ); // Resets to power-up state void Ym2612_reset( struct Ym2612_Emu* this_ ); -- cgit v1.2.3