diff --git a/GenSource/genlib.cpp b/GenSource/genlib.cpp index c5ffd948..9cd17fb7 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; } @@ -282,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); diff --git a/LibSource/WavFile.h b/LibSource/WavFile.h index 24f9ef52..6314402f 100644 --- a/LibSource/WavFile.h +++ b/LibSource/WavFile.h @@ -78,7 +78,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()) @@ -87,14 +87,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 @@ -106,8 +106,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