Remove unnecessary Channels field
[openal-soft.git] / Alc / dsound.c
bloba05c4034bf0d19d2f700a4612eb5aeb651de4281
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 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
27 #include <windows.h>
28 #include <mmsystem.h>
29 #include <dsound.h>
31 #include "alMain.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
35 #ifndef DSSPEAKER_7POINT1
36 #define DSSPEAKER_7POINT1 7
37 #endif
39 typedef struct {
40 // DirectSound Playback Device
41 LPDIRECTSOUND lpDS;
42 LPDIRECTSOUNDBUFFER DSpbuffer;
43 LPDIRECTSOUNDBUFFER DSsbuffer;
45 int killNow;
46 ALvoid *thread;
47 } DSoundData;
50 static ALCchar *DeviceList[16];
53 static ALuint DSoundProc(ALvoid *ptr)
55 ALCdevice *pDevice = (ALCdevice*)ptr;
56 DSoundData *pData = (DSoundData*)pDevice->ExtraData;
57 DWORD LastCursor = 0;
58 DWORD PlayCursor;
59 VOID *WritePtr1, *WritePtr2;
60 DWORD WriteCnt1, WriteCnt2;
61 DWORD avail;
62 HRESULT err;
64 while(!pData->killNow)
66 // Get current play and write cursors
67 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer, &PlayCursor, NULL);
68 avail = (PlayCursor-LastCursor) % (pDevice->UpdateFreq*pDevice->FrameSize);
70 if(avail == 0)
72 Sleep(1);
73 continue;
76 // Lock output buffer
77 WriteCnt1 = 0;
78 WriteCnt2 = 0;
79 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
81 // If the buffer is lost, restore it, play and lock
82 if(err == DSERR_BUFFERLOST)
84 err = IDirectSoundBuffer_Restore(pData->DSsbuffer);
85 if(SUCCEEDED(err))
86 err = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
87 if(SUCCEEDED(err))
88 err = IDirectSoundBuffer_Lock(pData->DSsbuffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
91 // Successfully locked the output buffer
92 if(SUCCEEDED(err))
94 // If we have an active context, mix data directly into output buffer otherwise fill with silence
95 SuspendContext(NULL);
96 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
97 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
98 ProcessContext(NULL);
100 // Unlock output buffer only when successfully locked
101 IDirectSoundBuffer_Unlock(pData->DSsbuffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
103 else
104 AL_PRINT("Buffer lock error: %#lx\n", err);
106 // Update old write cursor location
107 LastCursor += WriteCnt1+WriteCnt2;
108 LastCursor %= pDevice->UpdateFreq*pDevice->FrameSize;
111 return 0;
114 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
116 DSBUFFERDESC DSBDescription;
117 DSoundData *pData = NULL;
118 WAVEFORMATEX OutputType;
119 DWORD speakers;
120 HRESULT hr;
122 if(deviceName)
124 int i;
125 for(i = 0;DeviceList[i];i++)
127 if(strcmp(deviceName, DeviceList[i]) == 0)
129 device->szDeviceName = DeviceList[i];
130 break;
133 if(!DeviceList[i])
134 return ALC_FALSE;
136 else
137 device->szDeviceName = DeviceList[0];
139 memset(&OutputType, 0, sizeof(WAVEFORMATEX));
141 //Initialise requested device
143 pData = calloc(1, sizeof(DSoundData));
144 if(!pData)
146 SetALCError(ALC_OUT_OF_MEMORY);
147 return ALC_FALSE;
150 //Init COM
151 CoInitialize(NULL);
153 //DirectSound Init code
154 hr = CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectSound, (LPVOID*)&pData->lpDS);
155 if(SUCCEEDED(hr))
156 hr = IDirectSound_Initialize(pData->lpDS, NULL);
157 if(SUCCEEDED(hr))
158 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
160 if(SUCCEEDED(hr))
161 hr = IDirectSound_GetSpeakerConfig(pData->lpDS, &speakers);
162 if(SUCCEEDED(hr))
164 speakers = DSSPEAKER_CONFIG(speakers);
165 if(speakers == DSSPEAKER_MONO)
167 if(aluBytesFromFormat(device->Format) == 8)
168 device->Format = AL_FORMAT_MONO8;
169 else
170 device->Format = AL_FORMAT_MONO16;
172 else if(speakers == DSSPEAKER_STEREO)
174 if(aluBytesFromFormat(device->Format) == 8)
175 device->Format = AL_FORMAT_STEREO8;
176 else
177 device->Format = AL_FORMAT_STEREO16;
179 else if(speakers == DSSPEAKER_QUAD)
181 if(aluBytesFromFormat(device->Format) == 8)
182 device->Format = AL_FORMAT_QUAD8;
183 else
184 device->Format = AL_FORMAT_QUAD16;
186 else if(speakers == DSSPEAKER_5POINT1)
188 if(aluBytesFromFormat(device->Format) == 8)
189 device->Format = AL_FORMAT_51CHN8;
190 else
191 device->Format = AL_FORMAT_51CHN16;
193 else if(speakers == DSSPEAKER_7POINT1)
195 if(aluBytesFromFormat(device->Format) == 8)
196 device->Format = AL_FORMAT_71CHN8;
197 else
198 device->Format = AL_FORMAT_71CHN16;
201 OutputType.wFormatTag = WAVE_FORMAT_PCM;
202 OutputType.nChannels = aluChannelsFromFormat(device->Format);
203 OutputType.wBitsPerSample = aluBytesFromFormat(device->Format) * 8;
204 OutputType.nBlockAlign = OutputType.nChannels*OutputType.wBitsPerSample/8;
205 OutputType.nSamplesPerSec = device->Frequency;
206 OutputType.nAvgBytesPerSec = OutputType.nSamplesPerSec*OutputType.nBlockAlign;
207 OutputType.cbSize = 0;
210 if(SUCCEEDED(hr))
212 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
213 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
214 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
215 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
217 if(SUCCEEDED(hr))
218 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType);
220 if(SUCCEEDED(hr))
222 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
223 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
224 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
225 DSBDescription.dwBufferBytes=device->UpdateFreq * device->FrameSize;
226 DSBDescription.lpwfxFormat=&OutputType;
227 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
230 if(SUCCEEDED(hr))
231 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
233 device->ExtraData = pData;
234 pData->thread = StartThread(DSoundProc, device);
235 if(!pData->thread)
236 hr = E_FAIL;
238 if(FAILED(hr))
240 if (pData->DSsbuffer)
241 IDirectSoundBuffer_Release(pData->DSsbuffer);
242 if (pData->DSpbuffer)
243 IDirectSoundBuffer_Release(pData->DSpbuffer);
244 if (pData->lpDS)
245 IDirectSound_Release(pData->lpDS);
247 free(pData);
248 return ALC_FALSE;
251 return ALC_TRUE;
254 static void DSoundClosePlayback(ALCdevice *device)
256 DSoundData *pData = device->ExtraData;
258 pData->killNow = 1;
259 StopThread(pData->thread);
261 IDirectSoundBuffer_Release(pData->DSsbuffer);
262 IDirectSoundBuffer_Release(pData->DSpbuffer);
263 IDirectSound_Release(pData->lpDS);
265 //Deinit COM
266 CoUninitialize();
268 free(pData);
269 device->ExtraData = NULL;
273 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
275 (void)pDevice;
276 (void)deviceName;
277 (void)frequency;
278 (void)format;
279 (void)SampleSize;
280 return ALC_FALSE;
283 static void DSoundCloseCapture(ALCdevice *pDevice)
285 (void)pDevice;
288 static void DSoundStartCapture(ALCdevice *pDevice)
290 (void)pDevice;
293 static void DSoundStopCapture(ALCdevice *pDevice)
295 (void)pDevice;
298 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
300 (void)pDevice;
301 (void)pBuffer;
302 (void)lSamples;
305 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
307 (void)pDevice;
308 return 0;
312 BackendFuncs DSoundFuncs = {
313 DSoundOpenPlayback,
314 DSoundClosePlayback,
315 DSoundOpenCapture,
316 DSoundCloseCapture,
317 DSoundStartCapture,
318 DSoundStopCapture,
319 DSoundCaptureSamples,
320 DSoundAvailableSamples
323 void alcDSoundInit(BackendFuncs *FuncList)
325 *FuncList = DSoundFuncs;
327 DeviceList[0] = AppendDeviceList("DirectSound Software");
328 AppendAllDeviceList(DeviceList[0]);