diff options
author | Jef <jef@targetspot.com> | 2024-09-24 08:54:57 -0400 |
---|---|---|
committer | Jef <jef@targetspot.com> | 2024-09-24 08:54:57 -0400 |
commit | 20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (patch) | |
tree | 12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/Winamp/ResamplingReader.cpp | |
parent | 537bcbc86291b32fc04ae4133ce4d7cac8ebe9a7 (diff) | |
download | winamp-20d28e80a5c861a9d5f449ea911ab75b4f37ad0d.tar.gz |
Initial community commit
Diffstat (limited to 'Src/Winamp/ResamplingReader.cpp')
-rw-r--r-- | Src/Winamp/ResamplingReader.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/Src/Winamp/ResamplingReader.cpp b/Src/Winamp/ResamplingReader.cpp new file mode 100644 index 00000000..6aa71116 --- /dev/null +++ b/Src/Winamp/ResamplingReader.cpp @@ -0,0 +1,89 @@ +/** (c) Nullsoft, Inc. C O N F I D E N T I A L + ** Filename: + ** Project: + ** Description: + ** Author: Ben Allison benski@nullsoft.com + ** Created: + **/ +#include "main.h" +#include "ResamplingReader.h" + +ResamplingReader::ResamplingReader(Resampler *_resampler, CommonReader *_reader, size_t inputFrameSize) + : resampler(_resampler), reader(_reader), + bufferValid(0), + readState(READING) +{ + bufferAlloc = inputFrameSize * 1024; // enough room for 1024 samples + buffer = (__int8 *)calloc(bufferAlloc, sizeof(__int8)); +} + +ResamplingReader::~ResamplingReader() +{ + free(buffer); + delete resampler; + delete reader; +} + +size_t ResamplingReader::ReadAudio(void *outputBuffer, size_t sizeBytes) +{ + size_t origSize = sizeBytes; + __int8 *origBuffer = (__int8 *)outputBuffer; + size_t bytesResampled = 0; +read_again: + // First, read from the file decoder + switch (readState) + { + case READING: + { + size_t bytesToRead = bufferAlloc - bufferValid; + if (bytesToRead) + { + int decode_killswitch=0, decode_error; + size_t bytesRead = reader->ReadAudio(buffer + (bufferAlloc - bytesToRead), bytesToRead, &decode_killswitch, &decode_error); + bufferValid += bytesRead; + if (bytesRead == 0) + { + readState = ENDOFFILE; + } + } + } + break; + case ENDOFFILE: + resampler->Flush(); + readState = FLUSHING; + + } + + // now, resample + size_t inputBytes = bufferValid; + size_t bytesDone; + + bytesDone = resampler->Convert(buffer, &inputBytes, outputBuffer, sizeBytes); + bytesResampled += bytesDone; + + // if we didn't use all of our input buffer, then we'll copy what's left to the beginning + if (inputBytes) + memmove(buffer, buffer + (bufferValid - inputBytes), inputBytes); + + // mark the number of bytes of data still valid in the input buffer + bufferValid = inputBytes; + + if (!bytesDone && readState == FLUSHING) + readState = DONE; + + if (bytesResampled != origSize && readState != DONE) // if we didn't provide enough data to fill the output buffer + { + sizeBytes = origSize - bytesResampled; + outputBuffer = origBuffer + bytesResampled; + goto read_again; + } + + + + return bytesResampled; + +} +#define CBCLASS ResamplingReader +START_DISPATCH; +CB(IFC_AUDIOSTREAM_READAUDIO, ReadAudio) +END_DISPATCH; |