1 | /* $Id: OS2SNDBUFFER.CPP,v 1.4 1999-10-23 23:00:49 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * DirectSound SoundBuffer class
|
---|
5 | *
|
---|
6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
9 | *
|
---|
10 | */
|
---|
11 |
|
---|
12 |
|
---|
13 | /*@Header***********************************************************************
|
---|
14 | * Header Files *
|
---|
15 | *******************************************************************************/
|
---|
16 | #include <os2win.h>
|
---|
17 | #include <stdlib.h>
|
---|
18 | #include <string.h>
|
---|
19 |
|
---|
20 | #define INITGUID
|
---|
21 | #include <dsound.h>
|
---|
22 |
|
---|
23 | #include "os2dsound.h"
|
---|
24 | #include "os2sndbuffer.h"
|
---|
25 | #include "misc.h"
|
---|
26 |
|
---|
27 |
|
---|
28 | //******************************************************************************
|
---|
29 | //******************************************************************************
|
---|
30 | OS2IDirectSoundBuffer::OS2IDirectSoundBuffer(const DSBUFFERDESC *lpDSBufferDesc) //KSO Apr 13 1999: const, SDK changes
|
---|
31 | : Referenced(0), lastError(DS_OK)
|
---|
32 | {
|
---|
33 | lpVtbl = &Vtbl;
|
---|
34 | Vtbl.AddRef = SoundBufAddRef;
|
---|
35 | Vtbl.Release = SoundBufRelease;
|
---|
36 | Vtbl.QueryInterface = SoundBufQueryInterface;
|
---|
37 | Vtbl.GetCaps = SoundBufGetCaps;
|
---|
38 | Vtbl.GetFormat = SoundBufGetFormat;
|
---|
39 | Vtbl.GetVolume = SoundBufGetVolume;
|
---|
40 | Vtbl.GetStatus = SoundBufGetStatus;
|
---|
41 | Vtbl.GetCurrentPosition = SoundBufGetCurrentPosition;
|
---|
42 | Vtbl.GetPan = SoundBufGetPan;
|
---|
43 | Vtbl.Initialize = SoundBufInitialize;
|
---|
44 | Vtbl.Restore = SoundBufRestore;
|
---|
45 | Vtbl.SetFormat = SoundBufSetFormat;
|
---|
46 | Vtbl.SetVolume = SoundBufSetVolume;
|
---|
47 | Vtbl.SetCurrentPosition = SoundBufSetCurrentPosition;
|
---|
48 | Vtbl.SetPan = SoundBufSetPan;
|
---|
49 | Vtbl.Lock = SoundBufLock;
|
---|
50 | Vtbl.Unlock = SoundBufUnlock;
|
---|
51 | Vtbl.Stop = SoundBufStop;
|
---|
52 | Vtbl.Play = SoundBufPlay;
|
---|
53 |
|
---|
54 | pan = 0;
|
---|
55 | writepos = 0;
|
---|
56 | playpos = 0;
|
---|
57 | frequency = 22050;
|
---|
58 | status = 0;;
|
---|
59 | fLocked = FALSE;
|
---|
60 | volume = 0;
|
---|
61 | lpfxFormat= NULL;
|
---|
62 | lpBuffer = NULL;
|
---|
63 |
|
---|
64 | memcpy(&bufferdesc, lpDSBufferDesc, sizeof(DSBUFFERDESC));
|
---|
65 | if(!(bufferdesc.dwFlags & DSBCAPS_PRIMARYBUFFER)) {
|
---|
66 | if(bufferdesc.lpwfxFormat == NULL || bufferdesc.dwBufferBytes == 0) {
|
---|
67 | dprintf(("bufferdesc not valid!"));
|
---|
68 | lastError = DSERR_INVALIDPARAM;
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | lpfxFormat = (WAVEFORMATEX *)malloc(bufferdesc.lpwfxFormat->cbSize + sizeof(WAVEFORMATEX));
|
---|
72 | memcpy(lpfxFormat, bufferdesc.lpwfxFormat, bufferdesc.lpwfxFormat->cbSize + sizeof(WAVEFORMATEX));
|
---|
73 |
|
---|
74 | lpBuffer = (LPSTR)VirtualAlloc(0, bufferdesc.dwBufferBytes, MEM_COMMIT, PAGE_READWRITE);
|
---|
75 | if(lpBuffer == NULL) {
|
---|
76 | dprintf(("VirtualAlloc failed"));
|
---|
77 | lpBuffer = NULL;
|
---|
78 | lastError = DSERR_OUTOFMEMORY;
|
---|
79 | return;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | else {//todo: primary buffer
|
---|
83 | }
|
---|
84 | }
|
---|
85 | //******************************************************************************
|
---|
86 | //******************************************************************************
|
---|
87 | OS2IDirectSoundBuffer::~OS2IDirectSoundBuffer()
|
---|
88 | {
|
---|
89 | if(lpBuffer)
|
---|
90 | VirtualFree(lpBuffer, 0, MEM_RELEASE);
|
---|
91 | if(lpfxFormat)
|
---|
92 | free(lpfxFormat);
|
---|
93 |
|
---|
94 | }
|
---|
95 | //******************************************************************************
|
---|
96 | //******************************************************************************
|
---|
97 | HRESULT WIN32API SoundBufQueryInterface(THIS, REFIID riid, LPVOID * ppvObj)
|
---|
98 | {
|
---|
99 | dprintf(("OS2IDirectSoundBuffer::QueryInterface\n"));
|
---|
100 | if(This == NULL) {
|
---|
101 | return DSERR_INVALIDPARAM;
|
---|
102 | }
|
---|
103 | *ppvObj = NULL;
|
---|
104 |
|
---|
105 | if(!IsEqualGUID(riid, IID_IDirectSoundBuffer))
|
---|
106 | return E_NOINTERFACE;
|
---|
107 |
|
---|
108 | *ppvObj = This;
|
---|
109 |
|
---|
110 | SoundBufAddRef(This);
|
---|
111 | return(DS_OK);
|
---|
112 | }
|
---|
113 | //******************************************************************************
|
---|
114 | //******************************************************************************
|
---|
115 | ULONG WIN32API SoundBufAddRef(THIS)
|
---|
116 | {
|
---|
117 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
118 |
|
---|
119 | dprintf(("OS2IDirectSoundBuffer::AddRef %d\n", me->Referenced+1));
|
---|
120 | if(me == NULL) {
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 | return ++me->Referenced;
|
---|
124 | }
|
---|
125 | //******************************************************************************
|
---|
126 | //******************************************************************************
|
---|
127 | ULONG WIN32API SoundBufRelease(THIS)
|
---|
128 | {
|
---|
129 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
130 |
|
---|
131 | dprintf((" OS2IDirectSoundBuffer::Release %d\n", me->Referenced-1));
|
---|
132 | if(me == NULL) {
|
---|
133 | return 0;
|
---|
134 | }
|
---|
135 | if(me->Referenced) {
|
---|
136 | me->Referenced--;
|
---|
137 | if(me->Referenced == 0) {
|
---|
138 | delete me;
|
---|
139 | return(0);
|
---|
140 | }
|
---|
141 | else return me->Referenced;
|
---|
142 | }
|
---|
143 | else return(0);
|
---|
144 | }
|
---|
145 | //******************************************************************************
|
---|
146 | //******************************************************************************
|
---|
147 | HRESULT __stdcall SoundBufGetCaps(THIS_ LPDSBCAPS lpDSCaps)
|
---|
148 | {
|
---|
149 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
150 |
|
---|
151 | dprintf(("SoundBufGetCaps"));
|
---|
152 | if(me == NULL || lpDSCaps == NULL) {
|
---|
153 | return DSERR_INVALIDPARAM;
|
---|
154 | }
|
---|
155 | return DS_OK;
|
---|
156 | }
|
---|
157 | //******************************************************************************
|
---|
158 | //******************************************************************************
|
---|
159 | HRESULT __stdcall SoundBufGetCurrentPosition(THIS_ LPDWORD lpdwCurrentPlayCursor,
|
---|
160 | LPDWORD lpdwCurrentWriteCursor)
|
---|
161 | {
|
---|
162 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
163 |
|
---|
164 | dprintf(("SoundBufGetCurrentPosition"));
|
---|
165 | if(me == NULL || lpdwCurrentPlayCursor == NULL || lpdwCurrentWriteCursor == NULL) {
|
---|
166 | return DSERR_INVALIDPARAM;
|
---|
167 | }
|
---|
168 | *lpdwCurrentPlayCursor = me->playpos;
|
---|
169 | *lpdwCurrentWriteCursor = me->writepos;
|
---|
170 | return DS_OK;
|
---|
171 | }
|
---|
172 | //******************************************************************************
|
---|
173 | //******************************************************************************
|
---|
174 | HRESULT __stdcall SoundBufGetFormat(THIS_ LPWAVEFORMATEX lpwfxFormat,
|
---|
175 | DWORD ddwSizeAllocated, LPDWORD lpdwSizeWritten)
|
---|
176 | {
|
---|
177 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
178 | int copysize;
|
---|
179 |
|
---|
180 | dprintf(("SoundBufGetFormat"));
|
---|
181 | if(me == NULL || lpwfxFormat == NULL || ddwSizeAllocated == 0) {
|
---|
182 | return DSERR_INVALIDPARAM;
|
---|
183 | }
|
---|
184 | copysize = min(ddwSizeAllocated, (me->lpfxFormat->cbSize + sizeof(WAVEFORMATEX)));
|
---|
185 | memcpy(lpwfxFormat, me->lpfxFormat, copysize);
|
---|
186 |
|
---|
187 | if(lpdwSizeWritten) {
|
---|
188 | *lpdwSizeWritten = copysize;
|
---|
189 | }
|
---|
190 | return DS_OK;
|
---|
191 | }
|
---|
192 | //******************************************************************************
|
---|
193 | //******************************************************************************
|
---|
194 | HRESULT __stdcall SoundBufGetVolume(THIS_ LPLONG lplVolume)
|
---|
195 | {
|
---|
196 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
197 |
|
---|
198 | dprintf(("SoundBufGetVolume"));
|
---|
199 | if(me == NULL || lplVolume == NULL) {
|
---|
200 | return DSERR_INVALIDPARAM;
|
---|
201 | }
|
---|
202 | *lplVolume = me->volume;
|
---|
203 | return DS_OK;
|
---|
204 | }
|
---|
205 | //******************************************************************************
|
---|
206 | //******************************************************************************
|
---|
207 | HRESULT __stdcall SoundBufGetPan(THIS_ LPLONG lplPan)
|
---|
208 | {
|
---|
209 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
210 |
|
---|
211 | dprintf(("SoundBufGetPan"));
|
---|
212 | if(me == NULL || lplPan == NULL) {
|
---|
213 | return DSERR_INVALIDPARAM;
|
---|
214 | }
|
---|
215 | *lplPan = me->pan;
|
---|
216 | return DS_OK;
|
---|
217 | }
|
---|
218 | //******************************************************************************
|
---|
219 | //******************************************************************************
|
---|
220 | HRESULT __stdcall SoundBufGetFrequency(THIS_ LPDWORD lpdwFrequency)
|
---|
221 | {
|
---|
222 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
223 |
|
---|
224 | dprintf(("SoundBufGetFrequency"));
|
---|
225 | if(me == NULL || lpdwFrequency == NULL) {
|
---|
226 | return DSERR_INVALIDPARAM;
|
---|
227 | }
|
---|
228 | *lpdwFrequency = me->frequency;
|
---|
229 | return DS_OK;
|
---|
230 | }
|
---|
231 | //******************************************************************************
|
---|
232 | //******************************************************************************
|
---|
233 | HRESULT __stdcall SoundBufGetStatus(THIS_ LPDWORD lpdwStatus)
|
---|
234 | {
|
---|
235 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
236 |
|
---|
237 | dprintf(("SoundBufGetStatus"));
|
---|
238 | if(me == NULL || lpdwStatus == NULL) {
|
---|
239 | return DSERR_INVALIDPARAM;
|
---|
240 | }
|
---|
241 | *lpdwStatus = me->status;
|
---|
242 | return DS_OK;
|
---|
243 | }
|
---|
244 | //******************************************************************************
|
---|
245 | //******************************************************************************
|
---|
246 | HRESULT __stdcall SoundBufInitialize(THIS_ LPDIRECTSOUND, LPDSBUFFERDESC )
|
---|
247 | {
|
---|
248 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
249 |
|
---|
250 | dprintf(("SoundBufInitialize"));
|
---|
251 | if(me == NULL) {
|
---|
252 | return DSERR_INVALIDPARAM;
|
---|
253 | }
|
---|
254 | return DSERR_ALREADYINITIALIZED; //todo: for future extensions (dx5/6 ??)
|
---|
255 | }
|
---|
256 | //******************************************************************************
|
---|
257 | //******************************************************************************
|
---|
258 | HRESULT __stdcall SoundBufLock(THIS_ DWORD dwWriteCursor, DWORD dwWriteBytes,
|
---|
259 | LPVOID lplpvAudioPtr1, LPDWORD lpdwAudioBytes1,
|
---|
260 | LPVOID lplpvAudioPtr2, LPDWORD lpdwAudioBytes2,
|
---|
261 | DWORD dwFlags)
|
---|
262 | {
|
---|
263 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
264 |
|
---|
265 | dprintf(("SoundBufLock"));
|
---|
266 | if(me == NULL || !lplpvAudioPtr1 || !lpdwAudioBytes1) {
|
---|
267 | return(DSERR_INVALIDPARAM);
|
---|
268 | }
|
---|
269 | //not sure if this is an error, but it's certainly a smart thing to do (cond. == true)
|
---|
270 | if(dwWriteBytes > me->bufferdesc.dwBufferBytes) {
|
---|
271 | dprintf(("SoundBufLock: dwWriteBytes > me->bufferdesc.dwBufferBytes"));
|
---|
272 | return(DSERR_INVALIDPARAM);
|
---|
273 | }
|
---|
274 | if(dwFlags & DSBLOCK_FROMWRITECURSOR) {
|
---|
275 | dwWriteCursor = me->writepos;
|
---|
276 | }
|
---|
277 | if(dwWriteCursor + dwWriteBytes > me->bufferdesc.dwBufferBytes ) {
|
---|
278 | *(DWORD *)lplpvAudioPtr1 = (DWORD)(me->lpBuffer + dwWriteCursor);
|
---|
279 | *lpdwAudioBytes1 = me->bufferdesc.dwBufferBytes - dwWriteCursor;
|
---|
280 | if(lplpvAudioPtr2 && lpdwAudioBytes2) {
|
---|
281 | *(DWORD *)lplpvAudioPtr2 = (DWORD)me->lpBuffer;
|
---|
282 | *lpdwAudioBytes2 = dwWriteBytes - *lpdwAudioBytes1;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | else {
|
---|
286 | *(DWORD *)lplpvAudioPtr1 = (DWORD)(me->lpBuffer + dwWriteCursor);
|
---|
287 | *lpdwAudioBytes1 = dwWriteBytes;
|
---|
288 | if(lplpvAudioPtr2 && lpdwAudioBytes2) {
|
---|
289 | *(DWORD *)lplpvAudioPtr2 = 0;
|
---|
290 | *lpdwAudioBytes2 = 0;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | me->fLocked = TRUE;
|
---|
294 | return DS_OK;
|
---|
295 | }
|
---|
296 | //******************************************************************************
|
---|
297 | //******************************************************************************
|
---|
298 | HRESULT __stdcall SoundBufPlay(THIS_ DWORD,DWORD,DWORD )
|
---|
299 | {
|
---|
300 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
301 |
|
---|
302 | dprintf(("SoundBufPlay"));
|
---|
303 | if(me == NULL) {
|
---|
304 | return DSERR_INVALIDPARAM;
|
---|
305 | }
|
---|
306 | return DS_OK;
|
---|
307 | }
|
---|
308 | //******************************************************************************
|
---|
309 | //******************************************************************************
|
---|
310 | HRESULT __stdcall SoundBufSetCurrentPosition(THIS_ DWORD dwNewPosition)
|
---|
311 | {
|
---|
312 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
313 |
|
---|
314 | dprintf(("SoundBufSetCurrentPosition"));
|
---|
315 | if(me == NULL) {
|
---|
316 | return DSERR_INVALIDPARAM;
|
---|
317 | }
|
---|
318 | //todo: check for primary buffer (can't do it with those)
|
---|
319 | me->playpos = dwNewPosition;
|
---|
320 | me->writepos = dwNewPosition;
|
---|
321 | return DS_OK;
|
---|
322 | }
|
---|
323 | //******************************************************************************
|
---|
324 | //******************************************************************************
|
---|
325 | HRESULT __stdcall SoundBufSetFormat(THIS_ LPWAVEFORMATEX )
|
---|
326 | {
|
---|
327 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
328 |
|
---|
329 | dprintf(("SoundBufSetFormat"));
|
---|
330 | if(me == NULL) {
|
---|
331 | return DSERR_INVALIDPARAM;
|
---|
332 | }
|
---|
333 | return DS_OK;
|
---|
334 | }
|
---|
335 | //******************************************************************************
|
---|
336 | //******************************************************************************
|
---|
337 | HRESULT __stdcall SoundBufSetVolume(THIS_ LONG lVolume)
|
---|
338 | {
|
---|
339 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
340 |
|
---|
341 | dprintf(("SoundBufSetVolume"));
|
---|
342 | if(me == NULL) {
|
---|
343 | return DSERR_INVALIDPARAM;
|
---|
344 | }
|
---|
345 | me->volume = lVolume;
|
---|
346 | return DS_OK;
|
---|
347 | }
|
---|
348 | //******************************************************************************
|
---|
349 | //******************************************************************************
|
---|
350 | HRESULT __stdcall SoundBufSetPan(THIS_ LONG lPan)
|
---|
351 | {
|
---|
352 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
353 |
|
---|
354 | dprintf(("SoundBufSetPan"));
|
---|
355 | if(me == NULL) {
|
---|
356 | return DSERR_INVALIDPARAM;
|
---|
357 | }
|
---|
358 | me->pan = lPan;
|
---|
359 | return DS_OK;
|
---|
360 | }
|
---|
361 | //******************************************************************************
|
---|
362 | //******************************************************************************
|
---|
363 | HRESULT __stdcall SoundBufSetFrequency(THIS_ DWORD dwFrequency)
|
---|
364 | {
|
---|
365 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
366 |
|
---|
367 | dprintf(("SoundBufSetFrequency"));
|
---|
368 | if(me == NULL) {
|
---|
369 | return DSERR_INVALIDPARAM;
|
---|
370 | }
|
---|
371 | me->frequency = dwFrequency;
|
---|
372 | return DS_OK;
|
---|
373 | }
|
---|
374 | //******************************************************************************
|
---|
375 | //******************************************************************************
|
---|
376 | HRESULT __stdcall SoundBufStop(THIS )
|
---|
377 | {
|
---|
378 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
379 |
|
---|
380 | dprintf(("SoundBufStop"));
|
---|
381 | if(me == NULL) {
|
---|
382 | return DSERR_INVALIDPARAM;
|
---|
383 | }
|
---|
384 | return DS_OK;
|
---|
385 | }
|
---|
386 | //******************************************************************************
|
---|
387 | //******************************************************************************
|
---|
388 | HRESULT __stdcall SoundBufUnlock(THIS_ LPVOID,DWORD,LPVOID,DWORD )
|
---|
389 | {
|
---|
390 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
391 |
|
---|
392 | dprintf(("SoundBufUnlock"));
|
---|
393 | if(me == NULL) {
|
---|
394 | return DSERR_INVALIDPARAM;
|
---|
395 | }
|
---|
396 | return DS_OK;
|
---|
397 | }
|
---|
398 | //******************************************************************************
|
---|
399 | //******************************************************************************
|
---|
400 | HRESULT __stdcall SoundBufRestore(THIS )
|
---|
401 | {
|
---|
402 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This;
|
---|
403 |
|
---|
404 | dprintf(("SoundBufRestore"));
|
---|
405 | if(me == NULL) {
|
---|
406 | return DSERR_INVALIDPARAM;
|
---|
407 | }
|
---|
408 | return DS_OK;
|
---|
409 | }
|
---|
410 | //******************************************************************************
|
---|
411 | //******************************************************************************
|
---|