- allow WavFileReader to read audio without resampling

This commit is contained in:
Dmytro Bogovych 2023-05-31 22:55:36 +03:00
parent 21fb865d80
commit 2aba79353a
2 changed files with 263 additions and 247 deletions

View File

@ -39,7 +39,7 @@ using namespace Audio;
// ---------------------- WavFileReader -------------------------
WavFileReader::WavFileReader()
:mHandle(nullptr), mRate(0), mLastError(0)
:mHandle(nullptr), mSamplerate(0), mLastError(0)
{
mDataOffset = 0;
}
@ -78,11 +78,11 @@ bool WavFileReader::open(const std::tstring& filename)
LOCK;
try
{
#ifdef WIN32
#ifdef WIN32
mHandle = _wfopen(filename.c_str(), L"rb");
#else
#else
mHandle = fopen(strx::makeUtf8(filename).c_str(), "rb");
#endif
#endif
if (NULL == mHandle)
{
#if defined(TARGET_ANDROID) || defined(TARGET_LINUX) || defined(TARGET_OSX)
@ -133,8 +133,8 @@ bool WavFileReader::open(const std::tstring& filename)
if (fread(&mChannels, 2, 1, mHandle) < 1)
THROW_READERROR;
mRate = 0;
if (fread(&mRate, 4, 1, mHandle) < 1)
mSamplerate = 0;
if (fread(&mSamplerate, 4, 1, mHandle) < 1)
THROW_READERROR;
unsigned int avgbytespersec = 0;
@ -162,7 +162,7 @@ bool WavFileReader::open(const std::tstring& filename)
mFileName = filename;
mDataOffset = ftell(mHandle);
mResampler.start(AUDIO_CHANNELS, mRate, AUDIO_SAMPLERATE);
mResampler.start(AUDIO_CHANNELS, mSamplerate, AUDIO_SAMPLERATE);
}
catch(...)
{
@ -181,25 +181,34 @@ void WavFileReader::close()
mHandle = nullptr;
}
int WavFileReader::rate() const
int WavFileReader::samplerate() const
{
return mRate;
return mSamplerate;
}
unsigned WavFileReader::read(void* buffer, unsigned bytes)
int WavFileReader::channels() const
{
return read((short*)buffer, bytes / (AUDIO_CHANNELS * 2)) * AUDIO_CHANNELS * 2;
return mChannels;
}
unsigned WavFileReader::read(short* buffer, unsigned samples)
unsigned WavFileReader::read(void* buffer, unsigned bytes, bool resample)
{
if (resample)
return read((short*)buffer, bytes / (AUDIO_CHANNELS * 2), true) * AUDIO_CHANNELS * 2;
else
return read((short*)buffer, bytes / channels() / sizeof(short), false) * channels() * sizeof(short);
}
unsigned WavFileReader::read(short* buffer, unsigned samples, bool resample)
{
LOCK;
if (!mHandle)
return 0;
// Get number of samples that must be read from source file
int requiredBytes = mResampler.getSourceLength(samples) * mChannels * mBits / 8;
int requiredBytes = resample ? mResampler.getSourceLength(samples) * mChannels * mBits / 8 : samples * mChannels * sizeof(short);
void* temp = alloca(requiredBytes);
memset(temp, 0, requiredBytes);
@ -214,11 +223,16 @@ unsigned WavFileReader::read(short* buffer, unsigned samples)
}
/*int readSamples = */fread(temp, 1, requiredBytes, mHandle);// / mChannels / (mBits / 8);
if (resample)
{
size_t processedBytes = 0;
size_t result = mResampler.processBuffer(temp, requiredBytes, processedBytes,
buffer, samples * 2 * AUDIO_CHANNELS);
return result / 2 / AUDIO_CHANNELS;
}
else
return requiredBytes / channels() / sizeof(short);
}
bool WavFileReader::isOpened()
@ -260,7 +274,7 @@ unsigned WavFileReader::lastError() const
#define BITS_PER_CHANNEL 16
WavFileWriter::WavFileWriter()
:mHandle(nullptr), mLengthOffset(0), mRate(AUDIO_SAMPLERATE), mChannels(1), mWritten(0)
:mHandle(nullptr), mLengthOffset(0), mRate(AUDIO_SAMPLERATE), mChannels(1), mWritten(0)
{
}

View File

@ -23,9 +23,10 @@ namespace Audio
FILE* mHandle;
short mChannels;
short mBits;
int mRate;
int mSamplerate;
std::tstring mFileName;
mutable std::recursive_mutex mFileMtx;
mutable std::recursive_mutex
mFileMtx;
unsigned mDataOffset;
unsigned mDataLength;
Resampler mResampler;
@ -40,13 +41,14 @@ namespace Audio
void close();
bool isOpened();
void rewind();
int rate() const;
int samplerate() const;
int channels() const;
// This method returns number of read bytes
unsigned read(void* buffer, unsigned bytes);
unsigned read(void* buffer, unsigned bytes, bool resample = true);
// This method returns number of read samples
unsigned read(short* buffer, unsigned samples);
unsigned read(short* buffer, unsigned samples, bool resample = true);
std::tstring filename() const;
unsigned size() const;