include: Implement C_ASSERT() using the C11 _Static_assert() if available.
[wine.git] / dlls / dmband / band.c
blobd71863a1f9d3cd8ac9bc7ec85fdcf43de6f5b8eb
1 /* IDirectMusicBand Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "dmband_private.h"
21 #include "dmobject.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmband);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
27 /*****************************************************************************
28 * IDirectMusicBandImpl implementation
30 typedef struct IDirectMusicBandImpl {
31 IDirectMusicBand IDirectMusicBand_iface;
32 struct dmobject dmobj;
33 LONG ref;
34 struct list Instruments;
35 } IDirectMusicBandImpl;
37 static inline IDirectMusicBandImpl *impl_from_IDirectMusicBand(IDirectMusicBand *iface)
39 return CONTAINING_RECORD(iface, IDirectMusicBandImpl, IDirectMusicBand_iface);
42 /* DirectMusicBandImpl IDirectMusicBand part: */
43 static HRESULT WINAPI IDirectMusicBandImpl_QueryInterface(IDirectMusicBand *iface, REFIID riid,
44 void **ret_iface)
46 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
48 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
50 *ret_iface = NULL;
52 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDirectMusicBand))
53 *ret_iface = iface;
54 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
55 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
56 else if (IsEqualIID(riid, &IID_IPersistStream))
57 *ret_iface = &This->dmobj.IPersistStream_iface;
58 else {
59 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
60 return E_NOINTERFACE;
63 IDirectMusicBand_AddRef((IUnknown*)*ret_iface);
64 return S_OK;
67 static ULONG WINAPI IDirectMusicBandImpl_AddRef(IDirectMusicBand *iface)
69 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
70 LONG ref = InterlockedIncrement(&This->ref);
72 TRACE("(%p) ref=%ld\n", This, ref);
74 return ref;
77 static ULONG WINAPI IDirectMusicBandImpl_Release(IDirectMusicBand *iface)
79 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
80 LONG ref = InterlockedDecrement(&This->ref);
82 TRACE("(%p) ref=%ld\n", This, ref);
84 if (!ref) {
85 HeapFree(GetProcessHeap(), 0, This);
86 DMBAND_UnlockModule();
89 return ref;
92 static HRESULT WINAPI IDirectMusicBandImpl_CreateSegment(IDirectMusicBand *iface,
93 IDirectMusicSegment **segment)
95 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
96 HRESULT hr;
97 DMUS_BAND_PARAM bandparam;
99 FIXME("(%p, %p): semi-stub\n", This, segment);
101 hr = CoCreateInstance(&CLSID_DirectMusicSegment, NULL, CLSCTX_INPROC,
102 &IID_IDirectMusicSegment, (void**)segment);
103 if (FAILED(hr))
104 return hr;
106 bandparam.mtTimePhysical = 0;
107 bandparam.pBand = &This->IDirectMusicBand_iface;
108 IDirectMusicBand_AddRef(bandparam.pBand);
109 hr = IDirectMusicSegment_SetParam(*segment, &GUID_BandParam, 0xffffffff, DMUS_SEG_ALLTRACKS,
110 0, &bandparam);
111 IDirectMusicBand_Release(bandparam.pBand);
113 return hr;
116 static HRESULT WINAPI IDirectMusicBandImpl_Download(IDirectMusicBand *iface,
117 IDirectMusicPerformance *pPerformance)
119 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
120 FIXME("(%p, %p): stub\n", This, pPerformance);
121 return S_OK;
124 static HRESULT WINAPI IDirectMusicBandImpl_Unload(IDirectMusicBand *iface,
125 IDirectMusicPerformance *pPerformance)
127 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
128 FIXME("(%p, %p): stub\n", This, pPerformance);
129 return S_OK;
132 static const IDirectMusicBandVtbl dmband_vtbl = {
133 IDirectMusicBandImpl_QueryInterface,
134 IDirectMusicBandImpl_AddRef,
135 IDirectMusicBandImpl_Release,
136 IDirectMusicBandImpl_CreateSegment,
137 IDirectMusicBandImpl_Download,
138 IDirectMusicBandImpl_Unload
141 /* IDirectMusicBandImpl IDirectMusicObject part: */
142 static HRESULT WINAPI band_IDirectMusicObject_ParseDescriptor(IDirectMusicObject *iface,
143 IStream *stream, DMUS_OBJECTDESC *desc)
145 struct chunk_entry riff = {0};
146 STATSTG stat;
147 HRESULT hr;
149 TRACE("(%p, %p, %p)\n", iface, stream, desc);
151 if (!stream || !desc)
152 return E_POINTER;
154 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
155 return hr;
156 if (riff.id != FOURCC_RIFF || riff.type != DMUS_FOURCC_BAND_FORM) {
157 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
158 stream_skip_chunk(stream, &riff);
159 return DMUS_E_INVALID_BAND;
162 hr = dmobj_parsedescriptor(stream, &riff, desc,
163 DMUS_OBJ_OBJECT|DMUS_OBJ_NAME|DMUS_OBJ_NAME_INAM|DMUS_OBJ_CATEGORY|DMUS_OBJ_VERSION);
164 if (FAILED(hr))
165 return hr;
167 desc->guidClass = CLSID_DirectMusicBand;
168 desc->dwValidData |= DMUS_OBJ_CLASS;
170 if (desc->dwValidData & DMUS_OBJ_CATEGORY) {
171 IStream_Stat(stream, &stat, STATFLAG_NONAME);
172 desc->ftDate = stat.mtime;
173 desc->dwValidData |= DMUS_OBJ_DATE;
176 TRACE("returning descriptor:\n");
177 dump_DMUS_OBJECTDESC(desc);
178 return S_OK;
181 static const IDirectMusicObjectVtbl dmobject_vtbl = {
182 dmobj_IDirectMusicObject_QueryInterface,
183 dmobj_IDirectMusicObject_AddRef,
184 dmobj_IDirectMusicObject_Release,
185 dmobj_IDirectMusicObject_GetDescriptor,
186 dmobj_IDirectMusicObject_SetDescriptor,
187 band_IDirectMusicObject_ParseDescriptor
190 #define DMUS_IO_INSTRUMENT_DX7_SIZE offsetof(DMUS_IO_INSTRUMENT, nPitchBendRange)
192 /* IDirectMusicBandImpl IPersistStream part: */
193 static HRESULT parse_instrument(IDirectMusicBandImpl *This, DMUS_PRIVATE_CHUNK *pChunk,
194 IStream *pStm)
196 DMUS_PRIVATE_CHUNK Chunk;
197 DWORD ListSize[3], ListCount[3];
198 LARGE_INTEGER liMove; /* used when skipping chunks */
199 HRESULT hr;
201 DMUS_IO_INSTRUMENT inst;
202 LPDMUS_PRIVATE_INSTRUMENT pNewInstrument;
203 IDirectMusicObject* pObject = NULL;
205 if (pChunk->fccID != DMUS_FOURCC_INSTRUMENT_LIST) {
206 ERR_(dmfile)(": %s chunk should be an INSTRUMENT list\n", debugstr_fourcc (pChunk->fccID));
207 return E_FAIL;
210 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
211 ListCount[0] = 0;
213 do {
214 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
215 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
216 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
217 switch (Chunk.fccID) {
218 case DMUS_FOURCC_INSTRUMENT_CHUNK: {
219 TRACE_(dmfile)(": Instrument chunk\n");
220 if (Chunk.dwSize != sizeof(DMUS_IO_INSTRUMENT) && Chunk.dwSize != DMUS_IO_INSTRUMENT_DX7_SIZE) {
221 ERR_(dmfile)("unexpected size %ld\n", Chunk.dwSize);
222 return E_FAIL;
224 IStream_Read (pStm, &inst, Chunk.dwSize, NULL);
225 if (Chunk.dwSize != sizeof(DMUS_IO_INSTRUMENT))
226 inst.nPitchBendRange = 0;
228 TRACE_(dmfile)(" - dwPatch: %lu\n", inst.dwPatch);
229 TRACE_(dmfile)(" - dwAssignPatch: %lu\n", inst.dwAssignPatch);
230 TRACE_(dmfile)(" - dwNoteRanges[0]: %lu\n", inst.dwNoteRanges[0]);
231 TRACE_(dmfile)(" - dwNoteRanges[1]: %lu\n", inst.dwNoteRanges[1]);
232 TRACE_(dmfile)(" - dwNoteRanges[2]: %lu\n", inst.dwNoteRanges[2]);
233 TRACE_(dmfile)(" - dwNoteRanges[3]: %lu\n", inst.dwNoteRanges[3]);
234 TRACE_(dmfile)(" - dwPChannel: %lu\n", inst.dwPChannel);
235 TRACE_(dmfile)(" - dwFlags: %lx\n", inst.dwFlags);
236 TRACE_(dmfile)(" - bPan: %u\n", inst.bPan);
237 TRACE_(dmfile)(" - bVolume: %u\n", inst.bVolume);
238 TRACE_(dmfile)(" - nTranspose: %d\n", inst.nTranspose);
239 TRACE_(dmfile)(" - dwChannelPriority: %lu\n", inst.dwChannelPriority);
240 TRACE_(dmfile)(" - nPitchBendRange: %d\n", inst.nPitchBendRange);
241 break;
243 case FOURCC_LIST: {
244 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
245 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
246 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
247 ListCount[1] = 0;
248 switch (Chunk.fccID) {
249 case DMUS_FOURCC_REF_LIST: {
250 FIXME_(dmfile)(": DMRF (DM References) list\n");
251 hr = IDirectMusicUtils_IPersistStream_ParseReference(&This->dmobj.IPersistStream_iface,
252 &Chunk, pStm, &pObject);
253 if (FAILED(hr)) {
254 ERR(": could not load Reference\n");
255 return hr;
257 break;
259 default: {
260 TRACE_(dmfile)(": unknown (skipping)\n");
261 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
262 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
263 break;
266 break;
268 default: {
269 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
270 liMove.QuadPart = Chunk.dwSize;
271 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
272 break;
275 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
276 } while (ListCount[0] < ListSize[0]);
279 * @TODO insert pNewInstrument into This
281 pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENT));
282 if (NULL == pNewInstrument) {
283 ERR(": no more memory\n");
284 return E_OUTOFMEMORY;
286 memcpy(&pNewInstrument->pInstrument, &inst, sizeof(DMUS_IO_INSTRUMENT));
287 pNewInstrument->ppReferenceCollection = NULL;
288 if (NULL != pObject) {
289 IDirectMusicCollection* pCol = NULL;
290 hr = IDirectMusicObject_QueryInterface (pObject, &IID_IDirectMusicCollection, (void**) &pCol);
291 if (FAILED(hr)) {
292 ERR(": failed to get IDirectMusicCollection Interface from DMObject\n");
293 HeapFree(GetProcessHeap(), 0, pNewInstrument);
295 return hr;
297 pNewInstrument->ppReferenceCollection = pCol;
298 IDirectMusicObject_Release(pObject);
300 list_add_tail (&This->Instruments, &pNewInstrument->entry);
302 return S_OK;
305 static HRESULT parse_instruments_list(IDirectMusicBandImpl *This, DMUS_PRIVATE_CHUNK *pChunk,
306 IStream *pStm)
308 HRESULT hr;
309 DMUS_PRIVATE_CHUNK Chunk;
310 DWORD ListSize[3], ListCount[3];
311 LARGE_INTEGER liMove; /* used when skipping chunks */
313 if (pChunk->fccID != DMUS_FOURCC_INSTRUMENTS_LIST) {
314 ERR_(dmfile)(": %s chunk should be an INSTRUMENTS list\n", debugstr_fourcc (pChunk->fccID));
315 return E_FAIL;
318 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
319 ListCount[0] = 0;
321 do {
322 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
323 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
324 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
325 switch (Chunk.fccID) {
326 case FOURCC_LIST: {
327 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
328 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
329 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
330 ListCount[1] = 0;
331 switch (Chunk.fccID) {
332 case DMUS_FOURCC_INSTRUMENT_LIST: {
333 TRACE_(dmfile)(": Instrument list\n");
334 hr = parse_instrument(This, &Chunk, pStm);
335 if (FAILED(hr)) return hr;
336 break;
338 default: {
339 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
340 liMove.QuadPart = ListSize[1];
341 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
342 break;
345 break;
347 default: {
348 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
349 liMove.QuadPart = Chunk.dwSize;
350 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
351 break;
354 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
355 } while (ListCount[0] < ListSize[0]);
357 return S_OK;
360 static HRESULT parse_band_form(IDirectMusicBandImpl *This, DMUS_PRIVATE_CHUNK *pChunk,
361 IStream *pStm)
363 HRESULT hr = E_FAIL;
364 DMUS_PRIVATE_CHUNK Chunk;
365 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
366 LARGE_INTEGER liMove; /* used when skipping chunks */
368 GUID tmp_guid;
370 if (pChunk->fccID != DMUS_FOURCC_BAND_FORM) {
371 ERR_(dmfile)(": %s chunk should be a BAND form\n", debugstr_fourcc (pChunk->fccID));
372 return E_FAIL;
375 StreamSize = pChunk->dwSize - sizeof(FOURCC);
376 StreamCount = 0;
378 do {
379 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
380 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
381 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
383 hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, &This->dmobj.desc);
384 if (FAILED(hr)) return hr;
386 if (hr == S_FALSE) {
387 switch (Chunk.fccID) {
388 case DMUS_FOURCC_GUID_CHUNK: {
389 TRACE_(dmfile)(": GUID\n");
390 IStream_Read (pStm, &tmp_guid, sizeof(GUID), NULL);
391 TRACE_(dmfile)(" - guid: %s\n", debugstr_dmguid(&tmp_guid));
392 break;
394 case FOURCC_LIST: {
395 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
396 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
397 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
398 ListCount[0] = 0;
399 switch (Chunk.fccID) {
400 case DMUS_FOURCC_UNFO_LIST: {
401 TRACE_(dmfile)(": UNFO list\n");
402 do {
403 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
404 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
405 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
407 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &This->dmobj.desc);
408 if (FAILED(hr)) return hr;
410 if (hr == S_FALSE) {
411 switch (Chunk.fccID) {
412 default: {
413 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
414 liMove.QuadPart = Chunk.dwSize;
415 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
416 break;
420 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
421 } while (ListCount[0] < ListSize[0]);
422 break;
424 case DMUS_FOURCC_INSTRUMENTS_LIST: {
425 TRACE_(dmfile)(": INSTRUMENTS list\n");
426 hr = parse_instruments_list(This, &Chunk, pStm);
427 if (FAILED(hr)) return hr;
428 break;
430 default: {
431 TRACE_(dmfile)(": unknown (skipping)\n");
432 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
433 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
434 break;
437 break;
439 default: {
440 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
441 liMove.QuadPart = Chunk.dwSize;
442 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
443 break;
447 TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
448 } while (StreamCount < StreamSize);
450 return S_OK;
453 static inline IDirectMusicBandImpl *impl_from_IPersistStream(IPersistStream *iface)
455 return CONTAINING_RECORD(iface, IDirectMusicBandImpl, dmobj.IPersistStream_iface);
458 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
460 IDirectMusicBandImpl *This = impl_from_IPersistStream(iface);
461 DMUS_PRIVATE_CHUNK Chunk;
462 LARGE_INTEGER liMove;
463 HRESULT hr;
465 TRACE("(%p,%p): loading\n", This, pStm);
467 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
468 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
469 switch (Chunk.fccID) {
470 case FOURCC_RIFF: {
471 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
472 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
473 switch (Chunk.fccID) {
474 case DMUS_FOURCC_BAND_FORM: {
475 TRACE_(dmfile)(": Band form\n");
476 hr = parse_band_form(This, &Chunk, pStm);
477 if (FAILED(hr)) return hr;
478 break;
480 default: {
481 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
482 liMove.QuadPart = Chunk.dwSize;
483 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
484 return E_FAIL;
487 TRACE_(dmfile)(": reading finished\n");
488 break;
490 default: {
491 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
492 liMove.QuadPart = Chunk.dwSize;
493 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
494 return E_FAIL;
498 return S_OK;
501 static const IPersistStreamVtbl persiststream_vtbl = {
502 dmobj_IPersistStream_QueryInterface,
503 dmobj_IPersistStream_AddRef,
504 dmobj_IPersistStream_Release,
505 unimpl_IPersistStream_GetClassID,
506 unimpl_IPersistStream_IsDirty,
507 IPersistStreamImpl_Load,
508 unimpl_IPersistStream_Save,
509 unimpl_IPersistStream_GetSizeMax
512 /* for ClassFactory */
513 HRESULT create_dmband(REFIID lpcGUID, void **ppobj)
515 IDirectMusicBandImpl* obj;
516 HRESULT hr;
518 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandImpl));
519 if (NULL == obj) {
520 *ppobj = NULL;
521 return E_OUTOFMEMORY;
523 obj->IDirectMusicBand_iface.lpVtbl = &dmband_vtbl;
524 obj->ref = 1;
525 dmobject_init(&obj->dmobj, &CLSID_DirectMusicBand, (IUnknown *)&obj->IDirectMusicBand_iface);
526 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
527 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
528 list_init (&obj->Instruments);
530 DMBAND_LockModule();
531 hr = IDirectMusicBand_QueryInterface(&obj->IDirectMusicBand_iface, lpcGUID, ppobj);
532 IDirectMusicBand_Release(&obj->IDirectMusicBand_iface);
534 return hr;