diff options
author | Chris Robinson <[email protected]> | 2018-12-27 12:55:43 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-12-27 12:55:43 -0800 |
commit | 1a4387d137acb57f69a2b5d073faf55d4a4c32ed (patch) | |
tree | 70b19a7ffa44c148c76996e3f715b0dff795dd4c /Alc | |
parent | 323cf58f02bd2a8a92006a5ee20014d2eb8ff3a9 (diff) |
Return unique_ptrs instead of raw pointers
For the ring buffer, channel converter, and sample converter.
Diffstat (limited to 'Alc')
-rw-r--r-- | Alc/alc.cpp | 2 | ||||
-rw-r--r-- | Alc/backends/alsa.cpp | 4 | ||||
-rw-r--r-- | Alc/backends/coreaudio.cpp | 8 | ||||
-rw-r--r-- | Alc/backends/dsound.cpp | 4 | ||||
-rw-r--r-- | Alc/backends/jack.cpp | 4 | ||||
-rw-r--r-- | Alc/backends/opensl.cpp | 8 | ||||
-rw-r--r-- | Alc/backends/oss.cpp | 3 | ||||
-rw-r--r-- | Alc/backends/portaudio.cpp | 2 | ||||
-rw-r--r-- | Alc/backends/sndio.cpp | 3 | ||||
-rw-r--r-- | Alc/backends/wasapi.cpp | 33 | ||||
-rw-r--r-- | Alc/backends/winmm.cpp | 2 | ||||
-rw-r--r-- | Alc/converter.cpp | 31 | ||||
-rw-r--r-- | Alc/converter.h | 14 | ||||
-rw-r--r-- | Alc/ringbuffer.cpp | 14 | ||||
-rw-r--r-- | Alc/ringbuffer.h | 6 |
15 files changed, 55 insertions, 83 deletions
diff --git a/Alc/alc.cpp b/Alc/alc.cpp index 9a93fbdc..1b3692e2 100644 --- a/Alc/alc.cpp +++ b/Alc/alc.cpp @@ -2369,7 +2369,7 @@ static ALvoid InitContext(ALCcontext *Context) listener.Params.mDistanceModel = Context->mDistanceModel; - Context->AsyncEvents.reset(ll_ringbuffer_create(511, sizeof(AsyncEvent), false)); + Context->AsyncEvents = CreateRingBuffer(511, sizeof(AsyncEvent), false); StartEventThrd(Context); } diff --git a/Alc/backends/alsa.cpp b/Alc/backends/alsa.cpp index c0ceb4f7..97b2e965 100644 --- a/Alc/backends/alsa.cpp +++ b/Alc/backends/alsa.cpp @@ -1065,8 +1065,8 @@ ALCenum ALCcaptureAlsa_open(ALCcaptureAlsa *self, const ALCchar *name) if(needring) { - self->mRing.reset(ll_ringbuffer_create(device->UpdateSize*device->NumUpdates, - device->frameSizeFromFmt(), false)); + self->mRing = CreateRingBuffer(device->UpdateSize*device->NumUpdates, + device->frameSizeFromFmt(), false); if(!self->mRing) { ERR("ring buffer create failed\n"); diff --git a/Alc/backends/coreaudio.cpp b/Alc/backends/coreaudio.cpp index c0b6601b..8f4a8d2a 100644 --- a/Alc/backends/coreaudio.cpp +++ b/Alc/backends/coreaudio.cpp @@ -321,7 +321,7 @@ struct ALCcoreAudioCapture final : public ALCbackend { ALuint mFrameSize{0u}; AudioStreamBasicDescription mFormat{}; // This is the OpenAL format as a CoreAudio ASBD - std::unique_ptr<SampleConverter> mConverter; + SampleConverterPtr mConverter; RingBufferPtr mRing{nullptr}; }; @@ -633,11 +633,11 @@ static ALCenum ALCcoreAudioCapture_open(ALCcoreAudioCapture *self, const ALCchar // Set up sample converter if needed if(outputFormat.mSampleRate != device->Frequency) - self->mConverter.reset(CreateSampleConverter(device->FmtType, device->FmtType, + self->mConverter = CreateSampleConverter(device->FmtType, device->FmtType, self->mFormat.mChannelsPerFrame, hardwareFormat.mSampleRate, device->Frequency, - BSinc24Resampler)); + BSinc24Resampler); - self->mRing.reset(ll_ringbuffer_create(outputFrameCount, self->mFrameSize, false)); + self->mRing = CreateRingBuffer(outputFrameCount, self->mFrameSize, false); if(!self->mRing) return ALC_INVALID_VALUE; device->DeviceName = name; diff --git a/Alc/backends/dsound.cpp b/Alc/backends/dsound.cpp index 516a7770..a1493fa8 100644 --- a/Alc/backends/dsound.cpp +++ b/Alc/backends/dsound.cpp @@ -835,8 +835,8 @@ ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *deviceName) self->DSC->CreateCaptureBuffer(&DSCBDescription, &self->DSCbuffer, nullptr); if(SUCCEEDED(hr)) { - self->Ring.reset(ll_ringbuffer_create(device->UpdateSize*device->NumUpdates, - InputType.Format.nBlockAlign, false)); + self->Ring = CreateRingBuffer(device->UpdateSize*device->NumUpdates, + InputType.Format.nBlockAlign, false); if(!self->Ring) hr = DSERR_OUTOFMEMORY; } diff --git a/Alc/backends/jack.cpp b/Alc/backends/jack.cpp index 02e29973..ca537323 100644 --- a/Alc/backends/jack.cpp +++ b/Alc/backends/jack.cpp @@ -222,7 +222,7 @@ int ALCjackPlayback_bufferSizeNotify(jack_nframes_t numframes, void *arg) TRACE("%u update size x%u\n", device->UpdateSize, device->NumUpdates); self->mRing = nullptr; - self->mRing.reset(ll_ringbuffer_create(bufsize, device->frameSizeFromFmt(), true)); + self->mRing = CreateRingBuffer(bufsize, device->frameSizeFromFmt(), true); if(!self->mRing) { ERR("Failed to reallocate ringbuffer\n"); @@ -431,7 +431,7 @@ ALCboolean ALCjackPlayback_reset(ALCjackPlayback *self) } self->mRing = nullptr; - self->mRing.reset(ll_ringbuffer_create(bufsize, device->frameSizeFromFmt(), true)); + self->mRing = CreateRingBuffer(bufsize, device->frameSizeFromFmt(), true); if(!self->mRing) { ERR("Failed to allocate ringbuffer\n"); diff --git a/Alc/backends/opensl.cpp b/Alc/backends/opensl.cpp index 872fbc91..e1a0faa6 100644 --- a/Alc/backends/opensl.cpp +++ b/Alc/backends/opensl.cpp @@ -534,8 +534,8 @@ static ALCboolean ALCopenslPlayback_reset(ALCopenslPlayback *self) } if(SL_RESULT_SUCCESS == result) { - self->mRing.reset(ll_ringbuffer_create(device->NumUpdates, - self->mFrameSize*device->UpdateSize, true)); + self->mRing = CreateRingBuffer(device->NumUpdates, + self->mFrameSize*device->UpdateSize, true); if(!self->mRing) { ERR("Out of memory allocating ring buffer %ux%u %u\n", device->UpdateSize, @@ -826,8 +826,8 @@ static ALCenum ALCopenslCapture_open(ALCopenslCapture *self, const ALCchar *name if(SL_RESULT_SUCCESS == result) { - self->mRing.reset(ll_ringbuffer_create(device->NumUpdates, - device->UpdateSize*self->mFrameSize, false)); + self->mRing = CreateRingBuffer(device->NumUpdates, + device->UpdateSize*self->mFrameSize, false); result = VCALL(self->mRecordObj,GetInterface)(SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &bufferQueue); diff --git a/Alc/backends/oss.cpp b/Alc/backends/oss.cpp index f6a75d12..ec9028eb 100644 --- a/Alc/backends/oss.cpp +++ b/Alc/backends/oss.cpp @@ -683,8 +683,7 @@ ALCenum ALCcaptureOSS_open(ALCcaptureOSS *self, const ALCchar *name) return ALC_INVALID_VALUE; } - self->mRing.reset(ll_ringbuffer_create(device->UpdateSize*device->NumUpdates, frameSize, - false)); + self->mRing = CreateRingBuffer(device->UpdateSize*device->NumUpdates, frameSize, false); if(!self->mRing) { ERR("Ring buffer create failed\n"); diff --git a/Alc/backends/portaudio.cpp b/Alc/backends/portaudio.cpp index f39661a7..354389c3 100644 --- a/Alc/backends/portaudio.cpp +++ b/Alc/backends/portaudio.cpp @@ -387,7 +387,7 @@ ALCenum ALCportCapture_open(ALCportCapture *self, const ALCchar *name) samples = maxu(samples, 100 * device->Frequency / 1000); frame_size = device->frameSizeFromFmt(); - self->Ring.reset(ll_ringbuffer_create(samples, frame_size, false)); + self->Ring = CreateRingBuffer(samples, frame_size, false); if(!self->Ring) return ALC_INVALID_VALUE; self->Params.device = -1; diff --git a/Alc/backends/sndio.cpp b/Alc/backends/sndio.cpp index 887b5703..027ff9f2 100644 --- a/Alc/backends/sndio.cpp +++ b/Alc/backends/sndio.cpp @@ -465,8 +465,7 @@ static ALCenum SndioCapture_open(SndioCapture *self, const ALCchar *name) return ALC_INVALID_VALUE; } - self->ring.reset(ll_ringbuffer_create(device->UpdateSize*device->NumUpdates, - par.bps*par.rchan, false)); + self->ring = CreateRingBuffer(device->UpdateSize*device->NumUpdates, par.bps*par.rchan, false); if(!self->ring) { ERR("Failed to allocate %u-byte ringbuffer\n", diff --git a/Alc/backends/wasapi.cpp b/Alc/backends/wasapi.cpp index e1783ef9..a7524ce1 100644 --- a/Alc/backends/wasapi.cpp +++ b/Alc/backends/wasapi.cpp @@ -1170,9 +1170,9 @@ struct ALCwasapiCapture final : public ALCbackend, WasapiProxy { HANDLE mMsgEvent{nullptr}; - ChannelConverter *mChannelConv{nullptr}; - SampleConverter *mSampleConv{nullptr}; - RingBufferPtr mRing{nullptr}; + ChannelConverterPtr mChannelConv; + SampleConverterPtr mSampleConv; + RingBufferPtr mRing; std::atomic<int> mKillNow{AL_TRUE}; std::thread mThread; @@ -1220,9 +1220,6 @@ void ALCwasapiCapture_Destruct(ALCwasapiCapture *self) CloseHandle(self->mNotifyEvent); self->mNotifyEvent = nullptr; - DestroySampleConverter(&self->mSampleConv); - DestroyChannelConverter(&self->mChannelConv); - ALCbackend_Destruct(STATIC_CAST(ALCbackend, self)); self->~ALCwasapiCapture(); } @@ -1266,35 +1263,31 @@ FORCE_ALIGN int ALCwasapiCapture_recordProc(ALCwasapiCapture *self) ERR("Failed to get capture buffer: 0x%08lx\n", hr); else { - if(self->mChannelConv) + if(ChannelConverter *conv{self->mChannelConv.get()}) { samples.resize(numsamples*2); - ChannelConverterInput(self->mChannelConv, rdata, samples.data(), numsamples); + ChannelConverterInput(conv, rdata, samples.data(), numsamples); rdata = reinterpret_cast<BYTE*>(samples.data()); } auto data = ring->getWriteVector(); size_t dstframes; - if(self->mSampleConv) + if(SampleConverter *conv{self->mSampleConv.get()}) { const ALvoid *srcdata = rdata; ALsizei srcframes = numsamples; - dstframes = SampleConverterInput(self->mSampleConv, - &srcdata, &srcframes, data.first.buf, - (ALsizei)minz(data.first.len, INT_MAX) - ); + dstframes = SampleConverterInput(conv, &srcdata, &srcframes, data.first.buf, + (ALsizei)minz(data.first.len, INT_MAX)); if(srcframes > 0 && dstframes == data.first.len && data.second.len > 0) { /* If some source samples remain, all of the first dest * block was filled, and there's space in the second * dest block, do another run for the second block. */ - dstframes += SampleConverterInput(self->mSampleConv, - &srcdata, &srcframes, data.second.buf, - (ALsizei)minz(data.second.len, INT_MAX) - ); + dstframes += SampleConverterInput(conv, &srcdata, &srcframes, + data.second.buf, (ALsizei)minz(data.second.len, INT_MAX)); } } else @@ -1576,8 +1569,8 @@ HRESULT ALCwasapiCapture::resetProxy() return hr; } - DestroySampleConverter(&mSampleConv); - DestroyChannelConverter(&mChannelConv); + mSampleConv = nullptr; + mChannelConv = nullptr; if(wfx != nullptr) { @@ -1694,7 +1687,7 @@ HRESULT ALCwasapiCapture::resetProxy() } buffer_len = maxu(device->UpdateSize*device->NumUpdates, buffer_len); - mRing.reset(ll_ringbuffer_create(buffer_len, device->frameSizeFromFmt(), false)); + mRing = CreateRingBuffer(buffer_len, device->frameSizeFromFmt(), false); if(!mRing) { ERR("Failed to allocate capture ring buffer\n"); diff --git a/Alc/backends/winmm.cpp b/Alc/backends/winmm.cpp index 7bfb7f6c..485586f6 100644 --- a/Alc/backends/winmm.cpp +++ b/Alc/backends/winmm.cpp @@ -581,7 +581,7 @@ ALCenum ALCwinmmCapture_open(ALCwinmmCapture *self, const ALCchar *deviceName) std::max<size_t>(device->UpdateSize*device->NumUpdates, BufferSize*self->WaveBuffer.size()) ); - self->Ring.reset(ll_ringbuffer_create(CapturedDataSize, self->Format.nBlockAlign, false)); + self->Ring = CreateRingBuffer(CapturedDataSize, self->Format.nBlockAlign, false); if(!self->Ring) return ALC_INVALID_VALUE; al_free(self->WaveBuffer[0].lpData); diff --git a/Alc/converter.cpp b/Alc/converter.cpp index 66627879..52472bde 100644 --- a/Alc/converter.cpp +++ b/Alc/converter.cpp @@ -135,14 +135,14 @@ void Stereo2Mono(ALfloat *RESTRICT dst, const void *src, ALsizei frames) } // namespace -SampleConverter *CreateSampleConverter(DevFmtType srcType, DevFmtType dstType, ALsizei numchans, - ALsizei srcRate, ALsizei dstRate, Resampler resampler) +SampleConverterPtr CreateSampleConverter(DevFmtType srcType, DevFmtType dstType, ALsizei numchans, + ALsizei srcRate, ALsizei dstRate, Resampler resampler) { if(numchans <= 0 || srcRate <= 0 || dstRate <= 0) return nullptr; - size_t alloc_size{FAM_SIZE(SampleConverter, Chan, numchans)}; - auto converter = new (al_calloc(16, alloc_size)) SampleConverter{}; + const size_t alloc_size{FAM_SIZE(SampleConverter, Chan, numchans)}; + SampleConverterPtr converter{new (al_calloc(16, alloc_size)) SampleConverter{}}; converter->mSrcType = srcType; converter->mDstType = dstType; converter->mNumChannels = numchans; @@ -171,16 +171,6 @@ SampleConverter *CreateSampleConverter(DevFmtType srcType, DevFmtType dstType, A return converter; } -void DestroySampleConverter(SampleConverter **converter) -{ - if(converter) - { - delete *converter; - *converter = nullptr; - } -} - - ALsizei SampleConverterAvailableOut(SampleConverter *converter, ALsizei srcframes) { ALint prepcount{converter->mSrcPrepCount}; @@ -339,22 +329,13 @@ ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALs } -ChannelConverter *CreateChannelConverter(DevFmtType srcType, DevFmtChannels srcChans, DevFmtChannels dstChans) +ChannelConverterPtr CreateChannelConverter(DevFmtType srcType, DevFmtChannels srcChans, DevFmtChannels dstChans) { if(srcChans != dstChans && !((srcChans == DevFmtMono && dstChans == DevFmtStereo) || (srcChans == DevFmtStereo && dstChans == DevFmtMono))) return nullptr; - return new ChannelConverter{srcType, srcChans, dstChans}; -} - -void DestroyChannelConverter(ChannelConverter **converter) -{ - if(converter) - { - delete *converter; - *converter = nullptr; - } + return ChannelConverterPtr{new ChannelConverter{srcType, srcChans, dstChans}}; } void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALfloat *dst, ALsizei frames) diff --git a/Alc/converter.h b/Alc/converter.h index 1c473f49..6c76be07 100644 --- a/Alc/converter.h +++ b/Alc/converter.h @@ -1,6 +1,8 @@ #ifndef CONVERTER_H #define CONVERTER_H +#include <memory> + #include "alMain.h" #include "alu.h" #include "almalloc.h" @@ -28,10 +30,10 @@ struct SampleConverter { DEF_PLACE_NEWDEL() }; +using SampleConverterPtr = std::unique_ptr<SampleConverter>; -SampleConverter *CreateSampleConverter(DevFmtType srcType, DevFmtType dstType, ALsizei numchans, +SampleConverterPtr CreateSampleConverter(DevFmtType srcType, DevFmtType dstType, ALsizei numchans, ALsizei srcRate, ALsizei dstRate, Resampler resampler); -void DestroySampleConverter(SampleConverter **converter); ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALsizei *srcframes, ALvoid *dst, ALsizei dstframes); ALsizei SampleConverterAvailableOut(SampleConverter *converter, ALsizei srcframes); @@ -47,10 +49,12 @@ struct ChannelConverter { { } DEF_NEWDEL(ChannelConverter) }; +using ChannelConverterPtr = std::unique_ptr<ChannelConverter>; -ChannelConverter *CreateChannelConverter(DevFmtType srcType, DevFmtChannels srcChans, DevFmtChannels dstChans); -void DestroyChannelConverter(ChannelConverter **converter); +ChannelConverterPtr CreateChannelConverter(DevFmtType srcType, DevFmtChannels srcChans, + DevFmtChannels dstChans); -void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALfloat *dst, ALsizei frames); +void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALfloat *dst, + ALsizei frames); #endif /* CONVERTER_H */ diff --git a/Alc/ringbuffer.cpp b/Alc/ringbuffer.cpp index 9e531316..767db246 100644 --- a/Alc/ringbuffer.cpp +++ b/Alc/ringbuffer.cpp @@ -33,11 +33,9 @@ #include "compat.h" -RingBuffer *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_writes) +RingBufferPtr CreateRingBuffer(size_t sz, size_t elem_sz, int limit_writes) { - RingBuffer *rb; - size_t power_of_two = 0; - + size_t power_of_two{0u}; if(sz > 0) { power_of_two = sz; @@ -50,14 +48,14 @@ RingBuffer *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_writes) power_of_two |= power_of_two>>32; #endif } - power_of_two++; - if(power_of_two < sz) return NULL; - - rb = new (al_malloc(16, sizeof(*rb) + power_of_two*elem_sz)) RingBuffer{}; + ++power_of_two; + if(power_of_two < sz) return nullptr; + RingBufferPtr rb{new (al_malloc(16, sizeof(*rb) + power_of_two*elem_sz)) RingBuffer{}}; rb->mSize = limit_writes ? sz : power_of_two; rb->mSizeMask = power_of_two - 1; rb->mElemSize = elem_sz; + return rb; } diff --git a/Alc/ringbuffer.h b/Alc/ringbuffer.h index a2871859..317995b0 100644 --- a/Alc/ringbuffer.h +++ b/Alc/ringbuffer.h @@ -81,6 +81,7 @@ struct RingBuffer { DEF_PLACE_NEWDEL() }; +using RingBufferPtr = std::unique_ptr<RingBuffer>; /** @@ -88,9 +89,6 @@ struct RingBuffer { * The number of elements is rounded up to the next power of two (even if it is * already a power of two, to ensure the requested amount can be written). */ -RingBuffer *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_writes); - - -using RingBufferPtr = std::unique_ptr<RingBuffer>; +RingBufferPtr CreateRingBuffer(size_t sz, size_t elem_sz, int limit_writes); #endif /* RINGBUFFER_H */ |