aluBytesFromFormat returns bytes, not bits
[openal-soft.git] / Alc / dsound.c
blobe7bfb1394aaeaa1e378f2c7a84be16fe9eb2c9cb
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) == 1)
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) == 1)
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) == 1)
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) == 1)
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) == 1)
196 device->Format = AL_FORMAT_71CHN8;
197 else
198 device->Format = AL_FORMAT_71CHN16;
200 device->FrameSize = aluBytesFromFormat(device->Format) *
201 aluChannelsFromFormat(device->Format);
203 OutputType.wFormatTag = WAVE_FORMAT_PCM;
204 OutputType.nChannels = aluChannelsFromFormat(device->Format);
205 OutputType.wBitsPerSample = aluBytesFromFormat(device->Format) * 8;
206 OutputType.nBlockAlign = OutputType.nChannels*OutputType.wBitsPerSample/8;
207 OutputType.nSamplesPerSec = device->Frequency;
208 OutputType.nAvgBytesPerSec = OutputType.nSamplesPerSec*OutputType.nBlockAlign;
209 OutputType.cbSize = 0;
212 if(SUCCEEDED(hr))
214 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
215 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
216 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
217 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
219 if(SUCCEEDED(hr))
220 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType);
222 if(SUCCEEDED(hr))
224 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
225 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
226 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
227 DSBDescription.dwBufferBytes=device->UpdateFreq * device->FrameSize;
228 DSBDescription.lpwfxFormat=&OutputType;
229 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
232 if(SUCCEEDED(hr))
233 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
235 device->ExtraData = pData;
236 pData->thread = StartThread(DSoundProc, device);
237 if(!pData->thread)
238 hr = E_FAIL;
240 if(FAILED(hr))
242 if (pData->DSsbuffer)
243 IDirectSoundBuffer_Release(pData->DSsbuffer);
244 if (pData->DSpbuffer)
245 IDirectSoundBuffer_Release(pData->DSpbuffer);
246 if (pData->lpDS)
247 IDirectSound_Release(pData->lpDS);
249 free(pData);
250 return ALC_FALSE;
253 return ALC_TRUE;
256 static void DSoundClosePlayback(ALCdevice *device)
258 DSoundData *pData = device->ExtraData;
260 pData->killNow = 1;
261 StopThread(pData->thread);
263 IDirectSoundBuffer_Release(pData->DSsbuffer);
264 IDirectSoundBuffer_Release(pData->DSpbuffer);
265 IDirectSound_Release(pData->lpDS);
267 //Deinit COM
268 CoUninitialize();
270 free(pData);
271 device->ExtraData = NULL;
275 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
277 (void)pDevice;
278 (void)deviceName;
279 (void)frequency;
280 (void)format;
281 (void)SampleSize;
282 return ALC_FALSE;
285 static void DSoundCloseCapture(ALCdevice *pDevice)
287 (void)pDevice;
290 static void DSoundStartCapture(ALCdevice *pDevice)
292 (void)pDevice;
295 static void DSoundStopCapture(ALCdevice *pDevice)
297 (void)pDevice;
300 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
302 (void)pDevice;
303 (void)pBuffer;
304 (void)lSamples;
307 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
309 (void)pDevice;
310 return 0;
314 BackendFuncs DSoundFuncs = {
315 DSoundOpenPlayback,
316 DSoundClosePlayback,
317 DSoundOpenCapture,
318 DSoundCloseCapture,
319 DSoundStartCapture,
320 DSoundStopCapture,
321 DSoundCaptureSamples,
322 DSoundAvailableSamples
325 void alcDSoundInit(BackendFuncs *FuncList)
327 *FuncList = DSoundFuncs;
329 DeviceList[0] = AppendDeviceList("DirectSound Software");
330 AppendAllDeviceList(DeviceList[0]);