Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

panning and sample interpellation

  • Loading branch information...
commit fb689a4a0dc6c48eb57c987065f357979efebe67 1 parent 66cc990
@yaxu yaxu authored
Showing with 76 additions and 12 deletions.
  1. +35 −5 audio.c
  2. +4 −2 audio.h
  3. +7 −4 server.c
  4. +30 −1 test.c
View
40 audio.c
@@ -9,6 +9,8 @@
#include "jack.h"
#include "audio.h"
+#define HALF_PI 1.5707963267948966
+
pthread_mutex_t queue_waiting_lock;
t_queue *waiting = NULL;
@@ -113,7 +115,7 @@ void queue_remove(t_queue **queue, t_queue *old) {
//free(old);
}
-extern int audio_play(double when, char *samplename, float offset, float duration, float speed) {
+extern int audio_play(double when, char *samplename, float offset, float duration, float speed, float pan, float velocity) {
int result = 0;
t_sound *sound;
t_sample *sample = file_get(samplename);
@@ -133,7 +135,10 @@ extern int audio_play(double when, char *samplename, float offset, float duratio
new->next = NULL;
new->prev = NULL;
new->position = 0;
- new->speed = 1;
+
+ new->speed = speed;
+ new->pan = pan;
+ new->velocity = velocity;
pthread_mutex_lock(&queue_waiting_lock);
queue_add(&waiting, new);
@@ -185,10 +190,11 @@ inline void playback(float **buffers, int frame, jack_nframes_t frametime) {
for (channel = 0; channel < CHANNELS; ++channel) {
buffers[channel][frame] = 0;
}
- //printf("ah\n");
+
while (p != NULL) {
int channels;
t_queue *tmp;
+
//printf("compare start %d with frametime %d\n", p->startFrame, frametime);
if (p->startFrame > frametime) {
p = p->next;
@@ -198,9 +204,33 @@ inline void playback(float **buffers, int frame, jack_nframes_t frametime) {
channels = p->sound->sample->info->channels;
//printf("channels: %d\n", channels);
for (channel = 0; channel < channels; ++channel) {
- /* todo tween */
- buffers[channel][frame] +=
+ float value =
p->sound->sample->items[(channels * ((int) p->position)) + channel];
+
+ if ((((int) p->position) + 1) < p->sound->sample->info->frames) {
+ float next =
+ p->sound->sample->items[(channels * (((int) p->position) + 1))
+ + channel
+ ];
+ float tween_amount = (p->position - (int) p->position);
+ /* linear interpolation */
+ value += (next - value) * tween_amount;
+ }
+
+ float c = (float) channel + p->pan;
+ float d = c - floor(c);
+ int channel_a = ((int) c) % CHANNELS;
+ int channel_b = ((int) c + 1) % CHANNELS;
+
+ if (channel_a < 0) {
+ channel_a += CHANNELS;
+ }
+ if (channel_b < 0) {
+ channel_b += CHANNELS;
+ }
+
+ buffers[channel_a][frame] += value * cos((HALF_PI / 2) * d);
+ buffers[channel_b][frame] += value * sin((HALF_PI / 2) * d);
}
p->position += p->speed;
View
6 audio.h
@@ -12,9 +12,11 @@ typedef struct t_node {
t_sound *sound;
struct t_node *next, *prev;
double position;
- double speed;
+ float speed;
+ float pan;
+ float velocity;
} t_queue;
extern int audio_callback(int frames, float **buffers);
extern void audio_init(void);
-extern int audio_play(double when, char *samplename, float offset, float duration, float speed);
+extern int audio_play(double when, char *samplename, float offset, float duration, float speed, float pan, float velocity);
View
11 server.c
@@ -44,21 +44,24 @@ int play_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data) {
/* lo_timetag ts = lo_message_get_timestamp(data); */
- int i = 0;
- double when = (double) argv[0]->d + ((double) argv[1]->d / 1000000.0);
+ double when = (double) argv[0]->i + ((double) argv[1]->i / 1000000.0);
char *sample_name = strdup((char *) argv[2]);
float offset = argv[3]->f;
float duration = argv[4]->f;
float speed = argv[5]->f;
+ float pan = argv[6]->f;
+ float velocity = argv[7]->f;
audio_play(when,
sample_name,
offset,
duration,
- speed
+ speed,
+ pan,
+ velocity
);
return 0;
}
@@ -70,7 +73,7 @@ extern int server_init(void) {
//lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
- lo_server_thread_add_method(st, "/play", "ddsfff",
+ lo_server_thread_add_method(st, "/play", "iisfffff",
play_handler,
NULL
);
View
31 test.c
@@ -16,7 +16,36 @@ int main(int argc, char *argv[]) {
printf("sample name?");
exit(-1);
}
- lo_send(t, "/play", "iis", tv.tv_sec, tv.tv_usec, argv[1]);
+ lo_send(t, "/play", "iisfffff",
+ tv.tv_sec, tv.tv_usec,
+ argv[1], /* sample_name */
+ 0.0f, /* offset */
+ 1.0f, /* duration */
+ 1.0f, /* speed */
+ 0.5f, /* pan */
+ 0.0f /* velocity */
+ );
+
+ lo_send(t, "/play", "iisfffff",
+ tv.tv_sec + 1, tv.tv_usec,
+ argv[1], /* sample_name */
+ 0.0f, /* offset */
+ 1.0f, /* duration */
+ 1.0f, /* speed */
+ 0.0f, /* pan */
+ 0.0f /* velocity */
+ );
+
+
+ lo_send(t, "/play", "iisfffff",
+ tv.tv_sec + 2, tv.tv_usec,
+ argv[1], /* sample_name */
+ 0.0f, /* offset */
+ 1.0f, /* duration */
+ 1.0f, /* speed */
+ 0.5f, /* pan */
+ 0.0f /* velocity */
+ );
/* for(i = 0; i < STRENGTH; ++i) {
tv.tv_usec += 8000 + 100 * i;
Please sign in to comment.
Something went wrong with that request. Please try again.