From d3c4bab7bb0590a976bc70301d655638b7ec0f51 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 11 Nov 2018 03:29:43 -0800 Subject: Use a vector in the wave backend --- Alc/backends/wave.cpp | 43 +++++++++++++------------------------------ 1 file changed, 13 insertions(+), 30 deletions(-) (limited to 'Alc') diff --git a/Alc/backends/wave.cpp b/Alc/backends/wave.cpp index 118ed309..2b10d286 100644 --- a/Alc/backends/wave.cpp +++ b/Alc/backends/wave.cpp @@ -27,6 +27,7 @@ #include #include +#include #include "alMain.h" #include "alu.h" @@ -83,8 +84,7 @@ struct ALCwaveBackend final : public ALCbackend { FILE *mFile; long mDataStart; - ALvoid *mBuffer; - ALuint mSize; + std::vector mBuffer; ATOMIC(ALenum) killNow; std::thread thread; @@ -117,9 +117,6 @@ static void ALCwaveBackend_Construct(ALCwaveBackend *self, ALCdevice *device) self->mFile = nullptr; self->mDataStart = -1; - self->mBuffer = nullptr; - self->mSize = 0; - ATOMIC_INIT(&self->killNow, AL_TRUE); } @@ -159,7 +156,7 @@ static int ALCwaveBackend_mixerProc(ALCwaveBackend *self) while(avail-done >= device->UpdateSize) { ALCwaveBackend_lock(self); - aluMixData(device, self->mBuffer, device->UpdateSize); + aluMixData(device, self->mBuffer.data(), device->UpdateSize); ALCwaveBackend_unlock(self); done += device->UpdateSize; @@ -170,8 +167,8 @@ static int ALCwaveBackend_mixerProc(ALCwaveBackend *self) if(bytesize == 2) { - ALushort *samples = static_cast(self->mBuffer); - ALuint len = self->mSize / 2; + ALushort *samples = reinterpret_cast(self->mBuffer.data()); + ALuint len = self->mBuffer.size() / 2; for(i = 0;i < len;i++) { ALushort samp = samples[i]; @@ -180,8 +177,8 @@ static int ALCwaveBackend_mixerProc(ALCwaveBackend *self) } else if(bytesize == 4) { - ALuint *samples = static_cast(self->mBuffer); - ALuint len = self->mSize / 4; + ALuint *samples = reinterpret_cast(self->mBuffer.data()); + ALuint len = self->mBuffer.size() / 4; for(i = 0;i < len;i++) { ALuint samp = samples[i]; @@ -191,7 +188,7 @@ static int ALCwaveBackend_mixerProc(ALCwaveBackend *self) } } - size_t fs{fwrite(self->mBuffer, frameSize, device->UpdateSize, self->mFile)}; + size_t fs{fwrite(self->mBuffer.data(), frameSize, device->UpdateSize, self->mFile)}; (void)fs; if(ferror(self->mFile)) { @@ -347,23 +344,16 @@ static ALCboolean ALCwaveBackend_reset(ALCwaveBackend *self) SetDefaultWFXChannelOrder(device); + ALuint bufsize{FrameSizeFromDevFmt( + device->FmtChans, device->FmtType, device->AmbiOrder + ) * device->UpdateSize}; + self->mBuffer.resize(bufsize); + return ALC_TRUE; } static ALCboolean ALCwaveBackend_start(ALCwaveBackend *self) { - ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice; - - self->mSize = device->UpdateSize * FrameSizeFromDevFmt( - device->FmtChans, device->FmtType, device->AmbiOrder - ); - self->mBuffer = malloc(self->mSize); - if(!self->mBuffer) - { - ERR("Buffer malloc failed\n"); - return ALC_FALSE; - } - try { ATOMIC_STORE(&self->killNow, AL_FALSE, almemory_order_release); self->thread = std::thread(ALCwaveBackend_mixerProc, self); @@ -374,10 +364,6 @@ static ALCboolean ALCwaveBackend_start(ALCwaveBackend *self) } catch(...) { } - - free(self->mBuffer); - self->mBuffer = nullptr; - self->mSize = 0; return ALC_FALSE; } @@ -388,9 +374,6 @@ static void ALCwaveBackend_stop(ALCwaveBackend *self) return; self->thread.join(); - free(self->mBuffer); - self->mBuffer = nullptr; - long size{ftell(self->mFile)}; if(size > 0) { -- cgit v1.2.3