aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/qt_sdl/main.cpp
diff options
context:
space:
mode:
authorArisotura <thetotalworm@gmail.com>2021-08-08 15:32:29 +0200
committerArisotura <thetotalworm@gmail.com>2021-08-08 15:32:29 +0200
commitc7325469c23fc1625affd14f7855c520094a44e1 (patch)
treeae53f0ed195ba87467570a1398347bb6257ce8e2 /src/frontend/qt_sdl/main.cpp
parentbba14b2bb0492e66ee24b9b8e188d79d8a0b9467 (diff)
factor duplicate code in micLoadWav(). also add support for all sorts of WAV formats SDL can support.
Diffstat (limited to 'src/frontend/qt_sdl/main.cpp')
-rw-r--r--src/frontend/qt_sdl/main.cpp89
1 files changed, 46 insertions, 43 deletions
diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp
index a62a100..927d9ea 100644
--- a/src/frontend/qt_sdl/main.cpp
+++ b/src/frontend/qt_sdl/main.cpp
@@ -195,64 +195,67 @@ void micLoadWav(const char* name)
const u64 dstfreq = 44100;
- if (format.format == AUDIO_S16 || format.format == AUDIO_U16)
- {
- int srcinc = format.channels;
- len /= (2 * srcinc);
+ int srcinc = format.channels;
+ len /= ((SDL_AUDIO_BITSIZE(format.format) / 8) * srcinc);
+
+ micWavLength = (len * dstfreq) / format.freq;
+ if (micWavLength < 735) micWavLength = 735;
+ micWavBuffer = new s16[micWavLength];
- micWavLength = (len * dstfreq) / format.freq;
- if (micWavLength < 735) micWavLength = 735;
- micWavBuffer = new s16[micWavLength];
+ float res_incr = len / (float)micWavLength;
+ float res_timer = 0;
+ int res_pos = 0;
- float res_incr = len / (float)micWavLength;
- float res_timer = 0;
- int res_pos = 0;
+ for (int i = 0; i < micWavLength; i++)
+ {
+ u16 val;
- for (int i = 0; i < micWavLength; i++)
+ switch (SDL_AUDIO_BITSIZE(format.format))
{
- u16 val = ((u16*)buf)[res_pos];
- if (SDL_AUDIO_ISUNSIGNED(format.format)) val ^= 0x8000;
+ case 8:
+ val = buf[res_pos] << 8;
+ break;
- micWavBuffer[i] = val;
+ case 16:
+ if (SDL_AUDIO_ISBIGENDIAN(format.format))
+ val = (buf[res_pos*2] << 8) | buf[res_pos*2 + 1];
+ else
+ val = (buf[res_pos*2 + 1] << 8) | buf[res_pos*2];
+ break;
- res_timer += res_incr;
- while (res_timer >= 1.0)
+ case 32:
+ if (SDL_AUDIO_ISFLOAT(format.format))
{
- res_timer -= 1.0;
- res_pos += srcinc;
+ u32 rawval;
+ if (SDL_AUDIO_ISBIGENDIAN(format.format))
+ rawval = (buf[res_pos*4] << 24) | (buf[res_pos*4 + 1] << 16) | (buf[res_pos*4 + 2] << 8) | buf[res_pos * 4 + 3];
+ else
+ rawval = (buf[res_pos*4 + 3] << 24) | (buf[res_pos*4 + 2] << 16) | (buf[res_pos*4 + 1] << 8) | buf[res_pos * 4];
+
+ float fval = *(float*)&rawval;
+ s32 ival = (s32)(fval * 0x8000);
+ ival = std::clamp(ival, -0x8000, 0x7FFF);
+ val = (s16)ival;
}
+ else if (SDL_AUDIO_ISBIGENDIAN(format.format))
+ val = (buf[res_pos*4] << 8) | buf[res_pos*4 + 1];
+ else
+ val = (buf[res_pos*4 + 3] << 8) | buf[res_pos*4 + 2];
+ break;
}
- }
- else if (format.format == AUDIO_S8 || format.format == AUDIO_U8)
- {
- int srcinc = format.channels;
- len /= srcinc;
- micWavLength = (len * dstfreq) / format.freq;
- if (micWavLength < 735) micWavLength = 735;
- micWavBuffer = new s16[micWavLength];
+ if (SDL_AUDIO_ISUNSIGNED(format.format))
+ val ^= 0x8000;
- float res_incr = len / (float)micWavLength;
- float res_timer = 0;
- int res_pos = 0;
+ micWavBuffer[i] = val;
- for (int i = 0; i < micWavLength; i++)
+ res_timer += res_incr;
+ while (res_timer >= 1.0)
{
- u16 val = buf[res_pos] << 8;
- if (SDL_AUDIO_ISUNSIGNED(format.format)) val ^= 0x8000;
-
- micWavBuffer[i] = val;
-
- res_timer += res_incr;
- while (res_timer >= 1.0)
- {
- res_timer -= 1.0;
- res_pos += srcinc;
- }
+ res_timer -= 1.0;
+ res_pos += srcinc;
}
}
- else
- printf("bad WAV format %08X\n", format.format);
SDL_FreeWAV(buf);
}