Files
MuditaOS/module-audio/Audio/decoder/decoderWAV.cpp
Marcin Smoczyński 871b250d86 [EGD-4534] Change audio data path synchronization
Refactor audio data path to fix several synchronization issues and
excessive copy operations on large memory blocks. Introduce
audio::Stream data structure to allow connecting audio source and sink
with a zero-copy capability.

Introduce system mechanisms:
 - critical section guard lock needed for stream synchronization
 - non-cacheable memory allocator to allocate memory for DMA safe
   buffers

Update the Googletest CMake template to match the capabilities of the
Catch2 template.

Signed-off-by: Marcin Smoczyński <smoczynski.marcin@gmail.com>
Signed-off-by: Hubert Chrzaniuk <hubert.chrzaniuk@mudita.com>
2020-12-17 12:20:40 +01:00

81 lines
2.1 KiB
C++

// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "decoderWAV.hpp"
#include "Audio/AudioCommon.hpp"
namespace audio
{
decoderWAV::decoderWAV(const char *fileName) : Decoder(fileName)
{
if (fileSize == 0) {
return;
}
if (fd == NULL) {
return;
}
if (std::fread(&waveHeader, 1, sizeof(waveHeader), fd) != sizeof(WAVE_FormatTypeDef)) {
return;
}
// TODO:M.P; implement support for sample size different than 16bit
// pcmsamplesbuffer.reserve(1024);
sampleRate = waveHeader.SampleRate;
bitsPerSample = waveHeader.BitPerSample;
chanNumber = waveHeader.NbrChannels;
isInitialized = true;
}
uint32_t decoderWAV::decode(uint32_t samplesToRead, int16_t *pcmData)
{
uint32_t samples_read = 0;
/* TODO:M.P; implement support for sample size different than 16bit
if(samplesToRead > pcmsamplesbuffer.max_size()){
pcmsamplesbuffer.resize(samplesToRead);
}*/
switch (bitsPerSample) {
case 8:
// TODO:M.P not supported
break;
case 16:
samples_read = std::fread(pcmData, sizeof(int16_t), samplesToRead, fd);
break;
case 24:
// TODO:M.P not supported
break;
case 32:
// TODO:M.P not supported
break;
}
if (samples_read) {
/* Calculate frame duration in seconds */
position +=
(float)((float)(chanNumber == 2 ? samplesToRead / chanNumber : samplesToRead) / (float)(sampleRate));
}
return samples_read;
}
void decoderWAV::setPosition(float pos)
{
std::fseek(fd, (fileSize * pos) + sizeof(WAVE_FormatTypeDef), SEEK_SET);
// Calculate new position
position = (float)((float)(std::ftell(fd) / sizeof(int16_t) / chanNumber) / (float)(sampleRate));
}
} // namespace audio