diff options
author | rekado <rekado@elephly.net> | 2013-01-19 21:40:02 +0800 |
---|---|---|
committer | rekado <rekado@elephly.net> | 2013-01-19 21:57:22 +0800 |
commit | e55e6f5ae2fb57bb8a2c341fa1cc726195aae2ed (patch) | |
tree | 61ee460265389ec2b56082f607ff2893e9de008a /src | |
parent | dd86a9c084b01c1f5f5f74dfc921a8167d6fcd7c (diff) |
load individual samples
Diffstat (limited to 'src')
-rw-r--r-- | src/mdaPiano.cpp | 31 | ||||
-rw-r--r-- | src/mdaPiano.h | 9 | ||||
-rw-r--r-- | src/mdaPianoCommon.h | 8 |
3 files changed, 34 insertions, 14 deletions
diff --git a/src/mdaPiano.cpp b/src/mdaPiano.cpp index 9ecbc3e..4f9fa2c 100644 --- a/src/mdaPiano.cpp +++ b/src/mdaPiano.cpp @@ -21,17 +21,26 @@ #include <math.h> #define STRING_BUF 2048 -static const char* sample_file = "samples.raw"; - mdaPiano::mdaPiano(double rate) : LV2::Synth<mdaPianoVoice, mdaPiano>(p_n_ports, p_midi) { - load_samples(&waves); + static const char* sample_names[] = + { "0c.raw", "0e.raw", "0g.raw" + , "1c.raw", "1e.raw", "1g.raw" + , "2c.raw", "2e.raw", "2g.raw" + , "3c.raw", "3e.raw", "3g.raw" + , "4c.raw", "4e.raw", "4a.raw" + }; + + for (unsigned char i=0; i<15; ++i) { + load_sample(&samples[i], sample_names[i]); + } + load_kgrp(kgrp); for(uint32_t i=0; i<NVOICES; ++i) { - voices[i] = new mdaPianoVoice(rate, waves, kgrp); + voices[i] = new mdaPianoVoice(rate, samples, kgrp); add_voices(voices[i]); } @@ -190,7 +199,7 @@ void mdaPiano::load_kgrp(KGRP *kgrp) } -void mdaPiano::load_samples(short **buffer) +void mdaPiano::load_sample(Sample *s, const char* name) { FILE *f; long num, size; @@ -198,7 +207,7 @@ void mdaPiano::load_samples(short **buffer) strncpy(filepath, bundle_path(), STRING_BUF); strncat(filepath, - sample_file, + name, STRING_BUF - strlen(filepath)); f = fopen(filepath, "rb"); if (f == NULL) { @@ -212,19 +221,23 @@ void mdaPiano::load_samples(short **buffer) rewind(f); // allocate memory to contain the whole file - *buffer = (short*) malloc (sizeof(short)*size); - if (*buffer == NULL) { + s->buffer = (short*) malloc (sizeof(short)*size); + if (s->buffer == NULL) { fputs("Memory error", stderr); exit(2); } // copy the file into the buffer - num = fread(*buffer, 1, size, f); + num = fread(s->buffer, 1, size, f); if (num != size) { fputs ("Reading error", stderr); exit (3); } fclose (f); + + // 16 bit + s->size = size / 2; + return; } diff --git a/src/mdaPiano.h b/src/mdaPiano.h index e808a09..bda11cc 100644 --- a/src/mdaPiano.h +++ b/src/mdaPiano.h @@ -13,11 +13,14 @@ class mdaPiano : public LV2::Synth<mdaPianoVoice, mdaPiano> { public: mdaPiano(double rate); ~mdaPiano() { - free(waves); + for (unsigned char i = 0; i < 15; i++) { + free(samples[i].buffer); + } + free(samples); } void load_kgrp(KGRP*); - void load_samples(short**); + void load_sample(Sample*, const char*); unsigned find_free_voice(unsigned char, unsigned char); void handle_midi(uint32_t size, unsigned char* data); @@ -35,7 +38,7 @@ private: ///global internal variables KGRP kgrp[16]; mdaPianoVoice *voices[NVOICES]; - short *waves; + Sample *samples = (Sample*) malloc (15 * sizeof(Sample)); uint32_t sustain; }; diff --git a/src/mdaPianoCommon.h b/src/mdaPianoCommon.h index ed0c01d..581ffd5 100644 --- a/src/mdaPianoCommon.h +++ b/src/mdaPianoCommon.h @@ -12,11 +12,15 @@ struct KGRP //keygroup { long root; //MIDI root note long high; //highest note - long pos; - long end; long loop; }; +typedef struct +{ + long size; //length of sample data + short* buffer; //pointer to sample data +} Sample; + static float scale_midi_to_f(unsigned char data) { return 0.0078f * (float)(data); |