diff --git a/README.md b/README.md index 189372e..f447e2b 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,8 @@ AudioOutputI2SNoDAC: Abuses the I2S interface to play music without a DAC. Tur AudioOutputSerialWAV: Writes a binary WAV format with headers to the Serial port. If you capture the serial output to a file you can play it back on your development system. +AudioOutputSPIFFSWAV: Writes a binary WAV format with headers to a SPIFFS filesystem. Ensure the FS is mounted and SPIFFS is started before calling. USe the SetFilename() call to pick the output file before starting. + AudioOutputNull: Just dumps samples to /dev/null. Used for speed testing as it doesn't artificially limit the AudioGenerator output speed since there are no buffers to fill/drain. ## Software I2S Delta-Sigma DAC (i.e. playing music with a single transistor and speaker) diff --git a/keywords.txt b/keywords.txt index 03be651..c7536a0 100644 --- a/keywords.txt +++ b/keywords.txt @@ -13,3 +13,4 @@ AudioOutputI2SDAC KEYWORD1 AudioOutputI2SNoDAC KEYWORD1 AudioOutputNull KEYWORD1 AudioOutputSerialWAV KEYWORD1 +AudioOutputSPIFFSWAV KEYWORD1 diff --git a/src/AudioOutputSPIFFSWAV.cpp b/src/AudioOutputSPIFFSWAV.cpp new file mode 100644 index 0000000..73a0318 --- /dev/null +++ b/src/AudioOutputSPIFFSWAV.cpp @@ -0,0 +1,110 @@ +/* + AudioOutputSPIFFSWAV + Writes a WAV file to the SPIFFS filesystem + + 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 +#include + +#include "AudioOutputSPIFFSWAV.h" + +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 AudioOutputSPIFFSWAV::SetFilename(const char *name) +{ + if (filename) free(filename); + filename = strdup(name); +} + +bool AudioOutputSPIFFSWAV::begin() +{ + uint8_t wavHeader[sizeof(wavHeaderTemplate)]; + memset(wavHeader, 0, sizeof(wavHeader)); + + if (f) return false; // Already open! + SPIFFS.remove(filename); + f = SPIFFS.open(filename, "w+"); + if (!f) return false; + + // We'll fix the header up when we close the file + f.write(wavHeader, sizeof(wavHeader)); + return true; +} + +bool AudioOutputSPIFFSWAV::ConsumeSample(int16_t sample[2]) +{ + for (int i=0; i> 8) & 0xff; + f.write(&l, sizeof(l)); + f.write(&h, sizeof(h)); + } + } + return true; +} + + +bool AudioOutputSPIFFSWAV::stop() +{ + uint8_t wavHeader[sizeof(wavHeaderTemplate)]; + + memcpy_P(wavHeader, wavHeaderTemplate, sizeof(wavHeaderTemplate)); + + int chunksize = f.size() - 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 = f.size() - 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 + f.seek(0, SeekSet); + f.write(wavHeader, sizeof(wavHeader)); + f.close(); + return true; +} + diff --git a/src/AudioOutputSPIFFSWAV.h b/src/AudioOutputSPIFFSWAV.h new file mode 100644 index 0000000..4a2afaf --- /dev/null +++ b/src/AudioOutputSPIFFSWAV.h @@ -0,0 +1,45 @@ +/* + AudioOutputSPIFFSWAV + Writes a WAV file to the SPIFFS filesystem + + 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 _AUDIOOUTPUTSPIFFSWAV_H +#define _AUDIOOUTPUTSPIFFSWAV_H + +#include +#include + +#include "AudioOutput.h" + +class AudioOutputSPIFFSWAV : public AudioOutput +{ + public: + AudioOutputSPIFFSWAV() { filename = NULL; }; + ~AudioOutputSPIFFSWAV() { 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 +