Permalink
Browse files

foo

  • Loading branch information...
yaxu committed Feb 10, 2012
1 parent ab0fe60 commit f4296e1b355fc4c9023140c9e00fc53e16683020
Showing with 239 additions and 110 deletions.
  1. +6 −5 Makefile
  2. +133 −3 audio.c
  3. +18 −0 audio.h
  4. +4 −2 dirt.c
  5. +18 −8 file.c
  6. +5 −1 file.h
  7. +29 −21 jack.c
  8. +4 −2 jack.h
  9. +22 −68 server.c
View
@@ -1,11 +1,12 @@
CC=gcc
-CFLAGS = -O3 -g -Wall -pedantic
-LDFLAGS = -llo -lsndfile -lsamplerate -ljack
+# -O3
+CFLAGS = -g -Wall -pedantic
+LDFLAGS = -llo -lsndfile -lsamplerate -ljack -laubio
all: dirt
-clean :
+clean:
rm -f *.o *~ dirt
-dirt: dirt.o jack.o audio.o file.o Makefile
- $(CC) dirt.o jack.o audio.o file.o $(CFLAGS) $(LDFLAGS) -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
View
136 audio.c
@@ -1,8 +1,138 @@
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <jack/jack.h>
-extern int audio_callback(int samples, float *buffer) {
+#include "jack.h"
+#include "audio.h"
+
+pthread_mutex_t queue_lock;
+
+t_queue *waiting = NULL;
+t_queue *playing = NULL;
+
+double offset = 0;
+
+jack_client_t *client = NULL;
+
+extern void audio_init(void) {
+ int samplerate;
+
+ pthread_mutex_init(&queue_lock, NULL);
+ client = jack_start(audio_callback);
+ samplerate = jack_get_sample_rate(client);
+ file_set_samplerate(samplerate);
+}
+
+void queue_add(t_queue *queue, t_queue *new) {
+ if (queue == NULL) {
+ queue = new;
+ }
+ else {
+ t_queue *tmp = queue;
+ while (1) {
+ if (tmp->next == NULL) {
+ tmp->next = new;
+ new->last = tmp;
+ break;
+ }
+
+ if (tmp->when > new->when) {
+ new->next = tmp;
+ new->last = tmp->last;
+ if (tmp->last) {
+ tmp->last->next = new;
+ tmp->last = new;
+ }
+
+ if (queue == tmp) {
+ queue = new;
+ }
+ break;
+ }
+
+ tmp = tmp->next;
+ }
+ }
+}
+
+void queue_remove(t_queue *queue, t_queue *old) {
+ if (queue == old) {
+ queue = old->next;
+ }
+ if (old->last) {
+ old->last->next = old->next;
+ }
+ if (old->next) {
+ old->next->last = old->last;
+ }
+ free(old);
+}
+
+extern int audio_play(char *samplename) {
+ int result = 0;
+ t_sound *sound;
+ t_sample *sample = file_get(samplename);
+
+ if (sample != NULL) {
+ t_queue *new;
+
+ sound = (t_sound *) malloc(sizeof(t_sound));
+ strncpy(sound->samplename, samplename, MAXPATHSIZE);
+ sound->sample = sample;
+
+ new = (t_queue *) malloc(sizeof(t_queue));
+ new->when = -1;
+ new->sound = sound;
+ new->next = NULL;
+ new->last = NULL;
+ new->position = 0;
+ new->speed = 1;
+
+ pthread_mutex_lock(&queue_lock);
+ queue_add(waiting, new);
+ pthread_mutex_unlock(&queue_lock);
+
+ result = 1;
+ }
+ return(result);
+}
+
+t_queue *queue_next(t_queue *queue, double now) {
+ t_queue *result = NULL;
+ if (queue != NULL && queue->when <= now) {
+ result = queue;
+ queue = queue->next;
+ }
+ return(result);
+}
+
+void dequeue() {
+ t_queue *p;
+ while (p = queue_next(waiting, now) != NULL) {
+ p->last = NULL;
+ p->next = playing;
+ if (playing != NULL) {
+ playing->last = p;
+ }
+ playing = p;
+ }
+}
+
+extern int audio_callback(int frames, float *buffer) {
int i;
- for (i=0; i < samples; ++i) {
- buffer[0] = 0;
+ if (offset == 0) {
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ offset = (tv.sec + (tv.usec / 1000000)) - jack_get_time();
+ }
+
+ for (i=0; i < frames; ++i) {
+ pthread_mutex_lock(&queue_lock);
+ dequeue();
+ pthread_mutex_unlock(&queue_lock);
+ buffer[0] = playback();
}
return(0);
}
View
18 audio.h
@@ -1,2 +1,20 @@
+#include "file.h"
+
+typedef struct {
+ char samplename[MAXPATHSIZE+1];
+ t_sample *sample;
+ double position;
+ double speed;
+} t_sound;
+
+
+typedef struct t_node {
+ double when;
+ t_sound *sound;
+ struct t_node *next, *last;
+ double position;
+} t_queue;
extern int audio_callback(int samples, float *buffer);
+extern void audio_init(void);
+extern int audio_play(char *samplename);
View
6 dirt.c
@@ -6,8 +6,10 @@
#include "audio.h"
int main (int argc, char **argv) {
- int samplerate = jack_start(audio_callback);
-
+ printf("init audio\n");
+ audio_init();
+ printf("init osc\n");
+ server_init();
sleep(-1);
return(0);
}
View
26 file.c
@@ -9,6 +9,11 @@
t_sample *samples[MAXSAMPLES];
int sample_count = 0;
+int samplerate = 44100;
+
+extern void file_set_samplerate(int s) {
+ samplerate = s;
+}
t_sample *find_sample (char *samplename) {
int c;
@@ -25,13 +30,12 @@ t_sample *find_sample (char *samplename) {
int wav_filter (const struct dirent *d) {
if (strlen(d->d_name) > 4) {
- printf("%s\n", d->d_name + strlen(d->d_name) - 4);
return(strcmp(d->d_name + strlen(d->d_name) - 4, ".wav") == 0);
}
return(0);
}
-void fix_samplerate (t_sample *sample, int samplerate) {
+void fix_samplerate (t_sample *sample) {
SRC_DATA data;
int max_output_frames;
int channels = sample->info->channels;
@@ -56,7 +60,7 @@ void fix_samplerate (t_sample *sample, int samplerate) {
sample->info->frames = data.output_frames_gen;
}
-t_sample *get_sample(char *samplename, int samplerate) {
+extern t_sample *file_get(char *samplename) {
SNDFILE *sndfile;
char path[MAXPATHSIZE];
char error[62];
@@ -68,6 +72,7 @@ t_sample *get_sample(char *samplename, int samplerate) {
int set_n = 0;
struct dirent **namelist;
+ printf("find %s\n", samplename);
sample = find_sample(samplename);
if (sample == NULL) {
@@ -98,11 +103,11 @@ t_sample *get_sample(char *samplename, int samplerate) {
}
else {
frames = (float *) malloc(sizeof(float) * info->frames);
- snprintf(error, (size_t) 61, "hm: %d\n", sf_error(sndfile));
- perror(error);
+ /*snprintf(error, (size_t) 61, "hm: %d\n", sf_error(sndfile));
+ perror(error);*/
count = sf_read_float(sndfile, frames, info->frames);
- snprintf(error, (size_t) 61, "hmm: %d vs %d %d\n", (int) count, (int) info->frames, sf_error(sndfile));
- perror(error);
+ /* snprintf(error, (size_t) 61, "hmm: %d vs %d %d\n", (int) count, (int) info->frames, sf_error(sndfile));
+ perror(error);*/
if (count == info->frames) {
sample = (t_sample *) calloc(1, sizeof(t_sample));
@@ -118,7 +123,12 @@ t_sample *get_sample(char *samplename, int samplerate) {
free(frames);
}
}
- fix_samplerate(sample, samplerate);
+ if (sample == NULL) {
+ printf("failed.\n");
+ }
+ else {
+ fix_samplerate(sample);
+ }
}
return(sample);
View
6 file.h
@@ -1,12 +1,16 @@
+#include <sndfile.h>
#define MAXSAMPLES 1024
#define MAXFILES 4096
#define MAXPATHSIZE 256
-#define SAMPLEROOT "./"
+#define SAMPLEROOT "./samples"
typedef struct {
char name[MAXPATHSIZE];
SF_INFO *info;
float *frames;
} t_sample;
+
+extern void file_set_samplerate(int s);
+extern t_sample *file_get(char *samplename);
View
50 jack.c
@@ -8,30 +8,34 @@
#include "jack.h"
-jack_port_t *output_port;
jack_client_t *client;
+jack_port_t output_ports[CHANNELS+1];
int process(jack_nframes_t nframes, void *arg) {
jack_default_audio_sample_t *out;
t_callback callback = (t_callback) arg;
- out = jack_port_get_buffer(output_port, nframes);
+ out = jack_port_get_buffer(output_ports, nframes);
callback(nframes, out);
return 0;
+
}
void jack_shutdown(void *arg) {
exit(1);
}
-extern int jack_start(t_callback callback) {
+extern jack_client_t *jack_start(t_callback callback) {
const char **ports;
const char *client_name = "dirt";
const char *server_name = NULL;
jack_options_t options = JackNullOption;
jack_status_t status;
-
+ int i;
+ char portname[16];
+
+
/* open a client connection to the JACK server */
client = jack_client_open(client_name, options, &status, server_name);
@@ -51,21 +55,24 @@ extern int jack_start(t_callback callback) {
fprintf(stderr, "unique name `%s' assigned\n", client_name);
}
- jack_set_process_callback(client, process, callback);
+ jack_set_process_callback(client, process, (void *) callback);
jack_on_shutdown(client, jack_shutdown, 0);
printf("engine sample rate: %" PRIu32 "\n",
jack_get_sample_rate(client));
-
- output_port = jack_port_register(client, "output",
- JACK_DEFAULT_AUDIO_TYPE,
- JackPortIsOutput, 0);
-
- if (output_port == NULL) {
- fprintf(stderr, "no more JACK ports available\n");
- exit(1);
+
+ for (int i = 0; i < CHANNELS; ++i) {
+ sprintf(portname, "output_%d", i);
+ output_ports[i] = jack_port_register(client, portname,
+ JACK_DEFAULT_AUDIO_TYPE,
+ JackPortIsOutput, 0);
+ if (output_ports[i] == NULL) {
+ fprintf(stderr, "no more JACK ports available\n");
+ exit(1);
+ }
}
+ output_ports[CHANNELS] = NULL;
if (jack_activate(client)) {
fprintf(stderr, "cannot activate client");
@@ -74,17 +81,18 @@ extern int jack_start(t_callback callback) {
ports = jack_get_ports(client, NULL, NULL,
JackPortIsPhysical|JackPortIsInput);
- if (ports == NULL) {
- fprintf(stderr, "no physical playback ports\n");
- exit(1);
- }
-
- if (jack_connect(client, jack_port_name(output_port), ports[0])) {
- fprintf(stderr, "cannot connect output ports\n");
+ for (int i = 0; i < CHANNELS; ++i) {
+ if (ports[i] == NULL) {
+ break;
+ }
+ sprintf(portname, "output_%d", i);
+ if (jack_connect(client, jack_port_name(output_ports[i]), ports[i])) {
+ fprintf(stderr, "cannot connect output ports\n");
+ }
}
free(ports);
- return(jack_get_sample_rate(client));
+ return(client);
}
View
6 jack.h
@@ -1,4 +1,6 @@
-
+#define CHANNELS 2
typedef int (*t_callback)(int, float *);
-extern int jack_start(t_callback callback);
+
+extern jack_client_t *jack_start(t_callback callback);
+
Oops, something went wrong.

0 comments on commit f4296e1

Please sign in to comment.