Control to set kit index

This commit is contained in:
Nick Lanham 2012-02-07 12:00:07 +01:00
parent 44cd116946
commit a2e14e6652
5 changed files with 92 additions and 24 deletions

View File

@ -8,7 +8,7 @@ $(BUNDLE): manifest.ttl drmr.ttl drmr.so
cp manifest.ttl drmr.ttl drmr.so $(BUNDLE)
drmr.so: drmr.c drmr_hydrogen.c
$(CC) -g -shared -fPIC -DPIC drmr.c drmr_hydrogen.c `pkg-config --cflags --libs lv2-plugin sndfile` -lexpat -o drmr.so
$(CC) -g -shared -fPIC -DPIC drmr.c drmr_hydrogen.c `pkg-config --cflags --libs lv2-plugin sndfile` -lexpat -lm -o drmr.so
install: $(BUNDLE)
mkdir -p $(INSTALL_DIR)

46
drmr.c
View File

@ -18,6 +18,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "drmr.h"
#include "drmr_hydrogen.h"
@ -55,6 +56,19 @@ int load_sample(char* path, drmr_sample* samp) {
return 0;
}
static void* load_thread(void* arg) {
DrMr* drmr = (DrMr*)arg;
pthread_mutex_lock(&drmr->load_mutex);
for(;;) {
pthread_cond_wait(&drmr->load_cond,
&drmr->load_mutex);
load_hydrogen_kit(drmr,drmr->kits->kits[drmr->curKit].path);
}
pthread_mutex_unlock(&drmr->load_mutex);
return 0;
}
static LV2_Handle
instantiate(const LV2_Descriptor* descriptor,
double rate,
@ -65,6 +79,22 @@ instantiate(const LV2_Descriptor* descriptor,
drmr->map = NULL;
drmr->num_samples = 0;
if (pthread_mutex_init(&drmr->load_mutex, 0)) {
fprintf(stderr, "Could not initialize load_mutex.\n");
free(drmr);
return 0;
}
if (pthread_cond_init(&drmr->load_cond, 0)) {
fprintf(stderr, "Could not initialize load_cond.\n");
free(drmr);
return 0;
}
if (pthread_create(&drmr->load_thread, 0, load_thread, drmr)) {
fprintf(stderr, "Could not initialize loading thread.\n");
free(drmr);
return 0;
}
// Map midi uri
while(*features) {
if (!strcmp((*features)->URI, LV2_URI_MAP_URI)) {
@ -89,13 +119,11 @@ instantiate(const LV2_Descriptor* descriptor,
return 0;
}
load_hydrogen_kit(drmr,drmr->kits->kits->path);
drmr->curKit = 0;
drmr->gains = malloc(16*sizeof(float*));
for(i = 0;i<16;i++) drmr->gains[i] = NULL;
//load_hydrogen_kit(drmr,"/usr/share/hydrogen/data/drumkits/3355606kit/");
return (LV2_Handle)drmr;
}
@ -114,6 +142,10 @@ connect_port(LV2_Handle instance,
case DRMR_RIGHT:
drmr->right = (float*)data;
break;
case DRMR_KITNUM:
if(data) drmr->kitReq = (float*)data;
printf("Connected kit\n");
break;
case DRMR_GAIN_ONE:
if (data) drmr->gains[0] = (float*)data;
break;
@ -171,10 +203,16 @@ static void activate(LV2_Handle instance) { }
static void run(LV2_Handle instance, uint32_t n_samples) {
int i;
int i,kitInt;
char first_active, one_active;
DrMr* drmr = (DrMr*)instance;
kitInt = (int)floorf(*(drmr->kitReq));
if (kitInt != drmr->curKit) { // requested a new kit
drmr->curKit = kitInt;
pthread_cond_signal(&drmr->load_cond);
}
LV2_Event_Iterator eit;
if (lv2_event_begin(&eit,drmr->midi_port)) { // if we have any events
LV2_Event *cur_ev;

13
drmr.h
View File

@ -19,11 +19,13 @@
#ifndef DRMR_H
#define DRMR_H
#include <sndfile.h>
#include <pthread.h>
#include "lv2/lv2plug.in/ns/lv2core/lv2.h"
#include "lv2/lv2plug.in/ns/ext/event/event.h"
#include "lv2/lv2plug.in/ns/ext/event/event-helpers.h"
#include "lv2/lv2plug.in/ns/ext/uri-map/uri-map.h"
#include <sndfile.h>
// drumkit scanned from a hydrogen xml file
typedef struct {
@ -57,6 +59,7 @@ typedef enum {
DRMR_MIDI = 0,
DRMR_LEFT,
DRMR_RIGHT,
DRMR_KITNUM,
DRMR_GAIN_ONE,
DRMR_GAIN_TWO,
DRMR_GAIN_THREE,
@ -84,7 +87,7 @@ typedef struct {
// params
float** gains;
float* kitReq;
// URIs
LV2_URI_Map_Feature* map;
@ -94,11 +97,17 @@ typedef struct {
// Available kits
kits* kits;
int curKit;
// Samples
drmr_sample* samples;
uint8_t num_samples;
// loading thread stuff
pthread_mutex_t load_mutex;
pthread_cond_t load_cond;
pthread_t load_thread;
} DrMr;

View File

@ -39,6 +39,17 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 3;
lv2:symbol "kit_num";
lv2:name "Kit Index";
lv2:minimum 0;
lv2:maximum 50;
lv2:default 0;
lv2:portProperty lv2:integer;
],
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 4;
lv2:symbol "gain_one";
lv2:name "Sample One Gain";
lv2:minimum 0.0;
@ -48,7 +59,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 4;
lv2:index 5;
lv2:symbol "gain_two";
lv2:name "Sample Two Gain";
lv2:minimum 0.0;
@ -58,7 +69,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 5;
lv2:index 6;
lv2:symbol "gain_three";
lv2:name "Sample Three Gain";
lv2:minimum 0.0;
@ -68,7 +79,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 6;
lv2:index 7;
lv2:symbol "gain_four";
lv2:name "Sample Four Gain";
lv2:minimum 0.0;
@ -78,7 +89,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 7;
lv2:index 8;
lv2:symbol "gain_five";
lv2:name "Sample Five Gain";
lv2:minimum 0.0;
@ -88,7 +99,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 8;
lv2:index 9;
lv2:symbol "gain_siz";
lv2:name "Sample Siz Gain";
lv2:minimum 0.0;
@ -98,7 +109,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 9;
lv2:index 10;
lv2:symbol "gain_seven";
lv2:name "Sample Seven Gain";
lv2:minimum 0.0;
@ -108,7 +119,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 10;
lv2:index 11;
lv2:symbol "gain_eight";
lv2:name "Sample Eight Gain";
lv2:minimum 0.0;
@ -118,7 +129,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 11;
lv2:index 12;
lv2:symbol "gain_nine";
lv2:name "Sample Nine Gain";
lv2:minimum 0.0;
@ -128,7 +139,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 12;
lv2:index 13;
lv2:symbol "gain_ten";
lv2:name "Sample Ten Gain";
lv2:minimum 0.0;
@ -138,7 +149,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 13;
lv2:index 14;
lv2:symbol "gain_eleven";
lv2:name "Sample Eleven Gain";
lv2:minimum 0.0;
@ -148,7 +159,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 14;
lv2:index 15;
lv2:symbol "gain_twelve";
lv2:name "Sample Twelve Gain";
lv2:minimum 0.0;
@ -158,7 +169,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 15;
lv2:index 16;
lv2:symbol "gain_thirteen";
lv2:name "Sample Thirteen Gain";
lv2:minimum 0.0;
@ -168,7 +179,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 16;
lv2:index 17;
lv2:symbol "gain_fourteen";
lv2:name "Sample Fourteen Gain";
lv2:minimum 0.0;
@ -178,7 +189,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 17;
lv2:index 18;
lv2:symbol "gain_fifteen";
lv2:name "Sample Fifteen Gain";
lv2:minimum 0.0;
@ -188,7 +199,7 @@
[
a lv2:ControlPort, lv2:InputPort;
lv2:index 18;
lv2:index 19;
lv2:symbol "gain_sixteen";
lv2:name "Sample Sixteen Gain";
lv2:minimum 0.0;

View File

@ -249,6 +249,13 @@ kits* scan_kits() {
return ret;
}
static void free_samples(drmr_sample* samples, int num_samples) {
int i;
for (i=0;i<num_samples;i++)
free(samples[i].data);
free(samples);
}
int load_hydrogen_kit(DrMr* drmr, char* path) {
char* fp_buf;
FILE* file;
@ -292,9 +299,9 @@ int load_hydrogen_kit(DrMr* drmr, char* path) {
XML_ParserFree(parser);
{
drmr_sample* samples;
drmr_sample *samples, *old_samples;
struct instrument_info * cur_i;
int i = 0, num_inst = 0;
int i = 0, num_inst = 0, old_scount;
printf("Read kit: %s\n",kit_info.name);
cur_i = kit_info.instruments;
while(cur_i) { // first count how many samples we have
@ -314,6 +321,8 @@ int load_hydrogen_kit(DrMr* drmr, char* path) {
i++;
cur_i = cur_i->next;
}
old_samples = drmr->samples;
old_scount = drmr->num_samples;
if (num_inst > drmr->num_samples) {
// we have more, so we can safely swap our sample list in before updating num_samples
drmr->samples = samples;
@ -323,6 +332,7 @@ int load_hydrogen_kit(DrMr* drmr, char* path) {
drmr->num_samples = num_inst;
drmr->samples = samples;
}
if (old_samples) free_samples(old_samples,old_scount);
}
return 0;