Mercurial > mplayer.hg
annotate libao2/ao_win32.c @ 12494:0e76a9c38546
credit for Ross Finlayson
author | diego |
---|---|
date | Fri, 21 May 2004 17:48:20 +0000 |
parents | deb95ac21f14 |
children | 47598beff93a |
rev | line source |
---|---|
7913 | 1 /****************************************************************************** |
2 * ao_win32.c: Windows waveOut interface for MPlayer | |
12151
75fdb659f5bf
round len to outburst and increment full_buffers at the correct time, patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
12146
diff
changeset
|
3 * Copyright (c) 2002 - 2004 Sascha Sommer <saschasommer@freenet.de>. |
7913 | 4 * |
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. | |
9 * | |
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. | |
14 * | |
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. | |
18 * | |
19 *****************************************************************************/ | |
20 | |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 #include <windows.h> | |
24 #include <mmsystem.h> | |
25 | |
26 #include "afmt.h" | |
27 #include "audio_out.h" | |
28 #include "audio_out_internal.h" | |
29 #include "../mp_msg.h" | |
30 #include "../libvo/fastmemcpy.h" | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
31 #include "osdep/timer.h" |
7913 | 32 |
12155
deb95ac21f14
restore original ringbuffer constants as the current values are causing heavy stutter here, especially noticable after moving the video window
faust3
parents:
12151
diff
changeset
|
33 #define SAMPLESIZE 1024 |
7913 | 34 #define BUFFER_SIZE 4096 |
12155
deb95ac21f14
restore original ringbuffer constants as the current values are causing heavy stutter here, especially noticable after moving the video window
faust3
parents:
12151
diff
changeset
|
35 #define BUFFER_COUNT 16 |
7913 | 36 |
37 | |
38 static WAVEHDR* waveBlocks; //pointer to our ringbuffer memory | |
39 static HWAVEOUT hWaveOut; //handle to the waveout device | |
40 static unsigned int buf_write=0; | |
41 static unsigned int buf_write_pos=0; | |
42 static int full_buffers=0; | |
43 static int buffered_bytes=0; | |
44 | |
45 | |
46 static ao_info_t info = | |
47 { | |
48 "Windows waveOut audio output", | |
49 "win32", | |
50 "Sascha Sommer <saschasommer@freenet.de>", | |
51 "" | |
52 }; | |
53 | |
54 LIBAO_EXTERN(win32) | |
55 | |
56 static void CALLBACK waveOutProc(HWAVEOUT hWaveOut,UINT uMsg,DWORD dwInstance, | |
57 DWORD dwParam1,DWORD dwParam2) | |
58 { | |
59 if(uMsg != WOM_DONE) | |
60 return; | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
61 if (full_buffers) { |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
62 buffered_bytes-=BUFFER_SIZE; |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
63 --full_buffers; |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
64 } else { |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
65 buffered_bytes=0; |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
66 } |
7913 | 67 } |
68 | |
69 // to set/get/query special features/parameters | |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
9589
diff
changeset
|
70 static int control(int cmd,void *arg) |
7913 | 71 { |
72 DWORD volume; | |
73 switch (cmd) | |
74 { | |
75 case AOCONTROL_GET_VOLUME: | |
76 { | |
77 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
78 waveOutGetVolume(hWaveOut,&volume); | |
79 vol->left = (float)(LOWORD(volume)/655.35); | |
80 vol->right = (float)(HIWORD(volume)/655.35); | |
81 mp_msg(MSGT_AO, MSGL_DBG2,"ao_win32: volume left:%f volume right:%f\n",vol->left,vol->right); | |
82 return CONTROL_OK; | |
83 } | |
84 case AOCONTROL_SET_VOLUME: | |
85 { | |
86 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
87 volume = MAKELONG(vol->left*655.35,vol->right*655.35); | |
88 waveOutSetVolume(hWaveOut,volume); | |
89 return CONTROL_OK; | |
90 } | |
91 } | |
92 return -1; | |
93 } | |
94 | |
95 // open & setup audio device | |
96 // return: 1=success 0=fail | |
97 static int init(int rate,int channels,int format,int flags) | |
98 { | |
99 WAVEFORMATEX wformat; | |
100 DWORD totalBufferSize = (BUFFER_SIZE + sizeof(WAVEHDR)) * BUFFER_COUNT; | |
101 MMRESULT result; | |
102 unsigned char* buffer; | |
103 int i; | |
104 | |
105 //fill global ao_data | |
106 ao_data.channels=channels; | |
107 ao_data.samplerate=rate; | |
108 ao_data.format=format; | |
109 ao_data.bps=channels*rate; | |
110 if(format != AFMT_U8 && format != AFMT_S8) | |
111 ao_data.bps*=2; | |
112 if(ao_data.buffersize==-1) | |
113 { | |
114 ao_data.buffersize=audio_out_format_bits(format)/8; | |
115 ao_data.buffersize*= channels; | |
116 ao_data.buffersize*= SAMPLESIZE; | |
117 } | |
118 mp_msg(MSGT_AO, MSGL_V,"ao_win32: Samplerate:%iHz Channels:%i Format:%s\n",rate, channels, audio_out_format_name(format)); | |
119 mp_msg(MSGT_AO, MSGL_V,"ao_win32: Buffersize:%d\n",ao_data.buffersize); | |
120 | |
121 //fill waveformatex | |
122 ZeroMemory( &wformat, sizeof(WAVEFORMATEX)); | |
123 wformat.cbSize = 0; /* size of _extra_ info */ | |
124 wformat.wFormatTag = WAVE_FORMAT_PCM; | |
125 wformat.nChannels = channels; | |
126 wformat.nSamplesPerSec = rate; | |
127 wformat.wBitsPerSample = audio_out_format_bits(format); | |
128 wformat.nBlockAlign = wformat.nChannels * (wformat.wBitsPerSample >> 3); | |
129 wformat.nAvgBytesPerSec = wformat.nSamplesPerSec * wformat.nBlockAlign; | |
130 | |
131 //open sound device | |
132 //WAVE_MAPPER always points to the default wave device on the system | |
133 result = waveOutOpen(&hWaveOut,WAVE_MAPPER,&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION); | |
134 if(result == WAVERR_BADFORMAT) | |
135 { | |
136 mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: format not supported switching to default\n"); | |
137 ao_data.channels = wformat.nChannels = 2; | |
138 ao_data.samplerate = wformat.nSamplesPerSec = 44100; | |
139 ao_data.format = AFMT_S16_LE; | |
11511 | 140 ao_data.bps=ao_data.channels * ao_data.samplerate*2; |
7913 | 141 ao_data.buffersize=wformat.wBitsPerSample=16; |
142 wformat.nBlockAlign = wformat.nChannels * (wformat.wBitsPerSample >> 3); | |
143 wformat.nAvgBytesPerSec = wformat.nSamplesPerSec * wformat.nBlockAlign; | |
144 ao_data.buffersize/=8; | |
145 ao_data.buffersize*= ao_data.channels; | |
146 ao_data.buffersize*= SAMPLESIZE; | |
147 result = waveOutOpen(&hWaveOut,WAVE_MAPPER,&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION); | |
148 } | |
149 if(result != MMSYSERR_NOERROR) | |
150 { | |
151 mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: unable to open wave mapper device\n"); | |
152 return 0; | |
153 } | |
154 //save volume | |
155 //allocate buffer memory as one big block | |
156 buffer = malloc(totalBufferSize); | |
9589 | 157 memset(buffer,0x0,totalBufferSize); |
7913 | 158 //and setup pointers to each buffer |
159 waveBlocks = (WAVEHDR*)buffer; | |
160 buffer += sizeof(WAVEHDR) * BUFFER_COUNT; | |
161 for(i = 0; i < BUFFER_COUNT; i++) { | |
162 waveBlocks[i].lpData = buffer; | |
163 buffer += BUFFER_SIZE; | |
164 } | |
165 | |
166 return 1; | |
167 } | |
168 | |
169 // close audio device | |
12145 | 170 static void uninit(int immed) |
7913 | 171 { |
12146 | 172 if(!immed)while(buffered_bytes > 0)usec_sleep(50000); |
173 else buffered_bytes=0; | |
7913 | 174 waveOutReset(hWaveOut); |
175 waveOutClose(hWaveOut); | |
176 mp_msg(MSGT_AO, MSGL_V,"waveOut device closed\n"); | |
177 free(waveBlocks); | |
178 mp_msg(MSGT_AO, MSGL_V,"buffer memory freed\n"); | |
179 } | |
180 | |
181 // stop playing and empty buffers (for seeking/pause) | |
182 static void reset() | |
183 { | |
184 waveOutReset(hWaveOut); | |
185 buf_write=0; | |
186 buf_write_pos=0; | |
187 full_buffers=0; | |
188 buffered_bytes=0; | |
189 } | |
190 | |
191 // stop playing, keep buffers (for pause) | |
192 static void audio_pause() | |
193 { | |
194 waveOutPause(hWaveOut); | |
195 } | |
196 | |
197 // resume playing, after audio_pause() | |
198 static void audio_resume() | |
199 { | |
200 waveOutRestart(hWaveOut); | |
201 } | |
202 | |
203 // return: how many bytes can be played without blocking | |
204 static int get_space() | |
205 { | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
206 return BUFFER_COUNT*BUFFER_SIZE - buffered_bytes; |
7913 | 207 } |
208 | |
209 //writes data into buffer, based on ringbuffer code in ao_sdl.c | |
210 static int write_waveOutBuffer(unsigned char* data,int len){ | |
211 WAVEHDR* current; | |
212 int len2=0; | |
213 int x; | |
214 while(len>0){ | |
215 current = &waveBlocks[buf_write]; | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
216 if(buffered_bytes==BUFFER_COUNT*BUFFER_SIZE) break; |
7913 | 217 //unprepare the header if it is prepared |
218 if(current->dwFlags & WHDR_PREPARED) | |
219 waveOutUnprepareHeader(hWaveOut, current, sizeof(WAVEHDR)); | |
220 x=BUFFER_SIZE-buf_write_pos; | |
221 if(x>len) x=len; | |
222 memcpy(current->lpData+buf_write_pos,data+len2,x); | |
12151
75fdb659f5bf
round len to outburst and increment full_buffers at the correct time, patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
12146
diff
changeset
|
223 if(buf_write_pos==0)full_buffers++; |
7913 | 224 len2+=x; len-=x; |
225 buffered_bytes+=x; buf_write_pos+=x; | |
226 //prepare header and write data to device | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
227 current->dwBufferLength = buf_write_pos; |
7913 | 228 waveOutPrepareHeader(hWaveOut, current, sizeof(WAVEHDR)); |
229 waveOutWrite(hWaveOut, current, sizeof(WAVEHDR)); | |
230 | |
231 if(buf_write_pos>=BUFFER_SIZE){ //buffer is full find next | |
232 // block is full, find next! | |
233 buf_write=(buf_write+1)%BUFFER_COUNT; | |
234 buf_write_pos=0; | |
235 } | |
236 } | |
237 return len2; | |
238 } | |
239 | |
240 // plays 'len' bytes of 'data' | |
241 // it should round it down to outburst*n | |
242 // return: number of bytes played | |
243 static int play(void* data,int len,int flags) | |
244 { | |
12151
75fdb659f5bf
round len to outburst and increment full_buffers at the correct time, patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
12146
diff
changeset
|
245 len = (len/ao_data.outburst)*ao_data.outburst; |
7913 | 246 return write_waveOutBuffer(data,len); |
247 } | |
10106 | 248 |
7913 | 249 // return: delay in seconds between first and last sample in buffer |
250 static float get_delay() | |
251 { | |
252 return (float)(buffered_bytes + ao_data.buffersize)/(float)ao_data.bps; | |
253 } |