Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

wacky kreole stuff

  • Loading branch information...
commit 18c0cec04ff97469b8383656f0a3c4221881b877 1 parent f2fef4a
@yaxu yaxu authored
View
4 Makefile
@@ -8,8 +8,8 @@ all: dirt
clean:
rm -f *.o *~ dirt
-dirt: dirt.o jack.o audio.o file.o server.o segment.o Makefile
- $(CC) dirt.o jack.o audio.o file.o server.o segment.o $(CFLAGS) $(LDFLAGS) -o dirt
+dirt: dirt.o jack.o audio.o file.o server.o segment.o pitch.o Makefile
+ $(CC) dirt.o jack.o audio.o file.o server.o segment.o pitch.o $(CFLAGS) $(LDFLAGS) -o dirt
test : test.c Makefile
$(CC) test.c -llo -o test
View
118 audio.c
@@ -9,6 +9,8 @@
#include "config.h"
#include "jack.h"
#include "audio.h"
+#include "server.h"
+#include "pitch.h"
#define HALF_PI 1.5707963267948966
@@ -22,17 +24,25 @@ t_loop *loop = NULL;
#endif
double epochOffset = 0;
+float starttime = 0;
jack_client_t *client = NULL;
static int samplerate = 0;
extern void audio_init(void) {
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ starttime = (float) tv.tv_sec + ((float) tv.tv_usec / 1000000.0);
+#ifdef FEEDBACK
+ loop = new_loop(60 * 10);
+#endif
pthread_mutex_init(&queue_waiting_lock, NULL);
client = jack_start(audio_callback);
samplerate = jack_get_sample_rate(client);
file_set_samplerate(samplerate);
+
#ifdef FEEDBACK
- loop = new_loop(8);
+ pitch_init(loop, samplerate);
#endif
}
@@ -204,10 +214,38 @@ 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, float shape) {
+extern void audio_kriole(double when,
+ float duration,
+ float pitch_start,
+ float pitch_stop) {
+
+ int lowchunk = loop->chunk_n - (loop->frames / loop->chunksz);
+
+ // subtract a bit for some legroom..
+ lowchunk -= 16;
+
+ lowchunk = lowchunk < 0 ? 0 : lowchunk;
+
+ float chunklen = (float) loop->chunksz / (float) samplerate;
+ int chunks = (int) (duration / chunklen);
+ if (chunks == 0) {
+ chunks = 1;
+ }
+ float pitch_diff = pitch_stop - pitch_start;
+ for (int i = 0; i < chunks; ++i) {
+ float pitch = pitch_start + (((float) i / (float) chunks) * pitch_diff);
+ osc_send_play(when + ((float) i) * chunklen,
+ lowchunk,
+ pitch,
+ 0 // flux
+ );
+ }
+}
+
+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, int kriole_chunk) {
struct timeval tv;
#ifdef FEEDBACK
- int is_loop = 0;
+ int is_kriole = 0;
#endif
t_sample *sample = NULL;
t_sound *new;
@@ -220,8 +258,8 @@ extern int audio_play(double when, char *samplename, float offset, float start,
#ifdef FEEDBACK
- if (strcmp(samplename, "loop") == 0) {
- is_loop = 1;
+ if (strcmp(samplename, "kriole") == 0) {
+ is_kriole = 1;
}
else {
#endif
@@ -238,22 +276,40 @@ extern int audio_play(double when, char *samplename, float offset, float start,
strncpy(new->samplename, samplename, MAXPATHSIZE);
#ifdef FEEDBACK
- if (is_loop) {
+ if (is_kriole) {
new->loop = loop;
- new->end = loop->frames;
+ //printf("calculating chunk %d\n", kriole_chunk);
+ new->kriole_chunk = kriole_chunk;
+
+ //printf("now %d\n", loop->now);
+ //printf("since_chunk %d\n", loop->since_chunk);
+ int last_chunk_start = (loop->now - loop->since_chunk);
+ //printf("last_chunk_start %d\n", last_chunk_start);
+ int chunks_back = loop->chunk_n - kriole_chunk;
+ //printf("chunks_back %d\n", chunks_back);
+ int samples_back = (chunks_back * loop->chunksz);
+ //printf("samples_back %d\n", samples_back);
+ int unmodded = last_chunk_start - samples_back;
+ //printf("unmodded %d\n", unmodded);
+ int modded = unmodded % loop->frames;
+ //printf("modded %d\n", modded);
+ new->start = unmodded;
+ new->end = new->start + loop->chunksz;
new->items = loop->items;
new->channels = 1;
- new->loop_start = (loop->now + (loop->frames / 2)) % loop->frames;
+ printf("kriole %d: start %f end %f\n", new->kriole_chunk, new->start, new->end);
+ //new->loop_start = (loop->now + (loop->frames / 2)) % loop->frames;
}
else {
#endif
new->sample = sample;
+ new->start = 0;
new->end = sample->info->frames;
new->items = sample->items;
new->channels = sample->info->channels;
#ifdef FEEDBACK
}
- new->is_loop = is_loop;
+ new->is_kriole = is_kriole;
#endif
new->startFrame =
@@ -261,7 +317,6 @@ extern int audio_play(double when, char *samplename, float offset, float start,
new->next = NULL;
new->prev = NULL;
- new->start = 0;
new->speed = fabsf(speed);
new->reverse = speed < 0;
new->pan = pan;
@@ -298,6 +353,7 @@ extern int audio_play(double when, char *samplename, float offset, float start,
}
*/
new->position = new->reverse ? new->end : new->start;
+ printf("position: %f\n", new->position);
new->formant_vowelnum = vowelnum;
pthread_mutex_lock(&queue_waiting_lock);
@@ -358,6 +414,7 @@ void playback(float **buffers, int frame, jack_nframes_t frametime) {
p = p->next;
continue;
}
+
//printf("playing %s\n", p->samplename);
channels = p->channels;
//printf("channels: %d\n", channels);
@@ -366,9 +423,10 @@ void playback(float **buffers, int frame, jack_nframes_t frametime) {
float value;
#ifdef FEEDBACK
- if (p->is_loop) {
- // only one channel, but relative to 'now'
- unsigned int i = (p->loop_start + ((int) p->position)) % p->loop->frames;
+ if (p->is_kriole) {
+ unsigned int i = ((int) (p->position)
+ % p->loop->frames
+ );
value = p->items[i];
}
else {
@@ -380,7 +438,7 @@ void playback(float **buffers, int frame, jack_nframes_t frametime) {
int pos = ((int) p->position) + 1;
#ifdef FEEDBACK
- if ((!p->is_loop) && pos < p->end) {
+ if ((!p->is_kriole) && pos < p->end) {
#else
if (pos < p->end) {
#endif
@@ -463,13 +521,13 @@ void playback(float **buffers, int frame, jack_nframes_t frametime) {
p = p->next;
if (tmp->speed > 0) {
if (tmp->position >= tmp->end) {
- //printf("remove %s\n", tmp->samplename);
+ printf("remove %s %f\n", tmp->samplename, tmp->position);
queue_remove(&playing, tmp);
}
}
else {
if (tmp->position <= tmp->end) {
- //printf("remove %s\n", tmp->samplename);
+ printf("remove %s (zerospeed)\n", tmp->samplename);
queue_remove(&playing, tmp);
}
}
@@ -491,11 +549,39 @@ extern int audio_callback(int frames, float *input, float **outputs) {
for (i=0; i < frames; ++i) {
#ifdef FEEDBACK
+ /*if (loop->now % (samplerate / 4) == 0) {
+ printf("loop->now %d\n", loop->now);
+ }*/
loop->items[loop->now++] = input[i];
if (loop->now >= loop->frames) {
loop->now = 0;
+ loop->loops++;
+ }
+ if (loop->since_chunk == loop->chunksz) {
+ loop->since_chunk = 0;
+ static int onlyonce = 0;
+ float pitch = pitch_calc(loop);
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ float nowtime = tv.tv_sec + (tv.tv_usec / 1000000.0);
+
+ if (pitch > 0) {
+ /*
+ printf("found pitch at %d - %d [loop %d] (%f/%f secs)\n",
+ loop->now - loop->chunksz,
+ loop->now,
+ loop->loops,
+ (float) (loop->now + (loop->loops * loop->chunksz))
+ / (float) samplerate,
+ nowtime - starttime
+ );*/
+ osc_send_pitch(starttime, loop->chunk_n, pitch);
+ onlyonce++;
+ }
+ loop->chunk_n++;
}
+ loop->since_chunk++;
#endif
dequeue(now + frames);
View
10 audio.h
@@ -54,8 +54,16 @@ typedef struct t_node {
float accellerate;
int shape;
float shape_k;
+ int kriole_chunk;
+ int is_kriole;
} 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, float shape);
+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, int kriole_chunk);
+extern void audio_kriole(double when,
+ float duration,
+ float pitch_start,
+ float pitch_stop
+ );
+
View
2  config.h
@@ -1,2 +1,2 @@
-//#define FEEDBACK
+#define FEEDBACK
View
2  file.c
@@ -21,6 +21,8 @@ t_loop *new_loop(float seconds) {
result->frames = seconds * (float) samplerate;
result->items = (float *) calloc(result->frames, sizeof(float));
result->now = 0;
+ result->loops = 0;
+ result->chunksz = samplerate / 16; // 16 Hz
return(result);
}
View
19 file.h
@@ -1,4 +1,5 @@
#include <sndfile.h>
+#include <aubio/aubio.h>
#define MAXSAMPLES 1024
#define MAXFILES 4096
@@ -16,7 +17,25 @@ typedef struct {
typedef struct {
unsigned int frames;
unsigned int now;
+ int loops;
+ unsigned int chunksz;
+ unsigned int since_chunk;
+ unsigned int chunk_n;
float *items;
+
+ unsigned int win_s;
+ unsigned int hop_s;
+ unsigned int samplerate;
+ unsigned int channels;
+
+ fvec_t *ibuf;
+
+ aubio_pitchdetection_mode mode;
+ aubio_pitchdetection_type type;
+ fvec_t *in;
+ aubio_pitchdetection_t *pitch_output;
+
+ int initialised;
} t_loop;
extern void file_set_samplerate(int s);
View
78 pitch.c
@@ -0,0 +1,78 @@
+#include <stdlib.h>
+#include <string.h>
+#include <sndfile.h>
+#include <aubio/aubio.h>
+#include <math.h>
+#include "file.h"
+
+extern void pitch_init(t_loop *loop, int samplerate) {
+ // loop->win_s = 1024;
+ // loop->hop_s = loop->win_s/4;
+ loop->win_s = loop->chunksz;
+ loop->hop_s = loop->chunksz;
+ loop->samplerate = samplerate;
+ loop->channels = 1;
+
+ loop->mode = aubio_pitchm_freq; // or midi
+ loop->type = aubio_pitch_yinfft;
+ loop->in = new_fvec(loop->hop_s, loop->channels);
+ loop->pitch_output =
+ new_aubio_pitchdetection(loop->win_s,
+ loop->hop_s,
+ loop->channels,
+ loop->samplerate,
+ loop->type,
+ loop->mode);
+ loop->initialised = 1;
+}
+
+void pitch_destruct(t_loop *loop) {
+ del_fvec(loop->in);
+ del_aubio_pitchdetection(loop->pitch_output);
+
+}
+extern float pitch_calc(t_loop *loop) {
+ float rmsAmplitude = 0;
+ float pitch = -1;
+
+ if (loop->initialised == 0) {
+ return(-1);
+ }
+
+ int j = (loop->now - loop->chunksz) % loop->frames;
+
+ for (int i = 0; i < loop->chunksz; i++){
+ //calculate the root mean square amplitude
+ rmsAmplitude += sqrt(loop->items[j]*loop->items[j]);
+
+ loop->in->data[0][i] = loop->items[j];
+ ++j;
+ if (j >= loop->frames) {
+ j = 0;
+ }
+ }
+
+ //now we need to get the average
+ rmsAmplitude /= loop->chunksz;
+ //printf("loud: %f\n", rmsAmplitude);
+
+ //don't update the pitch if the sound is very quiet
+ if( rmsAmplitude > 0.2 ){
+ //finally get the pitch of the sound
+ pitch = aubio_pitchdetection(loop->pitch_output, loop->in);
+ }
+
+ return(pitch);
+}
+
+
+
+/*
+extern int *segment_get_onsets(t_sample *sample) {
+ int *result;
+ pitch_init(sample->info->channels);
+ result = pitch_process(sample, sample->items, sample->info->frames);
+ pitch_destruct();
+ return(result);
+}
+*/
View
3  pitch.h
@@ -0,0 +1,3 @@
+
+extern void pitch_init(t_loop *loop, int samplerate);
+extern float pitch_calc(t_loop *loop);
View
76 server.c
@@ -3,6 +3,8 @@
#include <unistd.h>
#include <string.h>
#include <lo/lo.h>
+#include <sys/types.h>
+#include <math.h>
#include "server.h"
#include "audio.h"
@@ -40,6 +42,21 @@ int generic_handler(const char *path, const char *types, lo_arg **argv,
/**/
+int kriole_handler(const char *path, const char *types, lo_arg **argv,
+ int argc, void *data, void *user_data) {
+
+ double when = (double) argv[0]->i + ((double) argv[1]->i / 1000000.0);
+ float duration = argv[2]->f;
+ float pitch_start = argv[3]->f;
+ float pitch_stop = argv[4]->f;
+
+ audio_kriole(when, duration, pitch_start, pitch_stop);
+
+ return(0);
+}
+
+/**/
+
int play_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data) {
@@ -60,6 +77,7 @@ int play_handler(const char *path, const char *types, lo_arg **argv,
float resonance = argv[11]->f;
float accellerate = argv[12]->f;
float shape = argv[13]->f;
+ int kriole_chunk = argv[14]->i;
int vowelnum = -1;
@@ -84,7 +102,8 @@ int play_handler(const char *path, const char *types, lo_arg **argv,
cutoff,
resonance,
accellerate,
- shape
+ shape,
+ kriole_chunk
);
return 0;
}
@@ -94,13 +113,62 @@ int play_handler(const char *path, const char *types, lo_arg **argv,
extern int server_init(void) {
lo_server_thread st = lo_server_thread_new("7771", error);
- //lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
-
- lo_server_thread_add_method(st, "/play", "iisffffffsffff",
+ lo_server_thread_add_method(st, "/play", "iisffffffsffffi",
play_handler,
NULL
);
+
+ lo_server_thread_add_method(st, "/kriole", "iifff",
+ kriole_handler,
+ NULL
+ );
+ lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
lo_server_thread_start(st);
return(1);
}
+
+extern void osc_send_pitch(float starttime, unsigned int chunk, float pitch) {
+ static lo_address t = NULL;
+ static int pid = 0;
+ if (t == NULL) {
+ t = lo_address_new(NULL, "6010");
+ }
+ if (pid == 0) {
+ pid = (int) getpid();
+ }
+ printf("send [%d] %f\n", chunk, pitch);
+ // pid, starttime, chunk, v_pitch, v_flux
+ lo_send(t, "/chunk", "ififf",
+ pid,
+ starttime,
+ (int) chunk,
+ pitch,
+ 0.0f
+ );
+
+}
+
+
+extern void osc_send_play(double when, int lowchunk, float pitch, float flux) {
+ static lo_address t = NULL;
+ static int pid = 0;
+ if (t == NULL) {
+ t = lo_address_new(NULL, "6010");
+ }
+ if (pid == 0) {
+ pid = (int) getpid();
+ }
+ printf("play [%d] %f\n", lowchunk, pitch);
+ // pid, starttime, chunk, v_pitch, v_flux
+ lo_send(t, "/play", "iiiiff",
+ pid,
+ (int) when,
+ (int) ((when - floor(when)) * 1000000.0),
+ lowchunk,
+ pitch,
+ flux
+ );
+
+}
+
View
2  server.h
@@ -1 +1,3 @@
extern int server_init(void);
+extern void osc_send_pitch(float starttime, unsigned int chunk, float pitch);
+extern void osc_send_play(double when, int lowchunk, float pitch, float flux);
Please sign in to comment.
Something went wrong with that request. Please try again.