From 534525ce9b41b107f310ac45b832e985cdd773fb Mon Sep 17 00:00:00 2001 From: Nick Lanham Date: Mon, 2 Apr 2012 11:35:08 +0200 Subject: [PATCH] Use midi velocity to set intensity of sample. Core only --- drmr.c | 29 ++++++++++++++++++++++++----- drmr.h | 1 + 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/drmr.c b/drmr.c index 2880d14..c958672 100644 --- a/drmr.c +++ b/drmr.c @@ -23,6 +23,7 @@ #include "drmr_hydrogen.h" #define REQ_BUF_SIZE 10 +#define VELOCITY_MAX 127 static int current_kit_changed = 0; @@ -228,6 +229,21 @@ static inline void trigger_sample(DrMr *drmr, int nn, uint8_t* const data) { } drmr->samples[nn].active = 1; drmr->samples[nn].offset = 0; + drmr->samples[nn].velocity = ((float)data[2])/VELOCITY_MAX; + } + pthread_mutex_unlock(&drmr->load_mutex); +} + +static inline void untrigger_sample(DrMr *drmr, int nn) { + pthread_mutex_lock(&drmr->load_mutex); + if (nn >= 0 && nn < drmr->num_samples) { + if (drmr->samples[nn].layer_count > 0) { + layer_to_sample(drmr->samples+nn,*(drmr->gains[nn])); + if (drmr->samples[nn].limit == 0) + fprintf(stderr,"Failed to find layer at: %i for %f\n",nn,*drmr->gains[nn]); + } + drmr->samples[nn].active = 0; + drmr->samples[nn].offset = 0; } pthread_mutex_unlock(&drmr->load_mutex); } @@ -253,14 +269,17 @@ static void run(LV2_Handle instance, uint32_t n_samples) { LV2_SEQUENCE_FOREACH(drmr->control_port, i) { LV2_Atom_Event* const ev = lv2_sequence_iter_get(i); if (ev->body.type == drmr->uris.midi_event) { + uint8_t nn; uint8_t* const data = (uint8_t* const)(ev + 1); //int channel = *data & 15; switch ((*data) >> 4) { - case 8: // ignore note-offs for now, should probably be a setting - //if (drmr->cur_samp) drmr->cur_samp->active = 0; + case 8: + nn = data[1]; + nn-=baseNote; + untrigger_sample(drmr,nn); break; case 9: { - uint8_t nn = data[1]; + nn = data[1]; nn-=baseNote; trigger_sample(drmr,nn,data); break; @@ -331,8 +350,8 @@ static void run(LV2_Handle instance, uint32_t n_samples) { float gain = DB_CO(*(drmr->gains[i])); float pan_right = ((*drmr->pans[i])+1)/2.0f; float pan_left = 1-pan_right; - coef_right = (pan_right * (DB3SCALE * pan_right + DB3SCALEPO))*gain; - coef_left = (pan_left * (DB3SCALE * pan_left + DB3SCALEPO))*gain; + coef_right = (pan_right * (DB3SCALE * pan_right + DB3SCALEPO))*gain*cs->velocity; + coef_left = (pan_left * (DB3SCALE * pan_left + DB3SCALEPO))*gain*cs->velocity; } else { coef_right = coef_left = 1.0f; diff --git a/drmr.h b/drmr.h index 618a98f..8ba97c8 100644 --- a/drmr.h +++ b/drmr.h @@ -58,6 +58,7 @@ typedef struct { uint32_t offset; uint32_t limit; uint32_t layer_count; + float velocity; drmr_layer *layers; float* data; } drmr_sample;