00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #include "stdinc.h"
00032
00033 #include <ao/ao.h>
00034
00035 #include "audio_file.h"
00036 #include "audio_output.h"
00037 #include "config.h"
00038 #include "gui.h"
00039
00043 static ao_device* devptr = NULL;
00047 static ao_sample_format devfmt;
00052 static ao_option *devopt = NULL;
00053
00054 int
00055 audio_output_open(void)
00056 {
00057 const char *host;
00058 char *henv, *hend;
00059
00060 ao_initialize();
00061
00062
00063 devfmt.bits = 16;
00064 devfmt.byte_format = AO_FMT_NATIVE;
00065
00066 host = config_getopt("audio.output.ao.host");
00067 if (strcmp(host, "env_ssh") == 0) {
00068
00069 henv = g_strdup(getenv("SSH_CLIENT"));
00070 if (henv != NULL) {
00071
00072 if ((hend = strchr(henv, ' ')) != NULL)
00073 *hend = '\0';
00074 ao_append_option(&devopt, "host", henv);
00075 g_free(henv);
00076 }
00077 } else if (host[0] != '\0') {
00078
00079 ao_append_option(&devopt, "host", host);
00080 }
00081
00082 return (0);
00083 }
00084
00085 int
00086 audio_output_play(struct audio_file *fd)
00087 {
00088 const char *drvname;
00089 int16_t buf[2048];
00090 int len, drvnum;
00091
00092 if ((len = audio_file_read(fd, buf, sizeof buf / sizeof(int16_t))) == 0)
00093 return (-1);
00094
00095 if ((unsigned int)devfmt.rate != fd->srate ||
00096 (unsigned int)devfmt.channels != fd->channels) {
00097
00098 audio_output_close();
00099
00100 devfmt.rate = fd->srate;
00101 devfmt.channels = fd->channels;
00102 }
00103
00104 if (devptr == NULL) {
00105 drvname = config_getopt("audio.output.ao.driver");
00106 if (drvname[0] != '\0') {
00107
00108 drvnum = ao_driver_id(drvname);
00109 } else {
00110
00111 drvnum = ao_default_driver_id();
00112 }
00113
00114 devptr = ao_open_live(drvnum, &devfmt, devopt);
00115 if (devptr == NULL) {
00116 gui_msgbar_warn(_("Cannot open the audio device."));
00117 return (-1);
00118 }
00119 }
00120
00121 if (ao_play(devptr, (char *)buf, len * sizeof(int16_t)) == 0) {
00122
00123 audio_output_close();
00124 return (-1);
00125 }
00126
00127 return (0);
00128 }
00129
00130 void
00131 audio_output_close(void)
00132 {
00133 if (devptr != NULL) {
00134
00135 ao_close(devptr);
00136 devptr = NULL;
00137 }
00138 }