From 9296cfd334f93c86d26d610d1956471c1fd5f1b2 Mon Sep 17 00:00:00 2001 From: zsilencer Date: Wed, 29 Apr 2015 17:29:37 -0600 Subject: [PATCH 1/2] fix audio panning --- src/audio/mixer.cpp | 29 +++++++++++++++++++---------- src/audio/mixer.h | 1 + src/ride/ride.c | 38 ++++++++++++++++++++------------------ 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/audio/mixer.cpp b/src/audio/mixer.cpp index 247e1aa395..fa2d1d1109 100644 --- a/src/audio/mixer.cpp +++ b/src/audio/mixer.cpp @@ -341,6 +341,8 @@ Channel::Channel() SetRate(1); SetVolume(SDL_MIX_MAXVOLUME); oldvolume = 0; + oldvolume_l = 0; + oldvolume_r = 0; SetPan(0.5f); done = true; stopping = false; @@ -395,8 +397,15 @@ void Channel::SetPan(float pan) if (pan < 0) { Channel::pan = 0; } - volume_l = (float)sin((1.0 - Channel::pan) * M_PI / 2.0); - volume_r = (float)sin(Channel::pan * M_PI / 2.0); + double decibels = (abs(Channel::pan - 0.5) * 2.0) * 100.0; + double attenuation = pow(10, decibels / 20.0); + if (Channel::pan <= 0.5) { + volume_l = 1.0; + volume_r = float(1.0 / attenuation); + } else { + volume_r = 1.0; + volume_l = float(1.0 / attenuation); + } } bool Channel::IsPlaying() @@ -692,6 +701,8 @@ void Mixer::MixChannel(Channel& channel, uint8* data, int length) } while(loaded < length && channel.loop != 0 && !channel.stopping); channel.oldvolume = channel.volume; + channel.oldvolume_l = channel.volume_l; + channel.oldvolume_r = channel.volume_r; if (channel.loop == 0 && channel.offset >= channel.source->Length()) { channel.done = true; } @@ -700,21 +711,19 @@ void Mixer::MixChannel(Channel& channel, uint8* data, int length) void Mixer::EffectPanS16(Channel& channel, sint16* data, int length) { - float left = channel.volume_l; - float right = channel.volume_r; for (int i = 0; i < length * 2; i += 2) { - data[i] = (sint16)(data[i] * left); - data[i + 1] = (sint16)(data[i + 1] * right); + float t = (float)i / (length * 2); + data[i] = (sint16)(data[i] * ((1.0 - t) * channel.oldvolume_l + t * channel.volume_l)); + data[i + 1] = (sint16)(data[i + 1] * ((1.0 - t) * channel.oldvolume_r + t * channel.volume_r)); } } void Mixer::EffectPanU8(Channel& channel, uint8* data, int length) { - float left = channel.volume_l; - float right = channel.volume_r; for (int i = 0; i < length * 2; i += 2) { - data[i] = (uint8)(data[i] * left); - data[i + 1] = (uint8)(data[i + 1] * right); + float t = (float)i / (length * 2); + data[i] = (uint8)(data[i] * ((1.0 - t) * channel.oldvolume_l + t * channel.volume_l)); + data[i + 1] = (uint8)(data[i + 1] * ((1.0 - t) * channel.oldvolume_r + t * channel.volume_r)); } } diff --git a/src/audio/mixer.h b/src/audio/mixer.h index e62dd3cad0..6575401262 100644 --- a/src/audio/mixer.h +++ b/src/audio/mixer.h @@ -133,6 +133,7 @@ private: double rate; int volume; float volume_l, volume_r; + float oldvolume_l, oldvolume_r; float pan; bool done; bool deleteondone; diff --git a/src/ride/ride.c b/src/ride/ride.c index a703dbf104..395223874e 100644 --- a/src/ride/ride.c +++ b/src/ride/ride.c @@ -2447,32 +2447,34 @@ int ride_music_params_update(sint16 x, sint16 y, sint16 z, uint8 rideIndex, uint uint8 vol1 = -1; uint8 vol2 = -1; - if (pany < 0) { - pany = -pany; + int panx2 = panx; + int pany2 = pany; + if (pany2 < 0) { + pany2 = -pany2; } - if (pany > 6143) { - pany = 6143; + if (pany2 > 6143) { + pany2 = 6143; } - pany -= 2048; - if (pany > 0) { - pany = -((pany / 4) - 1024) / 4; - vol1 = (uint8)pany; - if (pany >= 256) { + pany2 -= 2048; + if (pany2 > 0) { + pany2 = -((pany2 / 4) - 1024) / 4; + vol1 = (uint8)pany2; + if (pany2 >= 256) { vol1 = -1; } } - if (panx < 0) { - panx = -panx; + if (panx2 < 0) { + panx2 = -panx2; } - if (panx > 6143) { - panx = 6143; + if (panx2 > 6143) { + panx2 = 6143; } - panx -= 2048; - if (panx > 0) { - panx = -((panx / 4) - 1024) / 4; - vol2 = (uint8)panx; - if (panx >= 256) { + panx2 -= 2048; + if (panx2 > 0) { + panx2 = -((panx2 / 4) - 1024) / 4; + vol2 = (uint8)panx2; + if (panx2 >= 256) { vol2 = -1; } } From bcad8c200eae8cd1b085e7ec7b9249a20a385845 Mon Sep 17 00:00:00 2001 From: zsilencer Date: Wed, 6 May 2015 19:14:41 -0600 Subject: [PATCH 2/2] #1042 --- src/audio/mixer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/audio/mixer.cpp b/src/audio/mixer.cpp index fa2d1d1109..87105a355c 100644 --- a/src/audio/mixer.cpp +++ b/src/audio/mixer.cpp @@ -268,8 +268,10 @@ bool Source_SampleStream::LoadWAV(SDL_RWops* rw) log_verbose("Could not find FMT chunk"); return false; } + Uint64 chunkstart = SDL_RWtell(rw); PCMWAVEFORMAT waveformat; SDL_RWread(rw, &waveformat, sizeof(waveformat), 1); + SDL_RWseek(rw, chunkstart + fmtchunk_size, RW_SEEK_SET); if (waveformat.wf.wFormatTag != WAVE_FORMAT_PCM) { log_verbose("Not in proper format"); return false;