natsuki.mplayerhq.hu -> rsync.mplayerhq.hu
[mplayer/glamo.git] / libao2 / ao_win32.c
bloba48419f288cb503550afc2f106f52e7270eb6a8a
1 /******************************************************************************
2 * ao_win32.c: Windows waveOut interface for MPlayer
3 * Copyright (c) 2002 - 2004 Sascha Sommer <saschasommer@freenet.de>.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 *****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <windows.h>
24 #include <mmsystem.h>
26 #include "config.h"
27 #include "libaf/af_format.h"
28 #include "audio_out.h"
29 #include "audio_out_internal.h"
30 #include "mp_msg.h"
31 #include "libvo/fastmemcpy.h"
32 #include "osdep/timer.h"
34 #define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
35 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
37 static const GUID KSDATAFORMAT_SUBTYPE_PCM = {
38 0x1,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}
41 typedef struct {
42 WAVEFORMATEX Format;
43 union {
44 WORD wValidBitsPerSample;
45 WORD wSamplesPerBlock;
46 WORD wReserved;
47 } Samples;
48 DWORD dwChannelMask;
49 GUID SubFormat;
50 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
52 #define SPEAKER_FRONT_LEFT 0x1
53 #define SPEAKER_FRONT_RIGHT 0x2
54 #define SPEAKER_FRONT_CENTER 0x4
55 #define SPEAKER_LOW_FREQUENCY 0x8
56 #define SPEAKER_BACK_LEFT 0x10
57 #define SPEAKER_BACK_RIGHT 0x20
58 #define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
59 #define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
60 #define SPEAKER_BACK_CENTER 0x100
61 #define SPEAKER_SIDE_LEFT 0x200
62 #define SPEAKER_SIDE_RIGHT 0x400
63 #define SPEAKER_TOP_CENTER 0x800
64 #define SPEAKER_TOP_FRONT_LEFT 0x1000
65 #define SPEAKER_TOP_FRONT_CENTER 0x2000
66 #define SPEAKER_TOP_FRONT_RIGHT 0x4000
67 #define SPEAKER_TOP_BACK_LEFT 0x8000
68 #define SPEAKER_TOP_BACK_CENTER 0x10000
69 #define SPEAKER_TOP_BACK_RIGHT 0x20000
71 static const int channel_mask[] = {
72 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY,
73 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY,
74 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_CENTER | SPEAKER_LOW_FREQUENCY,
75 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY
80 #define SAMPLESIZE 1024
81 #define BUFFER_SIZE 4096
82 #define BUFFER_COUNT 16
85 static WAVEHDR* waveBlocks; //pointer to our ringbuffer memory
86 static HWAVEOUT hWaveOut; //handle to the waveout device
87 static unsigned int buf_write=0;
88 static unsigned int buf_write_pos=0;
89 static int full_buffers=0;
90 static int buffered_bytes=0;
93 static ao_info_t info =
95 "Windows waveOut audio output",
96 "win32",
97 "Sascha Sommer <saschasommer@freenet.de>",
101 LIBAO_EXTERN(win32)
103 static void CALLBACK waveOutProc(HWAVEOUT hWaveOut,UINT uMsg,DWORD dwInstance,
104 DWORD dwParam1,DWORD dwParam2)
106 if(uMsg != WOM_DONE)
107 return;
108 if (full_buffers) {
109 buffered_bytes-=BUFFER_SIZE;
110 --full_buffers;
111 } else {
112 buffered_bytes=0;
116 // to set/get/query special features/parameters
117 static int control(int cmd,void *arg)
119 DWORD volume;
120 switch (cmd)
122 case AOCONTROL_GET_VOLUME:
124 ao_control_vol_t* vol = (ao_control_vol_t*)arg;
125 waveOutGetVolume(hWaveOut,&volume);
126 vol->left = (float)(LOWORD(volume)/655.35);
127 vol->right = (float)(HIWORD(volume)/655.35);
128 mp_msg(MSGT_AO, MSGL_DBG2,"ao_win32: volume left:%f volume right:%f\n",vol->left,vol->right);
129 return CONTROL_OK;
131 case AOCONTROL_SET_VOLUME:
133 ao_control_vol_t* vol = (ao_control_vol_t*)arg;
134 volume = MAKELONG(vol->left*655.35,vol->right*655.35);
135 waveOutSetVolume(hWaveOut,volume);
136 return CONTROL_OK;
139 return -1;
142 // open & setup audio device
143 // return: 1=success 0=fail
144 static int init(int rate,int channels,int format,int flags)
146 WAVEFORMATEXTENSIBLE wformat;
147 DWORD totalBufferSize = (BUFFER_SIZE + sizeof(WAVEHDR)) * BUFFER_COUNT;
148 MMRESULT result;
149 unsigned char* buffer;
150 int i;
152 switch(format){
153 case AF_FORMAT_AC3:
154 case AF_FORMAT_S24_LE:
155 case AF_FORMAT_S16_LE:
156 case AF_FORMAT_S8:
157 break;
158 default:
159 mp_msg(MSGT_AO, MSGL_V,"ao_win32: format %s not supported defaulting to Signed 16-bit Little-Endian\n",af_fmt2str_short(format));
160 format=AF_FORMAT_S16_LE;
162 //fill global ao_data
163 ao_data.channels=channels;
164 ao_data.samplerate=rate;
165 ao_data.format=format;
166 ao_data.bps=channels*rate;
167 if(format != AF_FORMAT_U8 && format != AF_FORMAT_S8)
168 ao_data.bps*=2;
169 if(ao_data.buffersize==-1)
171 ao_data.buffersize=af_fmt2bits(format)/8;
172 ao_data.buffersize*= channels;
173 ao_data.buffersize*= SAMPLESIZE;
175 mp_msg(MSGT_AO, MSGL_V,"ao_win32: Samplerate:%iHz Channels:%i Format:%s\n",rate, channels, af_fmt2str_short(format));
176 mp_msg(MSGT_AO, MSGL_V,"ao_win32: Buffersize:%d\n",ao_data.buffersize);
178 //fill waveformatex
179 ZeroMemory( &wformat, sizeof(WAVEFORMATEXTENSIBLE));
180 wformat.Format.cbSize = (channels>2)?sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX):0;
181 wformat.Format.nChannels = channels;
182 wformat.Format.nSamplesPerSec = rate;
183 if(format == AF_FORMAT_AC3)
185 wformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
186 wformat.Format.wBitsPerSample = 16;
187 wformat.Format.nBlockAlign = 4;
189 else
191 wformat.Format.wFormatTag = (channels>2)?WAVE_FORMAT_EXTENSIBLE:WAVE_FORMAT_PCM;
192 wformat.Format.wBitsPerSample = af_fmt2bits(format);
193 wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3);
195 if(channels>2)
197 wformat.dwChannelMask = channel_mask[channels-3];
198 wformat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
199 wformat.Samples.wValidBitsPerSample=af_fmt2bits(format);
202 wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign;
204 //open sound device
205 //WAVE_MAPPER always points to the default wave device on the system
206 result = waveOutOpen(&hWaveOut,WAVE_MAPPER,(WAVEFORMATEX*)&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION);
207 if(result == WAVERR_BADFORMAT)
209 mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: format not supported switching to default\n");
210 ao_data.channels = wformat.Format.nChannels = 2;
211 ao_data.samplerate = wformat.Format.nSamplesPerSec = 44100;
212 ao_data.format = AF_FORMAT_S16_LE;
213 ao_data.bps=ao_data.channels * ao_data.samplerate*2;
214 wformat.Format.wBitsPerSample=16;
215 wformat.Format.wFormatTag=WAVE_FORMAT_PCM;
216 wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3);
217 wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign;
218 ao_data.buffersize=(wformat.Format.wBitsPerSample>>3)*wformat.Format.nChannels*SAMPLESIZE;
219 result = waveOutOpen(&hWaveOut,WAVE_MAPPER,(WAVEFORMATEX*)&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION);
221 if(result != MMSYSERR_NOERROR)
223 mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: unable to open wave mapper device (result=%i)\n",result);
224 return 0;
226 //allocate buffer memory as one big block
227 buffer = malloc(totalBufferSize);
228 memset(buffer,0x0,totalBufferSize);
229 //and setup pointers to each buffer
230 waveBlocks = (WAVEHDR*)buffer;
231 buffer += sizeof(WAVEHDR) * BUFFER_COUNT;
232 for(i = 0; i < BUFFER_COUNT; i++) {
233 waveBlocks[i].lpData = buffer;
234 buffer += BUFFER_SIZE;
236 buf_write=0;
237 buf_write_pos=0;
238 full_buffers=0;
239 buffered_bytes=0;
241 return 1;
244 // close audio device
245 static void uninit(int immed)
247 if(!immed)while(buffered_bytes > 0)usec_sleep(50000);
248 else buffered_bytes=0;
249 waveOutReset(hWaveOut);
250 waveOutClose(hWaveOut);
251 mp_msg(MSGT_AO, MSGL_V,"waveOut device closed\n");
252 free(waveBlocks);
253 mp_msg(MSGT_AO, MSGL_V,"buffer memory freed\n");
256 // stop playing and empty buffers (for seeking/pause)
257 static void reset()
259 waveOutReset(hWaveOut);
260 buf_write=0;
261 buf_write_pos=0;
262 full_buffers=0;
263 buffered_bytes=0;
266 // stop playing, keep buffers (for pause)
267 static void audio_pause()
269 waveOutPause(hWaveOut);
272 // resume playing, after audio_pause()
273 static void audio_resume()
275 waveOutRestart(hWaveOut);
278 // return: how many bytes can be played without blocking
279 static int get_space()
281 return BUFFER_COUNT*BUFFER_SIZE - buffered_bytes;
284 //writes data into buffer, based on ringbuffer code in ao_sdl.c
285 static int write_waveOutBuffer(unsigned char* data,int len){
286 WAVEHDR* current;
287 int len2=0;
288 int x;
289 while(len>0){
290 current = &waveBlocks[buf_write];
291 if(buffered_bytes==BUFFER_COUNT*BUFFER_SIZE) break;
292 //unprepare the header if it is prepared
293 if(current->dwFlags & WHDR_PREPARED)
294 waveOutUnprepareHeader(hWaveOut, current, sizeof(WAVEHDR));
295 x=BUFFER_SIZE-buf_write_pos;
296 if(x>len) x=len;
297 memcpy(current->lpData+buf_write_pos,data+len2,x);
298 if(buf_write_pos==0)full_buffers++;
299 len2+=x; len-=x;
300 buffered_bytes+=x; buf_write_pos+=x;
301 //prepare header and write data to device
302 current->dwBufferLength = buf_write_pos;
303 waveOutPrepareHeader(hWaveOut, current, sizeof(WAVEHDR));
304 waveOutWrite(hWaveOut, current, sizeof(WAVEHDR));
306 if(buf_write_pos>=BUFFER_SIZE){ //buffer is full find next
307 // block is full, find next!
308 buf_write=(buf_write+1)%BUFFER_COUNT;
309 buf_write_pos=0;
312 return len2;
315 // plays 'len' bytes of 'data'
316 // it should round it down to outburst*n
317 // return: number of bytes played
318 static int play(void* data,int len,int flags)
320 len = (len/ao_data.outburst)*ao_data.outburst;
321 return write_waveOutBuffer(data,len);
324 // return: delay in seconds between first and last sample in buffer
325 static float get_delay()
327 return (float)(buffered_bytes + ao_data.buffersize)/(float)ao_data.bps;