diff --git a/src/AudioFileSourceSTDIO.cpp b/src/AudioFileSourceSTDIO.cpp new file mode 100644 index 0000000..8e6013a --- /dev/null +++ b/src/AudioFileSourceSTDIO.cpp @@ -0,0 +1,82 @@ +/* + AudioFileSourceSTDIO + Input STDIO "file" to be used by AudioGenerator + Only for hot-based testing + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#ifndef ARDUINO + +#include "AudioFileSourceSTDIO.h" + +AudioFileSourceSTDIO::AudioFileSourceSTDIO() +{ + f = NULL; +} + +AudioFileSourceSTDIO::AudioFileSourceSTDIO(const char *filename) +{ + open(filename); +} + +bool AudioFileSourceSTDIO::open(const char *filename) +{ + f = fopen(filename, "rb"); + return f; +} + +AudioFileSourceSTDIO::~AudioFileSourceSTDIO() +{ + if (f) fclose(f); + f = NULL; +} + +uint32_t AudioFileSourceSTDIO::read(void *data, uint32_t len) +{ + return fread(reinterpret_cast(data), 1, len, f); +} + +bool AudioFileSourceSTDIO::seek(int32_t pos, int dir) +{ + return fseek(f, pos, dir); +} + +bool AudioFileSourceSTDIO::close() +{ + fclose(f); + f = NULL; + return true; +} + +bool AudioFileSourceSTDIO::isOpen() +{ + return f?true:false; +} + +uint32_t AudioFileSourceSTDIO::getSize() +{ + if (!f) return 0; + uint32_t p = ftell(f); + fseek(f, 0, SEEK_END); + uint32_t len = ftell(f); + fseek(f, p, SEEK_SET); + return len; +} + + +#endif diff --git a/src/AudioFileSourceSTDIO.h b/src/AudioFileSourceSTDIO.h new file mode 100644 index 0000000..46a4681 --- /dev/null +++ b/src/AudioFileSourceSTDIO.h @@ -0,0 +1,53 @@ +/* + AudioFileSourceSTDIO + Input SPIFFS "file" to be used by AudioGenerator + Only for host-based testing, not Arduino + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _AUDIOFILESOURCESTDIO_H +#define _AUDIOFILESOURCESTDIO_H + +#include + +#ifndef ARDUINO + +#include "AudioFileSource.h" + +class AudioFileSourceSTDIO : public AudioFileSource +{ + public: + AudioFileSourceSTDIO(); + AudioFileSourceSTDIO(const char *filename); + virtual ~AudioFileSourceSTDIO() override; + + virtual bool open(const char *filename) override; + virtual uint32_t read(void *data, uint32_t len) override; + virtual bool seek(int32_t pos, int dir) override; + virtual bool close() override; + virtual bool isOpen() override; + virtual uint32_t getSize() override; + virtual uint32_t getPos() override { if (!f) return 0; else return (uint32_t)ftell(f); }; + + private: + FILE *f; +}; + +#endif // !ARDUINO + +#endif + diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index 6a3672f..296b582 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -236,7 +236,7 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output) #undef stack extern "C" { #ifdef ESP32 - //TOTO - add ESP32 checks + //TODO - add ESP32 checks void stack(const char *s, const char *t, int i) { } @@ -244,7 +244,7 @@ extern "C" { { return 8192; } -#else +#elif defined(ESP8266) #include extern cont_t g_cont; @@ -278,6 +278,14 @@ extern "C" { int freestack = 4 * (sp - g_cont.stack); return freestack; } -#endif +#else + void stack(const char *s, const char *t, int i) + { + } + int stackfree() + { + return 8192; + } +#endif } diff --git a/src/AudioOutputSTDIO.cpp b/src/AudioOutputSTDIO.cpp new file mode 100644 index 0000000..33bbd34 --- /dev/null +++ b/src/AudioOutputSTDIO.cpp @@ -0,0 +1,115 @@ +/* + AudioOutputSTDIO + Writes a WAV file to the STDIO filesystem + Only for host-based testing + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#ifndef ARDUINO + +#include "AudioOutputSTDIO.h" +#include + +static const uint8_t wavHeaderTemplate[] PROGMEM = { // Hardcoded simple WAV header with 0xffffffff lengths all around + 0x52, 0x49, 0x46, 0x46, 0xff, 0xff, 0xff, 0xff, 0x57, 0x41, 0x56, 0x45, + 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x22, 0x56, 0x00, 0x00, 0x88, 0x58, 0x01, 0x00, 0x04, 0x00, 0x10, 0x00, + 0x64, 0x61, 0x74, 0x61, 0xff, 0xff, 0xff, 0xff }; + +void AudioOutputSTDIO::SetFilename(const char *name) +{ + free(filename); + filename = strdup(name); +} + +bool AudioOutputSTDIO::begin() +{ + uint8_t wavHeader[sizeof(wavHeaderTemplate)]; + memset(wavHeader, 0, sizeof(wavHeader)); + + if (f) return false; // Already open! + unlink(filename); + f = fopen(filename, "wb+"); + if (!f) return false; + + // We'll fix the header up when we close the file + fwrite(wavHeader, sizeof(wavHeader), 1, f); + return true; +} + +bool AudioOutputSTDIO::ConsumeSample(int16_t sample[2]) +{ + for (int i=0; i> 8) & 0xff; + fwrite(&l, sizeof(l), 1, f); + fwrite(&h, sizeof(h), 1, f); + } + } + return true; +} + + +bool AudioOutputSTDIO::stop() +{ + uint8_t wavHeader[sizeof(wavHeaderTemplate)]; + + memcpy_P(wavHeader, wavHeaderTemplate, sizeof(wavHeaderTemplate)); + + int chunksize = ftell(f) - 8; + wavHeader[4] = chunksize & 0xff; + wavHeader[5] = (chunksize>>8)&0xff; + wavHeader[6] = (chunksize>>16)&0xff; + wavHeader[7] = (chunksize>>24)&0xff; + + wavHeader[22] = channels & 0xff; + wavHeader[23] = 0; + + wavHeader[24] = hertz & 0xff; + wavHeader[25] = (hertz >> 8) & 0xff; + wavHeader[26] = (hertz >> 16) & 0xff; + wavHeader[27] = (hertz >> 24) & 0xff; + int byteRate = hertz * bps * channels / 8; + wavHeader[28] = byteRate & 0xff; + wavHeader[29] = (byteRate >> 8) & 0xff; + wavHeader[30] = (byteRate >> 16) & 0xff; + wavHeader[31] = (byteRate >> 24) & 0xff; + wavHeader[32] = channels * bps / 8; + wavHeader[33] = 0; + wavHeader[34] = bps; + wavHeader[35] = 0; + + int datasize = ftell(f) - sizeof(wavHeader); + wavHeader[40] = datasize & 0xff; + wavHeader[41] = (datasize>>8)&0xff; + wavHeader[42] = (datasize>>16)&0xff; + wavHeader[43] = (datasize>>24)&0xff; + + // Write real header out + fseek(f, 0, SEEK_SET); + fwrite(wavHeader, sizeof(wavHeader), 1, f); + fclose(f); + return true; +} + +#endif + diff --git a/src/AudioOutputSTDIO.h b/src/AudioOutputSTDIO.h new file mode 100644 index 0000000..f5a1028 --- /dev/null +++ b/src/AudioOutputSTDIO.h @@ -0,0 +1,47 @@ +/* + AudioOutputSTDIO + Writes a WAV file to the STDIO filesystem + Only for host-based testing + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _AUDIOOUTPUTSTDIO_H +#define _AUDIOOUTPUTSTDIO_H + +#include +#ifndef ARDUINO + +#include "AudioOutput.h" + +class AudioOutputSTDIO : public AudioOutput +{ + public: + AudioOutputSTDIO() { filename = NULL; f = NULL; }; + ~AudioOutputSTDIO() { free(filename); }; + virtual bool begin() override; + virtual bool ConsumeSample(int16_t sample[2]) override; + virtual bool stop() override; + void SetFilename(const char *name); + + private: + FILE *f; + char *filename; +}; + +#endif + +#endif diff --git a/src/libtinysoundfont/tsf.h b/src/libtinysoundfont/tsf.h index 5306f50..0dbdb59 100644 --- a/src/libtinysoundfont/tsf.h +++ b/src/libtinysoundfont/tsf.h @@ -1257,11 +1257,7 @@ static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, i void DumpF32P32(char *name, long long x) { -#ifdef ESP32 - printf("%s = %08x.%08x\n", name, (int)((x>>32)&0xffffffff), (int)(x&0xffffffff)); -#else - printf("%s = %08x.%08x\n", name, (int32)((x>>32)&0xffffffff), (int32)(x&0xffffffff)); -#endif + printf("%s = %08x.%08x\n", name, (int32_t)((x>>32)&0xffffffff), (int32_t)(x&0xffffffff)); } static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuffer, int numSamples) { diff --git a/tests/host/Arduino.h b/tests/host/Arduino.h new file mode 100644 index 0000000..f83789a --- /dev/null +++ b/tests/host/Arduino.h @@ -0,0 +1,80 @@ +#ifdef ARDUINO +#error This file is only used for host builds +#endif + +#ifndef MINIARD +#define MINIARD + +#include +#include +#include +#include +#include + +#define PROGMEM +#define PSTR +#define memcpy_P memcpy +#define sprintf_P sprintf +#define yield() do {} while(0) +#define printf_P printf +#define strcpy_P strcpy +#define snprintf_P snprintf + +#ifdef __cplusplus +class SerialEmulator { + public: + SerialEmulator() {}; + ~SerialEmulator() {}; + int printf_P(const char *fmt, ...) { va_list ap; va_start(ap, fmt); int r = vprintf(fmt, ap); va_end(ap); return r; }; + void flush() {}; +}; +extern SerialEmulator Serial; +#endif + +#ifndef ICACHE_RODATA_ATTR +#define ICACHE_RODATA_ATTR +#endif + +#ifndef PROGMEM +#define PROGMEM +#endif + +#ifndef PGM_P +#define PGM_P +#endif + +#ifndef PGM_VOID_P +#define PGM_VOID_P +#endif + +#ifndef PSTR +#define PSTR +#endif + +#ifdef __cplusplus + #define pgm_read_byte(addr) (*reinterpret_cast(addr)) + #define pgm_read_word(addr) (*reinterpret_cast(addr)) + #define pgm_read_dword(addr) (*reinterpret_cast(addr)) + #define pgm_read_float(addr) (*reinterpret_cast(addr)) + #define pgm_read_ptr(addr) (*reinterpret_cast(addr)) +#else + #define pgm_read_byte(addr) (*(const uint8_t*)(addr)) + #define pgm_read_word(addr) (*(const uint16_t*)(addr)) + #define pgm_read_dword(addr) (*(const uint32_t*)(addr)) + #define pgm_read_float(addr) (*(const float)(addr)) + #define pgm_read_ptr(addr) (*(const void const *)(addr)) +#endif + +#define pgm_read_byte_near(addr) pgm_read_byte(addr) +#define pgm_read_word_near(addr) pgm_read_word(addr) +#define pgm_read_dword_near(addr) pgm_read_dword(addr) +#define pgm_read_float_near(addr) pgm_read_float(addr) +#define pgm_read_ptr_near(addr) pgm_read_ptr(addr) +#define pgm_read_byte_far(addr) pgm_read_byte(addr) +#define pgm_read_word_far(addr) pgm_read_word(addr) +#define pgm_read_dword_far(addr) pgm_read_dword(addr) +#define pgm_read_float_far(addr) pgm_read_float(addr) +#define pgm_read_ptr_far(addr) pgm_read_ptr(addr) + + +#endif diff --git a/tests/host/Makefile b/tests/host/Makefile new file mode 100644 index 0000000..1af8ade --- /dev/null +++ b/tests/host/Makefile @@ -0,0 +1,45 @@ +all: mp3 + +libmad=../../src/libmad/decoder.c ../../src/libmad/frame.c ../../src/libmad/bit.c ../../src/libmad/stream.c ../../src/libmad/fixed.c \ +../../src/libmad/timer.c ../../src/libmad/layer3.c ../../src/libmad/synth.c ../../src/libmad/huffman.c ../../src/libmad/version.c + + +libhelix_mp3=../../src/libhelix-mp3/dequant.c ../../src/libhelix-mp3/mp3dec.c ../../src/libhelix-mp3/stproc.c \ +../../src/libhelix-mp3/hufftabs.c ../../src/libhelix-mp3/dct32.c ../../src/libhelix-mp3/trigtabs.c \ +../../src/libhelix-mp3/dqchan.c ../../src/libhelix-mp3/scalfact.c ../../src/libhelix-mp3/polyphase.c ../../src/libhelix-mp3/buffers.c \ +../../src/libhelix-mp3/bitstream.c ../../src/libhelix-mp3/imdct.c ../../src/libhelix-mp3/subband.c ../../src/libhelix-mp3/huffman.c \ +../../src/libhelix-mp3/mp3tabs.c + +audiolib=../../src/AudioGeneratorWAV.cpp ../../src/AudioGeneratorMIDI.cpp ../../src/AudioFileSourceSTDIO.cpp ../../src/AudioOutputSTDIO.cpp \ +../../src/AudioFileSourceID3.cpp ../../src/AudioGeneratorAAC.cpp ../../src/AudioGeneratorMP3.cpp ../../src/AudioOutputFilterDecimate.cpp \ +../../src/AudioGeneratorFLAC.cpp ../../src/AudioGeneratorMOD.cpp ../../src/AudioFileSourceBuffer.cpp ../../src/AudioGeneratorMP3a.cpp \ +Serial.cpp + +libhelix_aac=../../src/libhelix-aac/decelmnt.c ../../src/libhelix-aac/dct4.c ../../src/libhelix-aac/dequant.c ../../src/libhelix-aac/sbrhuff.c \ +../../src/libhelix-aac/sbrmath.c ../../src/libhelix-aac/aactabs.c ../../src/libhelix-aac/stproc.c ../../src/libhelix-aac/hufftabs.c \ +../../src/libhelix-aac/sbrtabs.c ../../src/libhelix-aac/sbrfft.c ../../src/libhelix-aac/filefmt.c ../../src/libhelix-aac/tns.c \ +../../src/libhelix-aac/trigtabs.c ../../src/libhelix-aac/fft.c ../../src/libhelix-aac/pns.c ../../src/libhelix-aac/sbrfreq.c \ +../../src/libhelix-aac/sbrside.c ../../src/libhelix-aac/sbrhfadj.c ../../src/libhelix-aac/buffers.c ../../src/libhelix-aac/bitstream.c \ +../../src/libhelix-aac/noiseless.c ../../src/libhelix-aac/imdct.c ../../src/libhelix-aac/aacdec.c ../../src/libhelix-aac/sbrhfgen.c \ +../../src/libhelix-aac/sbrqmf.c ../../src/libhelix-aac/huffman.c ../../src/libhelix-aac/sbr.c ../../src/libhelix-aac/sbrimdct.c + +libflac=../../src/libflac/md5.c ../../src/libflac/window.c ../../src/libflac/memory.c ../../src/libflac/cpu.c ../../src/libflac/fixed.c \ +../../src/libflac/format.c ../../src/libflac/lpc.c ../../src/libflac/crc.c ../../src/libflac/bitreader.c ../../src/libflac/bitmath.c \ +../../src/libflac/stream_decoder.c ../../src/libflac/float.c + + +CCOPTS=-g -Wall -m32 -include Arduino.h +CPPOPTS=-g -Wall -std=c++11 -m32 -include Arduino.h + +mp3: FORCE + rm -f *.o + gcc $(CCOPTS) -c $(libmad) -I ../../src/ -I. + g++ $(CPPOPTS) -o mp3 mp3.cpp Serial.cpp *.o ../../src/AudioFileSourceSTDIO.cpp ../../src/AudioOutputSTDIO.cpp ../../src/AudioFileSourceID3.cpp ../../src/AudioGeneratorMP3.cpp -I ../../src/ -I. + rm -f *.o + valgrind --leak-check=full --track-origins=yes -v --error-limit=no --show-leak-kinds=all ./mp3 + +clean: + rm -f mp3 *.o + + +FORCE: diff --git a/tests/host/Serial.cpp b/tests/host/Serial.cpp new file mode 100644 index 0000000..7e9f0a5 --- /dev/null +++ b/tests/host/Serial.cpp @@ -0,0 +1,4 @@ +#include + +SerialEmulator Serial; + diff --git a/tests/host/mp3.cpp b/tests/host/mp3.cpp new file mode 100644 index 0000000..f022abc --- /dev/null +++ b/tests/host/mp3.cpp @@ -0,0 +1,22 @@ +#include +#include "AudioFileSourceSTDIO.h" +#include "AudioOutputSTDIO.h" +#include "AudioGeneratorMP3.h" + +int main(int argc, char **argv) +{ + (void) argc; + (void) argv; + AudioFileSourceSTDIO *in = new AudioFileSourceSTDIO("jamonit.mp3"); + AudioOutputSTDIO *out = new AudioOutputSTDIO(); + out->SetFilename("jamonit.wav"); + AudioGeneratorMP3 *mp3 = new AudioGeneratorMP3(); + + mp3->begin(in, out); + while (mp3->loop()) { /*noop*/ } + mp3->stop(); + + delete mp3; + delete out; + delete in; +}