This commit is contained in:
2026-02-22 23:15:08 +01:00
parent 2cc96f30ae
commit 24b39bee60
8 changed files with 101 additions and 32 deletions

View File

@@ -5,7 +5,10 @@ all:
(cd build; make)
install:
(cd build; cp *.so /usr/local/lib)
mkdir -p ../lib
FILES=`ls build/*.so` 2>/dev/null; if [ "$$FILES" != "" ]; then cp $$FILES ../lib; fi
FILES=`ls build/*.dll` 2>/dev/null; if [ "$$FILES" != "" ]; then cp $$FILES ../lib; fi
clean:
rm -rf build

View File

@@ -4,7 +4,7 @@
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <dlfcn.h>
//#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -26,15 +26,17 @@ typedef struct _queue_ {
} Queue_t;
typedef struct {
Queue_t *play_queue;
Queue_t *last_frame;
ao_device *ao_device;
Queue_t *play_queue;
Queue_t *last_frame;
ao_device *ao_device;
pthread_mutex_t mutex;
pthread_t thread;
double at_second;
pthread_t thread;
double at_second;
ao_play_func_t ao_play_f;
int buf_size;
} AO_Handle;
static int(*ao_play)(void *device, char *samples, uint32_t n) = NULL;
//static int(*ao_play)(void *device, char *samples, uint32_t n) = NULL;
static Queue_t *front(AO_Handle *h)
@@ -53,6 +55,7 @@ static Queue_t *get(AO_Handle *h)
} else {
h->play_queue->prev = NULL;
}
h->buf_size -= q->buflen;
return q;
}
@@ -69,6 +72,7 @@ static void add(AO_Handle *h, Queue_t *elem)
elem->next = NULL;
h->last_frame = elem;
}
h->buf_size += elem->buflen;
}
static Queue_t *new_elem(int command, double at_second, int buf_len, void *buf)
@@ -125,7 +129,7 @@ static void *run(void *arg)
if (q->command == STOP) {
go_on = (1 == 0);
} else {
ao_play(handle->ao_device, q->buf, q->buflen);
handle->ao_play_f(handle->ao_device, q->buf, q->buflen);
}
del_elem(q);
@@ -138,6 +142,7 @@ static void *run(void *arg)
return NULL;
}
/*
static void get_ao_play(void)
{
char *lib = "libao.so";
@@ -155,11 +160,12 @@ static void get_ao_play(void)
exit(EXIT_FAILURE);
}
}
*/
void *ao_create_async(void *ao_device_yeah)
void *ao_create_async(void *ao_device_yeah, void *ao_play_f)
{
if (ao_play == NULL) { get_ao_play(); }
//if (ao_play == NULL) { get_ao_play(); }
AO_Handle *handle = (AO_Handle *) malloc(sizeof(AO_Handle));
@@ -169,6 +175,8 @@ void *ao_create_async(void *ao_device_yeah)
handle->at_second = -1;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
handle->mutex = m;
handle->ao_play_f = ao_play_f;
handle->buf_size = 0;
pthread_create(&handle->thread, NULL, run, handle);
@@ -202,8 +210,19 @@ void ao_play_async(void *ao_handle, double at_second, int buf_size, void *mem)
double ao_is_at_second_async(void *ao_handle)
{
AO_Handle *h = (AO_Handle *) ao_handle;
return h->at_second;
pthread_mutex_lock(&h->mutex);
double s = h->at_second;
pthread_mutex_unlock(&h->mutex);
return s;
}
int ao_bufsize_async(void *ao_handle)
{
AO_Handle *h = (AO_Handle *) ao_handle;
pthread_mutex_lock(&h->mutex);
int s = h->buf_size;
pthread_mutex_unlock(&h->mutex);
return s;
}

View File

@@ -1,9 +1,14 @@
#ifndef AO_PLAYASYNC_H
#define AO_PLAYASYNC_H
extern void *ao_create_async(void *ao_handle);
#include <stdint.h>
typedef int(*ao_play_func_t)(void *, char *, uint32_t);
extern void *ao_create_async(void *ao_handle, void *ao_play_f);
extern void ao_stop_async(void *handle);
extern void ao_play_async(void *handle, double at_second, int buf_size, void *mem);
extern double ao_is_at_second_async(void *handle);
extern int ao_bufsize_async(void *handle);
#endif // AO_PLAYASYNC_H