diff options
author | Chris Robinson <[email protected]> | 2018-12-29 12:26:45 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-12-29 12:26:45 -0800 |
commit | 8a84e7b662959015ee1e7d066351e5b7cc4fe283 (patch) | |
tree | 61d58fc4dc8d638ee45dff138754d1ccef84bd53 /Alc | |
parent | 9b0b722d7209d3cb8a31ba0823ef252906e2eb5d (diff) |
Use member functions for the sample and channel converters
Diffstat (limited to 'Alc')
-rw-r--r-- | Alc/backends/coreaudio.cpp | 7 | ||||
-rw-r--r-- | Alc/backends/wasapi.cpp | 10 | ||||
-rw-r--r-- | Alc/converter.cpp | 92 | ||||
-rw-r--r-- | Alc/converter.h | 12 |
4 files changed, 55 insertions, 66 deletions
diff --git a/Alc/backends/coreaudio.cpp b/Alc/backends/coreaudio.cpp index 2caa0852..c5da9f24 100644 --- a/Alc/backends/coreaudio.cpp +++ b/Alc/backends/coreaudio.cpp @@ -656,14 +656,13 @@ ALCenum CoreAudioCapture::captureSamples(void *buffer, ALCuint samples) auto rec_vec = mRing->getReadVector(); const void *src0{rec_vec.first.buf}; auto src0len = static_cast<ALsizei>(rec_vec.first.len); - auto got = static_cast<ALuint>(SampleConverterInput(mConverter.get(), &src0, &src0len, - buffer, samples)); + auto got = static_cast<ALuint>(mConverter->convert(&src0, &src0len, buffer, samples)); size_t total_read{rec_vec.first.len - src0len}; if(got < samples && !src0len && rec_vec.second.len > 0) { const void *src1{rec_vec.second.buf}; auto src1len = static_cast<ALsizei>(rec_vec.second.len); - got += static_cast<ALuint>(SampleConverterInput(mConverter.get(), &src1, &src1len, + got += static_cast<ALuint>(mConverter->convert(&src1, &src1len, static_cast<char*>(buffer)+got, samples-got)); total_read += rec_vec.second.len - src1len; } @@ -675,7 +674,7 @@ ALCenum CoreAudioCapture::captureSamples(void *buffer, ALCuint samples) ALCuint CoreAudioCapture::availableSamples() { if(!mConverter) return mRing->readSpace(); - return SampleConverterAvailableOut(mConverter.get(), mRing->readSpace()); + return mConverter->availableOut(mRing->readSpace()); } } // namespace diff --git a/Alc/backends/wasapi.cpp b/Alc/backends/wasapi.cpp index d6b50985..cb0b665b 100644 --- a/Alc/backends/wasapi.cpp +++ b/Alc/backends/wasapi.cpp @@ -1223,7 +1223,7 @@ FORCE_ALIGN int WasapiCapture::recordProc() if(mChannelConv) { samples.resize(numsamples*2); - ChannelConverterInput(mChannelConv.get(), rdata, samples.data(), numsamples); + mChannelConv->convert(rdata, samples.data(), numsamples); rdata = reinterpret_cast<BYTE*>(samples.data()); } @@ -1235,16 +1235,16 @@ FORCE_ALIGN int WasapiCapture::recordProc() const ALvoid *srcdata{rdata}; auto srcframes = static_cast<ALsizei>(numsamples); - dstframes = SampleConverterInput(mSampleConv.get(), &srcdata, &srcframes, - data.first.buf, (ALsizei)minz(data.first.len, INT_MAX)); + dstframes = mSampleConv->convert(&srcdata, &srcframes, data.first.buf, + static_cast<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(mSampleConv.get(), &srcdata, &srcframes, - data.second.buf, (ALsizei)minz(data.second.len, INT_MAX)); + dstframes += mSampleConv->convert(&srcdata, &srcframes, data.second.buf, + static_cast<ALsizei>(minz(data.second.len, INT_MAX))); } } else diff --git a/Alc/converter.cpp b/Alc/converter.cpp index 52472bde..0ee97fa7 100644 --- a/Alc/converter.cpp +++ b/Alc/converter.cpp @@ -171,9 +171,9 @@ SampleConverterPtr CreateSampleConverter(DevFmtType srcType, DevFmtType dstType, return converter; } -ALsizei SampleConverterAvailableOut(SampleConverter *converter, ALsizei srcframes) +ALsizei SampleConverter::availableOut(ALsizei srcframes) const { - ALint prepcount{converter->mSrcPrepCount}; + ALint prepcount{mSrcPrepCount}; if(prepcount < 0) { /* Negative prepcount means we need to skip that many input samples. */ @@ -196,23 +196,21 @@ ALsizei SampleConverterAvailableOut(SampleConverter *converter, ALsizei srcframe return 0; } - ALsizei increment{converter->mIncrement}; - ALsizei DataPosFrac{converter->mFracOffset}; auto DataSize64 = static_cast<ALuint64>(prepcount); DataSize64 += srcframes; DataSize64 -= MAX_RESAMPLE_PADDING*2; DataSize64 <<= FRACTIONBITS; - DataSize64 -= DataPosFrac; + DataSize64 -= mFracOffset; /* If we have a full prep, we can generate at least one sample. */ - return (ALsizei)clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE); + return (ALsizei)clampu64((DataSize64 + mIncrement-1)/mIncrement, 1, BUFFERSIZE); } -ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALsizei *srcframes, ALvoid *dst, ALsizei dstframes) +ALsizei SampleConverter::convert(const ALvoid **src, ALsizei *srcframes, ALvoid *dst, ALsizei dstframes) { - const ALsizei SrcFrameSize{converter->mNumChannels * converter->mSrcTypeSize}; - const ALsizei DstFrameSize{converter->mNumChannels * converter->mDstTypeSize}; - const ALsizei increment{converter->mIncrement}; + const ALsizei SrcFrameSize{mNumChannels * mSrcTypeSize}; + const ALsizei DstFrameSize{mNumChannels * mDstTypeSize}; + const ALsizei increment{mIncrement}; auto SamplesIn = static_cast<const ALbyte*>(*src); ALsizei NumSrcSamples{*srcframes}; @@ -220,19 +218,19 @@ ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALs ALsizei pos{0}; while(pos < dstframes && NumSrcSamples > 0) { - ALint prepcount{converter->mSrcPrepCount}; + ALint prepcount{mSrcPrepCount}; if(prepcount < 0) { /* Negative prepcount means we need to skip that many input samples. */ if(-prepcount >= NumSrcSamples) { - converter->mSrcPrepCount = prepcount + NumSrcSamples; + mSrcPrepCount = prepcount + NumSrcSamples; NumSrcSamples = 0; break; } SamplesIn += SrcFrameSize*-prepcount; NumSrcSamples += prepcount; - converter->mSrcPrepCount = 0; + mSrcPrepCount = 0; continue; } ALint toread{mini(NumSrcSamples, BUFFERSIZE - MAX_RESAMPLE_PADDING*2)}; @@ -243,20 +241,18 @@ ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALs /* Not enough input samples to generate an output sample. Store * what we're given for later. */ - for(ALsizei chan{0};chan < converter->mNumChannels;chan++) - LoadSamples(&converter->Chan[chan].mPrevSamples[prepcount], - SamplesIn + converter->mSrcTypeSize*chan, - converter->mNumChannels, converter->mSrcType, toread - ); + for(ALsizei chan{0};chan < mNumChannels;chan++) + LoadSamples(&Chan[chan].mPrevSamples[prepcount], SamplesIn + mSrcTypeSize*chan, + mNumChannels, mSrcType, toread); - converter->mSrcPrepCount = prepcount + toread; + mSrcPrepCount = prepcount + toread; NumSrcSamples = 0; break; } - ALfloat *RESTRICT SrcData{converter->mSrcSamples}; - ALfloat *RESTRICT DstData{converter->mDstSamples}; - ALsizei DataPosFrac{converter->mFracOffset}; + ALfloat *RESTRICT SrcData{mSrcSamples}; + ALfloat *RESTRICT DstData{mDstSamples}; + ALsizei DataPosFrac{mFracOffset}; auto DataSize64 = static_cast<ALuint64>(prepcount); DataSize64 += toread; DataSize64 -= MAX_RESAMPLE_PADDING*2; @@ -268,51 +264,46 @@ ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALs clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE)); DstSize = mini(DstSize, dstframes-pos); - for(ALsizei chan{0};chan < converter->mNumChannels;chan++) + for(ALsizei chan{0};chan < mNumChannels;chan++) { - const ALbyte *SrcSamples = SamplesIn + converter->mSrcTypeSize*chan; - ALbyte *DstSamples = (ALbyte*)dst + converter->mDstTypeSize*chan; + const ALbyte *SrcSamples = SamplesIn + mSrcTypeSize*chan; + ALbyte *DstSamples = (ALbyte*)dst + mDstTypeSize*chan; /* Load the previous samples into the source data first, then the * new samples from the input buffer. */ - std::copy_n(converter->Chan[chan].mPrevSamples, prepcount, SrcData); - LoadSamples(SrcData + prepcount, SrcSamples, - converter->mNumChannels, converter->mSrcType, toread - ); + std::copy_n(Chan[chan].mPrevSamples, prepcount, SrcData); + LoadSamples(SrcData + prepcount, SrcSamples, mNumChannels, mSrcType, toread); /* Store as many prep samples for next time as possible, given the * number of output samples being generated. */ ALsizei SrcDataEnd{(DstSize*increment + DataPosFrac)>>FRACTIONBITS}; if(SrcDataEnd >= prepcount+toread) - std::fill(std::begin(converter->Chan[chan].mPrevSamples), - std::end(converter->Chan[chan].mPrevSamples), 0.0f); + std::fill(std::begin(Chan[chan].mPrevSamples), + std::end(Chan[chan].mPrevSamples), 0.0f); else { size_t len = mini(MAX_RESAMPLE_PADDING*2, prepcount+toread-SrcDataEnd); - std::copy_n(SrcData+SrcDataEnd, len, converter->Chan[chan].mPrevSamples); - std::fill(std::begin(converter->Chan[chan].mPrevSamples)+len, - std::end(converter->Chan[chan].mPrevSamples), 0.0f); + std::copy_n(SrcData+SrcDataEnd, len, Chan[chan].mPrevSamples); + std::fill(std::begin(Chan[chan].mPrevSamples)+len, + std::end(Chan[chan].mPrevSamples), 0.0f); } /* Now resample, and store the result in the output buffer. */ - const ALfloat *ResampledData{converter->mResample(&converter->mState, - SrcData+MAX_RESAMPLE_PADDING, DataPosFrac, increment, - DstData, DstSize - )}; + const ALfloat *ResampledData{mResample(&mState, SrcData+MAX_RESAMPLE_PADDING, + DataPosFrac, increment, DstData, DstSize)}; - StoreSamples(DstSamples, ResampledData, converter->mNumChannels, - converter->mDstType, DstSize); + StoreSamples(DstSamples, ResampledData, mNumChannels, mDstType, DstSize); } /* Update the number of prep samples still available, as well as the * fractional offset. */ DataPosFrac += increment*DstSize; - converter->mSrcPrepCount = mini(prepcount + toread - (DataPosFrac>>FRACTIONBITS), - MAX_RESAMPLE_PADDING*2); - converter->mFracOffset = DataPosFrac & FRACTIONMASK; + mSrcPrepCount = mini(prepcount + toread - (DataPosFrac>>FRACTIONBITS), + MAX_RESAMPLE_PADDING*2); + mFracOffset = DataPosFrac & FRACTIONMASK; /* Update the src and dst pointers in case there's still more to do. */ SamplesIn += SrcFrameSize*(DataPosFrac>>FRACTIONBITS); @@ -338,18 +329,17 @@ ChannelConverterPtr CreateChannelConverter(DevFmtType srcType, DevFmtChannels sr return ChannelConverterPtr{new ChannelConverter{srcType, srcChans, dstChans}}; } -void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALfloat *dst, ALsizei frames) +void ChannelConverter::convert(const ALvoid *src, ALfloat *dst, ALsizei frames) const { - if(converter->mSrcChans == converter->mDstChans) + if(mSrcChans == mDstChans) { - LoadSamples(dst, src, 1, converter->mSrcType, - frames*ChannelsFromDevFmt(converter->mSrcChans, 0)); + LoadSamples(dst, src, 1, mSrcType, frames*ChannelsFromDevFmt(mSrcChans, 0)); return; } - if(converter->mSrcChans == DevFmtStereo && converter->mDstChans == DevFmtMono) + if(mSrcChans == DevFmtStereo && mDstChans == DevFmtMono) { - switch(converter->mSrcType) + switch(mSrcType) { #define HANDLE_FMT(T) case T: Stereo2Mono<T>(dst, src, frames); break HANDLE_FMT(DevFmtByte); @@ -362,9 +352,9 @@ void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALflo #undef HANDLE_FMT } } - else /*if(converter->mSrcChans == DevFmtMono && converter->mDstChans == DevFmtStereo)*/ + else /*if(mSrcChans == DevFmtMono && mDstChans == DevFmtStereo)*/ { - switch(converter->mSrcType) + switch(mSrcType) { #define HANDLE_FMT(T) case T: Mono2Stereo<T>(dst, src, frames); break HANDLE_FMT(DevFmtByte); diff --git a/Alc/converter.h b/Alc/converter.h index 6c76be07..504d60a7 100644 --- a/Alc/converter.h +++ b/Alc/converter.h @@ -28,6 +28,9 @@ struct SampleConverter { alignas(16) ALfloat mPrevSamples[MAX_RESAMPLE_PADDING*2]; } Chan[]; + ALsizei convert(const ALvoid **src, ALsizei *srcframes, ALvoid *dst, ALsizei dstframes); + ALsizei availableOut(ALsizei srcframes) const; + DEF_PLACE_NEWDEL() }; using SampleConverterPtr = std::unique_ptr<SampleConverter>; @@ -35,9 +38,6 @@ using SampleConverterPtr = std::unique_ptr<SampleConverter>; SampleConverterPtr CreateSampleConverter(DevFmtType srcType, DevFmtType dstType, ALsizei numchans, ALsizei srcRate, ALsizei dstRate, Resampler resampler); -ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALsizei *srcframes, ALvoid *dst, ALsizei dstframes); -ALsizei SampleConverterAvailableOut(SampleConverter *converter, ALsizei srcframes); - struct ChannelConverter { DevFmtType mSrcType; @@ -47,6 +47,9 @@ struct ChannelConverter { ChannelConverter(DevFmtType srctype, DevFmtChannels srcchans, DevFmtChannels dstchans) : mSrcType(srctype), mSrcChans(srcchans), mDstChans(dstchans) { } + + void convert(const ALvoid *src, ALfloat *dst, ALsizei frames) const; + DEF_NEWDEL(ChannelConverter) }; using ChannelConverterPtr = std::unique_ptr<ChannelConverter>; @@ -54,7 +57,4 @@ using ChannelConverterPtr = std::unique_ptr<ChannelConverter>; ChannelConverterPtr CreateChannelConverter(DevFmtType srcType, DevFmtChannels srcChans, DevFmtChannels dstChans); -void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALfloat *dst, - ALsizei frames); - #endif /* CONVERTER_H */ |