From 6616113ea4e0bfb4022fe726d0a33ba769f37757 Mon Sep 17 00:00:00 2001 From: Jonathan Oakley Date: Tue, 20 Jun 2023 21:38:06 +0100 Subject: [PATCH] Permit playback of WAV file stored in RAM (#58) Duplicates some of the WaveHeaderParser stuff to extract the correct start point and channel count from the buffered WAV data, then calls playRaw() --- src/ResamplingReader.h | 29 +++++++++++++++++++++++++++++ src/playresmp.h | 6 ++++++ 2 files changed, 35 insertions(+) diff --git a/src/ResamplingReader.h b/src/ResamplingReader.h index ad74274..a7455c7 100644 --- a/src/ResamplingReader.h +++ b/src/ResamplingReader.h @@ -61,6 +61,35 @@ class ResamplingReader { return playRaw(array, true); } + bool playWav(TArray *array) { + bool result = false; + + do + { + wav_header hdr; + unsigned infoTagsSize; + wav_data_header wav_data_hdr; + WaveHeaderParser parser; + + if (!parser.readWaveHeaderFromBuffer((char*) array, hdr)) + break; + + // make unwarranted assumptions about the header format + // by using a magic number... + if (!parser.readInfoTags((unsigned char*) array, 36, infoTagsSize)) + break; + + if (!parser.readDataHeader((unsigned char*) array, 36 + infoTagsSize, wav_data_hdr)) + break; + + result = playRaw((TArray*)((char*) array + (36 + infoTagsSize + sizeof wav_data_hdr)), + wav_data_hdr.data_bytes / 2, + hdr.num_channels); + } while (0); + + return result; + } + bool play(const char *filename, bool isWave, uint16_t numChannelsIfRaw = 0) { close(); diff --git a/src/playresmp.h b/src/playresmp.h index 74784f6..4ca6052 100644 --- a/src/playresmp.h +++ b/src/playresmp.h @@ -54,6 +54,12 @@ class AudioPlayResmp : public AudioStream return playWav((int16_t *) data, fileSize); } + bool playWav(int16_t *data) + { + stop(); + return reader->playWav(data); + } + void setPlaybackRate(float f) { reader->setPlaybackRate(f); }