Skip to content

Commit 04ca3b0

Browse files
committed
Resource loading
1 parent 0d6c6f2 commit 04ca3b0

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

GenSource/genlib.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020

2121
#include "genlib.h"
2222
#include "genlib_exportfunctions.h"
23+
#include "Resource.h"
24+
#include "WavFile.h"
2325
#include <stdlib.h>
2426
#include <stdio.h>
2527
#include <string.h> // for memcpy
@@ -152,7 +154,7 @@ unsigned long systime_ticks(void)
152154
// void operator delete[](void *p) { sysmem_freeptr(p); }
153155

154156
void * genlib_obtain_reference_from_string(const char * name) {
155-
return 0; // to be implemented
157+
return (void*)Resource::load(name);
156158
}
157159

158160
// 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)
217219
self->info.channels = 0;
218220
self->info.data = 0;
219221
self->cursor = 0;
222+
if (ref != 0) {
223+
Resource* resource = (Resource*)ref;
224+
WavFile wav(resource->getData(), resource->getSize());
225+
if (wav.isValid()) {
226+
self->info.dim = wav.getNumberOfSamples();
227+
self->info.channels = wav.getNumberOfChannels();
228+
self->info.data = wav.createFloatArray().getData();
229+
self->cursor = 0;
230+
debugMessage("Num samples = ", (int)self->info.dim);
231+
debugMessage("Num channels = ", (int)self->info.channels);
232+
}
233+
Resource::destroy(resource);
234+
}
220235
return (t_genlib_data *)self;
221236
}
222237

LibSource/WavFile.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class WavFile {
7878
void* getData(){
7979
return datachunk->id + sizeof(WavDataChunk);
8080
}
81-
void read(size_t channel, FloatArray output){
81+
void read(size_t channel, FloatArray output, bool all=false){
8282
size_t channels = getNumberOfChannels();
8383
size_t len = getNumberOfSamples();
8484
if(len > output.getSize())
@@ -87,14 +87,14 @@ class WavFile {
8787
if(header->audio_format == 1 && header->bps == 8){ // WAVE_FORMAT_PCM 8-bit
8888
int8_t* data = (int8_t*)getData();
8989
for(size_t i=0; i<len; ++i){
90-
output[i] = (float)data[pos] / 128.0f;
91-
pos += channels;
90+
output[i] = (float)data[pos] / 128.0f;
91+
pos += all ? 1 : channels;
9292
}
9393
}else if(header->audio_format == 1 && header->bps == 16){ // WAVE_FORMAT_PCM 16-bit
9494
int16_t* data = (int16_t*)getData();
9595
for(size_t i=0; i<len; ++i){
96-
output[i] = (float)data[pos] / 32768.0f;
97-
pos += channels;
96+
output[i] = (float)data[pos] / 32768.0f;
97+
pos += all ? 1 : channels;
9898
}
9999
// todo: 24-bit data needs decoding
100100
// }else if(header->audio_format == 1 && header->bps == 24){ // WAVE_FORMAT_PCM 24-bit
@@ -106,8 +106,8 @@ class WavFile {
106106
}else if(header->audio_format == 3 && header->bps == 32){ // WAVE_FORMAT_IEEE_FLOAT
107107
float* data = (float*)getData();
108108
for(size_t i=0; i<len; ++i){
109-
output[i] = data[pos];
110-
pos += channels;
109+
output[i] = data[pos];
110+
pos += all ? 1 : channels;
111111
}
112112
}
113113
}
@@ -116,6 +116,11 @@ class WavFile {
116116
read(channel, output);
117117
return output;
118118
}
119+
FloatArray createFloatArray(){
120+
FloatArray output = FloatArray::create(getNumberOfSamples() * getNumberOfChannels());
121+
read(0, output, true);
122+
return output;
123+
}
119124
};
120125

121126
#endif // __WavFile_h__

0 commit comments

Comments
 (0)