1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <jack/jack.h>
#include <jack/ringbuffer.h>
#include <rtosc/rtosc.h>
#define OFF (0)
//Shape of oscillator
//0 - sinusoidal
//1 - sawtooth
//2 - rectangular (25% duty cycle)
static int shape = 0;
//Current frequency
static float freq = 440.0f;
//Whether the note is running or not
static bool gate = OFF;
//Synthesis
float get_sample(const float phase)
{
if(shape == 0)
return sin(phase*2*M_PI);
else if(shape == 1)
return phase*2.0f-1.0f;
else if(shape == 2)
return phase > 0.75 ? 1.0 : 0.0f;
return 0.0f;
}
//OSC
static char dsp_osc_buf[2048];
extern char gui_osc_buf[];
void gui_message(const char *msg);
#define message(path, type, ...) do { \
if(rtosc_message(dsp_osc_buf, 2048, path, ...)) \
gui_message(dsp_osc_buf); \
else \
warnx("Message to %s is too long...", path);\
}
//Setter functions
#define setter(type_ch, type) void type_ch##setter(const char *osc_msg, void *param) { \
*((type*)param) = rtosc_argument(osc_msg, 0).type_ch; \
}
setter(i,int);
setter(f,float);
setter(T,bool);
void plot_data_cb(const char *msg, void*)
{
const int samples = rtosc_argument(msg, 0).i;
//Construct blob piecewise
if(rtosc_message(dsp_osc_buf, 2048, "/ui/plot", "b", samples, NULL)) {
//Fill reserved space
float *data = (float*) rtosc_argument(dsp_osc_buf,0).b.data;
for(int i=0; i < samples; ++i)
data[i] = get_sample((float)i/samples);
}
gui_message(dsp_osc_buf);
}
struct dispatch_t
{
const char *label;
void (*fn)(const char *,void*);
void *data;
};
//Simple dispatch table
dispatch_t dispatch_table[] = {
{"/synth/shape", isetter, &shape},
{"/synth/freq", fsetter, &freq},
{"/synth/gate", Tsetter, &gate},
{"/synth/plot/data", plot_data_cb, 0},
{0,0,0}
};
void dsp_dispatch(const char *msg)
{
dispatch_t *itr = dispatch_table;
while(itr->label && strcmp(msg, itr->label))
++itr;
if(itr->label)
itr->fn(msg, itr->data);
}
//JACK stuff
jack_port_t *jport;
jack_client_t *jclient;
jack_ringbuffer_t *gui_ring;
jack_ringbuffer_t *dsp_ring;
int process(unsigned nframes, void *args);
void dsp_ev(void);
void gui_dispatch(const char *msg);
void dsp_message(const char *msg)
{
if(jack_ringbuffer_write_space(dsp_ring) >= 2048)
jack_ringbuffer_write(dsp_ring, msg, 2048);
else
puts("dsp ringbuffer full...");
}
void gui_message(const char *msg)
{
if(jack_ringbuffer_write_space(gui_ring) >= 2048)
jack_ringbuffer_write(gui_ring, msg, 2048);
}
void dsp_ev(void)
{
while(jack_ringbuffer_read_space(dsp_ring) >= 2048) {
jack_ringbuffer_read(dsp_ring, dsp_osc_buf, 2048);
dsp_dispatch(dsp_osc_buf);
}
}
void gui_ev(void)
{
while(jack_ringbuffer_read_space(gui_ring) >= 2048) {
jack_ringbuffer_read(gui_ring, gui_osc_buf, 2048);
gui_dispatch(gui_osc_buf);
}
}
void start_synth(void)
{
//Setup Ringbuffers
gui_ring = jack_ringbuffer_create(2048*8);
dsp_ring = jack_ringbuffer_create(2048*32);
jack_ringbuffer_mlock(gui_ring);
jack_ringbuffer_mlock(dsp_ring);
jclient = jack_client_open("rtosc-demo", JackNullOption, NULL, NULL);
if(!jclient)
errx(1, "jack_client_open() failure");
jack_set_process_callback(jclient, process, NULL);
jport = jack_port_register(jclient, "output",
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0);
if(!jport)
errx(1, "jack_port_register() failure");
if(jack_activate(jclient))
errx(1, "jack_activate() failure");
}
void stop_synth(void)
{
jack_client_close(jclient);
}
float phase = 0.0f;
int process(unsigned nframes, void *)
{
dsp_ev();
float *smps = (float*) jack_port_get_buffer(jport, nframes);
memset(smps, 0, nframes*sizeof(float));
const float dp = freq/jack_get_sample_rate(jclient);
for(unsigned i=0; i<nframes; ++i) {
smps[i] = get_sample(phase);
phase += dp;
if(phase > 1.0f)
phase -= 1.0f;
}
return 0;
}
|