Use WAVEFORMATEXTENSIBLE for multichannel dsound output, and don't create a primary...
[openal-soft.git] / Alc / dsound.c
blob313284c6a17693db2fc4596285272193daa91c1e
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #define INITGUID
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <memory.h>
28 #include <windows.h>
29 #include <mmsystem.h>
30 #include <mmreg.h>
31 #include <dsound.h>
33 #include "alMain.h"
34 #include "AL/al.h"
35 #include "AL/alc.h"
37 #ifndef DSSPEAKER_7POINT1
38 #define DSSPEAKER_7POINT1 7
39 #endif
41 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
43 typedef struct {
44 // DirectSound Playback Device
45 LPDIRECTSOUND lpDS;
46 LPDIRECTSOUNDBUFFER DSpbuffer;
47 LPDIRECTSOUNDBUFFER DSsbuffer;
49 int killNow;
50 ALvoid *thread;
51 } DSoundData;
54 static ALCchar *DeviceList[16];
57 static ALuint DSoundProc(ALvoid *ptr)
59 ALCdevice *pDevice = (ALCdevice*)ptr;
60 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
61 DWORD LastCursor = 0;
62 DWORD PlayCursor;
63 VOID *WritePtr1, *WritePtr2;
64 DWORD WriteCnt1, WriteCnt2;
65 DWORD BufferSize;
66 DWORD avail;
67 HRESULT err;
69 BufferSize = pDevice->UpdateFreq*pDevice->FrameSize;
71 while(!pData->killNow)
73 // Get current play and write cursors
74 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
75 avail = (PlayCursor-LastCursor+BufferSize) % BufferSize;
77 if(avail == 0)
79 Sleep(1);
80 continue;
83 // Lock output buffer
84 WriteCnt1 = 0;
85 WriteCnt2 = 0;
86 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
88 // If the buffer is lost, restore it, play and lock
89 if(err == DSERR_BUFFERLOST)
91 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
92 if(SUCCEEDED(err))
93 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
94 if(SUCCEEDED(err))
95 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
98 // Successfully locked the output buffer
99 if(SUCCEEDED(err))
101 // If we have an active context, mix data directly into output buffer otherwise fill with silence
102 SuspendContext(NULL);
103 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
104 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
105 ProcessContext(NULL);
107 // Unlock output buffer only when successfully locked
108 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
110 else
111 AL_PRINT("Buffer lock error: %#lx\n", err);
113 // Update old write cursor location
114 LastCursor += WriteCnt1+WriteCnt2;
115 LastCursor %= BufferSize;
118 return 0;
121 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
123 DSBUFFERDESC DSBDescription;
124 DSoundData *pData = NULL;
125 WAVEFORMATEXTENSIBLE OutputType;
126 DWORD speakers;
127 HRESULT hr;
129 if(deviceName)
131 int i;
132 for(i = 0;DeviceList[i];i++)
134 if(strcmp(deviceName, DeviceList[i]) == 0)
136 device->szDeviceName = DeviceList[i];
137 break;
140 if(!DeviceList[i])
141 return ALC_FALSE;
143 else
144 device->szDeviceName = DeviceList[0];
146 memset(&OutputType, 0, sizeof(OutputType));
148 //Initialise requested device
150 pData = calloc(1, sizeof(DSoundData));
151 if(!pData)
153 SetALCError(ALC_OUT_OF_MEMORY);
154 return ALC_FALSE;
157 //Init COM
158 CoInitialize(NULL);
160 //DirectSound Init code
161 hr = CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectSound, (LPVOID*)&pData->lpDS);
162 if(SUCCEEDED(hr))
163 hr = IDirectSound_Initialize(pData->lpDS, NULL);
164 if(SUCCEEDED(hr))
165 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
167 if(SUCCEEDED(hr))
168 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
169 if(SUCCEEDED(hr))
171 speakers = DSSPEAKER_CONFIG(speakers);
172 if(speakers == DSSPEAKER_MONO)
174 if(aluBytesFromFormat(device->Format) == 1)
175 device->Format = AL_FORMAT_MONO8;
176 else
177 device->Format = AL_FORMAT_MONO16;
179 else if(speakers == DSSPEAKER_STEREO)
181 if(aluBytesFromFormat(device->Format) == 1)
182 device->Format = AL_FORMAT_STEREO8;
183 else
184 device->Format = AL_FORMAT_STEREO16;
186 else if(speakers == DSSPEAKER_QUAD)
188 if(aluBytesFromFormat(device->Format) == 1)
189 device->Format = AL_FORMAT_QUAD8;
190 else
191 device->Format = AL_FORMAT_QUAD16;
192 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
193 SPEAKER_FRONT_RIGHT |
194 SPEAKER_BACK_LEFT |
195 SPEAKER_BACK_RIGHT;
197 else if(speakers == DSSPEAKER_5POINT1)
199 if(aluBytesFromFormat(device->Format) == 1)
200 device->Format = AL_FORMAT_51CHN8;
201 else
202 device->Format = AL_FORMAT_51CHN16;
203 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
204 SPEAKER_FRONT_RIGHT |
205 SPEAKER_FRONT_CENTER |
206 SPEAKER_LOW_FREQUENCY |
207 SPEAKER_BACK_LEFT |
208 SPEAKER_BACK_RIGHT;
210 else if(speakers == DSSPEAKER_7POINT1)
212 if(aluBytesFromFormat(device->Format) == 1)
213 device->Format = AL_FORMAT_71CHN8;
214 else
215 device->Format = AL_FORMAT_71CHN16;
216 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
217 SPEAKER_FRONT_RIGHT |
218 SPEAKER_FRONT_CENTER |
219 SPEAKER_LOW_FREQUENCY |
220 SPEAKER_BACK_LEFT |
221 SPEAKER_BACK_RIGHT |
222 SPEAKER_SIDE_LEFT |
223 SPEAKER_SIDE_RIGHT;
225 device->FrameSize = aluBytesFromFormat(device->Format) *
226 aluChannelsFromFormat(device->Format);
228 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
229 OutputType.Format.nChannels = aluChannelsFromFormat(device->Format);
230 OutputType.Format.wBitsPerSample = aluBytesFromFormat(device->Format) * 8;
231 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
232 OutputType.Format.nSamplesPerSec = device->Frequency;
233 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
234 OutputType.Format.cbSize = 0;
237 if(OutputType.Format.nChannels > 2)
239 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
240 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
241 OutputType.Format.cbSize = 22;
242 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
244 else
246 if(SUCCEEDED(hr))
248 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
249 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
250 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
251 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
253 if(SUCCEEDED(hr))
254 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType.Format);
257 if(SUCCEEDED(hr))
259 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
260 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
261 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
262 DSBDescription.dwBufferBytes=device->UpdateFreq * device->FrameSize;
263 DSBDescription.lpwfxFormat=&OutputType.Format;
264 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
267 if(SUCCEEDED(hr))
268 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
270 device->ExtraData = pData;
271 pData->thread = StartThread(DSoundProc, device);
272 if(!pData->thread)
273 hr = E_FAIL;
275 if(FAILED(hr))
277 if (pData->DSsbuffer)
278 IDirectSoundBuffer_Release(pData->DSsbuffer);
279 if (pData->DSpbuffer)
280 IDirectSoundBuffer_Release(pData->DSpbuffer);
281 if (pData->lpDS)
282 IDirectSound_Release(pData->lpDS);
284 free(pData);
285 return ALC_FALSE;
288 return ALC_TRUE;
291 static void DSoundClosePlayback(ALCdevice *device)
293 DSoundData *pData = device->ExtraData;
295 pData->killNow = 1;
296 StopThread(pData->thread);
298 IDirectSoundBuffer_Release(pData->DSsbuffer);
299 if (pData->DSpbuffer)
300 IDirectSoundBuffer_Release(pData->DSpbuffer);
301 IDirectSound_Release(pData->lpDS);
303 //Deinit COM
304 CoUninitialize();
306 free(pData);
307 device->ExtraData = NULL;
311 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
313 (void)pDevice;
314 (void)deviceName;
315 (void)frequency;
316 (void)format;
317 (void)SampleSize;
318 return ALC_FALSE;
321 static void DSoundCloseCapture(ALCdevice *pDevice)
323 (void)pDevice;
326 static void DSoundStartCapture(ALCdevice *pDevice)
328 (void)pDevice;
331 static void DSoundStopCapture(ALCdevice *pDevice)
333 (void)pDevice;
336 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
338 (void)pDevice;
339 (void)pBuffer;
340 (void)lSamples;
343 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
345 (void)pDevice;
346 return 0;
350 BackendFuncs DSoundFuncs = {
351 DSoundOpenPlayback,
352 DSoundClosePlayback,
353 DSoundOpenCapture,
354 DSoundCloseCapture,
355 DSoundStartCapture,
356 DSoundStopCapture,
357 DSoundCaptureSamples,
358 DSoundAvailableSamples
361 void alcDSoundInit(BackendFuncs *FuncList)
363 *FuncList = DSoundFuncs;
365 DeviceList[0] = AppendDeviceList("DirectSound Software");
366 AppendAllDeviceList(DeviceList[0]);