ao_pulse: support native mute control
[mplayer.git] / libao2 / ao_sgi.c
blob492c8ff3ba267834d055e9fbdf87fffc542c9882
1 /*
2 * SGI/IRIX audio output driver
4 * copyright (c) 2001 oliver.schoenbrunner@jku.at
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <dmedia/audio.h>
29 #include "audio_out.h"
30 #include "audio_out_internal.h"
31 #include "mp_msg.h"
32 #include "libaf/af_format.h"
34 static const ao_info_t info =
36 "sgi audio output",
37 "sgi",
38 "Oliver Schoenbrunner",
42 LIBAO_EXTERN(sgi)
45 static ALconfig ao_config;
46 static ALport ao_port;
47 static int sample_rate;
48 static int queue_size;
49 static int bytes_per_frame;
51 /**
52 * \param [in/out] format
53 * \param [out] width
55 * \return the closest matching SGI AL sample format
57 * \note width is set to required per-channel sample width
58 * format is updated to match the SGI AL sample format
60 static int fmt2sgial(int *format, int *width) {
61 int smpfmt = AL_SAMPFMT_TWOSCOMP;
63 /* SGI AL only supports float and signed integers in native
64 * endianness. If this is something else, we must rely on the audio
65 * filter to convert it to a compatible format. */
67 /* 24-bit audio is supported, but only with 32-bit alignment.
68 * mplayer's 24-bit format is packed, unfortunately.
69 * So we must upgrade 24-bit requests to 32 bits. Then we drop the
70 * lowest 8 bits during playback. */
72 switch(*format) {
73 case AF_FORMAT_U8:
74 case AF_FORMAT_S8:
75 *width = AL_SAMPLE_8;
76 *format = AF_FORMAT_S8;
77 break;
79 case AF_FORMAT_U16_LE:
80 case AF_FORMAT_U16_BE:
81 case AF_FORMAT_S16_LE:
82 case AF_FORMAT_S16_BE:
83 *width = AL_SAMPLE_16;
84 *format = AF_FORMAT_S16_NE;
85 break;
87 case AF_FORMAT_U24_LE:
88 case AF_FORMAT_U24_BE:
89 case AF_FORMAT_S24_LE:
90 case AF_FORMAT_S24_BE:
91 case AF_FORMAT_U32_LE:
92 case AF_FORMAT_U32_BE:
93 case AF_FORMAT_S32_LE:
94 case AF_FORMAT_S32_BE:
95 *width = AL_SAMPLE_24;
96 *format = AF_FORMAT_S32_NE;
97 break;
99 case AF_FORMAT_FLOAT_LE:
100 case AF_FORMAT_FLOAT_BE:
101 *width = 4;
102 *format = AF_FORMAT_FLOAT_NE;
103 smpfmt = AL_SAMPFMT_FLOAT;
104 break;
106 default:
107 *width = AL_SAMPLE_16;
108 *format = AF_FORMAT_S16_NE;
109 break;
113 return smpfmt;
116 // to set/get/query special features/parameters
117 static int control(int cmd, void *arg){
119 mp_tmsg(MSGT_AO, MSGL_INFO, "[AO SGI] control.\n");
121 return CONTROL_UNKNOWN;
124 // open & setup audio device
125 // return: 1=success 0=fail
126 static int init(int rate, int channels, int format, int flags) {
128 int smpwidth, smpfmt;
129 int rv = AL_DEFAULT_OUTPUT;
131 smpfmt = fmt2sgial(&format, &smpwidth);
133 mp_tmsg(MSGT_AO, MSGL_INFO, "[AO SGI] init: Samplerate: %iHz Channels: %s Format %s\n", rate, (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format));
135 { /* from /usr/share/src/dmedia/audio/setrate.c */
137 double frate, realrate;
138 ALpv x[2];
140 if(ao_subdevice) {
141 rv = alGetResourceByName(AL_SYSTEM, ao_subdevice, AL_OUTPUT_DEVICE_TYPE);
142 if (!rv) {
143 mp_tmsg(MSGT_AO, MSGL_ERR, "[AO SGI] play: invalid device.\n");
144 return 0;
148 frate = rate;
150 x[0].param = AL_RATE;
151 x[0].value.ll = alDoubleToFixed(rate);
152 x[1].param = AL_MASTER_CLOCK;
153 x[1].value.i = AL_CRYSTAL_MCLK_TYPE;
155 if (alSetParams(rv,x, 2)<0) {
156 mp_tmsg(MSGT_AO, MSGL_WARN, "[AO SGI] init: setparams failed: %s\nCould not set desired samplerate.\n", alGetErrorString(oserror()));
159 if (x[0].sizeOut < 0) {
160 mp_tmsg(MSGT_AO, MSGL_WARN, "[AO SGI] init: AL_RATE was not accepted on the given resource.\n");
163 if (alGetParams(rv,x, 1)<0) {
164 mp_tmsg(MSGT_AO, MSGL_WARN, "[AO SGI] init: getparams failed: %s\n", alGetErrorString(oserror()));
167 realrate = alFixedToDouble(x[0].value.ll);
168 if (frate != realrate) {
169 mp_tmsg(MSGT_AO, MSGL_INFO, "[AO SGI] init: samplerate is now %f (desired rate is %f)\n", realrate, frate);
171 sample_rate = (int)realrate;
174 bytes_per_frame = channels * smpwidth;
176 ao_data.samplerate = sample_rate;
177 ao_data.channels = channels;
178 ao_data.format = format;
179 ao_data.bps = sample_rate * bytes_per_frame;
180 ao_data.buffersize=131072;
181 ao_data.outburst = ao_data.buffersize/16;
183 ao_config = alNewConfig();
185 if (!ao_config) {
186 mp_tmsg(MSGT_AO, MSGL_ERR, "[AO SGI] init: %s\n", alGetErrorString(oserror()));
187 return 0;
190 if(alSetChannels(ao_config, channels) < 0 ||
191 alSetWidth(ao_config, smpwidth) < 0 ||
192 alSetSampFmt(ao_config, smpfmt) < 0 ||
193 alSetQueueSize(ao_config, sample_rate) < 0 ||
194 alSetDevice(ao_config, rv) < 0) {
195 mp_tmsg(MSGT_AO, MSGL_ERR, "[AO SGI] init: %s\n", alGetErrorString(oserror()));
196 return 0;
199 ao_port = alOpenPort("mplayer", "w", ao_config);
201 if (!ao_port) {
202 mp_tmsg(MSGT_AO, MSGL_ERR, "[AO SGI] init: Unable to open audio channel: %s\n", alGetErrorString(oserror()));
203 return 0;
206 // printf("ao_sgi, init: port %d config %d\n", ao_port, ao_config);
207 queue_size = alGetQueueSize(ao_config);
208 return 1;
212 // close audio device
213 static void uninit(int immed) {
215 /* TODO: samplerate should be set back to the value before mplayer was started! */
217 mp_tmsg(MSGT_AO, MSGL_INFO, "[AO SGI] uninit: ...\n");
219 if (ao_config) {
220 alFreeConfig(ao_config);
221 ao_config = NULL;
224 if (ao_port) {
225 if (!immed)
226 while(alGetFilled(ao_port) > 0) sginap(1);
227 alClosePort(ao_port);
228 ao_port = NULL;
233 // stop playing and empty buffers (for seeking/pause)
234 static void reset(void) {
236 mp_tmsg(MSGT_AO, MSGL_INFO, "[AO SGI] reset: ...\n");
238 alDiscardFrames(ao_port, queue_size);
241 // stop playing, keep buffers (for pause)
242 static void audio_pause(void) {
244 mp_tmsg(MSGT_AO, MSGL_INFO, "[AO SGI] audio_pause: ...\n");
248 // resume playing, after audio_pause()
249 static void audio_resume(void) {
251 mp_tmsg(MSGT_AO, MSGL_INFO, "[AO SGI] audio_resume: ...\n");
255 // return: how many bytes can be played without blocking
256 static int get_space(void) {
258 // printf("ao_sgi, get_space: (ao_outburst %d)\n", ao_data.outburst);
259 // printf("ao_sgi, get_space: alGetFillable [%d] \n", alGetFillable(ao_port));
261 return alGetFillable(ao_port) * bytes_per_frame;
266 // plays 'len' bytes of 'data'
267 // it should round it down to outburst*n
268 // return: number of bytes played
269 static int play(void* data, int len, int flags) {
271 /* Always process data in quadword-aligned chunks (64-bits). */
272 const int plen = len / (sizeof(uint64_t) * bytes_per_frame);
273 const int framecount = plen * sizeof(uint64_t);
275 // printf("ao_sgi, play: len %d flags %d (%d %d)\n", len, flags, ao_port, ao_config);
276 // printf("channels %d\n", ao_data.channels);
278 if(ao_data.format == AF_FORMAT_S32_NE) {
279 /* The zen of this is explained in fmt2sgial() */
280 int32_t *smpls = data;
281 const int32_t *smple = smpls + (framecount * ao_data.channels);
282 while(smpls < smple)
283 *smpls++ >>= 8;
286 alWriteFrames(ao_port, data, framecount);
288 return framecount * bytes_per_frame;
292 // return: delay in seconds between first and last sample in buffer
293 static float get_delay(void){
295 // printf("ao_sgi, get_delay: (ao_buffersize %d)\n", ao_buffersize);
297 // return (float)queue_size/((float)sample_rate);
298 const int outstanding = alGetFilled(ao_port);
299 return (float)((outstanding < 0) ? queue_size : outstanding) /
300 ((float)sample_rate);