sleep(1); | sleep(1); | ||||
} | } | ||||
out_samplerate = voice_samplerate; | out_samplerate = voice_samplerate; | ||||
if (!wave_init(voice_samplerate)) { | |||||
my_audio = wave_open(voice_samplerate); | |||||
if (!my_audio) { | |||||
err = EE_INTERNAL_ERROR; | err = EE_INTERNAL_ERROR; | ||||
return -1; | return -1; | ||||
} | } | ||||
wave_set_callback_is_output_enabled(fifo_is_command_enabled); | wave_set_callback_is_output_enabled(fifo_is_command_enabled); | ||||
my_audio = wave_open(); | |||||
event_init(); | event_init(); | ||||
} | } | ||||
} | } | ||||
switch (my_mode) | switch (my_mode) | ||||
{ | { | ||||
case AUDIO_OUTPUT_PLAYBACK: | case AUDIO_OUTPUT_PLAYBACK: | ||||
// wave_init() is now called just before the first wave_write() | |||||
synchronous_mode = 0; | |||||
break; | |||||
case AUDIO_OUTPUT_RETRIEVAL: | case AUDIO_OUTPUT_RETRIEVAL: | ||||
synchronous_mode = 0; | synchronous_mode = 0; | ||||
break; | break; |
#ifdef USE_PULSEAUDIO | #ifdef USE_PULSEAUDIO | ||||
// create some wrappers for runtime detection | // create some wrappers for runtime detection | ||||
// checked on wave_init | |||||
// checked on wave_open | |||||
static int pulse_running; | static int pulse_running; | ||||
// wave.cpp (this file) | // wave.cpp (this file) | ||||
int wave_port_init(int); | |||||
void *wave_port_open(); | |||||
void *wave_port_open(int); | |||||
size_t wave_port_write(void *theHandler, char *theMono16BitsWaveBuffer, size_t theSize); | size_t wave_port_write(void *theHandler, char *theMono16BitsWaveBuffer, size_t theSize); | ||||
int wave_port_close(void *theHandler); | int wave_port_close(void *theHandler); | ||||
int wave_port_is_busy(void *theHandler); | int wave_port_is_busy(void *theHandler); | ||||
// wave_pulse.cpp | // wave_pulse.cpp | ||||
int is_pulse_running(); | int is_pulse_running(); | ||||
int wave_pulse_init(int); | |||||
void *wave_pulse_open(); | |||||
void *wave_pulse_open(int); | |||||
size_t wave_pulse_write(void *theHandler, char *theMono16BitsWaveBuffer, size_t theSize); | size_t wave_pulse_write(void *theHandler, char *theMono16BitsWaveBuffer, size_t theSize); | ||||
int wave_pulse_close(void *theHandler); | int wave_pulse_close(void *theHandler); | ||||
int wave_pulse_is_busy(void *theHandler); | int wave_pulse_is_busy(void *theHandler); | ||||
int wave_pulse_get_remaining_time(uint32_t sample, uint32_t *time); | int wave_pulse_get_remaining_time(uint32_t sample, uint32_t *time); | ||||
// wrappers | // wrappers | ||||
int wave_init(int srate) | |||||
{ | |||||
pulse_running = is_pulse_running(); | |||||
if (pulse_running) | |||||
return wave_pulse_init(srate); | |||||
else | |||||
return wave_port_init(srate); | |||||
} | |||||
void *wave_open() | |||||
void *wave_open(int srate) | |||||
{ | { | ||||
if (pulse_running) | if (pulse_running) | ||||
return wave_pulse_open(); | |||||
return wave_pulse_open(srate); | |||||
else | else | ||||
return wave_port_open(); | |||||
return wave_port_open(srate); | |||||
} | } | ||||
size_t wave_write(void *theHandler, char *theMono16BitsWaveBuffer, size_t theSize) | size_t wave_write(void *theHandler, char *theMono16BitsWaveBuffer, size_t theSize) | ||||
} | } | ||||
// rename functions to be wrapped | // rename functions to be wrapped | ||||
#define wave_init wave_port_init | |||||
#define wave_open wave_port_open | #define wave_open wave_port_open | ||||
#define wave_write wave_port_write | #define wave_write wave_port_write | ||||
#define wave_close wave_port_close | #define wave_close wave_port_close | ||||
my_callback_is_output_enabled = cb; | my_callback_is_output_enabled = cb; | ||||
} | } | ||||
int wave_init(int srate) | |||||
void *wave_open(int srate) | |||||
{ | { | ||||
PaError err; | PaError err; | ||||
// PortAudio sound output library | // PortAudio sound output library | ||||
err = Pa_Initialize(); | err = Pa_Initialize(); | ||||
pa_init_err = err; | pa_init_err = err; | ||||
return err == paNoError; | |||||
} | |||||
if (err != paNoError) | |||||
return NULL; | |||||
void *wave_open() | |||||
{ | |||||
static int once = 0; | static int once = 0; | ||||
if (!once) { | if (!once) { | ||||
select_device(); | select_device(); | ||||
once = 1; | once = 1; | ||||
} | } | ||||
return (void *)1; | return (void *)1; | ||||
} | } | ||||
#else | #else | ||||
int wave_init(int srate) | |||||
void *wave_open(int srate) | |||||
{ | { | ||||
(void)srate; // unused | (void)srate; // unused | ||||
return 1; | |||||
} | |||||
void *wave_open() | |||||
{ | |||||
return (void *)1; | return (void *)1; | ||||
} | } | ||||
extern int option_device_number; | extern int option_device_number; | ||||
extern int wave_init(int samplerate); | |||||
extern void *wave_open(); | |||||
extern void *wave_open(int samplerate); | |||||
extern size_t wave_write(void *theHandler, char *theMono16BitsWaveBuffer, size_t theSize); | extern size_t wave_write(void *theHandler, char *theMono16BitsWaveBuffer, size_t theSize); | ||||
extern int wave_close(void *theHandler); | extern int wave_close(void *theHandler); |
#ifdef USE_PORTAUDIO | #ifdef USE_PORTAUDIO | ||||
// rename functions to be wrapped | // rename functions to be wrapped | ||||
#define wave_init wave_pulse_init | |||||
#define wave_open wave_pulse_open | #define wave_open wave_pulse_open | ||||
#define wave_write wave_pulse_write | #define wave_write wave_pulse_write | ||||
#define wave_close wave_pulse_close | #define wave_close wave_pulse_close | ||||
my_callback_is_output_enabled = cb; | my_callback_is_output_enabled = cb; | ||||
} | } | ||||
int wave_init(int srate) | |||||
void *wave_open(int srate) | |||||
{ | { | ||||
stream = NULL; | stream = NULL; | ||||
wave_samplerate = srate; | wave_samplerate = srate; | ||||
return pulse_open() == PULSE_OK; | |||||
} | |||||
if (pulse_open() != PULSE_OK) | |||||
return NULL; | |||||
void *wave_open() | |||||
{ | |||||
return (void *)1; | return (void *)1; | ||||
} | } | ||||
#else | #else | ||||
int wave_init(int srate) | |||||
void *wave_open(int srate) | |||||
{ | { | ||||
(void)srate; // unused | (void)srate; // unused | ||||
return 1; | |||||
} | |||||
void *wave_open() | |||||
{ | |||||
return (void *)1; | return (void *)1; | ||||
} | } | ||||
static uint32_t wave_samplerate; | static uint32_t wave_samplerate; | ||||
// wave_init | |||||
// wave_open | |||||
// | // | ||||
// DESCRIPTION: | // DESCRIPTION: | ||||
// | // | ||||
// sun_audio_fd: modified to hold the file descriptor of the opened | // sun_audio_fd: modified to hold the file descriptor of the opened | ||||
// audio device. | // audio device. | ||||
// | // | ||||
int wave_init(int srate) | |||||
void *wave_open(int srate) | |||||
{ | { | ||||
audio_info_t ainfo; | audio_info_t ainfo; | ||||
char *audio_device = NULL; | char *audio_device = NULL; | ||||
audio_device = getenv("AUDIODEV"); | audio_device = getenv("AUDIODEV"); | ||||
if (audio_device != NULL) { | if (audio_device != NULL) { | ||||
if ((sun_audio_fd = open(audio_device, O_WRONLY)) < 0) { | if ((sun_audio_fd = open(audio_device, O_WRONLY)) < 0) { | ||||
fprintf(stderr, "wave_init() could not open: %s (%d)\n", | |||||
fprintf(stderr, "wave_open() could not open: %s (%d)\n", | |||||
audio_device, sun_audio_fd); | audio_device, sun_audio_fd); | ||||
} | } | ||||
} | } | ||||
if (sun_audio_fd < 0) { | if (sun_audio_fd < 0) { | ||||
if ((sun_audio_fd = open(sun_audio_device, O_WRONLY)) < 0) { | if ((sun_audio_fd = open(sun_audio_device, O_WRONLY)) < 0) { | ||||
fprintf(stderr, "wave_init() could not open: %s (%d)\n", | |||||
fprintf(stderr, "wave_open() could not open: %s (%d)\n", | |||||
sun_audio_device, sun_audio_fd); | sun_audio_device, sun_audio_fd); | ||||
} | } | ||||
} | } | ||||
if (sun_audio_fd < 0) | if (sun_audio_fd < 0) | ||||
return 0; | |||||
return NULL; | |||||
ioctl(sun_audio_fd, AUDIO_GETINFO, &ainfo); | ioctl(sun_audio_fd, AUDIO_GETINFO, &ainfo); | ||||
ainfo.play.encoding = AUDIO_ENCODING_LINEAR; | ainfo.play.encoding = AUDIO_ENCODING_LINEAR; | ||||
ainfo.play.precision = SAMPLE_SIZE; | ainfo.play.precision = SAMPLE_SIZE; | ||||
if (ioctl(sun_audio_fd, AUDIO_SETINFO, &ainfo) == -1) { | if (ioctl(sun_audio_fd, AUDIO_SETINFO, &ainfo) == -1) { | ||||
fprintf(stderr, "wave_init() failed to set audio params: %s\n", strerror(errno)); | |||||
fprintf(stderr, "wave_open() failed to set audio params: %s\n", strerror(errno)); | |||||
close(sun_audio_fd); | close(sun_audio_fd); | ||||
return 0; | |||||
return NULL; | |||||
} | } | ||||
return 1; | |||||
} | |||||
// wave_open | |||||
// | |||||
// DESCRIPTION: | |||||
// | |||||
// opens the audio subsystem. We just return the sun_audio_fd we | |||||
// opened in wave_init. This return value will be passed in as the | |||||
// theHandler parameter in all other methods. | |||||
// | |||||
// GLOBALS USED/MODIFIED: | |||||
// | |||||
// sun_audio_fd: used as return value | |||||
// | |||||
// RETURNS: | |||||
// | |||||
// sun_audio_fd opened in wave_init, which is passed in as theHandler | |||||
// parameter in all other methods | |||||
// | |||||
void *wave_open() | |||||
{ | |||||
return (void *)sun_audio_fd; | return (void *)sun_audio_fd; | ||||
} | } | ||||
#else | #else | ||||
int wave_init(int srate) | |||||
void *wave_open(int srate) | |||||
{ | { | ||||
(void)srate; // unused | (void)srate; // unused | ||||
return 1; | |||||
} | |||||
void *wave_open() | |||||
{ | |||||
return (void *)1; | return (void *)1; | ||||
} | } | ||||