Add new output formats to sample config
[openal-soft.git] / Alc / dsound.c
blob90a422a22cfa62f13728143d5c950a89acaf5827
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 <stdlib.h>
22 #include <stdio.h>
23 #include <memory.h>
25 #include <windows.h>
26 #include <mmsystem.h>
27 #include <dsound.h>
29 #include "alMain.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
34 typedef struct {
35 // DirectSound Playback Device
36 LPDIRECTSOUND lpDS;
37 LPDIRECTSOUNDBUFFER DSpbuffer;
38 LPDIRECTSOUNDBUFFER DSsbuffer;
39 MMRESULT ulDSTimerID;
40 DWORD OldWriteCursor;
41 } DSoundData;
44 static ALCchar *DeviceList[16];
46 static void CALLBACK DirectSoundProc(UINT uID,UINT uReserved,DWORD_PTR dwUser,DWORD_PTR dwReserved1,DWORD_PTR dwReserved2)
48 ALCdevice *pDevice = (ALCdevice *)dwUser;
49 DSoundData *pData = pDevice->ExtraData;
50 DWORD PlayCursor,WriteCursor;
51 BYTE *WritePtr1,*WritePtr2;
52 DWORD WriteCnt1,WriteCnt2;
53 WAVEFORMATEX OutputType;
54 DWORD BytesPlayed;
55 DWORD BufSize;
56 HRESULT DSRes;
58 (void)uID;
59 (void)uReserved;
60 (void)dwReserved1;
61 (void)dwReserved2;
63 BufSize = pDevice->UpdateFreq * pDevice->FrameSize;
65 // Get current play and write cursors
66 IDirectSoundBuffer_GetCurrentPosition(pData->DSsbuffer,&PlayCursor,&WriteCursor);
67 if (!pData->OldWriteCursor) pData->OldWriteCursor=WriteCursor-PlayCursor;
69 // Get the output format and figure the number of bytes played (block aligned)
70 IDirectSoundBuffer_GetFormat(pData->DSsbuffer,&OutputType,sizeof(WAVEFORMATEX),NULL);
71 BytesPlayed=((((WriteCursor<pData->OldWriteCursor)?(BufSize+WriteCursor-pData->OldWriteCursor):(WriteCursor-pData->OldWriteCursor))/OutputType.nBlockAlign)*OutputType.nBlockAlign);
73 // Lock output buffer started at 40msec in front of the old write cursor (15msec in front of the actual write cursor)
74 DSRes=IDirectSoundBuffer_Lock(pData->DSsbuffer,(pData->OldWriteCursor+(OutputType.nSamplesPerSec/25)*OutputType.nBlockAlign)%BufSize,BytesPlayed,(LPVOID*)&WritePtr1,&WriteCnt1,(LPVOID*)&WritePtr2,&WriteCnt2,0);
76 // If the buffer is lost, restore it, play and lock
77 if (DSRes==DSERR_BUFFERLOST)
79 IDirectSoundBuffer_Restore(pData->DSsbuffer);
80 IDirectSoundBuffer_Play(pData->DSsbuffer,0,0,DSBPLAY_LOOPING);
81 DSRes=IDirectSoundBuffer_Lock(pData->DSsbuffer,(pData->OldWriteCursor+(OutputType.nSamplesPerSec/25)*OutputType.nBlockAlign)%BufSize,BytesPlayed,(LPVOID*)&WritePtr1,&WriteCnt1,(LPVOID*)&WritePtr2,&WriteCnt2,0);
84 // Successfully locked the output buffer
85 if (DSRes==DS_OK)
87 // If we have an active context, mix data directly into output buffer otherwise fill with silence
88 SuspendContext(NULL);
89 if (WritePtr1)
90 aluMixData(pDevice->Context, WritePtr1, WriteCnt1, pDevice->Format);
91 if (WritePtr2)
92 aluMixData(pDevice->Context, WritePtr2, WriteCnt2, pDevice->Format);
93 ProcessContext(NULL);
95 // Unlock output buffer only when successfully locked
96 IDirectSoundBuffer_Unlock(pData->DSsbuffer,WritePtr1,WriteCnt1,WritePtr2,WriteCnt2);
99 // Update old write cursor location
100 pData->OldWriteCursor=((pData->OldWriteCursor+BytesPlayed)%BufSize);
104 static ALCboolean DSoundOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
106 DSBUFFERDESC DSBDescription;
107 DSoundData *pData = NULL;
108 WAVEFORMATEX OutputType;
109 HRESULT hr;
111 if(deviceName)
113 int i;
114 for(i = 0;DeviceList[i];i++)
116 if(strcmp(deviceName, DeviceList[i]) == 0)
118 device->szDeviceName = DeviceList[i];
119 break;
122 if(!DeviceList[i])
123 return ALC_FALSE;
125 else
126 device->szDeviceName = DeviceList[0];
128 //Platform specific
129 memset(&OutputType, 0, sizeof(WAVEFORMATEX));
130 OutputType.wFormatTag = WAVE_FORMAT_PCM;
131 OutputType.nChannels = device->Channels;
132 OutputType.wBitsPerSample = aluBytesFromFormat(device->Format) * 8;
133 OutputType.nBlockAlign = OutputType.nChannels*OutputType.wBitsPerSample/8;
134 OutputType.nSamplesPerSec = device->Frequency;
135 OutputType.nAvgBytesPerSec = OutputType.nSamplesPerSec*OutputType.nBlockAlign;
136 OutputType.cbSize = 0;
138 //Initialise requested device
140 pData = calloc(1, sizeof(DSoundData));
141 if(!pData)
143 SetALCError(ALC_OUT_OF_MEMORY);
144 return ALC_FALSE;
147 //Init COM
148 CoInitialize(NULL);
150 //DirectSound Init code
151 hr = CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectSound, (LPVOID*)&pData->lpDS);
152 if(SUCCEEDED(hr))
153 hr = IDirectSound_Initialize(pData->lpDS, NULL);
154 if(SUCCEEDED(hr))
155 hr = IDirectSound_SetCooperativeLevel(pData->lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
157 if(SUCCEEDED(hr))
159 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
160 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
161 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
162 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSpbuffer, NULL);
164 if(SUCCEEDED(hr))
165 hr = IDirectSoundBuffer_SetFormat(pData->DSpbuffer,&OutputType);
167 if(SUCCEEDED(hr))
169 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
170 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
171 DSBDescription.dwFlags=DSBCAPS_GLOBALFOCUS|DSBCAPS_GETCURRENTPOSITION2;
172 DSBDescription.dwBufferBytes=device->UpdateFreq * device->FrameSize;
173 DSBDescription.lpwfxFormat=&OutputType;
174 hr = IDirectSound_CreateSoundBuffer(pData->lpDS, &DSBDescription, &pData->DSsbuffer, NULL);
177 if(SUCCEEDED(hr))
178 hr = IDirectSoundBuffer_Play(pData->DSsbuffer, 0, 0, DSBPLAY_LOOPING);
180 if(FAILED(hr))
182 if (pData->DSsbuffer)
183 IDirectSoundBuffer_Release(pData->DSsbuffer);
184 if (pData->DSpbuffer)
185 IDirectSoundBuffer_Release(pData->DSpbuffer);
186 if (pData->lpDS)
187 IDirectSound_Release(pData->lpDS);
189 free(pData);
190 return ALC_FALSE;
193 pData->ulDSTimerID = timeSetEvent(25, 0, (LPTIMECALLBACK)DirectSoundProc, (DWORD)device, (UINT)TIME_CALLBACK_FUNCTION|TIME_PERIODIC);
194 device->MaxNoOfSources = 256;
196 device->ExtraData = pData;
197 return ALC_TRUE;
200 static void DSoundClosePlayback(ALCdevice *device)
202 DSoundData *pData = device->ExtraData;
204 // Stop and release the DS timer
205 if (pData->ulDSTimerID)
206 timeKillEvent(pData->ulDSTimerID);
208 // Wait ... just in case any timer events happen
209 Sleep(100);
211 SuspendContext(NULL);
213 if (pData->DSsbuffer)
214 IDirectSoundBuffer_Release(pData->DSsbuffer);
215 if (pData->DSpbuffer)
216 IDirectSoundBuffer_Release(pData->DSpbuffer);
217 IDirectSound_Release(pData->lpDS);
219 //Deinit COM
220 CoUninitialize();
222 ProcessContext(NULL);
224 free(pData);
225 device->ExtraData = NULL;
229 static ALCboolean DSoundOpenCapture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
231 (void)pDevice;
232 (void)deviceName;
233 (void)frequency;
234 (void)format;
235 (void)SampleSize;
236 return ALC_FALSE;
239 static void DSoundCloseCapture(ALCdevice *pDevice)
241 (void)pDevice;
244 static void DSoundStartCapture(ALCdevice *pDevice)
246 (void)pDevice;
249 static void DSoundStopCapture(ALCdevice *pDevice)
251 (void)pDevice;
254 static void DSoundCaptureSamples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
256 (void)pDevice;
257 (void)pBuffer;
258 (void)lSamples;
261 static ALCuint DSoundAvailableSamples(ALCdevice *pDevice)
263 (void)pDevice;
264 return 0;
268 BackendFuncs DSoundFuncs = {
269 DSoundOpenPlayback,
270 DSoundClosePlayback,
271 DSoundOpenCapture,
272 DSoundCloseCapture,
273 DSoundStartCapture,
274 DSoundStopCapture,
275 DSoundCaptureSamples,
276 DSoundAvailableSamples
279 void alcDSoundInit(BackendFuncs *FuncList)
281 *FuncList = DSoundFuncs;
283 DeviceList[0] = AppendDeviceList("DirectSound Software");
284 AppendAllDeviceList(DeviceList[0]);