summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/mdaPiano.cpp31
-rw-r--r--src/mdaPiano.h9
-rw-r--r--src/mdaPianoCommon.h8
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);