WiP: necessary changes in old decoder code

Added some unit tests
This commit is contained in:
Mateusz committed 2019-07-22 22:48:56 +02:00
1 parent fd17c1a0d9
commit fdbfc73b5e
13 files changed
+142 -42

No files matched your search

+8 -6
View File
@@ -7,7 +7,7 @@
* @details
*/
#include "decoder.h"
#include "decoder.hpp"
#include "decoderMP3.hpp"
#include "decoderFLAC.hpp"
@@ -20,19 +20,21 @@ namespace decoder {
: filePath(fileName),
workerBuffer(std::make_unique<int16_t[]>(workerBufferSize)) {
fd = core::vfs::fopen(fileName, "r");
fd = vfs.fopen(fileName, "r");
if (fd == NULL) {
return;
}
core::vfs::fseek(fd,0,SEEK_END);
fileSize = core::vfs::ftell(fd);
core::vfs::rewind(fd);
vfs.fseek(fd,0,SEEK_END);
fileSize = vfs.ftell(fd);
vfs.rewind(fd);
}
decoder::~decoder() {
core::vfs::fclose(fd);
if(fd){
vfs.fclose(fd);
}
}
std::unique_ptr<decoder> decoder::Create(const char *file) {
@@ -15,7 +15,7 @@
#include <memory>
#include <cstring>
#include "vfs/vfs.hpp"
#include "vfs.hpp"
namespace decoder {
@@ -106,12 +106,13 @@ namespace decoder {
uint32_t sampleRate = 0;
uint32_t chanNumber = 0;
float position = 0;
core::FILE *fd = nullptr;
vfs::FILE *fd = nullptr;
uint32_t fileSize = 0;
std::string filePath;
// Worker buffer used for converting mono stream to stereo
std::unique_ptr<int16_t[]> workerBuffer;
};
}
+3 -3
View File
@@ -42,7 +42,7 @@ namespace decoder {
if (pos == std::string::npos) {
tags->title.append(filePath);
} else {
tags->title.append(&filePath[pos]);
tags->title.append(&filePath[pos+1]);
}
}
return std::make_unique<Tags>(*tags);
@@ -96,13 +96,13 @@ namespace decoder {
size_t decoderFLAC::drflac_read(void *pUserData, void *pBufferOut, size_t bytesToRead) {
decoderFLAC *userdata = (decoderFLAC *) pUserData;
return core::vfs::fread(pBufferOut, 1, bytesToRead, userdata->fd);
return vfs.fread(pBufferOut, 1, bytesToRead, userdata->fd);
}
drflac_bool32 decoderFLAC::drflac_seek(void *pUserData, int offset, drflac_seek_origin origin) {
decoderFLAC *userdata = (decoderFLAC *) pUserData;
return !core::vfs::fseek(userdata->fd, offset, origin == drflac_seek_origin_start ? SEEK_SET : SEEK_CUR);
return !vfs.fseek(userdata->fd, offset, origin == drflac_seek_origin_start ? SEEK_SET : SEEK_CUR);
}
+1 -1
View File
@@ -10,7 +10,7 @@
#ifndef UNTITLED1_DECODERFLAC_HPP
#define UNTITLED1_DECODERFLAC_HPP
#include "decoder.h"
#include "decoder.hpp"
#include "dr_flac.h"
+17 -17
View File
@@ -43,7 +43,7 @@ namespace decoder {
void decoderMP3::setPosition(float pos){
decoderNotFirstRun = false;
core::vfs::fseek(fd,(pos*fileSize)+firstValidFrameFileOffset,SEEK_SET);
vfs.fseek(fd,(pos*fileSize)+firstValidFrameFileOffset,SEEK_SET);
//TODO: M.P Currently calculating MP3 position is unsupported, in general seeking is supported though.
//position += (float) ((float) (samplesToReadChann / chanNumber) / (float) sampleRate);
@@ -51,10 +51,10 @@ namespace decoder {
std::unique_ptr<Tags> decoderMP3::fetchTags() {
core::vfs::fseek(fd, firstValidFrameFileOffset + 4, SEEK_SET);
vfs.fseek(fd, firstValidFrameFileOffset + 4, SEEK_SET);
auto buff = std::make_unique<uint8_t[]>(firstValidFrameByteSize);
core::vfs::fread(buff.get(), 1, firstValidFrameByteSize, fd);
vfs.fread(buff.get(), 1, firstValidFrameByteSize, fd);
xing_info_t xinfo = {};
if (parseXingHeader(buff.get(),firstValidFrameByteSize, &xinfo)) {
@@ -74,13 +74,13 @@ namespace decoder {
tag->duration_hour = tag->duration_min / 60;
tag->duration_sec = tag->total_duration_s % 60;
core::vfs::rewind(fd);
vfs.rewind(fd);
// Parse ID3 tags
// Allocate buffer for fetching initial part of MP3, 1Mbyte should be sufficient to fetch all necessary tags
auto source = std::make_unique<std::array<char, 1024 * 1024>>();
core::vfs::fread(&(*source)[0], 1, source->size(), fd);
vfs.fread(&(*source)[0], 1, source->size(), fd);
if (atag::id3v2::is_tagged(*source)) {
@@ -99,11 +99,11 @@ namespace decoder {
if (pos == std::string::npos) {
tag->title.append(filePath);
} else {
tag->title.append(&filePath[pos]);
tag->title.append(&filePath[pos+1]);
}
}
core::vfs::rewind(fd);
vfs.rewind(fd);
return std::make_unique<Tags>(*tag);
}
@@ -118,9 +118,9 @@ namespace decoder {
auto decBuffer = std::make_unique<uint8_t[]>(DECODER_BUFFER_SIZE);
core::vfs::rewind(fd);
vfs.rewind(fd);
if (core::vfs::fread(decBuffer.get(), 1, DECODER_BUFFER_SIZE, fd) == 0) {
if (vfs.fread(decBuffer.get(), 1, DECODER_BUFFER_SIZE, fd) == 0) {
return false;
}
@@ -129,7 +129,7 @@ namespace decoder {
//refill buffer if necessary(only if over 87,5% of bytes are consumed)
if (bufferIndex > (DECODER_BUFFER_SIZE - (DECODER_BUFFER_SIZE / 8))) {
memcpy(&decBuffer[0], &decBuffer[bufferIndex], bytesAvailable);
uint32_t bytesRead = core::vfs::fread(&decBuffer[bytesAvailable], 1, DECODER_BUFFER_SIZE - bytesAvailable, fd);
uint32_t bytesRead = vfs.fread(&decBuffer[bytesAvailable], 1, DECODER_BUFFER_SIZE - bytesAvailable, fd);
if (bytesRead == 0) {
return false;
@@ -153,9 +153,9 @@ namespace decoder {
sampleRate = info.hz;
chanNumber = info.channels;
firstValidFrameByteSize = (144 * info.bitrate_kbps * 1000 / info.hz);
firstValidFrameFileOffset = core::vfs::ftell(fd) - bytesAvailable - firstValidFrameByteSize;
firstValidFrameFileOffset = vfs.ftell(fd) - bytesAvailable - firstValidFrameByteSize;
core::vfs::rewind(fd);
vfs.rewind(fd);
return true;
}
@@ -183,10 +183,10 @@ namespace decoder {
auto decBuffer = std::make_unique<uint8_t[]>(DECODER_BUFFER_SIZE);
// Jump to the file beginning
core::vfs::rewind(fd);
vfs.rewind(fd);
/* Fill decBuffer */
if (core::vfs::fread(decBuffer.get(), 1, DECODER_BUFFER_SIZE, fd) == 0) {
if (vfs.fread(decBuffer.get(), 1, DECODER_BUFFER_SIZE, fd) == 0) {
return 0;
}
@@ -195,7 +195,7 @@ namespace decoder {
//refill buffer if necessary(only if over 87,5% of bytes are consumed)
if (bufferIndex > (DECODER_BUFFER_SIZE - (DECODER_BUFFER_SIZE / 8))) {
memcpy(&decBuffer[0], &decBuffer[bufferIndex], bytesAvailable);
uint32_t bytesRead = core::vfs::fread(decBuffer.get() + bytesAvailable, 1, DECODER_BUFFER_SIZE - bytesAvailable, fd);
uint32_t bytesRead = vfs.fread(decBuffer.get() + bytesAvailable, 1, DECODER_BUFFER_SIZE - bytesAvailable, fd);
if (bytesRead != (DECODER_BUFFER_SIZE - bytesAvailable)) {
last_refill = true;
@@ -242,7 +242,7 @@ namespace decoder {
pcmsamplesbuffer = std::make_unique<uint16_t[]>(pcmsamplesbuffer_size);
// Fill decoderBuffer
uint32_t bytesRead = core::vfs::fread(decoderBuffer.get(), 1, DECODER_BUFFER_SIZE, fd);
uint32_t bytesRead = vfs.fread(decoderBuffer.get(), 1, DECODER_BUFFER_SIZE, fd);
if (bytesRead == 0) {
return 0;
@@ -264,7 +264,7 @@ namespace decoder {
//refill buffer if necessary(only if over 87,5% of bytes consumed)
if ( !lastRefill && (decoderBufferIdx > (DECODER_BUFFER_SIZE - (DECODER_BUFFER_SIZE / 8)))) {
memcpy(&decoderBuffer[0], &decoderBuffer[decoderBufferIdx], bytesAvailable);
uint32_t bytesRead = core::vfs::fread(&decoderBuffer[bytesAvailable], 1, DECODER_BUFFER_SIZE - bytesAvailable, fd);
uint32_t bytesRead = vfs.fread(&decoderBuffer[bytesAvailable], 1, DECODER_BUFFER_SIZE - bytesAvailable, fd);
if (bytesRead != (DECODER_BUFFER_SIZE - bytesAvailable)) {
lastRefill = true;
+1 -1
View File
@@ -15,7 +15,7 @@
#include <iostream>
#include <cstring>
#include "decoder.h"
#include "decoder.hpp"
+11 -7
View File
@@ -16,8 +16,12 @@ namespace decoder {
decoder(fileName),
tag(std::make_unique<Tags>()) {
if (core::vfs::fread(&waveHeader, 1, sizeof(waveHeader), fd) != sizeof(WAVE_FormatTypeDef)) {
//error
if(fd == NULL){
return;
}
if (vfs.fread(&waveHeader, 1, sizeof(waveHeader), fd) != sizeof(WAVE_FormatTypeDef)) {
return;
}
tag->total_duration_s = (fileSize - sizeof(WAVE_FormatTypeDef)) / waveHeader.ByteRate;
@@ -39,7 +43,7 @@ namespace decoder {
if (pos == std::string::npos) {
tag->title.append(filePath);
} else {
tag->title.append(&filePath[pos]);
tag->title.append(&filePath[pos+1]);
}
return std::make_unique<Tags>(*tag);
@@ -51,7 +55,7 @@ namespace decoder {
// mono
if (chanNumber == 1) {
samples_read = core::vfs::fread(pcmData, sizeof(int16_t), samplesToReadChann, fd);
samples_read = vfs.fread(pcmData, sizeof(int16_t), samplesToReadChann, fd);
if (samples_read == 0) {
return samples_read;
}
@@ -63,7 +67,7 @@ namespace decoder {
}
//stereo
else {
samples_read = core::vfs::fread(pcmData, sizeof(int16_t), samplesToReadChann, fd);
samples_read = vfs.fread(pcmData, sizeof(int16_t), samplesToReadChann, fd);
}
if (samples_read) {
@@ -76,9 +80,9 @@ namespace decoder {
void decoderWAV::setPosition(float pos){
core::vfs::fseek(fd,(fileSize*pos)+ sizeof(WAVE_FormatTypeDef),SEEK_SET);
vfs.fseek(fd,(fileSize*pos)+ sizeof(WAVE_FormatTypeDef),SEEK_SET);
// Calculate new position
position = (float) ((float) (core::vfs::ftell(fd)/sizeof(int16_t) / chanNumber) / (float) (sampleRate));
position = (float) ((float) (vfs.ftell(fd)/sizeof(int16_t) / chanNumber) / (float) (sampleRate));
}
+1 -2
View File
@@ -10,7 +10,7 @@
#ifndef UNTITLED1_DECODERWAV_HPP
#define UNTITLED1_DECODERWAV_HPP
#include "decoder.h"
#include "decoder.hpp"
namespace decoder {
@@ -51,7 +51,6 @@ namespace decoder {
WAVE_FormatTypeDef waveHeader;
uint32_t bitsPerSample;
std::unique_ptr<Tags> tag = nullptr;
};
}
+1 -1
View File
@@ -7,7 +7,7 @@
* @details
*/
#define _GNU_SOURCE
#include "xing_header.h"
#include <string.h>
+96 -2
View File
@@ -16,8 +16,102 @@
#include "catch.hpp"
#include "Audio/decoder/decoder.hpp"
TEST_CASE( "Decoder tests" ) {
auto cwd = vfs.getcurrdir();
cwd = cwd.substr(0,cwd.find_last_of("/\\"));
cwd = cwd.append("/module-audio/tests/samples");
SECTION("Sample1.wav 16bit 44100Hz stereo")
{
auto dec = decoder::decoder::Create((cwd + "/sample1.wav").c_str());
REQUIRE(dec != nullptr);
auto tag = dec->fetchTags();
SECTION( "Init values:" ) {
REQUIRE( tag->total_duration_s == 5 );
REQUIRE( tag->artist == "" );
REQUIRE( tag->album == "" );
REQUIRE( tag->title == "sample1.wav" );
REQUIRE( tag->sample_rate == 44100 );
REQUIRE( tag->num_channel == 2 );
}
/* SECTION( "Set positions:" ) {
dec->setPosition(0.5);
REQUIRE( dec->getCurrentPosition() == 336/2 );
dec->setPosition(0);
REQUIRE( dec->getCurrentPosition() == 0 );
dec->setPosition(1);
REQUIRE( dec->getCurrentPosition() == 336 );
dec->setPosition(0.25);
REQUIRE( dec->getCurrentPosition() == 336/4 );
}*/
}
SECTION("Sample1.mp3-> CBR 320Kb stereo")
{
auto dec = decoder::decoder::Create((cwd + "/sample1.mp3").c_str());
REQUIRE(dec != nullptr);
auto tag = dec->fetchTags();
SECTION( "Init values:" ) {
REQUIRE( tag->total_duration_s == 5 );
REQUIRE( tag->artist == "" );
REQUIRE( tag->album == "" );
REQUIRE( tag->title == "sample1.mp3" );
REQUIRE( tag->sample_rate == 44100 );
REQUIRE( tag->num_channel == 2 );
}
/* SECTION( "Set positions:" ) {
dec->setPosition(0.5);
REQUIRE( dec->getCurrentPosition() == 336/2 );
dec->setPosition(0);
REQUIRE( dec->getCurrentPosition() == 0 );
dec->setPosition(1);
REQUIRE( dec->getCurrentPosition() == 336 );
dec->setPosition(0.25);
REQUIRE( dec->getCurrentPosition() == 336/4 );
}*/
}
SECTION("Sample1.flac-> stereo")
{
auto dec = decoder::decoder::Create((cwd + "/sample1.flac").c_str());
REQUIRE(dec != nullptr);
auto tag = dec->fetchTags();
SECTION( "Init values:" ) {
REQUIRE( tag->total_duration_s == 5 );
REQUIRE( tag->artist == "" );
REQUIRE( tag->album == "" );
REQUIRE( tag->title == "sample1.flac" );
REQUIRE( tag->sample_rate == 44100 );
REQUIRE( tag->num_channel == 2 );
}
/* SECTION( "Set positions:" ) {
dec->setPosition(0.5);
REQUIRE( dec->getCurrentPosition() == 336/2 );
dec->setPosition(0);
REQUIRE( dec->getCurrentPosition() == 0 );
dec->setPosition(1);
REQUIRE( dec->getCurrentPosition() == 336 );
dec->setPosition(0.25);
REQUIRE( dec->getCurrentPosition() == 336/4 );
}*/
}
TEST_CASE("Tokenizer tests")
{
}
Binary file not shown.
Binary file not shown.
Binary file not shown.