diff options
author | Chris Robinson <[email protected]> | 2018-11-19 04:46:49 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-19 04:46:49 -0800 |
commit | ad5f9d9b22f8860f0c6ca06004c134182dda95df (patch) | |
tree | aac46dac1585e5f58ba8bc1be6bb06d3a36e9ab6 /Alc | |
parent | a14f39ea06a458e6b3b70e0428264967847da7f4 (diff) |
Return the ringbuffer data pointers as a pair
Diffstat (limited to 'Alc')
-rw-r--r-- | Alc/backends/alsa.cpp | 9 | ||||
-rw-r--r-- | Alc/backends/jack.cpp | 26 | ||||
-rw-r--r-- | Alc/backends/opensl.cpp | 38 | ||||
-rw-r--r-- | Alc/backends/oss.cpp | 7 | ||||
-rw-r--r-- | Alc/backends/sndio.cpp | 19 | ||||
-rw-r--r-- | Alc/backends/wasapi.cpp | 19 | ||||
-rw-r--r-- | Alc/ringbuffer.cpp | 42 | ||||
-rw-r--r-- | Alc/ringbuffer.h | 34 |
8 files changed, 95 insertions, 99 deletions
diff --git a/Alc/backends/alsa.cpp b/Alc/backends/alsa.cpp index b3adfafa..e1e861db 100644 --- a/Alc/backends/alsa.cpp +++ b/Alc/backends/alsa.cpp @@ -1237,12 +1237,11 @@ ALCuint ALCcaptureAlsa_availableSamples(ALCcaptureAlsa *self) while(avail > 0) { - ll_ringbuffer_data_t vec[2]; - ll_ringbuffer_get_write_vector(self->ring, vec); - if(vec[0].len == 0) break; + auto vec = ll_ringbuffer_get_write_vector(self->ring); + if(vec.first.len == 0) break; - snd_pcm_sframes_t amt{std::min<snd_pcm_sframes_t>(vec[0].len, avail)}; - amt = snd_pcm_readi(self->pcmHandle, vec[0].buf, amt); + snd_pcm_sframes_t amt{std::min<snd_pcm_sframes_t>(vec.first.len, avail)}; + amt = snd_pcm_readi(self->pcmHandle, vec.first.buf, amt); if(amt < 0) { ERR("read error: %s\n", snd_strerror(amt)); diff --git a/Alc/backends/jack.cpp b/Alc/backends/jack.cpp index 924e1926..93dbaf13 100644 --- a/Alc/backends/jack.cpp +++ b/Alc/backends/jack.cpp @@ -249,33 +249,32 @@ static int ALCjackPlayback_process(jack_nframes_t numframes, void *arg) { ALCjackPlayback *self = static_cast<ALCjackPlayback*>(arg); jack_default_audio_sample_t *out[MAX_OUTPUT_CHANNELS]; - ll_ringbuffer_data_t data[2]; - jack_nframes_t total = 0; + jack_nframes_t total{0}; jack_nframes_t todo; ALsizei i, c, numchans; - ll_ringbuffer_get_read_vector(self->Ring, data); + auto data = ll_ringbuffer_get_read_vector(self->Ring); for(c = 0;c < MAX_OUTPUT_CHANNELS && self->Port[c];c++) out[c] = static_cast<float*>(jack_port_get_buffer(self->Port[c], numframes)); numchans = c; - todo = minu(numframes, data[0].len); + todo = minu(numframes, data.first.len); for(c = 0;c < numchans;c++) { - const ALfloat *RESTRICT in = ((ALfloat*)data[0].buf) + c; + const ALfloat *RESTRICT in = ((ALfloat*)data.first.buf) + c; for(i = 0;(jack_nframes_t)i < todo;i++) out[c][i] = in[i*numchans]; out[c] += todo; } total += todo; - todo = minu(numframes-total, data[1].len); + todo = minu(numframes-total, data.second.len); if(todo > 0) { for(c = 0;c < numchans;c++) { - const ALfloat *RESTRICT in = ((ALfloat*)data[1].buf) + c; + const ALfloat *RESTRICT in = ((ALfloat*)data.second.buf) + c; for(i = 0;(jack_nframes_t)i < todo;i++) out[c][i] = in[i*numchans]; out[c] += todo; @@ -303,7 +302,6 @@ static int ALCjackPlayback_mixerProc(void *arg) { ALCjackPlayback *self = static_cast<ALCjackPlayback*>(arg); ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice; - ll_ringbuffer_data_t data[2]; SetRTPriority(); althrd_setname(MIXER_THREAD_NAME); @@ -322,16 +320,16 @@ static int ALCjackPlayback_mixerProc(void *arg) continue; } - ll_ringbuffer_get_write_vector(self->Ring, data); - todo = data[0].len + data[1].len; + auto data = ll_ringbuffer_get_write_vector(self->Ring); + todo = data.first.len + data.second.len; todo -= todo%device->UpdateSize; - len1 = minu(data[0].len, todo); - len2 = minu(data[1].len, todo-len1); + len1 = minu(data.first.len, todo); + len2 = minu(data.second.len, todo-len1); - aluMixData(device, data[0].buf, len1); + aluMixData(device, data.first.buf, len1); if(len2 > 0) - aluMixData(device, data[1].buf, len2); + aluMixData(device, data.second.buf, len2); ll_ringbuffer_write_advance(self->Ring, todo); } ALCjackPlayback_unlock(self); diff --git a/Alc/backends/opensl.cpp b/Alc/backends/opensl.cpp index 0eb194e0..a53c0759 100644 --- a/Alc/backends/opensl.cpp +++ b/Alc/backends/opensl.cpp @@ -239,7 +239,6 @@ static int ALCopenslPlayback_mixerProc(void *arg) ALCopenslPlayback *self = static_cast<ALCopenslPlayback*>(arg); ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice; SLAndroidSimpleBufferQueueItf bufferQueue; - ll_ringbuffer_data_t data[2]; SLPlayItf player; SLresult result; @@ -291,13 +290,12 @@ static int ALCopenslPlayback_mixerProc(void *arg) } } - ll_ringbuffer_get_write_vector(self->mRing, data); + auto data = ll_ringbuffer_get_write_vector(self->mRing); + aluMixData(device, data.first.buf, data.first.len*device->UpdateSize); + if(data.second.len > 0) + aluMixData(device, data.second.buf, data.second.len*device->UpdateSize); - aluMixData(device, data[0].buf, data[0].len*device->UpdateSize); - if(data[1].len > 0) - aluMixData(device, data[1].buf, data[1].len*device->UpdateSize); - - todo = data[0].len+data[1].len; + todo = data.first.len+data.second.len; ll_ringbuffer_write_advance(self->mRing, todo); for(size_t i = 0;i < todo;i++) @@ -869,18 +867,17 @@ static ALCenum ALCopenslCapture_open(ALCopenslCapture *self, const ALCchar *name if(SL_RESULT_SUCCESS == result) { ALsizei chunk_size = device->UpdateSize * self->mFrameSize; - ll_ringbuffer_data_t data[2]; size_t i; - ll_ringbuffer_get_write_vector(self->mRing, data); - for(i = 0;i < data[0].len && SL_RESULT_SUCCESS == result;i++) + auto data = ll_ringbuffer_get_write_vector(self->mRing); + for(i = 0;i < data.first.len && SL_RESULT_SUCCESS == result;i++) { - result = VCALL(bufferQueue,Enqueue)(data[0].buf + chunk_size*i, chunk_size); + result = VCALL(bufferQueue,Enqueue)(data.first.buf + chunk_size*i, chunk_size); PRINTERR(result, "bufferQueue->Enqueue"); } - for(i = 0;i < data[1].len && SL_RESULT_SUCCESS == result;i++) + for(i = 0;i < data.second.len && SL_RESULT_SUCCESS == result;i++) { - result = VCALL(bufferQueue,Enqueue)(data[1].buf + chunk_size*i, chunk_size); + result = VCALL(bufferQueue,Enqueue)(data.second.buf + chunk_size*i, chunk_size); PRINTERR(result, "bufferQueue->Enqueue"); } } @@ -949,7 +946,6 @@ static ALCenum ALCopenslCapture_captureSamples(ALCopenslCapture *self, ALCvoid * ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice; ALsizei chunk_size = device->UpdateSize * self->mFrameSize; SLAndroidSimpleBufferQueueItf bufferQueue; - ll_ringbuffer_data_t data[2]; SLresult result; ALCuint i; @@ -960,12 +956,12 @@ static ALCenum ALCopenslCapture_captureSamples(ALCopenslCapture *self, ALCvoid * /* Read the desired samples from the ring buffer then advance its read * pointer. */ - ll_ringbuffer_get_read_vector(self->mRing, data); + auto data = ll_ringbuffer_get_read_vector(self->mRing); for(i = 0;i < samples;) { ALCuint rem = minu(samples - i, device->UpdateSize - self->mSplOffset); memcpy((ALCbyte*)buffer + i*self->mFrameSize, - data[0].buf + self->mSplOffset*self->mFrameSize, + data.first.buf + self->mSplOffset*self->mFrameSize, rem * self->mFrameSize); self->mSplOffset += rem; @@ -975,15 +971,15 @@ static ALCenum ALCopenslCapture_captureSamples(ALCopenslCapture *self, ALCvoid * self->mSplOffset = 0; ll_ringbuffer_read_advance(self->mRing, 1); - result = VCALL(bufferQueue,Enqueue)(data[0].buf, chunk_size); + result = VCALL(bufferQueue,Enqueue)(data.first.buf, chunk_size); PRINTERR(result, "bufferQueue->Enqueue"); if(SL_RESULT_SUCCESS != result) break; - data[0].len--; - if(!data[0].len) - data[0] = data[1]; + data.first.len--; + if(!data.first.len) + data.first = data.second; else - data[0].buf += chunk_size; + data.first.buf += chunk_size; } i += rem; diff --git a/Alc/backends/oss.cpp b/Alc/backends/oss.cpp index 32ee1022..3985cd27 100644 --- a/Alc/backends/oss.cpp +++ b/Alc/backends/oss.cpp @@ -582,11 +582,10 @@ int ALCcaptureOSS_recordProc(ALCcaptureOSS *self) continue; } - ll_ringbuffer_data_t vec[2]; - ll_ringbuffer_get_write_vector(self->ring, vec); - if(vec[0].len > 0) + auto vec = ll_ringbuffer_get_write_vector(self->ring); + if(vec.first.len > 0) { - amt = read(self->fd, vec[0].buf, vec[0].len*frame_size); + amt = read(self->fd, vec.first.buf, vec.first.len*frame_size); if(amt < 0) { ERR("read failed: %s\n", strerror(errno)); diff --git a/Alc/backends/sndio.cpp b/Alc/backends/sndio.cpp index cc5eacbe..2a20fb61 100644 --- a/Alc/backends/sndio.cpp +++ b/Alc/backends/sndio.cpp @@ -345,11 +345,10 @@ static int SndioCapture_recordProc(void* ptr) while(!ATOMIC_LOAD(&self->killNow, almemory_order_acquire) && ATOMIC_LOAD(&device->Connected, almemory_order_acquire)) { - ll_ringbuffer_data_t data[2]; size_t total, todo; - ll_ringbuffer_get_write_vector(self->ring, data); - todo = data[0].len + data[1].len; + auto data = ll_ringbuffer_get_write_vector(self->ring); + todo = data.first.len + data.second.len; if(todo == 0) { static char junk[4096]; @@ -358,17 +357,17 @@ static int SndioCapture_recordProc(void* ptr) } total = 0; - data[0].len *= frameSize; - data[1].len *= frameSize; + data.first.len *= frameSize; + data.second.len *= frameSize; todo = minz(todo, device->UpdateSize) * frameSize; while(total < todo) { size_t got; - if(!data[0].len) - data[0] = data[1]; + if(!data.first.len) + data.first = data.second; - got = sio_read(self->sndHandle, data[0].buf, minz(todo-total, data[0].len)); + got = sio_read(self->sndHandle, data.first.buf, minz(todo-total, data.first.len)); if(!got) { SndioCapture_lock(self); @@ -377,8 +376,8 @@ static int SndioCapture_recordProc(void* ptr) break; } - data[0].buf += got; - data[0].len -= got; + data.first.buf += got; + data.first.len -= got; total += got; } ll_ringbuffer_write_advance(self->ring, total / frameSize); diff --git a/Alc/backends/wasapi.cpp b/Alc/backends/wasapi.cpp index 751c56d8..e531a9ae 100644 --- a/Alc/backends/wasapi.cpp +++ b/Alc/backends/wasapi.cpp @@ -1275,8 +1275,7 @@ FORCE_ALIGN int ALCwasapiCapture_recordProc(ALCwasapiCapture *self) rdata = reinterpret_cast<BYTE*>(samples.data()); } - ll_ringbuffer_data_t data[2]; - ll_ringbuffer_get_write_vector(self->mRing, data); + auto data = ll_ringbuffer_get_write_vector(self->mRing); size_t dstframes; if(self->mSampleConv) @@ -1285,16 +1284,18 @@ FORCE_ALIGN int ALCwasapiCapture_recordProc(ALCwasapiCapture *self) ALsizei srcframes = numsamples; dstframes = SampleConverterInput(self->mSampleConv, - &srcdata, &srcframes, data[0].buf, (ALsizei)minz(data[0].len, INT_MAX) + &srcdata, &srcframes, data.first.buf, + (ALsizei)minz(data.first.len, INT_MAX) ); - if(srcframes > 0 && dstframes == data[0].len && data[1].len > 0) + 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[1].buf, (ALsizei)minz(data[1].len, INT_MAX) + &srcdata, &srcframes, data.second.buf, + (ALsizei)minz(data.second.len, INT_MAX) ); } } @@ -1302,12 +1303,12 @@ FORCE_ALIGN int ALCwasapiCapture_recordProc(ALCwasapiCapture *self) { ALuint framesize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->mAmbiOrder); - size_t len1 = minz(data[0].len, numsamples); - size_t len2 = minz(data[1].len, numsamples-len1); + size_t len1 = minz(data.first.len, numsamples); + size_t len2 = minz(data.second.len, numsamples-len1); - memcpy(data[0].buf, rdata, len1*framesize); + memcpy(data.first.buf, rdata, len1*framesize); if(len2 > 0) - memcpy(data[1].buf, rdata+len1*framesize, len2*framesize); + memcpy(data.second.buf, rdata+len1*framesize, len2*framesize); dstframes = len1 + len2; } diff --git a/Alc/ringbuffer.cpp b/Alc/ringbuffer.cpp index 4d40ec93..e5f0f554 100644 --- a/Alc/ringbuffer.cpp +++ b/Alc/ringbuffer.cpp @@ -232,8 +232,9 @@ void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt) } -void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]) +ll_ringbuffer_data_pair ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb) { + ll_ringbuffer_data_pair ret; size_t free_cnt; size_t cnt2; @@ -248,23 +249,26 @@ void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data { /* Two part vector: the rest of the buffer after the current write ptr, * plus some from the start of the buffer. */ - vec[0].buf = const_cast<char*>(&rb->buf[r*rb->elem_size]); - vec[0].len = rb->size_mask+1 - r; - vec[1].buf = const_cast<char*>(rb->buf); - vec[1].len = cnt2 & rb->size_mask; + ret.first.buf = const_cast<char*>(&rb->buf[r*rb->elem_size]); + ret.first.len = rb->size_mask+1 - r; + ret.second.buf = const_cast<char*>(rb->buf); + ret.second.len = cnt2 & rb->size_mask; } else { /* Single part vector: just the rest of the buffer */ - vec[0].buf = const_cast<char*>(&rb->buf[r*rb->elem_size]); - vec[0].len = free_cnt; - vec[1].buf = nullptr; - vec[1].len = 0; + ret.first.buf = const_cast<char*>(&rb->buf[r*rb->elem_size]); + ret.first.len = free_cnt; + ret.second.buf = nullptr; + ret.second.len = 0; } + + return ret; } -void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]) +ll_ringbuffer_data_pair ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb) { + ll_ringbuffer_data_pair ret; size_t free_cnt; size_t cnt2; @@ -280,16 +284,18 @@ void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_dat { /* Two part vector: the rest of the buffer after the current write ptr, * plus some from the start of the buffer. */ - vec[0].buf = const_cast<char*>(&rb->buf[w*rb->elem_size]); - vec[0].len = rb->size_mask+1 - w; - vec[1].buf = const_cast<char*>(rb->buf); - vec[1].len = cnt2 & rb->size_mask; + ret.first.buf = const_cast<char*>(&rb->buf[w*rb->elem_size]); + ret.first.len = rb->size_mask+1 - w; + ret.second.buf = const_cast<char*>(rb->buf); + ret.second.len = cnt2 & rb->size_mask; } else { - vec[0].buf = const_cast<char*>(&rb->buf[w*rb->elem_size]); - vec[0].len = free_cnt; - vec[1].buf = nullptr; - vec[1].len = 0; + ret.first.buf = const_cast<char*>(&rb->buf[w*rb->elem_size]); + ret.first.len = free_cnt; + ret.second.buf = nullptr; + ret.second.len = 0; } + + return ret; } diff --git a/Alc/ringbuffer.h b/Alc/ringbuffer.h index 521d96cb..b516ab57 100644 --- a/Alc/ringbuffer.h +++ b/Alc/ringbuffer.h @@ -3,16 +3,18 @@ #include <stddef.h> +#include <utility> -#ifdef __cplusplus -extern "C" { -#endif -typedef struct ll_ringbuffer ll_ringbuffer_t; -typedef struct ll_ringbuffer_data { +struct ll_ringbuffer; +using ll_ringbuffer_t = struct ll_ringbuffer; + +struct ll_ringbuffer_data { char *buf; size_t len; -} ll_ringbuffer_data_t; +}; +using ll_ringbuffer_data_pair = std::pair<ll_ringbuffer_data,ll_ringbuffer_data>; +using ll_ringbuffer_data_t = struct ll_ringbuffer_data; /** @@ -27,17 +29,17 @@ void ll_ringbuffer_free(ll_ringbuffer_t *rb); void ll_ringbuffer_reset(ll_ringbuffer_t *rb); /** - * The non-copying data reader. `vec' is an array of two places. Set the values - * at `vec' to hold the current readable data at `rb'. If the readable data is - * in one segment the second segment has zero length. + * The non-copying data reader. Returns two ringbuffer data pointers that hold + * the current readable data at `rb'. If the readable data is in one segment + * the second segment has zero length. */ -void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]); +ll_ringbuffer_data_pair ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb); /** - * The non-copying data writer. `vec' is an array of two places. Set the values - * at `vec' to hold the current writeable data at `rb'. If the writeable data - * is in one segment the second segment has zero length. + * The non-copying data writer. Returns two ringbuffer data pointers that hold + * the current writeable data at `rb'. If the writeable data is in one segment + * the second segment has zero length. */ -void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]); +ll_ringbuffer_data_pair ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb); /** * Return the number of elements available for reading. This is the number of @@ -70,8 +72,4 @@ size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const void *src, size_t cnt); /** Advance the write pointer `cnt' places. */ void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt); -#ifdef __cplusplus -} /* extern "C" */ -#endif - #endif /* RINGBUFFER_H */ |