diff --git a/Makefile b/Makefile index 9505770..90083c1 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,9 @@ install: all SUBDIR=`racket -e "(display (format \"~a-~a\" (system-type 'os*) (system-type 'arch)))"`; \ FILES=`ls build/*.dll` 2>/dev/null; if [ "$$FILES" != "" ]; then cp $$FILES lib/$$SUBDIR; fi +test: install + cp lib/linux-x86_64/*.so ~/.local/share/racket/racket-sound-lib/linux-x86_64 + zip: install SUBDIR=`racket -e "(display (format \"~a-~a\" (system-type 'os*) (system-type 'arch)))"`; \ (cd lib; zip -y -r -9 $$SUBDIR.zip $$SUBDIR) diff --git a/ao-play-async/ao_playasync.c b/ao-play-async/ao_playasync.c index fdb4e7d..dff68b5 100644 --- a/ao-play-async/ao_playasync.c +++ b/ao-play-async/ao_playasync.c @@ -16,6 +16,7 @@ #ifdef USE_PTHREADS #include + #include #define MUTEX_LOCK(m) pthread_mutex_lock(&m) #define MUTEX_UNLOCK(m) pthread_mutex_unlock(&m) #endif @@ -55,21 +56,26 @@ typedef int(*ao_play_func_t)(void *, char *, uint32_t); typedef struct { Queue_t *play_queue; Queue_t *last_frame; + sem_t queue_sem; + + int paused; + ao_device *ao_device; #ifdef USE_WINDOWS_THREADS HANDLE mutex; + HANDLE pause_mutex; HANDLE thread; DWORD thread_id; #endif #ifdef USE_PTHREADS pthread_mutex_t mutex; + pthread_mutex_t pause_mutex; pthread_t thread; #endif double at_second; double music_duration; ao_play_func_t ao_play_f; int buf_size; - int paused; } AO_Handle; //static int(*ao_play)(void *device, char *samples, uint32_t n) = NULL; @@ -83,6 +89,8 @@ static Queue_t *front(AO_Handle *h) static Queue_t *get(AO_Handle *h) { + sem_wait(&h->queue_sem); + MUTEX_LOCK(h->mutex); assert(h->play_queue != NULL); Queue_t *q = h->play_queue; h->play_queue = h->play_queue->next; @@ -92,11 +100,13 @@ static Queue_t *get(AO_Handle *h) h->play_queue->prev = NULL; } h->buf_size -= q->buflen; + MUTEX_UNLOCK(h->mutex); return q; } static void add(AO_Handle *h, Queue_t *elem) { + MUTEX_LOCK(h->mutex); if (h->last_frame == NULL) { h->play_queue = elem; elem->next = NULL; @@ -109,6 +119,8 @@ static void add(AO_Handle *h, Queue_t *elem) h->last_frame = elem; } h->buf_size += elem->buflen; + MUTEX_UNLOCK(h->mutex); + sem_post(&h->queue_sem); } static Queue_t *new_elem(int command, double at_second, double music_duration, int buf_len, void *buf) @@ -126,7 +138,7 @@ static Queue_t *new_elem(int command, double at_second, double music_duration, i q->music_duration = music_duration; q->buf = new_buf; q->buflen = buf_len; - q->command = command; + q->command = (Command_t) command; q->next = NULL; q->prev = NULL; return q; @@ -160,27 +172,22 @@ static DWORD run(LPVOID arg) int go_on = (1 == 1); while(go_on) { - MUTEX_LOCK(handle->mutex); - int has_frames = (!handle->paused) && (handle->play_queue != NULL); + MUTEX_LOCK(handle->pause_mutex); + MUTEX_UNLOCK(handle->pause_mutex); - if (has_frames) { - Queue_t *q = get(handle); - handle->at_second = q->at_second; - handle->music_duration = q->music_duration; - MUTEX_UNLOCK(handle->mutex); + Queue_t *q = get(handle); - if (q->command == STOP) { - go_on = (1 == 0); - } else { - //fprintf(stderr, "playing buf at %lf\n", handle->at_second); - handle->ao_play_f(handle->ao_device, q->buf, q->buflen); - } + handle->at_second = q->at_second; + handle->music_duration = q->music_duration; - del_elem(q); + if (q->command == STOP) { + go_on = (1 == 0); } else { - MUTEX_UNLOCK(handle->mutex); - sleep_ms(5); // sleep for 5ms + //fprintf(stderr, "playing buf at %lf\n", handle->at_second); + handle->ao_play_f(handle->ao_device, (char *) q->buf, q->buflen); } + + del_elem(q); } #ifdef USE_PTHREADS @@ -207,23 +214,33 @@ void *ao_create_async(void *ao_device_yeah, void *ao_play_f) handle->last_frame = NULL; handle->at_second = -1; - handle->ao_play_f = ao_play_f; + handle->ao_play_f = (ao_play_func_t) ao_play_f; handle->buf_size = 0; + handle->paused = (1 == 0); #ifdef USE_PTHREADS + pthread_mutex_t p = PTHREAD_MUTEX_INITIALIZER; + handle->pause_mutex = p; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; handle->mutex = m; pthread_create(&handle->thread, NULL, run, handle); + sem_init(&handle->queue_sem, 0, 0); #endif #ifdef USE_WINDOWS_THREADS handle->mutex = CreateMutex(NULL, // default security attributes FALSE, // initially not owned NULL); + handle->pause_mutex = CreateMutex(NULL, // default security attributes + FALSE, // initially not owned + NULL); handle->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) run, handle, 0, &handle->thread_id); + // TODO Windows Semaphores! #endif + MUTEX_UNLOCK(handle->pause_mutex); + return (void *) handle; } @@ -231,11 +248,9 @@ void ao_stop_async(void *ao_handle) { AO_Handle *h = (AO_Handle *) ao_handle; - MUTEX_LOCK(h->mutex); clear(h); Queue_t *q = new_elem(STOP, 0.0, 0.0, 0, NULL); add(h, q); - MUTEX_UNLOCK(h->mutex); #ifdef USE_PTHREADS void *retval; @@ -375,17 +390,13 @@ void ao_play_async(void *ao_handle, double at_second, double music_duration, int break; } - MUTEX_LOCK(h->mutex); add(h, q); - MUTEX_UNLOCK(h->mutex); } void ao_clear_async(void *ao_handle) { AO_Handle *h = (AO_Handle *) ao_handle; - MUTEX_LOCK(h->mutex); clear(h); - MUTEX_UNLOCK(h->mutex); } double ao_is_at_second_async(void *ao_handle) @@ -419,7 +430,19 @@ void ao_pause_async(void *ao_handle, int paused) { AO_Handle *h = (AO_Handle *) ao_handle; MUTEX_LOCK(h->mutex); - h->paused = paused; + + if (h->paused) { + if (!paused) { + h->paused = paused; + MUTEX_UNLOCK(h->pause_mutex); + } + } else { + if (paused) { + h->paused = paused; + MUTEX_LOCK(h->pause_mutex); + } + } + MUTEX_UNLOCK(h->mutex); } diff --git a/lib/linux-x86_64.zip b/lib/linux-x86_64.zip index 01e7efd..819d041 100644 Binary files a/lib/linux-x86_64.zip and b/lib/linux-x86_64.zip differ