format
This commit is contained in:
parent
cfe823b25d
commit
cd1efe40ad
5 changed files with 46 additions and 47 deletions
|
|
@ -4,23 +4,23 @@
|
||||||
#include <alsa/asoundlib.h>
|
#include <alsa/asoundlib.h>
|
||||||
|
|
||||||
typedef struct driver_context {
|
typedef struct driver_context {
|
||||||
snd_pcm_t* device;
|
snd_pcm_t* device;
|
||||||
snd_pcm_hw_params_t* params;
|
snd_pcm_hw_params_t* params;
|
||||||
} driver_context_t;
|
} driver_context_t;
|
||||||
|
|
||||||
#include "pthread/decl.c"
|
#include "pthread/decl.c"
|
||||||
|
|
||||||
static void drv_write(driver_t* drv, void* data, int frames){
|
static void drv_write(driver_t* drv, void* data, int frames) {
|
||||||
if(snd_pcm_writei(drv->context.device, data, frames) == -EPIPE){
|
if(snd_pcm_writei(drv->context.device, data, frames) == -EPIPE) {
|
||||||
snd_pcm_prepare(drv->context.device);
|
snd_pcm_prepare(drv->context.device);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int drv_open(driver_t* drv){
|
static int drv_open(driver_t* drv) {
|
||||||
snd_pcm_uframes_t frames;
|
snd_pcm_uframes_t frames;
|
||||||
int rate = MDEAudioRate;
|
int rate = MDEAudioRate;
|
||||||
|
|
||||||
if(snd_pcm_open(&drv->context.device, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0){
|
if(snd_pcm_open(&drv->context.device, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -28,32 +28,32 @@ static int drv_open(driver_t* drv){
|
||||||
snd_pcm_hw_params_malloc(&drv->context.params);
|
snd_pcm_hw_params_malloc(&drv->context.params);
|
||||||
snd_pcm_hw_params_any(drv->context.device, drv->context.params);
|
snd_pcm_hw_params_any(drv->context.device, drv->context.params);
|
||||||
|
|
||||||
if(snd_pcm_hw_params_set_access(drv->context.device, drv->context.params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0){
|
if(snd_pcm_hw_params_set_access(drv->context.device, drv->context.params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(snd_pcm_hw_params_set_format(drv->context.device, drv->context.params, SND_PCM_FORMAT_S16) < 0){
|
if(snd_pcm_hw_params_set_format(drv->context.device, drv->context.params, SND_PCM_FORMAT_S16) < 0) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(snd_pcm_hw_params_set_channels(drv->context.device, drv->context.params, 2) < 0){
|
if(snd_pcm_hw_params_set_channels(drv->context.device, drv->context.params, 2) < 0) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(snd_pcm_hw_params_set_rate_near(drv->context.device, drv->context.params, &rate, 0) < 0){
|
if(snd_pcm_hw_params_set_rate_near(drv->context.device, drv->context.params, &rate, 0) < 0) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(snd_pcm_hw_params_set_rate(drv->context.device, drv->context.params, rate, 0) < 0){
|
if(snd_pcm_hw_params_set_rate(drv->context.device, drv->context.params, rate, 0) < 0) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(snd_pcm_hw_params(drv->context.device, drv->context.params) < 0){
|
if(snd_pcm_hw_params(drv->context.device, drv->context.params) < 0) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -64,9 +64,8 @@ static int drv_open(driver_t* drv){
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void drv_close(driver_t* drv) {
|
||||||
static void drv_close(driver_t* drv){
|
if(drv->context.device != NULL) {
|
||||||
if(drv->context.device != NULL){
|
|
||||||
snd_pcm_drain(drv->context.device);
|
snd_pcm_drain(drv->context.device);
|
||||||
snd_pcm_close(drv->context.device);
|
snd_pcm_close(drv->context.device);
|
||||||
snd_pcm_hw_params_free(drv->context.params);
|
snd_pcm_hw_params_free(drv->context.params);
|
||||||
|
|
|
||||||
|
|
@ -5,28 +5,28 @@
|
||||||
|
|
||||||
typedef struct driver_context {
|
typedef struct driver_context {
|
||||||
ao_sample_format format;
|
ao_sample_format format;
|
||||||
ao_device* device;
|
ao_device* device;
|
||||||
} driver_context_t;
|
} driver_context_t;
|
||||||
|
|
||||||
#include "pthread/decl.c"
|
#include "pthread/decl.c"
|
||||||
|
|
||||||
static void drv_write(driver_t* drv, void* data, int frames){
|
static void drv_write(driver_t* drv, void* data, int frames) {
|
||||||
ao_play(drv->context.device, (char*)data, frames * 2 * 2);
|
ao_play(drv->context.device, (char*)data, frames * 2 * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int drv_open(driver_t* drv){
|
static int drv_open(driver_t* drv) {
|
||||||
int id;
|
int id;
|
||||||
|
|
||||||
ao_initialize();
|
ao_initialize();
|
||||||
|
|
||||||
drv->context.format.bits = 16;
|
drv->context.format.bits = 16;
|
||||||
drv->context.format.rate = MDEAudioRate;
|
drv->context.format.rate = MDEAudioRate;
|
||||||
drv->context.format.channels = 2;
|
drv->context.format.channels = 2;
|
||||||
drv->context.format.byte_format = AO_FMT_NATIVE;
|
drv->context.format.byte_format = AO_FMT_NATIVE;
|
||||||
drv->context.format.matrix = NULL;
|
drv->context.format.matrix = NULL;
|
||||||
|
|
||||||
id = ao_default_driver_id();
|
id = ao_default_driver_id();
|
||||||
if((drv->context.device = ao_open_live(id, &drv->context.format, NULL)) == NULL){
|
if((drv->context.device = ao_open_live(id, &drv->context.format, NULL)) == NULL) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -36,8 +36,8 @@ static int drv_open(driver_t* drv){
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void drv_close(driver_t* drv){
|
static void drv_close(driver_t* drv) {
|
||||||
if(drv->context.device != NULL){
|
if(drv->context.device != NULL) {
|
||||||
ao_close(drv->context.device);
|
ao_close(drv->context.device);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
static void* thread_routine(void* arg){
|
static void* thread_routine(void* arg) {
|
||||||
driver_t* drv = arg;
|
driver_t* drv = arg;
|
||||||
void* data;
|
void* data;
|
||||||
|
|
||||||
data = malloc(drv->frames * 2 * 2);
|
data = malloc(drv->frames * 2 * 2);
|
||||||
|
|
||||||
while(1){
|
while(1) {
|
||||||
drv->handler(drv, drv->user, data, drv->frames);
|
drv->handler(drv, drv->user, data, drv->frames);
|
||||||
|
|
||||||
drv_write(drv, data, drv->frames);
|
drv_write(drv, data, drv->frames);
|
||||||
|
|
||||||
pthread_mutex_lock(&drv->mutex);
|
pthread_mutex_lock(&drv->mutex);
|
||||||
if(drv->quit){
|
if(drv->quit) {
|
||||||
pthread_mutex_unlock(&drv->mutex);
|
pthread_mutex_unlock(&drv->mutex);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -22,19 +22,19 @@ static void* thread_routine(void* arg){
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* MDEAudioOpen(MDEAudioHandler handler, void* user){
|
void* MDEAudioOpen(MDEAudioHandler handler, void* user) {
|
||||||
int rate = MDEAudioRate;
|
int rate = MDEAudioRate;
|
||||||
driver_t* drv = malloc(sizeof(*drv));
|
driver_t* drv = malloc(sizeof(*drv));
|
||||||
memset(drv, 0, sizeof(*drv));
|
memset(drv, 0, sizeof(*drv));
|
||||||
|
|
||||||
drv->quit = 0;
|
drv->quit = 0;
|
||||||
drv->handler = handler;
|
drv->handler = handler;
|
||||||
drv->user = user;
|
drv->user = user;
|
||||||
drv->init = 0;
|
drv->init = 0;
|
||||||
|
|
||||||
pthread_mutex_init(&drv->mutex, NULL);
|
pthread_mutex_init(&drv->mutex, NULL);
|
||||||
|
|
||||||
if(!(drv->init = drv_open(drv))){
|
if(!(drv->init = drv_open(drv))) {
|
||||||
MDEAudioClose(drv);
|
MDEAudioClose(drv);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -42,11 +42,11 @@ void* MDEAudioOpen(MDEAudioHandler handler, void* user){
|
||||||
return drv;
|
return drv;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDEAudioClose(void* handle){
|
void MDEAudioClose(void* handle) {
|
||||||
driver_t* drv = handle;
|
driver_t* drv = handle;
|
||||||
void* ret;
|
void* ret;
|
||||||
|
|
||||||
if(drv->init){
|
if(drv->init) {
|
||||||
pthread_mutex_lock(&drv->mutex);
|
pthread_mutex_lock(&drv->mutex);
|
||||||
drv->quit = 1;
|
drv->quit = 1;
|
||||||
pthread_mutex_unlock(&drv->mutex);
|
pthread_mutex_unlock(&drv->mutex);
|
||||||
|
|
@ -60,7 +60,7 @@ void MDEAudioClose(void* handle){
|
||||||
free(drv);
|
free(drv);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDEAudioStart(void* handle){
|
void MDEAudioStart(void* handle) {
|
||||||
driver_t* drv = handle;
|
driver_t* drv = handle;
|
||||||
|
|
||||||
pthread_create(&drv->thread, NULL, thread_routine, handle);
|
pthread_create(&drv->thread, NULL, thread_routine, handle);
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,9 @@ typedef struct driver {
|
||||||
int frames;
|
int frames;
|
||||||
|
|
||||||
MDEAudioHandler handler;
|
MDEAudioHandler handler;
|
||||||
void* user;
|
void* user;
|
||||||
|
|
||||||
int quit;
|
int quit;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
} driver_t;
|
} driver_t;
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ extern "C" {
|
||||||
#define MDEAudioRate 48000
|
#define MDEAudioRate 48000
|
||||||
|
|
||||||
/* S16, stereo. frames*2*2 == bytes */
|
/* S16, stereo. frames*2*2 == bytes */
|
||||||
typedef void(*MDEAudioHandler)(void* handle, void* user, void* data, int frames);
|
typedef void (*MDEAudioHandler)(void* handle, void* user, void* data, int frames);
|
||||||
|
|
||||||
/* driver functions */
|
/* driver functions */
|
||||||
void* MDEAudioOpen(MDEAudioHandler handler, void* user);
|
void* MDEAudioOpen(MDEAudioHandler handler, void* user);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue