From 38bbbdde24a9ccf943940e1a4e04c21c43ef8cde Mon Sep 17 00:00:00 2001 From: antisvin Date: Sat, 2 Jul 2022 18:49:21 +0300 Subject: [PATCH 1/7] Resource loading --- GenSource/genlib.cpp | 17 ++++++++++++++++- LibSource/WavFile.h | 19 ++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/GenSource/genlib.cpp b/GenSource/genlib.cpp index c5ffd948..dd61c5a2 100644 --- a/GenSource/genlib.cpp +++ b/GenSource/genlib.cpp @@ -20,6 +20,8 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "genlib.h" #include "genlib_exportfunctions.h" +#include "Resource.h" +#include "WavFile.h" #include #include #include // for memcpy @@ -152,7 +154,7 @@ unsigned long systime_ticks(void) // void operator delete[](void *p) { sysmem_freeptr(p); } void * genlib_obtain_reference_from_string(const char * name) { - return 0; // to be implemented + return (void*)Resource::load(name); } // the rest is stuff to isolate gensym, attrs, atoms, buffers etc. @@ -217,6 +219,19 @@ t_genlib_data * genlib_obtain_data_from_reference(void *ref) self->info.channels = 0; self->info.data = 0; self->cursor = 0; + if (ref != 0) { + Resource* resource = (Resource*)ref; + WavFile wav(resource->getData(), resource->getSize()); + if (wav.isValid()) { + self->info.dim = wav.getNumberOfSamples(); + self->info.channels = wav.getNumberOfChannels(); + self->info.data = wav.createFloatArray().getData(); + self->cursor = 0; + // debugMessage("Num samples = ", (int)self->info.dim); + // debugMessage("Num channels = ", (int)self->info.channels); + } + Resource::destroy(resource); + } return (t_genlib_data *)self; } diff --git a/LibSource/WavFile.h b/LibSource/WavFile.h index 8b42715e..0e0941f3 100644 --- a/LibSource/WavFile.h +++ b/LibSource/WavFile.h @@ -80,7 +80,7 @@ class WavFile { void* getData(){ return datachunk->id + sizeof(WavDataChunk); } - void read(size_t channel, FloatArray output){ + void read(size_t channel, FloatArray output, bool all=false){ size_t channels = getNumberOfChannels(); size_t len = getNumberOfSamples(); if(len > output.getSize()) @@ -89,14 +89,14 @@ class WavFile { if(header->audio_format == 1 && header->bps == 8){ // WAVE_FORMAT_PCM 8-bit int8_t* data = (int8_t*)getData(); for(size_t i=0; iaudio_format == 1 && header->bps == 16){ // WAVE_FORMAT_PCM 16-bit int16_t* data = (int16_t*)getData(); for(size_t i=0; iaudio_format == 1 && header->bps == 24){ // WAVE_FORMAT_PCM 24-bit @@ -108,8 +108,8 @@ class WavFile { }else if(header->audio_format == 3 && header->bps == 32){ // WAVE_FORMAT_IEEE_FLOAT float* data = (float*)getData(); for(size_t i=0; i Date: Sat, 5 Nov 2022 09:40:54 +0200 Subject: [PATCH 2/7] Don't erase buffer to preserve WAV data --- GenSource/genlib.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/GenSource/genlib.cpp b/GenSource/genlib.cpp index dd61c5a2..9cd17fb7 100644 --- a/GenSource/genlib.cpp +++ b/GenSource/genlib.cpp @@ -297,11 +297,13 @@ void genlib_data_resize(t_genlib_data *b, long s, long c) { self->info.channels = c; } - set_zero64(self->info.data, s * c); + // This erases the buffer, but it seems to be a bug. If enabled, + // old buffer contents is lost. This prevents correct WAV file playback. + //set_zero64(self->info.data, s * c); return; } else { - + // allocate new: replaced = (t_sample *)sysmem_newptr(sz); From ac597d94c33bf11a53cfffcbbe35906714df30c2 Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:07:31 +0100 Subject: [PATCH 3/7] WavFile::read(all): read every channel, not one channel's worth of samples With all=true the loop ran getNumberOfSamples() (frames) times, so createFloatArray() filled only the first 1/channels of the interleaved buffer for multichannel files. --- LibSource/WavFile.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/LibSource/WavFile.h b/LibSource/WavFile.h index 0e0941f3..f72315f9 100644 --- a/LibSource/WavFile.h +++ b/LibSource/WavFile.h @@ -80,12 +80,17 @@ class WavFile { void* getData(){ return datachunk->id + sizeof(WavDataChunk); } + /** + * Read one channel, or with all=true every channel, interleaved + */ void read(size_t channel, FloatArray output, bool all=false){ size_t channels = getNumberOfChannels(); size_t len = getNumberOfSamples(); + if(all) + len *= channels; if(len > output.getSize()) len = output.getSize(); - size_t pos = channel % channels; + size_t pos = all ? 0 : channel % channels; if(header->audio_format == 1 && header->bps == 8){ // WAVE_FORMAT_PCM 8-bit int8_t* data = (int8_t*)getData(); for(size_t i=0; i Date: Sat, 28 Nov 2026 04:17:07 +0100 Subject: [PATCH 4/7] genlib: fix gen~ data loaded from a WAV resource - Copy all channels: allocate the interleaved buffer with sysmem_newptr(), which genlib_data_release() frees with sysmem_freeptr(), instead of FloatArray::create() (new[]). - Zero on resize again, except for data loaded from a resource; plain gen~ data is cleared on reset as before. - Report a declared size that differs from the WAV file with debugMessage(): the overlapping frames and channels are kept. --- GenSource/genlib.cpp | 37 ++++++++++++++++++++++++++++--------- HISTORY.md | 2 ++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/GenSource/genlib.cpp b/GenSource/genlib.cpp index 9cd17fb7..e6809c7f 100644 --- a/GenSource/genlib.cpp +++ b/GenSource/genlib.cpp @@ -22,6 +22,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "genlib_exportfunctions.h" #include "Resource.h" #include "WavFile.h" +#include "message.h" #include #include #include // for memcpy @@ -210,6 +211,7 @@ typedef struct { t_genlib_data_info info; t_sample cursor; // used by Delay //t_symbol * name; + int loaded; // filled from a WAV resource: never zeroed on resize } t_dsp_gen_data; t_genlib_data * genlib_obtain_data_from_reference(void *ref) @@ -219,16 +221,25 @@ t_genlib_data * genlib_obtain_data_from_reference(void *ref) self->info.channels = 0; self->info.data = 0; self->cursor = 0; + self->loaded = 0; if (ref != 0) { + static_assert(sizeof(t_sample) == sizeof(float), "WAV resources need 32-bit t_sample"); Resource* resource = (Resource*)ref; WavFile wav(resource->getData(), resource->getSize()); if (wav.isValid()) { - self->info.dim = wav.getNumberOfSamples(); - self->info.channels = wav.getNumberOfChannels(); - self->info.data = wav.createFloatArray().getData(); - self->cursor = 0; - // debugMessage("Num samples = ", (int)self->info.dim); - // debugMessage("Num channels = ", (int)self->info.channels); + size_t frames = wav.getNumberOfSamples(); + size_t channels = wav.getNumberOfChannels(); + // allocate with sysmem_newptr: genlib_data_release() frees with sysmem_freeptr + t_sample* data = (t_sample *)sysmem_newptr(sizeof(t_sample) * frames * channels); + if (data) { + wav.read(0, FloatArray((float*)data, frames * channels), true); + self->info.dim = frames; + self->info.channels = channels; + self->info.data = data; + self->loaded = 1; + } else { + genlib_report_error("allocating [data]: out of memory"); + } } Resource::destroy(resource); } @@ -276,6 +287,15 @@ void genlib_data_resize(t_genlib_data *b, long s, long c) { old = self->info.data; olddim = self->info.dim; oldchannels = self->info.channels; + + // keep data loaded from a resource + int keep = self->loaded; + if (keep) { + if (s != olddim) + debugMessage("data frames / WAV frames", (int)s, olddim); + if (c != oldchannels) + debugMessage("data channels / WAV channels", (int)c, oldchannels); + } // limit [data] size: if (s * c > DATA_MAXIMUM_ELEMENTS) { @@ -297,9 +317,8 @@ void genlib_data_resize(t_genlib_data *b, long s, long c) { self->info.channels = c; } - // This erases the buffer, but it seems to be a bug. If enabled, - // old buffer contents is lost. This prevents correct WAV file playback. - //set_zero64(self->info.data, s * c); + if (!keep) + set_zero64(self->info.data, s * c); return; } else { diff --git a/HISTORY.md b/HISTORY.md index 614aae06..1def4b67 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,5 @@ +* Added gen~ data loaded from WAV resources: a data object named after a resource is filled from it + v24.1 ----- From a96ab64c5e263d9f232a6549ebca37551c525b46 Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:17:07 +0100 Subject: [PATCH 5/7] Add WavFile and gen~ data resource unit tests WavFileTest: header, per-channel and interleaved reads. GenDataResourceTest: gen~ Data::reset() filled from a WAV file through the native resource loader, declared size longer and mono, size-mismatch messages, plain data zeroed on reset. --- TestPatches/GenDataResourceTestPatch.hpp | 88 ++++++++++++++++++++++++ TestPatches/WavFileTestPatch.hpp | 68 ++++++++++++++++++ TestPatches/WavTestData.h | 28 ++++++++ 3 files changed, 184 insertions(+) create mode 100644 TestPatches/GenDataResourceTestPatch.hpp create mode 100644 TestPatches/WavFileTestPatch.hpp create mode 100644 TestPatches/WavTestData.h diff --git a/TestPatches/GenDataResourceTestPatch.hpp b/TestPatches/GenDataResourceTestPatch.hpp new file mode 100644 index 00000000..045b4140 --- /dev/null +++ b/TestPatches/GenDataResourceTestPatch.hpp @@ -0,0 +1,88 @@ +#include "TestPatch.hpp" +#include "WavTestData.h" +#include "../GenSource/genlib.cpp" +#include +#include + +/* + * Unit test for gen~ data loaded from a WAV resource (genlib.cpp): a data + * object named after a resource is filled from the WAV file, interleaved; + * a declared size that differs keeps the overlapping frames and reports + * the mismatch with debugMessage(); plain data is still zeroed on resize. + * The native test harness loads resources from files and prints debug + * messages to stdout, which the test captures. + */ +class GenDataResourceTestPatch : public TestPatch { +public: + static void writeFile(const char* name, const uint8_t* data, size_t len){ + FILE* f = fopen(name, "wb"); + fwrite(data, 1, len, f); + fclose(f); + } + // run fn with stdout redirected; return whether its output contains text + template + static bool printed(const char* text, F fn){ + fflush(stdout); + int saved = dup(1); + FILE* tmp = tmpfile(); + dup2(fileno(tmp), 1); + fn(); + fflush(stdout); + dup2(saved, 1); + close(saved); + char out[1024] = {}; + rewind(tmp); + fread(out, 1, sizeof(out)-1, tmp); + fclose(tmp); + return strstr(out, text) != NULL; + } + GenDataResourceTestPatch(){ + const char* name = "Build/Test/gendata.wav"; + // stereo 16-bit PCM, 4 frames + const int16_t pcm[] = { 1000, -1000, 2000, -2000, 3000, -3000, 4000, -4000 }; + uint8_t buf[44 + sizeof(pcm)]; + writeFile(name, buf, makeWav(buf, 1, 2, 16, pcm, sizeof(pcm))); + { + TEST("data filled from resource"); + Data data; + CHECK(!printed("WAV", [&]{ data.reset(name, 4, 2); })); // sizes match: no message + CHECK_EQUAL((int)data.dim, 4); + CHECK_EQUAL((int)data.channels, 2); + for(int i=0; i<4; ++i){ + CHECK_CLOSE(data.read(i, 0), pcm[2*i]/32768.0f, 1e-6); + CHECK_CLOSE(data.read(i, 1), pcm[2*i+1]/32768.0f, 1e-6); + } + data.reset(name, 4, 2); // gen~ reset: resource data is kept + CHECK_CLOSE(data.read(3, 1), pcm[7]/32768.0f, 1e-6); + } + { + TEST("declared longer than resource"); + Data data; + CHECK(printed("data frames / WAV frames 6 4", [&]{ data.reset(name, 6, 2); })); + CHECK_EQUAL((int)data.dim, 6); + for(int i=0; i<4; ++i) + CHECK_CLOSE(data.read(i, 0), pcm[2*i]/32768.0f, 1e-6); + CHECK_EQUAL(data.read(4, 0), 0); + CHECK_EQUAL(data.read(5, 1), 0); + } + { + TEST("declared mono"); + Data data; + CHECK(printed("data channels / WAV channels 1 2", [&]{ data.reset(name, 4, 1); })); + CHECK_EQUAL((int)data.channels, 1); + for(int i=0; i<4; ++i) + CHECK_CLOSE(data.read(i, 0), pcm[2*i]/32768.0f, 1e-6); // left channel kept + } + { + TEST("plain data is zeroed"); + Data data; + data.reset("no-such-resource", 8, 1); + CHECK_EQUAL((int)data.dim, 8); + data.write(0.5f, 3, 0); + CHECK_EQUAL(data.read(3, 0), 0.5f); + data.reset("no-such-resource", 8, 1); // gen~ reset clears plain data + CHECK_EQUAL(data.read(3, 0), 0); + } + remove(name); + } +}; diff --git a/TestPatches/WavFileTestPatch.hpp b/TestPatches/WavFileTestPatch.hpp new file mode 100644 index 00000000..11b71ac5 --- /dev/null +++ b/TestPatches/WavFileTestPatch.hpp @@ -0,0 +1,68 @@ +#include "TestPatch.hpp" +#include "WavFile.h" +#include "WavTestData.h" + +/* + * Unit test for WavFile: header parsing, per-channel read and interleaved + * read of all channels (read(channel, output, true) / createFloatArray()). + */ +class WavFileTestPatch : public TestPatch { +public: + WavFileTestPatch(){ + // stereo 16-bit PCM, 3 frames: L = 1000, 2000, 3000; R = -1000, -2000, -3000 + const int16_t pcm[] = { 1000, -1000, 2000, -2000, 3000, -3000 }; + uint8_t buf[44 + sizeof(pcm)]; + size_t len = makeWav(buf, 1, 2, 16, pcm, sizeof(pcm)); + { + TEST("header"); + WavFile wav(buf, len); + CHECK(wav.isValid()); + CHECK_EQUAL(wav.getNumberOfChannels(), 2); + CHECK_EQUAL(wav.getNumberOfSamples(), 3); + } + { + TEST("read one channel"); + WavFile wav(buf, len); + FloatArray left = wav.createFloatArray(0); + FloatArray right = wav.createFloatArray(1); + CHECK_EQUAL(left.getSize(), 3); + for(int i=0; i<3; ++i){ + CHECK_CLOSE(left[i], (i+1)*1000/32768.0f, 1e-6); + CHECK_CLOSE(right[i], -(i+1)*1000/32768.0f, 1e-6); + } + FloatArray::destroy(left); + FloatArray::destroy(right); + } + { + TEST("read all channels interleaved"); + WavFile wav(buf, len); + FloatArray all = wav.createFloatArray(); + CHECK_EQUAL(all.getSize(), 6); + for(int i=0; i<6; ++i) + CHECK_CLOSE(all[i], pcm[i]/32768.0f, 1e-6); + FloatArray::destroy(all); + } + { + TEST("read all into a short buffer"); + WavFile wav(buf, len); + FloatArray out = FloatArray::create(4); + wav.read(0, out, true); + for(int i=0; i<4; ++i) + CHECK_CLOSE(out[i], pcm[i]/32768.0f, 1e-6); + FloatArray::destroy(out); + } + { + TEST("mono float"); + const float f[] = { 0.25f, -0.5f, 1.0f, 0.0f }; + uint8_t fbuf[44 + sizeof(f)]; + size_t flen = makeWav(fbuf, 3, 1, 32, f, sizeof(f)); + WavFile wav(fbuf, flen); + CHECK(wav.isValid()); + FloatArray all = wav.createFloatArray(); + CHECK_EQUAL(all.getSize(), 4); + for(int i=0; i<4; ++i) + CHECK_EQUAL(all[i], f[i]); + FloatArray::destroy(all); + } + } +}; diff --git a/TestPatches/WavTestData.h b/TestPatches/WavTestData.h new file mode 100644 index 00000000..36f38959 --- /dev/null +++ b/TestPatches/WavTestData.h @@ -0,0 +1,28 @@ +#ifndef __WavTestData_h__ +#define __WavTestData_h__ + +#include +#include + +// Build a minimal RIFF/WAVE file in memory: 44-byte header plus interleaved data. +// Returns the total size in bytes; out must hold 44 + data bytes. +static size_t makeWav(uint8_t* out, uint16_t format, uint16_t channels, uint16_t bps, + const void* data, uint32_t databytes){ + uint32_t u32; uint16_t u16; + memcpy(out, "RIFF", 4); + u32 = 36 + databytes; memcpy(out+4, &u32, 4); + memcpy(out+8, "WAVEfmt ", 8); + u32 = 16; memcpy(out+16, &u32, 4); + memcpy(out+20, &format, 2); + memcpy(out+22, &channels, 2); + u32 = 48000; memcpy(out+24, &u32, 4); + u32 = 48000*channels*(bps/8); memcpy(out+28, &u32, 4); + u16 = channels*(bps/8); memcpy(out+32, &u16, 2); + memcpy(out+34, &bps, 2); + memcpy(out+36, "data", 4); + memcpy(out+40, &databytes, 4); + memcpy(out+44, data, databytes); + return 44 + databytes; +} + +#endif // __WavTestData_h__ From a11071943c5d1a9c6010e5ec61883d26dbbcae38 Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:34:09 +0100 Subject: [PATCH 6/7] WavFile: decode 8-bit PCM as unsigned WAV stores 8-bit samples as unsigned with 128 as zero; they were read as int8_t, so 128 became -1.0 and the waveform was inverted and offset. Test added to WavFileTest. --- HISTORY.md | 1 + LibSource/WavFile.h | 4 ++-- TestPatches/WavFileTestPatch.hpp | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 1def4b67..67e629e8 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,4 @@ +* Fixed WavFile reading 8-bit PCM as signed (WAV stores 8-bit samples unsigned) * Added gen~ data loaded from WAV resources: a data object named after a resource is filled from it v24.1 diff --git a/LibSource/WavFile.h b/LibSource/WavFile.h index f72315f9..3205407b 100644 --- a/LibSource/WavFile.h +++ b/LibSource/WavFile.h @@ -92,9 +92,9 @@ class WavFile { len = output.getSize(); size_t pos = all ? 0 : channel % channels; if(header->audio_format == 1 && header->bps == 8){ // WAVE_FORMAT_PCM 8-bit - int8_t* data = (int8_t*)getData(); + uint8_t* data = (uint8_t*)getData(); // 8-bit WAV is unsigned, 128 = 0 for(size_t i=0; iaudio_format == 1 && header->bps == 16){ // WAVE_FORMAT_PCM 16-bit diff --git a/TestPatches/WavFileTestPatch.hpp b/TestPatches/WavFileTestPatch.hpp index 11b71ac5..ae14774b 100644 --- a/TestPatches/WavFileTestPatch.hpp +++ b/TestPatches/WavFileTestPatch.hpp @@ -51,6 +51,21 @@ class WavFileTestPatch : public TestPatch { CHECK_CLOSE(out[i], pcm[i]/32768.0f, 1e-6); FloatArray::destroy(out); } + { + TEST("8-bit PCM is unsigned"); + const uint8_t u8[] = { 128, 255, 0, 192, 64 }; + uint8_t ubuf[44 + sizeof(u8)]; + size_t ulen = makeWav(ubuf, 1, 1, 8, u8, sizeof(u8)); + WavFile wav(ubuf, ulen); + CHECK(wav.isValid()); + FloatArray all = wav.createFloatArray(); + CHECK_EQUAL(all[0], 0.0f); + CHECK_CLOSE(all[1], 127/128.0f, 1e-6); + CHECK_EQUAL(all[2], -1.0f); + CHECK_EQUAL(all[3], 0.5f); + CHECK_EQUAL(all[4], -0.5f); + FloatArray::destroy(all); + } { TEST("mono float"); const float f[] = { 0.25f, -0.5f, 1.0f, 0.0f }; From 03210999e49b2e36fd62355654373abda06db1ce Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:42:03 +0100 Subject: [PATCH 7/7] make tests: start from a clean build directory Native tests compile every source in Build/Source, so generated Gen/Faust/Heavy sources left by an earlier patch build were linked into each unit test (duplicate symbols with GenDataResourceTest, which includes genlib.cpp). --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5cea1d3a..e769d021 100644 --- a/Makefile +++ b/Makefile @@ -191,7 +191,7 @@ run: $(DEPS) ## run patch natively grind: $(DEPS) ## run valgrind on patch natively @$(MAKE) -s -f native.mk grind -tests: ## run all unit tests +tests: clean ## run all unit tests @for nm in $(TESTS) ; do \ $(MAKE) -s TEST=$$nm test || exit;\ done