wined3d: Use wined3d_bit_scan() in vshader_get_input().
[wine.git] / dlls / dmstyle / style.c
blobf8a62604a99b0a7d9c6b4dd5bc03532c37a8b9d0
1 /* IDirectMusicStyle8 Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
4 * Copyright (C) 2003-2004 Raphael Junqueira
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "dmstyle_private.h"
22 #include "dmobject.h"
23 #include "wine/heap.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(dmstyle);
26 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
28 struct style_band {
29 struct list entry;
30 IDirectMusicBand *pBand;
33 struct style_partref_item {
34 struct list entry;
35 DMUS_OBJECTDESC desc;
36 DMUS_IO_PARTREF part_ref;
39 struct style_motif {
40 struct list entry;
41 DWORD dwRhythm;
42 DMUS_IO_PATTERN pattern;
43 DMUS_OBJECTDESC desc;
44 /** optional for motifs */
45 DMUS_IO_MOTIFSETTINGS settings;
46 IDirectMusicBand *pBand;
47 struct list Items;
50 /*****************************************************************************
51 * IDirectMusicStyleImpl implementation
53 typedef struct IDirectMusicStyle8Impl {
54 IDirectMusicStyle8 IDirectMusicStyle8_iface;
55 struct dmobject dmobj;
56 LONG ref;
57 DMUS_IO_STYLE style;
58 struct list motifs;
59 struct list bands;
60 } IDirectMusicStyle8Impl;
62 static inline IDirectMusicStyle8Impl *impl_from_IDirectMusicStyle8(IDirectMusicStyle8 *iface)
64 return CONTAINING_RECORD(iface, IDirectMusicStyle8Impl, IDirectMusicStyle8_iface);
67 /* DirectMusicStyle8Impl IDirectMusicStyle8 part: */
68 static HRESULT WINAPI IDirectMusicStyle8Impl_QueryInterface(IDirectMusicStyle8 *iface, REFIID riid,
69 void **ret_iface)
71 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
73 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
75 *ret_iface = NULL;
77 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDirectMusicStyle) ||
78 IsEqualIID(riid, &IID_IDirectMusicStyle8))
79 *ret_iface = iface;
80 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
81 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
82 else if (IsEqualIID(riid, &IID_IPersistStream))
83 *ret_iface = &This->dmobj.IPersistStream_iface;
84 else {
85 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
86 return E_NOINTERFACE;
89 IUnknown_AddRef((IUnknown*)*ret_iface);
90 return S_OK;
93 static ULONG WINAPI IDirectMusicStyle8Impl_AddRef(IDirectMusicStyle8 *iface)
95 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
96 LONG ref = InterlockedIncrement(&This->ref);
98 TRACE("(%p) ref=%d\n", This, ref);
100 return ref;
103 static ULONG WINAPI IDirectMusicStyle8Impl_Release(IDirectMusicStyle8 *iface)
105 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
106 LONG ref = InterlockedDecrement(&This->ref);
108 TRACE("(%p) ref=%d\n", This, ref);
110 if (!ref) {
111 struct style_band *band, *band2;
112 struct style_motif *motif, *motif2;
113 struct style_partref_item *item, *item2;
115 LIST_FOR_EACH_ENTRY_SAFE(band, band2, &This->bands, struct style_band, entry) {
116 list_remove(&band->entry);
117 if (band->pBand)
118 IDirectMusicBand_Release(band->pBand);
119 heap_free(band);
121 LIST_FOR_EACH_ENTRY_SAFE(motif, motif2, &This->motifs, struct style_motif, entry) {
122 list_remove(&motif->entry);
123 LIST_FOR_EACH_ENTRY_SAFE(item, item2, &motif->Items, struct style_partref_item, entry) {
124 list_remove(&item->entry);
125 heap_free(item);
127 heap_free(motif);
129 heap_free(This);
130 DMSTYLE_UnlockModule();
133 return ref;
136 /* IDirectMusicStyle8Impl IDirectMusicStyle(8) part: */
137 static HRESULT WINAPI IDirectMusicStyle8Impl_GetBand(IDirectMusicStyle8 *iface, WCHAR *name,
138 IDirectMusicBand **band)
140 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
141 struct style_band *sband;
142 HRESULT hr;
144 TRACE("(%p, %s, %p)\n", This, debugstr_w(name), band);
146 if (!name)
147 return E_POINTER;
149 LIST_FOR_EACH_ENTRY(sband, &This->bands, struct style_band, entry) {
150 IDirectMusicObject *obj;
152 hr = IDirectMusicBand_QueryInterface(sband->pBand, &IID_IDirectMusicObject, (void**)&obj);
153 if (SUCCEEDED(hr)) {
154 DMUS_OBJECTDESC desc;
156 if (IDirectMusicObject_GetDescriptor(obj, &desc) == S_OK) {
157 if (desc.dwValidData & DMUS_OBJ_NAME && !lstrcmpW(name, desc.wszName)) {
158 IDirectMusicObject_Release(obj);
159 IDirectMusicBand_AddRef(sband->pBand);
160 *band = sband->pBand;
161 return S_OK;
165 IDirectMusicObject_Release(obj);
169 return S_FALSE;
172 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumBand(IDirectMusicStyle8 *iface, DWORD dwIndex,
173 WCHAR *pwszName)
175 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
176 FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
177 return S_OK;
180 static HRESULT WINAPI IDirectMusicStyle8Impl_GetDefaultBand(IDirectMusicStyle8 *iface,
181 IDirectMusicBand **band)
183 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
184 FIXME("(%p, %p): stub\n", This, band);
186 if (!band)
187 return E_POINTER;
189 *band = NULL;
191 return S_FALSE;
194 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumMotif(IDirectMusicStyle8 *iface, DWORD index,
195 WCHAR *name)
197 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
198 const struct style_motif *motif = NULL;
199 const struct list *cursor;
200 unsigned int i = 0;
202 TRACE("(%p, %u, %p)\n", This, index, name);
204 if (!name)
205 return E_POINTER;
207 /* index is zero based */
208 LIST_FOR_EACH(cursor, &This->motifs) {
209 if (i == index) {
210 motif = LIST_ENTRY(cursor, struct style_motif, entry);
211 break;
213 i++;
215 if (!motif)
216 return S_FALSE;
218 if (motif->desc.dwValidData & DMUS_OBJ_NAME)
219 lstrcpynW(name, motif->desc.wszName, DMUS_MAX_NAME);
220 else
221 name[0] = 0;
223 TRACE("returning name: %s\n", debugstr_w(name));
224 return S_OK;
227 static HRESULT WINAPI IDirectMusicStyle8Impl_GetMotif(IDirectMusicStyle8 *iface, WCHAR *pwszName,
228 IDirectMusicSegment **ppSegment)
230 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
231 FIXME("(%p, %s, %p): stub\n", This, debugstr_w(pwszName), ppSegment);
232 return S_FALSE;
235 static HRESULT WINAPI IDirectMusicStyle8Impl_GetDefaultChordMap(IDirectMusicStyle8 *iface,
236 IDirectMusicChordMap **ppChordMap)
238 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
239 FIXME("(%p, %p): stub\n", This, ppChordMap);
240 return S_OK;
243 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumChordMap(IDirectMusicStyle8 *iface, DWORD dwIndex,
244 WCHAR *pwszName)
246 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
247 FIXME("(%p, %d, %p): stub\n", This, dwIndex, pwszName);
248 return S_OK;
251 static HRESULT WINAPI IDirectMusicStyle8Impl_GetChordMap(IDirectMusicStyle8 *iface, WCHAR *pwszName,
252 IDirectMusicChordMap **ppChordMap)
254 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
255 FIXME("(%p, %p, %p): stub\n", This, pwszName, ppChordMap);
256 return S_OK;
259 static HRESULT WINAPI IDirectMusicStyle8Impl_GetTimeSignature(IDirectMusicStyle8 *iface,
260 DMUS_TIMESIGNATURE *pTimeSig)
262 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
263 FIXME("(%p, %p): stub\n", This, pTimeSig);
264 return S_OK;
267 static HRESULT WINAPI IDirectMusicStyle8Impl_GetEmbellishmentLength(IDirectMusicStyle8 *iface,
268 DWORD dwType, DWORD dwLevel, DWORD *pdwMin, DWORD *pdwMax)
270 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
271 FIXME("(%p, %d, %d, %p, %p): stub\n", This, dwType, dwLevel, pdwMin, pdwMax);
272 return S_OK;
275 static HRESULT WINAPI IDirectMusicStyle8Impl_GetTempo(IDirectMusicStyle8 *iface, double *pTempo)
277 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
278 FIXME("(%p, %p): stub\n", This, pTempo);
279 return S_OK;
282 static HRESULT WINAPI IDirectMusicStyle8Impl_EnumPattern(IDirectMusicStyle8 *iface, DWORD dwIndex,
283 DWORD dwPatternType, WCHAR *pwszName)
285 IDirectMusicStyle8Impl *This = impl_from_IDirectMusicStyle8(iface);
286 FIXME("(%p, %d, %d, %p): stub\n", This, dwIndex, dwPatternType, pwszName);
287 return S_OK;
290 static const IDirectMusicStyle8Vtbl dmstyle8_vtbl = {
291 IDirectMusicStyle8Impl_QueryInterface,
292 IDirectMusicStyle8Impl_AddRef,
293 IDirectMusicStyle8Impl_Release,
294 IDirectMusicStyle8Impl_GetBand,
295 IDirectMusicStyle8Impl_EnumBand,
296 IDirectMusicStyle8Impl_GetDefaultBand,
297 IDirectMusicStyle8Impl_EnumMotif,
298 IDirectMusicStyle8Impl_GetMotif,
299 IDirectMusicStyle8Impl_GetDefaultChordMap,
300 IDirectMusicStyle8Impl_EnumChordMap,
301 IDirectMusicStyle8Impl_GetChordMap,
302 IDirectMusicStyle8Impl_GetTimeSignature,
303 IDirectMusicStyle8Impl_GetEmbellishmentLength,
304 IDirectMusicStyle8Impl_GetTempo,
305 IDirectMusicStyle8Impl_EnumPattern
308 /* IDirectMusicStyle8Impl IDirectMusicObject part: */
309 static HRESULT WINAPI style_IDirectMusicObject_ParseDescriptor(IDirectMusicObject *iface,
310 IStream *stream, DMUS_OBJECTDESC *desc)
312 struct chunk_entry riff = {0};
313 HRESULT hr;
315 TRACE("(%p, %p, %p)\n", iface, stream, desc);
317 if (!stream || !desc)
318 return E_POINTER;
320 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
321 return hr;
322 if (riff.id != FOURCC_RIFF || riff.type != DMUS_FOURCC_STYLE_FORM) {
323 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
324 stream_skip_chunk(stream, &riff);
325 return DMUS_E_CHUNKNOTFOUND;
328 hr = dmobj_parsedescriptor(stream, &riff, desc,
329 DMUS_OBJ_OBJECT|DMUS_OBJ_NAME|DMUS_OBJ_NAME_INAM|DMUS_OBJ_VERSION);
330 if (FAILED(hr))
331 return hr;
333 desc->guidClass = CLSID_DirectMusicStyle;
334 desc->dwValidData |= DMUS_OBJ_CLASS;
336 dump_DMUS_OBJECTDESC(desc);
337 return S_OK;
340 static const IDirectMusicObjectVtbl dmobject_vtbl = {
341 dmobj_IDirectMusicObject_QueryInterface,
342 dmobj_IDirectMusicObject_AddRef,
343 dmobj_IDirectMusicObject_Release,
344 dmobj_IDirectMusicObject_GetDescriptor,
345 dmobj_IDirectMusicObject_SetDescriptor,
346 style_IDirectMusicObject_ParseDescriptor
349 /* IDirectMusicStyle8Impl IPersistStream part: */
350 static inline IDirectMusicStyle8Impl *impl_from_IPersistStream(IPersistStream *iface)
352 return CONTAINING_RECORD(iface, IDirectMusicStyle8Impl, dmobj.IPersistStream_iface);
355 static HRESULT load_band(IStream *pClonedStream, IDirectMusicBand **ppBand)
357 HRESULT hr = E_FAIL;
358 IPersistStream* pPersistStream = NULL;
360 hr = CoCreateInstance (&CLSID_DirectMusicBand, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicBand, (LPVOID*) ppBand);
361 if (FAILED(hr)) {
362 ERR(": could not create object\n");
363 return hr;
365 /* acquire PersistStream interface */
366 hr = IDirectMusicBand_QueryInterface (*ppBand, &IID_IPersistStream, (LPVOID*) &pPersistStream);
367 if (FAILED(hr)) {
368 ERR(": could not acquire IPersistStream\n");
369 return hr;
371 /* load */
372 hr = IPersistStream_Load (pPersistStream, pClonedStream);
373 if (FAILED(hr)) {
374 ERR(": failed to load object\n");
375 return hr;
378 /* release all loading-related stuff */
379 IPersistStream_Release (pPersistStream);
381 return S_OK;
384 static HRESULT parse_part_ref_list(DMUS_PRIVATE_CHUNK *pChunk, IStream *pStm,
385 struct style_motif *pNewMotif)
387 HRESULT hr = E_FAIL;
388 DMUS_PRIVATE_CHUNK Chunk;
389 DWORD ListSize[3], ListCount[3];
390 LARGE_INTEGER liMove; /* used when skipping chunks */
391 struct style_partref_item *pNewItem = NULL;
394 if (pChunk->fccID != DMUS_FOURCC_PARTREF_LIST) {
395 ERR_(dmfile)(": %s chunk should be a PARTREF list\n", debugstr_fourcc (pChunk->fccID));
396 return E_FAIL;
399 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
400 ListCount[0] = 0;
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 = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
406 switch (Chunk.fccID) {
407 case DMUS_FOURCC_PARTREF_CHUNK: {
408 TRACE_(dmfile)(": PartRef chunk\n");
409 pNewItem = heap_alloc_zero(sizeof(*pNewItem));
410 if (!pNewItem) {
411 ERR(": no more memory\n");
412 return E_OUTOFMEMORY;
414 hr = IStream_Read (pStm, &pNewItem->part_ref, sizeof(DMUS_IO_PARTREF), NULL);
415 /*TRACE_(dmfile)(" - sizeof %lu\n", sizeof(DMUS_IO_PARTREF));*/
416 list_add_tail (&pNewMotif->Items, &pNewItem->entry);
417 pNewItem->desc.dwSize = sizeof(pNewItem->desc);
418 break;
420 case FOURCC_LIST: {
421 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
422 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
423 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
424 ListCount[1] = 0;
425 switch (Chunk.fccID) {
426 case DMUS_FOURCC_UNFO_LIST: {
427 TRACE_(dmfile)(": UNFO list\n");
428 do {
429 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
430 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
431 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
433 if (!pNewItem) {
434 ERR(": pNewItem not yet allocated, chunk order bad?\n");
435 return E_OUTOFMEMORY;
437 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewItem->desc);
438 if (FAILED(hr)) return hr;
440 if (hr == S_FALSE) {
441 switch (Chunk.fccID) {
442 default: {
443 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
444 liMove.QuadPart = Chunk.dwSize;
445 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
446 break;
450 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
451 } while (ListCount[1] < ListSize[1]);
452 break;
454 default: {
455 TRACE_(dmfile)(": unknown chunk (skipping)\n");
456 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
457 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
458 break;
461 break;
463 default: {
464 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
465 liMove.QuadPart = Chunk.dwSize;
466 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
467 break;
470 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
471 } while (ListCount[0] < ListSize[0]);
473 return S_OK;
476 static HRESULT parse_part_list(DMUS_PRIVATE_CHUNK *pChunk, IStream *pStm)
478 HRESULT hr = E_FAIL;
479 DMUS_PRIVATE_CHUNK Chunk;
480 DWORD ListSize[3], ListCount[3];
481 LARGE_INTEGER liMove; /* used when skipping chunks */
483 DMUS_OBJECTDESC desc;
484 DWORD dwSize = 0;
485 DWORD cnt = 0;
487 if (pChunk->fccID != DMUS_FOURCC_PART_LIST) {
488 ERR_(dmfile)(": %s chunk should be a PART list\n", debugstr_fourcc (pChunk->fccID));
489 return E_FAIL;
492 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
493 ListCount[0] = 0;
495 do {
496 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
497 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
498 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
499 switch (Chunk.fccID) {
500 case DMUS_FOURCC_PART_CHUNK: {
501 TRACE_(dmfile)(": Part chunk (skipping for now)\n" );
502 liMove.QuadPart = Chunk.dwSize;
503 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
504 break;
506 case DMUS_FOURCC_NOTE_CHUNK: {
507 TRACE_(dmfile)(": Note chunk (skipping for now)\n");
508 IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
509 cnt = (Chunk.dwSize - sizeof(DWORD));
510 TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
511 TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
512 if (cnt % dwSize != 0) {
513 ERR("Invalid Array Size\n");
514 return E_FAIL;
516 cnt /= dwSize;
517 /** skip for now */
518 liMove.QuadPart = cnt * dwSize;
519 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
520 break;
522 case DMUS_FOURCC_CURVE_CHUNK: {
523 TRACE_(dmfile)(": Curve chunk (skipping for now)\n");
524 IStream_Read (pStm, &dwSize, sizeof(DWORD), NULL);
525 cnt = (Chunk.dwSize - sizeof(DWORD));
526 TRACE_(dmfile)(" - dwSize: %u\n", dwSize);
527 TRACE_(dmfile)(" - cnt: %u (%u / %u)\n", cnt / dwSize, (DWORD)(Chunk.dwSize - sizeof(DWORD)), dwSize);
528 if (cnt % dwSize != 0) {
529 ERR("Invalid Array Size\n");
530 return E_FAIL;
532 cnt /= dwSize;
533 /** skip for now */
534 liMove.QuadPart = cnt * dwSize;
535 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
536 break;
538 case DMUS_FOURCC_MARKER_CHUNK: {
539 TRACE_(dmfile)(": Marker chunk (skipping for now)\n");
540 liMove.QuadPart = Chunk.dwSize;
541 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
542 break;
544 case DMUS_FOURCC_RESOLUTION_CHUNK: {
545 TRACE_(dmfile)(": Resolution chunk (skipping for now)\n");
546 liMove.QuadPart = Chunk.dwSize;
547 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
548 break;
550 case DMUS_FOURCC_ANTICIPATION_CHUNK: {
551 TRACE_(dmfile)(": Anticipation chunk (skipping for now)\n");
552 liMove.QuadPart = Chunk.dwSize;
553 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
554 break;
556 case FOURCC_LIST: {
557 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
558 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
559 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
560 ListCount[1] = 0;
561 switch (Chunk.fccID) {
562 case DMUS_FOURCC_UNFO_LIST: {
563 TRACE_(dmfile)(": UNFO list\n");
564 do {
565 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
566 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
567 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
569 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &desc);
570 if (FAILED(hr)) return hr;
572 if (hr == S_FALSE) {
573 switch (Chunk.fccID) {
574 default: {
575 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
576 liMove.QuadPart = Chunk.dwSize;
577 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
578 break;
582 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
583 } while (ListCount[1] < ListSize[1]);
584 break;
586 default: {
587 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
588 liMove.QuadPart = Chunk.dwSize;
589 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
590 break;
593 break;
595 default: {
596 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
597 liMove.QuadPart = Chunk.dwSize;
598 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
599 break;
602 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
603 } while (ListCount[0] < ListSize[0]);
605 return S_OK;
608 static HRESULT parse_pattern_list(IDirectMusicStyle8Impl *This, DMUS_PRIVATE_CHUNK *pChunk,
609 IStream *pStm)
611 HRESULT hr = E_FAIL;
612 DMUS_PRIVATE_CHUNK Chunk;
613 DWORD ListSize[3], ListCount[3];
614 LARGE_INTEGER liMove; /* used when skipping chunks */
615 IDirectMusicBand* pBand = NULL;
616 struct style_motif *pNewMotif = NULL;
618 if (pChunk->fccID != DMUS_FOURCC_PATTERN_LIST) {
619 ERR_(dmfile)(": %s chunk should be a PATTERN list\n", debugstr_fourcc (pChunk->fccID));
620 return E_FAIL;
623 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
624 ListCount[0] = 0;
626 do {
627 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
628 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
629 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
630 switch (Chunk.fccID) {
631 case DMUS_FOURCC_PATTERN_CHUNK: {
632 TRACE_(dmfile)(": Pattern chunk\n");
633 /** alloc new motif entry */
634 pNewMotif = heap_alloc_zero(sizeof(*pNewMotif));
635 if (NULL == pNewMotif) {
636 ERR(": no more memory\n");
637 return E_OUTOFMEMORY;
639 list_add_tail(&This->motifs, &pNewMotif->entry);
641 IStream_Read (pStm, &pNewMotif->pattern, Chunk.dwSize, NULL);
642 /** TODO trace pattern */
644 /** reset all data, as a new pattern begin */
645 pNewMotif->desc.dwSize = sizeof(pNewMotif->desc);
646 list_init (&pNewMotif->Items);
647 break;
649 case DMUS_FOURCC_RHYTHM_CHUNK: {
650 TRACE_(dmfile)(": Rhythm chunk\n");
651 IStream_Read (pStm, &pNewMotif->dwRhythm, sizeof(DWORD), NULL);
652 TRACE_(dmfile)(" - dwRhythm: %u\n", pNewMotif->dwRhythm);
653 /** TODO understand why some Chunks have size > 4 */
654 liMove.QuadPart = Chunk.dwSize - sizeof(DWORD);
655 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
656 break;
658 case DMUS_FOURCC_MOTIFSETTINGS_CHUNK: {
659 TRACE_(dmfile)(": MotifSettings chunk (skipping for now)\n");
660 IStream_Read (pStm, &pNewMotif->settings, Chunk.dwSize, NULL);
661 /** TODO trace settings */
662 break;
664 case FOURCC_RIFF: {
666 * should be embedded Bands into pattern
668 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
669 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
670 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
671 ListCount[1] = 0;
672 switch (Chunk.fccID) {
673 case DMUS_FOURCC_BAND_FORM: {
674 LPSTREAM pClonedStream = NULL;
676 TRACE_(dmfile)(": BAND RIFF\n");
678 IStream_Clone (pStm, &pClonedStream);
680 liMove.QuadPart = 0;
681 liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
682 IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
684 hr = load_band(pClonedStream, &pBand);
685 if (FAILED(hr)) {
686 ERR(": could not load track\n");
687 return hr;
689 IStream_Release (pClonedStream);
691 pNewMotif->pBand = pBand;
692 IDirectMusicBand_AddRef(pBand);
694 IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release it as it's inserted */
696 /** now safe move the cursor */
697 liMove.QuadPart = ListSize[1];
698 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
700 break;
702 default: {
703 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
704 liMove.QuadPart = ListSize[1];
705 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
706 break;
709 break;
711 case FOURCC_LIST: {
712 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
713 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
714 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
715 ListCount[1] = 0;
716 switch (Chunk.fccID) {
717 case DMUS_FOURCC_UNFO_LIST: {
718 TRACE_(dmfile)(": UNFO list\n");
719 do {
720 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
721 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
722 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
724 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &pNewMotif->desc);
725 if (FAILED(hr)) return hr;
727 if (hr == S_FALSE) {
728 switch (Chunk.fccID) {
729 default: {
730 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
731 liMove.QuadPart = Chunk.dwSize;
732 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
733 break;
737 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
738 } while (ListCount[1] < ListSize[1]);
739 break;
741 case DMUS_FOURCC_PARTREF_LIST: {
742 TRACE_(dmfile)(": PartRef list\n");
743 hr = parse_part_ref_list(&Chunk, pStm, pNewMotif);
744 if (FAILED(hr)) return hr;
745 break;
747 default: {
748 TRACE_(dmfile)(": unknown (skipping)\n");
749 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
750 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
751 break;
754 break;
756 default: {
757 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
758 liMove.QuadPart = Chunk.dwSize;
759 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
760 break;
763 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
764 } while (ListCount[0] < ListSize[0]);
766 return S_OK;
769 static HRESULT parse_style_form(IDirectMusicStyle8Impl *This, DMUS_PRIVATE_CHUNK *pChunk,
770 IStream *pStm)
772 HRESULT hr = E_FAIL;
773 DMUS_PRIVATE_CHUNK Chunk;
774 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
775 LARGE_INTEGER liMove; /* used when skipping chunks */
777 IDirectMusicBand* pBand = NULL;
779 if (pChunk->fccID != DMUS_FOURCC_STYLE_FORM) {
780 ERR_(dmfile)(": %s chunk should be a STYLE form\n", debugstr_fourcc (pChunk->fccID));
781 return E_FAIL;
784 StreamSize = pChunk->dwSize - sizeof(FOURCC);
785 StreamCount = 0;
787 do {
788 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
789 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
790 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
792 hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, &This->dmobj.desc);
793 if (FAILED(hr)) return hr;
795 if (hr == S_FALSE) {
796 switch (Chunk.fccID) {
797 case DMUS_FOURCC_STYLE_CHUNK: {
798 TRACE_(dmfile)(": Style chunk\n");
799 IStream_Read (pStm, &This->style, sizeof(DMUS_IO_STYLE), NULL);
800 /** TODO dump DMUS_IO_TIMESIG style.timeSig */
801 TRACE_(dmfile)(" - dblTempo: %g\n", This->style.dblTempo);
802 break;
804 case FOURCC_RIFF: {
806 * should be embedded Bands into style
808 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
809 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
810 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
811 ListCount[0] = 0;
812 switch (Chunk.fccID) {
813 case DMUS_FOURCC_BAND_FORM: {
814 ULARGE_INTEGER save;
815 struct style_band *pNewBand;
817 TRACE_(dmfile)(": BAND RIFF\n");
819 /* Can be application provided IStream without Clone method */
820 liMove.QuadPart = 0;
821 liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
822 IStream_Seek(pStm, liMove, STREAM_SEEK_CUR, &save);
824 hr = load_band(pStm, &pBand);
825 if (FAILED(hr)) {
826 ERR(": could not load track\n");
827 return hr;
830 pNewBand = heap_alloc_zero(sizeof(*pNewBand));
831 if (NULL == pNewBand) {
832 ERR(": no more memory\n");
833 return E_OUTOFMEMORY;
835 pNewBand->pBand = pBand;
836 IDirectMusicBand_AddRef(pBand);
837 list_add_tail(&This->bands, &pNewBand->entry);
839 IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release it as it's inserted */
841 /** now safely move the cursor */
842 liMove.QuadPart = save.QuadPart - liMove.QuadPart + ListSize[0];
843 IStream_Seek(pStm, liMove, STREAM_SEEK_SET, NULL);
845 break;
847 default: {
848 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
849 liMove.QuadPart = ListSize[0];
850 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
851 break;
854 break;
856 case FOURCC_LIST: {
857 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
858 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
859 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
860 ListCount[0] = 0;
861 switch (Chunk.fccID) {
862 case DMUS_FOURCC_UNFO_LIST: {
863 TRACE_(dmfile)(": UNFO list\n");
864 do {
865 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
866 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
867 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
869 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, &This->dmobj.desc);
870 if (FAILED(hr)) return hr;
872 if (hr == S_FALSE) {
873 switch (Chunk.fccID) {
874 default: {
875 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
876 liMove.QuadPart = Chunk.dwSize;
877 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
878 break;
882 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
883 } while (ListCount[0] < ListSize[0]);
884 break;
886 case DMUS_FOURCC_PART_LIST: {
887 TRACE_(dmfile)(": PART list\n");
888 hr = parse_part_list(&Chunk, pStm);
889 if (FAILED(hr)) return hr;
890 break;
892 case DMUS_FOURCC_PATTERN_LIST: {
893 TRACE_(dmfile)(": PATTERN list\n");
894 hr = parse_pattern_list(This, &Chunk, pStm);
895 if (FAILED(hr)) return hr;
896 break;
898 default: {
899 TRACE_(dmfile)(": unknown (skipping)\n");
900 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
901 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
902 break;
905 break;
907 default: {
908 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
909 liMove.QuadPart = Chunk.dwSize;
910 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
911 break;
915 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
916 } while (StreamCount < StreamSize);
918 return S_OK;
921 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
923 IDirectMusicStyle8Impl *This = impl_from_IPersistStream(iface);
924 DMUS_PRIVATE_CHUNK Chunk;
925 LARGE_INTEGER liMove; /* used when skipping chunks */
926 HRESULT hr;
928 FIXME("(%p, %p): Loading\n", This, pStm);
930 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
931 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
932 switch (Chunk.fccID) {
933 case FOURCC_RIFF: {
934 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
935 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
936 switch (Chunk.fccID) {
937 case DMUS_FOURCC_STYLE_FORM: {
938 TRACE_(dmfile)(": Style form\n");
939 hr = parse_style_form(This, &Chunk, pStm);
940 if (FAILED(hr)) return hr;
941 break;
943 default: {
944 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
945 liMove.QuadPart = Chunk.dwSize;
946 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
947 return E_FAIL;
950 TRACE_(dmfile)(": reading finished\n");
951 break;
953 default: {
954 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
955 liMove.QuadPart = Chunk.dwSize;
956 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
957 return E_FAIL;
961 return S_OK;
964 static const IPersistStreamVtbl persiststream_vtbl = {
965 dmobj_IPersistStream_QueryInterface,
966 dmobj_IPersistStream_AddRef,
967 dmobj_IPersistStream_Release,
968 dmobj_IPersistStream_GetClassID,
969 unimpl_IPersistStream_IsDirty,
970 IPersistStreamImpl_Load,
971 unimpl_IPersistStream_Save,
972 unimpl_IPersistStream_GetSizeMax
975 /* for ClassFactory */
976 HRESULT WINAPI create_dmstyle(REFIID lpcGUID, void **ppobj)
978 IDirectMusicStyle8Impl* obj;
979 HRESULT hr;
981 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicStyle8Impl));
982 if (NULL == obj) {
983 *ppobj = NULL;
984 return E_OUTOFMEMORY;
986 obj->IDirectMusicStyle8_iface.lpVtbl = &dmstyle8_vtbl;
987 obj->ref = 1;
988 dmobject_init(&obj->dmobj, &CLSID_DirectMusicStyle, (IUnknown *)&obj->IDirectMusicStyle8_iface);
989 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
990 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
991 list_init(&obj->bands);
992 list_init(&obj->motifs);
994 DMSTYLE_LockModule();
995 hr = IDirectMusicStyle8_QueryInterface(&obj->IDirectMusicStyle8_iface, lpcGUID, ppobj);
996 IDirectMusicStyle8_Release(&obj->IDirectMusicStyle8_iface);
998 return hr;