Skip to content

Commit

Permalink
add waveshaping and tweak roundoff
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Dec 28, 2012
1 parent f0b121c commit ce0a29d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
41 changes: 24 additions & 17 deletions audio.c
Expand Up @@ -52,10 +52,12 @@ int queue_size(t_sound *queue) {
void queue_add(t_sound **queue, t_sound *new) {
//printf("queuing %s @ %lld\n", new->samplename, new->start);
int s = queue_size(*queue);
if (s >= MAXSOUNDS) {
printf("hit max sounds (%d)\n", MAXSOUNDS);
}
int added = 0;
if (*queue == NULL) {
*queue = new;
assert(s == (queue_size(*queue) - 1));
added++;
}
else {
Expand All @@ -76,9 +78,6 @@ void queue_add(t_sound **queue, t_sound *new) {
}
tmp->prev = new;

if (s != (queue_size(*queue) - 1)) {
assert(s == (queue_size(*queue) - 1));
}
added++;
break;
}
Expand All @@ -88,23 +87,18 @@ void queue_add(t_sound **queue, t_sound *new) {
tmp->next = new;
new->prev = tmp;
added++;
assert(s == (queue_size(*queue) - 1));
break;
}
++i;
tmp = tmp->next;
}
}

if (s != (queue_size(*queue) - added)) {
assert(s == (queue_size(*queue) - added));
}
assert(added == 1);
}


void queue_remove(t_sound **queue, t_sound *old) {
int s = queue_size(*queue);
if (old->prev == NULL) {
*queue = old->next;
if (*queue != NULL) {
Expand All @@ -118,7 +112,6 @@ void queue_remove(t_sound **queue, t_sound *old) {
old->next->prev = old->prev;
}
}
assert(s == (queue_size(*queue) + 1));
free(old);
}

Expand Down Expand Up @@ -211,7 +204,7 @@ float effect_vcf(float in, t_sound *sound, int channel) {
return vcf->y4;
}

extern int audio_play(double when, char *samplename, float offset, float start, float end, float speed, float pan, float velocity, int vowelnum, float cutoff, float resonance, float accellerate) {
extern int audio_play(double when, char *samplename, float offset, float start, float end, float speed, float pan, float velocity, int vowelnum, float cutoff, float resonance, float accellerate, float shape) {
struct timeval tv;
#ifdef FEEDBACK
int is_loop = 0;
Expand Down Expand Up @@ -241,7 +234,7 @@ extern int audio_play(double when, char *samplename, float offset, float start,
#endif

new = (t_sound *) calloc(1, sizeof(t_sound));
printf("samplename: %s when: %f\n", samplename, when - (float) tv.tv_sec);
//printf("samplename: %s when: %f\n", samplename, when);
strncpy(new->samplename, samplename, MAXPATHSIZE);

#ifdef FEEDBACK
Expand Down Expand Up @@ -278,11 +271,17 @@ extern int audio_play(double when, char *samplename, float offset, float start,

new->cutoff = cutoff;
new->resonance = resonance;

if (shape != 0) {
new->shape = 1;
new->shape_k = (2.0f * shape) / (1.0f - shape);
}

init_vcf(new);

new->accellerate = accellerate;

printf("frames: %f\n", new->end);
//printf("frames: %f\n", new->end);
if (start > 0 && start <= 1) {
new->start = start * new->end;
}
Expand Down Expand Up @@ -407,16 +406,24 @@ void playback(float **buffers, int frame, jack_nframes_t frametime) {
}

if ((p->end - p->position) < ROUNDOFF) {
// TODO what end < ROUNDOFF?)
// TODO what if end < ROUNDOFF?)
//printf("roundoff: %f\n", (p->end - pos) / (float) ROUNDOFF);
roundoff = (p->end - pos) / (float) ROUNDOFF;
roundoff = (p->end - p->position) / (float) ROUNDOFF;
//printf("end roundoff: %f (%f)\n", roundoff, p->end - p->position);
}
else {
if ((pos - p->start) < ROUNDOFF) {
roundoff = (pos - p->start) / (float) ROUNDOFF;
if ((p->position - p->start) < ROUNDOFF) {
roundoff = (p->position - p->start) / (float) ROUNDOFF;
//printf("start roundoff: %f (%f / %d)\n", roundoff, p->position - p->start, ROUNDOFF);
}
}

if (p->shape) {
value = (1+p->shape_k)*value/(1+p->shape_k*fabs(value));
}

value *= roundoff;
value *= 0.5;

float c = (float) channel + p->pan;
float d = c - floor(c);
Expand Down
10 changes: 6 additions & 4 deletions audio.h
Expand Up @@ -4,8 +4,8 @@

#define MAXDELAYS 16
#define MAXDELAY 44100

#define ROUNDOFF 8
#define MAXSOUNDS 64
#define ROUNDOFF 128

typedef struct {
float cutoff;
Expand Down Expand Up @@ -51,9 +51,11 @@ typedef struct t_node {
float cutoff;
float resonance;
t_vcf vcf[CHANNELS];
float accellerate;
float accellerate;
int shape;
float shape_k;
} t_sound;

extern int audio_callback(int frames, float *input, float **outputs);
extern void audio_init(void);
extern int audio_play(double when, char *samplename, float offset, float start, float end, float speed, float pan, float velocity, int vowelnum, float cutoff, float resonance, float accellerate);
extern int audio_play(double when, char *samplename, float offset, float start, float end, float speed, float pan, float velocity, int vowelnum, float cutoff, float resonance, float accellerate, float shape);
6 changes: 4 additions & 2 deletions server.c
Expand Up @@ -59,6 +59,7 @@ int play_handler(const char *path, const char *types, lo_arg **argv,
float cutoff = argv[10]->f;
float resonance = argv[11]->f;
float accellerate = argv[12]->f;
float shape = argv[13]->f;

int vowelnum = -1;

Expand All @@ -82,7 +83,8 @@ int play_handler(const char *path, const char *types, lo_arg **argv,
vowelnum,
cutoff,
resonance,
accellerate
accellerate,
shape
);
return 0;
}
Expand All @@ -94,7 +96,7 @@ extern int server_init(void) {

//lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);

lo_server_thread_add_method(st, "/play", "iisffffffsfff",
lo_server_thread_add_method(st, "/play", "iisffffffsffff",
play_handler,
NULL
);
Expand Down

0 comments on commit ce0a29d

Please sign in to comment.