123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #include <stdlib.h>
- #include <memory.h>
- #include <string.h>
- #include <assert.h>
- #include "FIFOSampleBuffer.h"
- using namespace soundtouch;
- FIFOSampleBuffer::FIFOSampleBuffer(int numChannels)
- {
- assert(numChannels > 0);
- sizeInBytes = 0;
- buffer = nullptr;
- bufferUnaligned = nullptr;
- samplesInBuffer = 0;
- bufferPos = 0;
- channels = (uint)numChannels;
- ensureCapacity(32);
- }
- FIFOSampleBuffer::~FIFOSampleBuffer()
- {
- delete[] bufferUnaligned;
- bufferUnaligned = nullptr;
- buffer = nullptr;
- }
- void FIFOSampleBuffer::setChannels(int numChannels)
- {
- uint usedBytes;
- if (!verifyNumberOfChannels(numChannels)) return;
- usedBytes = channels * samplesInBuffer;
- channels = (uint)numChannels;
- samplesInBuffer = usedBytes / channels;
- }
- void FIFOSampleBuffer::rewind()
- {
- if (buffer && bufferPos)
- {
- memmove(buffer, ptrBegin(), sizeof(SAMPLETYPE) * channels * samplesInBuffer);
- bufferPos = 0;
- }
- }
- void FIFOSampleBuffer::putSamples(const SAMPLETYPE *samples, uint nSamples)
- {
- memcpy(ptrEnd(nSamples), samples, sizeof(SAMPLETYPE) * nSamples * channels);
- samplesInBuffer += nSamples;
- }
- void FIFOSampleBuffer::putSamples(uint nSamples)
- {
- uint req;
- req = samplesInBuffer + nSamples;
- ensureCapacity(req);
- samplesInBuffer += nSamples;
- }
- SAMPLETYPE *FIFOSampleBuffer::ptrEnd(uint slackCapacity)
- {
- ensureCapacity(samplesInBuffer + slackCapacity);
- return buffer + samplesInBuffer * channels;
- }
- SAMPLETYPE *FIFOSampleBuffer::ptrBegin()
- {
- assert(buffer);
- return buffer + bufferPos * channels;
- }
- void FIFOSampleBuffer::ensureCapacity(uint capacityRequirement)
- {
- SAMPLETYPE *tempUnaligned, *temp;
- if (capacityRequirement > getCapacity())
- {
-
- sizeInBytes = (capacityRequirement * channels * sizeof(SAMPLETYPE) + 4095) & (uint)-4096;
- assert(sizeInBytes % 2 == 0);
- tempUnaligned = new SAMPLETYPE[sizeInBytes / sizeof(SAMPLETYPE) + 16 / sizeof(SAMPLETYPE)];
- if (tempUnaligned == nullptr)
- {
- ST_THROW_RT_ERROR("Couldn't allocate memory!\n");
- }
-
- temp = (SAMPLETYPE *)SOUNDTOUCH_ALIGN_POINTER_16(tempUnaligned);
- if (samplesInBuffer)
- {
- memcpy(temp, ptrBegin(), samplesInBuffer * channels * sizeof(SAMPLETYPE));
- }
- delete[] bufferUnaligned;
- buffer = temp;
- bufferUnaligned = tempUnaligned;
- bufferPos = 0;
- }
- else
- {
-
- rewind();
- }
- }
- uint FIFOSampleBuffer::getCapacity() const
- {
- return sizeInBytes / (channels * sizeof(SAMPLETYPE));
- }
- uint FIFOSampleBuffer::numSamples() const
- {
- return samplesInBuffer;
- }
- uint FIFOSampleBuffer::receiveSamples(SAMPLETYPE *output, uint maxSamples)
- {
- uint num;
- num = (maxSamples > samplesInBuffer) ? samplesInBuffer : maxSamples;
- memcpy(output, ptrBegin(), channels * sizeof(SAMPLETYPE) * num);
- return receiveSamples(num);
- }
- uint FIFOSampleBuffer::receiveSamples(uint maxSamples)
- {
- if (maxSamples >= samplesInBuffer)
- {
- uint temp;
- temp = samplesInBuffer;
- samplesInBuffer = 0;
- return temp;
- }
- samplesInBuffer -= maxSamples;
- bufferPos += maxSamples;
- return maxSamples;
- }
- int FIFOSampleBuffer::isEmpty() const
- {
- return (samplesInBuffer == 0) ? 1 : 0;
- }
- void FIFOSampleBuffer::clear()
- {
- samplesInBuffer = 0;
- bufferPos = 0;
- }
- uint FIFOSampleBuffer::adjustAmountOfSamples(uint numSamples)
- {
- if (numSamples < samplesInBuffer)
- {
- samplesInBuffer = numSamples;
- }
- return samplesInBuffer;
- }
- void FIFOSampleBuffer::addSilent(uint nSamples)
- {
- memset(ptrEnd(nSamples), 0, sizeof(SAMPLETYPE) * nSamples * channels);
- samplesInBuffer += nSamples;
- }
|