Skip to content

Commit

Permalink
[EGD-8081] Bump up dr_flac version
Browse files Browse the repository at this point in the history
Bumped up version of dr_flac library to recent version.
Unified decoder API and made sure it is working as expected.
  • Loading branch information
alekrudnik committed Dec 10, 2021
1 parent ed783b1 commit 055bc5e
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 6,131 deletions.
12 changes: 8 additions & 4 deletions module-audio/Audio/decoder/Decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ namespace audio
static std::unique_ptr<Decoder> Create(const char *file);

protected:
virtual auto getBitWidth() -> unsigned int = 0;
virtual auto getBitWidth() -> unsigned int
{
return bitsPerSample;
}
virtual std::unique_ptr<tags::fetcher::Tags> fetchTags();

void convertmono2stereo(int16_t *pcm, uint32_t samplecount);
Expand All @@ -81,10 +84,11 @@ namespace audio

uint32_t sampleRate = 0;
uint32_t chanNumber = 0;
float position = 0;
std::FILE *fd = nullptr;
uint32_t bitsPerSample;
float position = 0;
std::FILE *fd = nullptr;
std::unique_ptr<char[]> streamBuffer;
uint32_t fileSize = 0;
uint32_t fileSize = 0;
std::string filePath;

// Worker buffer used for converting mono stream to stereo
Expand Down
33 changes: 15 additions & 18 deletions module-audio/Audio/decoder/decoderFLAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#define DR_FLAC_NO_OGG
#define DR_FLAC_NO_CRC
#define DR_FLAC_NO_SIMD

#include "dr_flac.h"
#include <src/dr_flac.h>

namespace audio
{
Expand All @@ -23,11 +22,15 @@ namespace audio
return;
}

flac = drflac_open(drflac_read, drflac_seek, this);
flac = drflac_open(drflac_read, drflac_seek, this, NULL);
if (flac == NULL) {
return;
}

chanNumber = flac->channels;
sampleRate = flac->sampleRate;
// NOTE: Always convert to S16LE as internal format
bitsPerSample = 16;
isInitialized = true;
}

Expand All @@ -41,24 +44,25 @@ namespace audio

uint32_t samples_read = 0;

samples_read = drflac_read_s16(flac, samplesToRead, (drflac_int16 *)pcmData);

samples_read = drflac_read_pcm_frames_s16(flac, samplesToRead / chanNumber, (drflac_int16 *)pcmData);
if (samples_read) {
/* Calculate frame duration in seconds */
position +=
(float)((float)(chanNumber == 2 ? samplesToRead / chanNumber : samplesToRead) / (float)sampleRate);
position += float(samples_read) / float(sampleRate);
}

return samples_read;
return samples_read * chanNumber;
}

void decoderFLAC::setPosition(float pos)
{

drflac_seek_to_sample(flac, totalSamplesCount * pos);
if (!isInitialized) {
LOG_ERROR("Wav decoder not initialized");
return;
}
drflac_seek_to_pcm_frame(flac, flac->totalPCMFrameCount * pos);

// Calculate new position
position = (float)((float)(totalSamplesCount * pos) / chanNumber / (float)(sampleRate));
position = float(flac->totalPCMFrameCount) * pos / float(sampleRate);
}

/* Data encoded in UTF-8 */
Expand All @@ -82,11 +86,4 @@ namespace audio
return !std::fseek(userdata->fd, offset, origin == drflac_seek_origin_start ? SEEK_SET : SEEK_CUR);
}

auto decoderFLAC::getBitWidth() -> unsigned int
{
TagLib::FLAC::File flacFile(filePath.c_str());
auto properties = flacFile.audioProperties();
return properties->bitsPerSample();
}

} // namespace audio
4 changes: 1 addition & 3 deletions module-audio/Audio/decoder/decoderFLAC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once

#include "Decoder.hpp"
#include "dr_flac.h"
#include <src/dr_flac.h>

namespace audio
{
Expand All @@ -22,8 +22,6 @@ namespace audio
void setPosition(float pos) override;

private:
auto getBitWidth() -> unsigned int override;

drflac *flac = nullptr;

uint32_t totalSamplesCount = 0;
Expand Down
8 changes: 2 additions & 6 deletions module-audio/Audio/decoder/decoderMP3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace audio
return;
}

// NOTE: Always convert to S16LE as internal format
bitsPerSample = 16;
isInitialized = true;
}

Expand Down Expand Up @@ -255,10 +257,4 @@ namespace audio
return samplesToRead;
}

auto decoderMP3::getBitWidth() -> unsigned int
{
static constexpr auto mp3bitWidth = 16U;
return mp3bitWidth;
}

} // namespace audio
2 changes: 0 additions & 2 deletions module-audio/Audio/decoder/decoderMP3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ namespace audio
void setPosition(float pos) override;

private:
auto getBitWidth() -> unsigned int override;

bool find_first_valid_frame();

uint32_t get_frames_count();
Expand Down
7 changes: 2 additions & 5 deletions module-audio/Audio/decoder/decoderWAV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ namespace audio
}
auto dwav = &decoderContext->wav;
drwav_seek_to_pcm_frame(dwav, dwav->totalPCMFrameCount * pos);
}

auto decoderWAV::getBitWidth() -> unsigned int
{
return bitsPerSample;
// Calculate new position
position = float(dwav->totalPCMFrameCount) * pos / float(sampleRate);
}

} // namespace audio
5 changes: 0 additions & 5 deletions module-audio/Audio/decoder/decoderWAV.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace audio
}
class decoderWAV : public Decoder
{

public:
explicit decoderWAV(const char *fileName);
virtual ~decoderWAV();
Expand All @@ -24,12 +23,8 @@ namespace audio
void setPosition(float pos) override;

private:
auto getBitWidth() -> unsigned int override;


std::vector<int32_t> pcmsamplesbuffer;
std::unique_ptr<internal::wavContext> decoderContext;
uint32_t bitsPerSample;
};

} // namespace audio
Loading

0 comments on commit 055bc5e

Please sign in to comment.