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"
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
;
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
,
46 IDirectMusicBandImpl
*This
= impl_from_IDirectMusicBand(iface
);
48 TRACE("(%p, %s, %p)\n", This
, debugstr_dmguid(riid
), ret_iface
);
52 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IDirectMusicBand
))
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
;
59 WARN("(%p, %s, %p): not found\n", This
, debugstr_dmguid(riid
), ret_iface
);
63 IDirectMusicBand_AddRef((IUnknown
*)*ret_iface
);
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=%d\n", This
, 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=%d\n", This
, ref
);
85 HeapFree(GetProcessHeap(), 0, This
);
86 DMBAND_UnlockModule();
92 static HRESULT WINAPI
IDirectMusicBandImpl_CreateSegment(IDirectMusicBand
*iface
,
93 IDirectMusicSegment
**ppSegment
)
95 IDirectMusicBandImpl
*This
= impl_from_IDirectMusicBand(iface
);
96 FIXME("(%p, %p): stub\n", This
, ppSegment
);
100 static HRESULT WINAPI
IDirectMusicBandImpl_Download(IDirectMusicBand
*iface
,
101 IDirectMusicPerformance
*pPerformance
)
103 IDirectMusicBandImpl
*This
= impl_from_IDirectMusicBand(iface
);
104 FIXME("(%p, %p): stub\n", This
, pPerformance
);
108 static HRESULT WINAPI
IDirectMusicBandImpl_Unload(IDirectMusicBand
*iface
,
109 IDirectMusicPerformance
*pPerformance
)
111 IDirectMusicBandImpl
*This
= impl_from_IDirectMusicBand(iface
);
112 FIXME("(%p, %p): stub\n", This
, pPerformance
);
116 static const IDirectMusicBandVtbl dmband_vtbl
= {
117 IDirectMusicBandImpl_QueryInterface
,
118 IDirectMusicBandImpl_AddRef
,
119 IDirectMusicBandImpl_Release
,
120 IDirectMusicBandImpl_CreateSegment
,
121 IDirectMusicBandImpl_Download
,
122 IDirectMusicBandImpl_Unload
125 /* IDirectMusicBandImpl IDirectMusicObject part: */
126 static HRESULT WINAPI
band_IDirectMusicObject_ParseDescriptor(IDirectMusicObject
*iface
,
127 IStream
*stream
, DMUS_OBJECTDESC
*desc
)
129 struct chunk_entry riff
= {0};
133 TRACE("(%p, %p, %p)\n", iface
, stream
, desc
);
135 if (!stream
|| !desc
)
138 if ((hr
= stream_get_chunk(stream
, &riff
)) != S_OK
)
140 if (riff
.id
!= FOURCC_RIFF
|| riff
.type
!= DMUS_FOURCC_BAND_FORM
) {
141 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff
));
142 stream_skip_chunk(stream
, &riff
);
143 return DMUS_E_INVALID_BAND
;
146 hr
= dmobj_parsedescriptor(stream
, &riff
, desc
,
147 DMUS_OBJ_OBJECT
|DMUS_OBJ_NAME
|DMUS_OBJ_NAME_INAM
|DMUS_OBJ_CATEGORY
|DMUS_OBJ_VERSION
);
151 desc
->guidClass
= CLSID_DirectMusicBand
;
152 desc
->dwValidData
|= DMUS_OBJ_CLASS
;
154 if (desc
->dwValidData
& DMUS_OBJ_CATEGORY
) {
155 IStream_Stat(stream
, &stat
, STATFLAG_NONAME
);
156 desc
->ftDate
= stat
.mtime
;
157 desc
->dwValidData
|= DMUS_OBJ_DATE
;
160 TRACE("returning descriptor:\n");
161 debug_DMUS_OBJECTDESC(desc
);
165 static const IDirectMusicObjectVtbl dmobject_vtbl
= {
166 dmobj_IDirectMusicObject_QueryInterface
,
167 dmobj_IDirectMusicObject_AddRef
,
168 dmobj_IDirectMusicObject_Release
,
169 dmobj_IDirectMusicObject_GetDescriptor
,
170 dmobj_IDirectMusicObject_SetDescriptor
,
171 band_IDirectMusicObject_ParseDescriptor
174 /* IDirectMusicBandImpl IPersistStream part: */
175 static HRESULT
parse_instrument(IDirectMusicBandImpl
*This
, DMUS_PRIVATE_CHUNK
*pChunk
,
178 DMUS_PRIVATE_CHUNK Chunk
;
179 DWORD ListSize
[3], ListCount
[3];
180 LARGE_INTEGER liMove
; /* used when skipping chunks */
183 DMUS_IO_INSTRUMENT inst
;
184 LPDMUS_PRIVATE_INSTRUMENT pNewInstrument
;
185 IDirectMusicObject
* pObject
= NULL
;
187 if (pChunk
->fccID
!= DMUS_FOURCC_INSTRUMENT_LIST
) {
188 ERR_(dmfile
)(": %s chunk should be an INSTRUMENT list\n", debugstr_fourcc (pChunk
->fccID
));
192 ListSize
[0] = pChunk
->dwSize
- sizeof(FOURCC
);
196 IStream_Read (pStm
, &Chunk
, sizeof(FOURCC
)+sizeof(DWORD
), NULL
);
197 ListCount
[0] += sizeof(FOURCC
) + sizeof(DWORD
) + Chunk
.dwSize
;
198 TRACE_(dmfile
)(": %s chunk (size = %d)", debugstr_fourcc (Chunk
.fccID
), Chunk
.dwSize
);
199 switch (Chunk
.fccID
) {
200 case DMUS_FOURCC_INSTRUMENT_CHUNK
: {
201 TRACE_(dmfile
)(": Instrument chunk\n");
202 if (Chunk
.dwSize
!= sizeof(DMUS_IO_INSTRUMENT
)) return E_FAIL
;
203 IStream_Read (pStm
, &inst
, sizeof(DMUS_IO_INSTRUMENT
), NULL
);
204 TRACE_(dmfile
)(" - dwPatch: %u\n", inst
.dwPatch
);
205 TRACE_(dmfile
)(" - dwAssignPatch: %u\n", inst
.dwAssignPatch
);
206 TRACE_(dmfile
)(" - dwNoteRanges[0]: %u\n", inst
.dwNoteRanges
[0]);
207 TRACE_(dmfile
)(" - dwNoteRanges[1]: %u\n", inst
.dwNoteRanges
[1]);
208 TRACE_(dmfile
)(" - dwNoteRanges[2]: %u\n", inst
.dwNoteRanges
[2]);
209 TRACE_(dmfile
)(" - dwNoteRanges[3]: %u\n", inst
.dwNoteRanges
[3]);
210 TRACE_(dmfile
)(" - dwPChannel: %u\n", inst
.dwPChannel
);
211 TRACE_(dmfile
)(" - dwFlags: %x\n", inst
.dwFlags
);
212 TRACE_(dmfile
)(" - bPan: %u\n", inst
.bPan
);
213 TRACE_(dmfile
)(" - bVolume: %u\n", inst
.bVolume
);
214 TRACE_(dmfile
)(" - nTranspose: %d\n", inst
.nTranspose
);
215 TRACE_(dmfile
)(" - dwChannelPriority: %u\n", inst
.dwChannelPriority
);
216 TRACE_(dmfile
)(" - nPitchBendRange: %d\n", inst
.nPitchBendRange
);
220 IStream_Read (pStm
, &Chunk
.fccID
, sizeof(FOURCC
), NULL
);
221 TRACE_(dmfile
)(": LIST chunk of type %s", debugstr_fourcc(Chunk
.fccID
));
222 ListSize
[1] = Chunk
.dwSize
- sizeof(FOURCC
);
224 switch (Chunk
.fccID
) {
225 case DMUS_FOURCC_REF_LIST
: {
226 FIXME_(dmfile
)(": DMRF (DM References) list\n");
227 hr
= IDirectMusicUtils_IPersistStream_ParseReference(&This
->dmobj
.IPersistStream_iface
,
228 &Chunk
, pStm
, &pObject
);
230 ERR(": could not load Reference\n");
236 TRACE_(dmfile
)(": unknown (skipping)\n");
237 liMove
.QuadPart
= Chunk
.dwSize
- sizeof(FOURCC
);
238 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
);
245 TRACE_(dmfile
)(": unknown chunk (irrelevant & skipping)\n");
246 liMove
.QuadPart
= Chunk
.dwSize
;
247 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
);
251 TRACE_(dmfile
)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount
[0], ListSize
[0]);
252 } while (ListCount
[0] < ListSize
[0]);
255 * @TODO insert pNewInstrument into This
257 pNewInstrument
= HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY
, sizeof(DMUS_PRIVATE_INSTRUMENT
));
258 if (NULL
== pNewInstrument
) {
259 ERR(": no more memory\n");
260 return E_OUTOFMEMORY
;
262 memcpy(&pNewInstrument
->pInstrument
, &inst
, sizeof(DMUS_IO_INSTRUMENT
));
263 pNewInstrument
->ppReferenceCollection
= NULL
;
264 if (NULL
!= pObject
) {
265 IDirectMusicCollection
* pCol
= NULL
;
266 hr
= IDirectMusicObject_QueryInterface (pObject
, &IID_IDirectMusicCollection
, (void**) &pCol
);
268 ERR(": failed to get IDirectMusicCollection Interface from DMObject\n");
269 HeapFree(GetProcessHeap(), 0, pNewInstrument
);
273 pNewInstrument
->ppReferenceCollection
= pCol
;
274 IDirectMusicObject_Release(pObject
);
276 list_add_tail (&This
->Instruments
, &pNewInstrument
->entry
);
281 static HRESULT
parse_instruments_list(IDirectMusicBandImpl
*This
, DMUS_PRIVATE_CHUNK
*pChunk
,
285 DMUS_PRIVATE_CHUNK Chunk
;
286 DWORD ListSize
[3], ListCount
[3];
287 LARGE_INTEGER liMove
; /* used when skipping chunks */
289 if (pChunk
->fccID
!= DMUS_FOURCC_INSTRUMENTS_LIST
) {
290 ERR_(dmfile
)(": %s chunk should be an INSTRUMENTS list\n", debugstr_fourcc (pChunk
->fccID
));
294 ListSize
[0] = pChunk
->dwSize
- sizeof(FOURCC
);
298 IStream_Read (pStm
, &Chunk
, sizeof(FOURCC
)+sizeof(DWORD
), NULL
);
299 ListCount
[0] += sizeof(FOURCC
) + sizeof(DWORD
) + Chunk
.dwSize
;
300 TRACE_(dmfile
)(": %s chunk (size = %d)", debugstr_fourcc (Chunk
.fccID
), Chunk
.dwSize
);
301 switch (Chunk
.fccID
) {
303 IStream_Read (pStm
, &Chunk
.fccID
, sizeof(FOURCC
), NULL
);
304 TRACE_(dmfile
)(": LIST chunk of type %s", debugstr_fourcc(Chunk
.fccID
));
305 ListSize
[1] = Chunk
.dwSize
- sizeof(FOURCC
);
307 switch (Chunk
.fccID
) {
308 case DMUS_FOURCC_INSTRUMENT_LIST
: {
309 TRACE_(dmfile
)(": Instrument list\n");
310 hr
= parse_instrument(This
, &Chunk
, pStm
);
311 if (FAILED(hr
)) return hr
;
315 TRACE_(dmfile
)(": unknown chunk (irrelevant & skipping)\n");
316 liMove
.QuadPart
= ListSize
[1];
317 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
);
324 TRACE_(dmfile
)(": unknown chunk (irrelevant & skipping)\n");
325 liMove
.QuadPart
= Chunk
.dwSize
;
326 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
);
330 TRACE_(dmfile
)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount
[0], ListSize
[0]);
331 } while (ListCount
[0] < ListSize
[0]);
336 static HRESULT
parse_band_form(IDirectMusicBandImpl
*This
, DMUS_PRIVATE_CHUNK
*pChunk
,
340 DMUS_PRIVATE_CHUNK Chunk
;
341 DWORD StreamSize
, StreamCount
, ListSize
[3], ListCount
[3];
342 LARGE_INTEGER liMove
; /* used when skipping chunks */
346 if (pChunk
->fccID
!= DMUS_FOURCC_BAND_FORM
) {
347 ERR_(dmfile
)(": %s chunk should be a BAND form\n", debugstr_fourcc (pChunk
->fccID
));
351 StreamSize
= pChunk
->dwSize
- sizeof(FOURCC
);
355 IStream_Read (pStm
, &Chunk
, sizeof(FOURCC
)+sizeof(DWORD
), NULL
);
356 StreamCount
+= sizeof(FOURCC
) + sizeof(DWORD
) + Chunk
.dwSize
;
357 TRACE_(dmfile
)(": %s chunk (size = %d)", debugstr_fourcc (Chunk
.fccID
), Chunk
.dwSize
);
359 hr
= IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk
, pStm
, &This
->dmobj
.desc
);
360 if (FAILED(hr
)) return hr
;
363 switch (Chunk
.fccID
) {
364 case DMUS_FOURCC_GUID_CHUNK
: {
365 TRACE_(dmfile
)(": GUID\n");
366 IStream_Read (pStm
, &tmp_guid
, sizeof(GUID
), NULL
);
367 TRACE_(dmfile
)(" - guid: %s\n", debugstr_dmguid(&tmp_guid
));
371 IStream_Read (pStm
, &Chunk
.fccID
, sizeof(FOURCC
), NULL
);
372 TRACE_(dmfile
)(": LIST chunk of type %s", debugstr_fourcc(Chunk
.fccID
));
373 ListSize
[0] = Chunk
.dwSize
- sizeof(FOURCC
);
375 switch (Chunk
.fccID
) {
376 case DMUS_FOURCC_UNFO_LIST
: {
377 TRACE_(dmfile
)(": UNFO list\n");
379 IStream_Read (pStm
, &Chunk
, sizeof(FOURCC
)+sizeof(DWORD
), NULL
);
380 ListCount
[0] += sizeof(FOURCC
) + sizeof(DWORD
) + Chunk
.dwSize
;
381 TRACE_(dmfile
)(": %s chunk (size = %d)", debugstr_fourcc (Chunk
.fccID
), Chunk
.dwSize
);
383 hr
= IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk
, pStm
, &This
->dmobj
.desc
);
384 if (FAILED(hr
)) return hr
;
387 switch (Chunk
.fccID
) {
389 TRACE_(dmfile
)(": unknown chunk (irrelevant & skipping)\n");
390 liMove
.QuadPart
= Chunk
.dwSize
;
391 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
);
396 TRACE_(dmfile
)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount
[0], ListSize
[0]);
397 } while (ListCount
[0] < ListSize
[0]);
400 case DMUS_FOURCC_INSTRUMENTS_LIST
: {
401 TRACE_(dmfile
)(": INSTRUMENTS list\n");
402 hr
= parse_instruments_list(This
, &Chunk
, pStm
);
403 if (FAILED(hr
)) return hr
;
407 TRACE_(dmfile
)(": unknown (skipping)\n");
408 liMove
.QuadPart
= Chunk
.dwSize
- sizeof(FOURCC
);
409 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
);
416 TRACE_(dmfile
)(": unknown chunk (irrelevant & skipping)\n");
417 liMove
.QuadPart
= Chunk
.dwSize
;
418 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
);
423 TRACE_(dmfile
)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount
, StreamSize
);
424 } while (StreamCount
< StreamSize
);
429 static inline IDirectMusicBandImpl
*impl_from_IPersistStream(IPersistStream
*iface
)
431 return CONTAINING_RECORD(iface
, IDirectMusicBandImpl
, dmobj
.IPersistStream_iface
);
434 static HRESULT WINAPI
IPersistStreamImpl_Load(IPersistStream
*iface
, IStream
*pStm
)
436 IDirectMusicBandImpl
*This
= impl_from_IPersistStream(iface
);
437 DMUS_PRIVATE_CHUNK Chunk
;
438 LARGE_INTEGER liMove
;
441 TRACE("(%p,%p): loading\n", This
, pStm
);
443 IStream_Read (pStm
, &Chunk
, sizeof(FOURCC
)+sizeof(DWORD
), NULL
);
444 TRACE_(dmfile
)(": %s chunk (size = %d)", debugstr_fourcc (Chunk
.fccID
), Chunk
.dwSize
);
445 switch (Chunk
.fccID
) {
447 IStream_Read (pStm
, &Chunk
.fccID
, sizeof(FOURCC
), NULL
);
448 TRACE_(dmfile
)(": %s chunk (size = %d)", debugstr_fourcc (Chunk
.fccID
), Chunk
.dwSize
);
449 switch (Chunk
.fccID
) {
450 case DMUS_FOURCC_BAND_FORM
: {
451 TRACE_(dmfile
)(": Band form\n");
452 hr
= parse_band_form(This
, &Chunk
, pStm
);
453 if (FAILED(hr
)) return hr
;
457 TRACE_(dmfile
)(": unexpected chunk; loading failed)\n");
458 liMove
.QuadPart
= Chunk
.dwSize
;
459 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
);
463 TRACE_(dmfile
)(": reading finished\n");
467 TRACE_(dmfile
)(": unexpected chunk; loading failed)\n");
468 liMove
.QuadPart
= Chunk
.dwSize
;
469 IStream_Seek (pStm
, liMove
, STREAM_SEEK_CUR
, NULL
); /* skip the rest of the chunk */
477 static const IPersistStreamVtbl persiststream_vtbl
= {
478 dmobj_IPersistStream_QueryInterface
,
479 dmobj_IPersistStream_AddRef
,
480 dmobj_IPersistStream_Release
,
481 unimpl_IPersistStream_GetClassID
,
482 unimpl_IPersistStream_IsDirty
,
483 IPersistStreamImpl_Load
,
484 unimpl_IPersistStream_Save
,
485 unimpl_IPersistStream_GetSizeMax
488 /* for ClassFactory */
489 HRESULT WINAPI
create_dmband(REFIID lpcGUID
, void **ppobj
)
491 IDirectMusicBandImpl
* obj
;
494 obj
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirectMusicBandImpl
));
497 return E_OUTOFMEMORY
;
499 obj
->IDirectMusicBand_iface
.lpVtbl
= &dmband_vtbl
;
501 dmobject_init(&obj
->dmobj
, &CLSID_DirectMusicBand
, (IUnknown
*)&obj
->IDirectMusicBand_iface
);
502 obj
->dmobj
.IDirectMusicObject_iface
.lpVtbl
= &dmobject_vtbl
;
503 obj
->dmobj
.IPersistStream_iface
.lpVtbl
= &persiststream_vtbl
;
504 list_init (&obj
->Instruments
);
507 hr
= IDirectMusicBand_QueryInterface(&obj
->IDirectMusicBand_iface
, lpcGUID
, ppobj
);
508 IDirectMusicBand_Release(&obj
->IDirectMusicBand_iface
);