core: fix audio-only + framestep weird behavior
[mplayer/glamo.git] / libao2 / ao_dart.c
blobe1df7d32f7929cf08ac978529701ed6f4eff4a23
1 /*
2 * OS/2 DART audio output driver
4 * Copyright (c) 2007-2009 by KO Myung-Hun (komh@chollian.net)
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 #define INCL_DOS
24 #define INCL_DOSERRORS
25 #include <os2.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30 #include <float.h>
32 #include <dart.h>
34 #include "config.h"
35 #include "libaf/af_format.h"
36 #include "audio_out.h"
37 #include "audio_out_internal.h"
38 #include "mp_msg.h"
39 #include "libvo/fastmemcpy.h"
40 #include "subopt-helper.h"
41 #include "libavutil/fifo.h"
43 static const ao_info_t info = {
44 "DART audio output",
45 "dart",
46 "KO Myung-Hun <komh@chollian.net>",
50 LIBAO_EXTERN(dart)
52 #define OUTBURST_SAMPLES 512
53 #define DEFAULT_DART_SAMPLES (OUTBURST_SAMPLES << 2)
55 #define CHUNK_SIZE ao_data.outburst
57 static AVFifoBuffer *m_audioBuf;
59 static int m_nBufSize = 0;
61 static volatile int m_fQuit = FALSE;
63 static int write_buffer(unsigned char *data, int len)
65 int nFree = av_fifo_space(m_audioBuf);
67 if (len > nFree)
68 len = nFree;
70 return av_fifo_generic_write(m_audioBuf, data, len, NULL);
73 static int read_buffer(unsigned char *data, int len)
75 int nBuffered = av_fifo_size(m_audioBuf);
77 if (len > nBuffered)
78 len = nBuffered;
80 av_fifo_generic_read(m_audioBuf, data, len, NULL);
81 return len;
84 // end ring buffer stuff
86 static ULONG APIENTRY dart_audio_callback(PVOID pCBData, PVOID pBuffer,
87 ULONG ulSize)
89 int nReadLen;
91 nReadLen = read_buffer(pBuffer, ulSize);
92 if (nReadLen < ulSize && !m_fQuit) {
93 memset((uint8_t *)pBuffer + nReadLen, DART.bSilence, ulSize - nReadLen);
94 nReadLen = ulSize;
97 return nReadLen;
100 // to set/get/query special features/parameters
101 static int control(int cmd, void *arg)
103 switch (cmd) {
104 case AOCONTROL_GET_VOLUME:
106 ao_control_vol_t *vol = arg;
108 vol->left = vol->right = LOUSHORT(dartGetVolume());
110 return CONTROL_OK;
113 case AOCONTROL_SET_VOLUME:
115 int mid;
116 ao_control_vol_t *vol = arg;
118 mid = (vol->left + vol->right) / 2;
119 dartSetVolume(MCI_SET_AUDIO_ALL, mid);
121 return CONTROL_OK;
125 return CONTROL_UNKNOWN;
128 static void print_help(void)
130 mp_msg(MSGT_AO, MSGL_FATAL,
131 "\n-ao dart commandline help:\n"
132 "Example: mplayer -ao dart:noshare\n"
133 " open DART in exclusive mode\n"
134 "\nOptions:\n"
135 " (no)share\n"
136 " Open DART in shareable or exclusive mode\n"
137 " bufsize=<size>\n"
138 " Set buffer size to <size> in samples(default: 2048)\n");
141 // open & set up audio device
142 // return: 1=success 0=fail
143 static int init(int rate, int channels, int format, int flags)
145 int fShare = 1;
146 int nDartSamples = DEFAULT_DART_SAMPLES;
147 int nBytesPerSample;
149 const opt_t subopts[] = {
150 {"share", OPT_ARG_BOOL, &fShare, NULL},
151 {"bufsize", OPT_ARG_INT, &nDartSamples, int_non_neg},
152 {NULL}
155 if (subopt_parse(ao_subdevice, subopts) != 0) {
156 print_help();
157 return 0;
160 if (!nDartSamples)
161 nDartSamples = DEFAULT_DART_SAMPLES;
163 mp_msg(MSGT_AO, MSGL_V, "DART: opened in %s mode, buffer size = %d sample(s)\n",
164 fShare ? "shareable" : "exclusive", nDartSamples);
166 switch (format) {
167 case AF_FORMAT_S16_LE:
168 case AF_FORMAT_S8:
169 break;
171 default:
172 format = AF_FORMAT_S16_LE;
173 mp_msg(MSGT_AO, MSGL_V, "DART: format %s not supported defaulting to Signed 16-bit Little-Endian\n",
174 af_fmt2str_short(format));
175 break;
178 nBytesPerSample = (af_fmt2bits(format) >> 3) * channels;
180 if (dartInit(0, af_fmt2bits(format), rate, MCI_WAVE_FORMAT_PCM, channels,
181 2, nBytesPerSample * nDartSamples, fShare,
182 dart_audio_callback, NULL))
183 return 0;
185 mp_msg(MSGT_AO, MSGL_V, "DART: obtained buffer size = %lu bytes\n",
186 DART.ulBufferSize);
188 m_fQuit = FALSE;
190 ao_data.channels = channels;
191 ao_data.samplerate = rate;
192 ao_data.format = format;
193 ao_data.bps = nBytesPerSample * rate;
194 ao_data.outburst = nBytesPerSample * OUTBURST_SAMPLES;
195 ao_data.buffersize = DART.ulBufferSize;
197 // multiple of CHUNK_SIZE
198 m_nBufSize = ((DART.ulBufferSize << 2) / CHUNK_SIZE) * CHUNK_SIZE;
199 // and one more chunk plus round up
200 m_nBufSize += 2 * CHUNK_SIZE;
202 m_audioBuf = av_fifo_alloc(m_nBufSize);
204 dartPlay();
206 // might cause PM DLLs to be loaded which incorrectly enable SIG_FPE,
207 // which AAC decoding might trigger.
208 // so, mask off all floating-point exceptions.
209 _control87(MCW_EM, MCW_EM);
211 return 1;
214 // close audio device
215 static void uninit(int immed)
217 m_fQuit = TRUE;
219 if (!immed) {
220 while (DART.fPlaying)
221 DosSleep(1);
224 dartClose();
226 av_fifo_free(m_audioBuf);
229 // stop playing and empty buffers (for seeking/pause)
230 static void reset(void)
232 dartPause();
234 // Reset ring-buffer state
235 av_fifo_reset(m_audioBuf);
237 dartResume();
240 // stop playing, keep buffers (for pause)
241 static void audio_pause(void)
243 dartPause();
246 // resume playing, after audio_pause()
247 static void audio_resume(void)
249 dartResume();
252 // return: how many bytes can be played without blocking
253 static int get_space(void)
255 return av_fifo_space(m_audioBuf);
258 // plays 'len' bytes of 'data'
259 // it should round it down to outburst*n
260 // return: number of bytes played
261 static int play(void *data, int len, int flags)
264 if (!(flags & AOPLAY_FINAL_CHUNK))
265 len = (len / ao_data.outburst) * ao_data.outburst;
267 return write_buffer(data, len);
270 // return: delay in seconds between first and last sample in buffer
271 static float get_delay(void)
273 int nBuffered = av_fifo_size(m_audioBuf); // could be less
275 return (float)nBuffered / (float)ao_data.bps;