Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions GenSource/genlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "genlib.h"
#include "genlib_exportfunctions.h"
#include "Resource.h"
#include "WavFile.h"
#include "message.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h> // for memcpy
Expand Down Expand Up @@ -152,7 +155,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.
Expand Down Expand Up @@ -208,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)
Expand All @@ -217,6 +221,28 @@ 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()) {
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);
}
return (t_genlib_data *)self;
}

Expand Down Expand Up @@ -261,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) {
Expand All @@ -282,11 +317,12 @@ void genlib_data_resize(t_genlib_data *b, long s, long c) {
self->info.channels = c;
}

set_zero64(self->info.data, s * c);
if (!keep)
set_zero64(self->info.data, s * c);
return;

} else {

// allocate new:
replaced = (t_sample *)sysmem_newptr(sz);

Expand Down
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* 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
-----

Expand Down
31 changes: 22 additions & 9 deletions LibSource/WavFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,28 @@ class WavFile {
void* getData(){
return datachunk->id + sizeof(WavDataChunk);
}
void read(size_t channel, FloatArray output){
/**
* 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();
uint8_t* data = (uint8_t*)getData(); // 8-bit WAV is unsigned, 128 = 0
for(size_t i=0; i<len; ++i){
output[i] = (float)data[pos] / 128.0f;
pos += channels;
output[i] = ((float)data[pos] - 128.0f) / 128.0f;
pos += all ? 1 : channels;
}
}else if(header->audio_format == 1 && header->bps == 16){ // WAVE_FORMAT_PCM 16-bit
int16_t* data = (int16_t*)getData();
for(size_t i=0; i<len; ++i){
output[i] = (float)data[pos] / 32768.0f;
pos += channels;
output[i] = (float)data[pos] / 32768.0f;
pos += all ? 1 : channels;
}
// todo: 24-bit data needs decoding
// }else if(header->audio_format == 1 && header->bps == 24){ // WAVE_FORMAT_PCM 24-bit
Expand All @@ -108,8 +113,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<len; ++i){
output[i] = data[pos];
pos += channels;
output[i] = data[pos];
pos += all ? 1 : channels;
}
}
}
Expand All @@ -118,6 +123,14 @@ class WavFile {
read(channel, output);
return output;
}
/**
* Create an array with all channels, interleaved
*/
FloatArray createFloatArray(){
FloatArray output = FloatArray::create(getNumberOfSamples() * getNumberOfChannels());
read(0, output, true);
return output;
}
};

#endif // __WavFile_h__
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 88 additions & 0 deletions TestPatches/GenDataResourceTestPatch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "TestPatch.hpp"
#include "WavTestData.h"
#include "../GenSource/genlib.cpp"
#include <stdio.h>
#include <unistd.h>

/*
* 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<typename F>
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);
}
};
83 changes: 83 additions & 0 deletions TestPatches/WavFileTestPatch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#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("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 };
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);
}
}
};
28 changes: 28 additions & 0 deletions TestPatches/WavTestData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef __WavTestData_h__
#define __WavTestData_h__

#include <string.h>
#include <stdint.h>

// 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__