From e8dc7a6d0702c6724c4b3b9a4cf8efb7c6f712c9 Mon Sep 17 00:00:00 2001 From: Andree Buschmann Date: Sat, 10 Sep 2011 02:52:12 +0000 Subject: Migrate some floating point code to fixed point in libgme. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30493 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/libgme/emu8950.c | 4 ++-- apps/codecs/libgme/emu8950.h | 6 +++--- apps/codecs/libgme/emuadpcm.c | 15 +++++++-------- apps/codecs/nsf.c | 2 +- apps/codecs/vgm.c | 2 +- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/apps/codecs/libgme/emu8950.c b/apps/codecs/libgme/emu8950.c index e75bf66d64..2f8a32044f 100644 --- a/apps/codecs/libgme/emu8950.c +++ b/apps/codecs/libgme/emu8950.c @@ -494,8 +494,8 @@ void OPL_setSampleRate(struct Y8950* this, int sampleRate, int clockRate) makeDphaseARTable(sampleRate, clockRate); makeDphaseDRTable(sampleRate, clockRate); makeDphaseNoiseTable(sampleRate, clockRate); - this->pm_dphase = rate_adjust(PM_SPEED * PM_DP_WIDTH / (clockRate/72), sampleRate, clockRate); - this->am_dphase = rate_adjust(AM_SPEED * AM_DP_WIDTH / (clockRate/72), sampleRate, clockRate); + this->pm_dphase = rate_adjust( (int)(PM_SPEED * PM_DP_WIDTH) / (clockRate/72), sampleRate, clockRate); + this->am_dphase = rate_adjust( (int)(AM_SPEED * AM_DP_WIDTH) / (clockRate/72), sampleRate, clockRate); } // Reset whole of opl except patch datas. diff --git a/apps/codecs/libgme/emu8950.h b/apps/codecs/libgme/emu8950.h index 88d17b956e..02169050d2 100644 --- a/apps/codecs/libgme/emu8950.h +++ b/apps/codecs/libgme/emu8950.h @@ -238,11 +238,11 @@ void OPL_changeStatusMask(struct Y8950* this_, byte newMask); // Adjust envelope speed which depends on sampling rate -static inline unsigned int rate_adjust(double x, int rate, int clk) +static inline unsigned int rate_adjust(int x, int rate, int clk) { - double tmp = x * clk / 72 / rate + 0.5; // +0.5 to round + unsigned int tmp = (long long)x * clk / 72 / rate; // assert (tmp <= 4294967295U); - return (unsigned int)tmp; + return tmp; } #endif diff --git a/apps/codecs/libgme/emuadpcm.c b/apps/codecs/libgme/emuadpcm.c index d85aba3488..b426a74de5 100644 --- a/apps/codecs/libgme/emuadpcm.c +++ b/apps/codecs/libgme/emuadpcm.c @@ -117,7 +117,7 @@ void restart(struct Y8950Adpcm* this_) this_->diff = DDEF; this_->nextLeveling = 0; this_->sampleStep = 0; - this_->volumeWStep = (int)((double)this_->volume * this_->step / MAX_STEP); + this_->volumeWStep = (int)((long long)this_->volume * this_->step / MAX_STEP); } void ADPCM_writeReg(struct Y8950Adpcm* this_, byte rg, byte data) @@ -176,23 +176,22 @@ void ADPCM_writeReg(struct Y8950Adpcm* this_, byte rg, byte data) case 0x10: // DELTA-N (L) this_->delta = (this_->delta & 0xFF00) | data; this_->step = rate_adjust(this_->delta<sampleRate, this_->clockRate); - this_->volumeWStep = (int)((double)this_->volume * this_->step / MAX_STEP); + this_->volumeWStep = (int)((long long)this_->volume * this_->step / MAX_STEP); break; case 0x11: // DELTA-N (H) this_->delta = (this_->delta & 0x00FF) | (data << 8); this_->step = rate_adjust(this_->delta<sampleRate, this_->clockRate); - this_->volumeWStep = (int)((double)this_->volume * this_->step / MAX_STEP); + this_->volumeWStep = (int)((long long)this_->volume * this_->step / MAX_STEP); break; case 0x12: { // ENVELOP CONTROL int oldVol = this_->volume; this_->volume = (data * ADPCM_VOLUME) >> 8; if (oldVol != 0) { - double factor = (double)this_->volume / (double)oldVol; - this_->output = (int)((double)this_->output * factor); - this_->sampleStep = (int)((double)this_->sampleStep * factor); + this_->output = (int)(((long long)this_->output * this_->volume) / oldVol); + this_->sampleStep = (int)(((long long)this_->sampleStep * this_->volume) / oldVol); } - this_->volumeWStep = (int)((double)this_->volume * this_->step / MAX_STEP); + this_->volumeWStep = (int)((long long)this_->volume * this_->step / MAX_STEP); break; } case 0x0D: // PRESCALE (L) @@ -290,7 +289,7 @@ int ADPCM_calcSample(struct Y8950Adpcm* this_) /* TODO: Used fixed point math here */ #if !defined(ROCKBOX) - this_->output += (int)((double)this_->sampleStep * ((double)this_->nowStep/(double)this_->step)); + this_->output += (int)(((long long)this_->sampleStep * this_->nowStep) / this_->step); #endif } this_->output += this_->sampleStep; diff --git a/apps/codecs/nsf.c b/apps/codecs/nsf.c index 7214b5d2a2..4c5b37c3fa 100644 --- a/apps/codecs/nsf.c +++ b/apps/codecs/nsf.c @@ -126,7 +126,7 @@ next_track: /* Set elapsed time for one track files */ if (is_multitrack == 0) { - elapsed_time += (CHUNK_SIZE / 2) / 44.1; + elapsed_time += (CHUNK_SIZE / 2) * 10 / 441; ci->set_elapsed(elapsed_time); } } diff --git a/apps/codecs/vgm.c b/apps/codecs/vgm.c index d7390291e1..416f772f1d 100644 --- a/apps/codecs/vgm.c +++ b/apps/codecs/vgm.c @@ -134,7 +134,7 @@ enum codec_status codec_run(void) ci->pcmbuf_insert(samples, NULL, CHUNK_SIZE >> 1); - elapsed_time += (CHUNK_SIZE / 2) / 44.1; + elapsed_time += (CHUNK_SIZE / 2) * 10 / 441; ci->set_elapsed(elapsed_time); } -- cgit v1.2.3