d3d11/tests: Add some tests for ID3DUserDefinedAnnotation.
[wine.git] / dlls / dmusic / instrument.c
blobbaa4a2f57152a29bcdf060e161d8e15a0ddc231b
1 /*
2 * IDirectMusicInstrument Implementation
4 * Copyright (C) 2003-2004 Rok Mandeljc
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 "dmusic_private.h"
22 #include "dmobject.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
26 static const GUID IID_IDirectMusicInstrumentPRIVATE = { 0xbcb20080, 0xa40c, 0x11d1, { 0x86, 0xbc, 0x00, 0xc0, 0x4f, 0xbf, 0x8f, 0xef } };
28 /* IDirectMusicInstrument IUnknown part: */
29 static HRESULT WINAPI IDirectMusicInstrumentImpl_QueryInterface(LPDIRECTMUSICINSTRUMENT iface, REFIID riid, LPVOID *ret_iface)
31 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
33 if (IsEqualIID(riid, &IID_IUnknown) ||
34 IsEqualIID(riid, &IID_IDirectMusicInstrument))
36 *ret_iface = iface;
37 IDirectMusicInstrument_AddRef(iface);
38 return S_OK;
40 else if (IsEqualIID(riid, &IID_IDirectMusicInstrumentPRIVATE))
42 /* it seems to me that this interface is only basic IUnknown, without any
43 * other inherited functions... *sigh* this is the worst scenario, since it means
44 * that whoever calls it knows the layout of original implementation table and therefore
45 * tries to get data by direct access... expect crashes
47 FIXME("*sigh*... requested private/unspecified interface\n");
49 *ret_iface = iface;
50 IDirectMusicInstrument_AddRef(iface);
51 return S_OK;
54 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
56 return E_NOINTERFACE;
59 static ULONG WINAPI IDirectMusicInstrumentImpl_AddRef(LPDIRECTMUSICINSTRUMENT iface)
61 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
62 ULONG ref = InterlockedIncrement(&This->ref);
64 TRACE("(%p)->(): new ref = %u\n", iface, ref);
66 return ref;
69 static ULONG WINAPI IDirectMusicInstrumentImpl_Release(LPDIRECTMUSICINSTRUMENT iface)
71 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
72 ULONG ref = InterlockedDecrement(&This->ref);
74 TRACE("(%p)->(): new ref = %u\n", iface, ref);
76 if (!ref)
78 ULONG i;
80 HeapFree(GetProcessHeap(), 0, This->regions);
81 for (i = 0; i < This->nb_articulations; i++)
82 HeapFree(GetProcessHeap(), 0, This->articulations->connections);
83 HeapFree(GetProcessHeap(), 0, This->articulations);
84 HeapFree(GetProcessHeap(), 0, This);
85 DMUSIC_UnlockModule();
88 return ref;
91 /* IDirectMusicInstrumentImpl IDirectMusicInstrument part: */
92 static HRESULT WINAPI IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD* pdwPatch)
94 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
96 TRACE("(%p)->(%p)\n", This, pdwPatch);
98 *pdwPatch = MIDILOCALE2Patch(&This->header.Locale);
100 return S_OK;
103 static HRESULT WINAPI IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD dwPatch)
105 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
107 TRACE("(%p)->(%d): stub\n", This, dwPatch);
109 Patch2MIDILOCALE(dwPatch, &This->header.Locale);
111 return S_OK;
114 static const IDirectMusicInstrumentVtbl DirectMusicInstrument_Vtbl =
116 IDirectMusicInstrumentImpl_QueryInterface,
117 IDirectMusicInstrumentImpl_AddRef,
118 IDirectMusicInstrumentImpl_Release,
119 IDirectMusicInstrumentImpl_GetPatch,
120 IDirectMusicInstrumentImpl_SetPatch
123 /* for ClassFactory */
124 HRESULT DMUSIC_CreateDirectMusicInstrumentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
125 IDirectMusicInstrumentImpl* dminst;
126 HRESULT hr;
128 dminst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicInstrumentImpl));
129 if (NULL == dminst) {
130 *ppobj = NULL;
131 return E_OUTOFMEMORY;
133 dminst->IDirectMusicInstrument_iface.lpVtbl = &DirectMusicInstrument_Vtbl;
134 dminst->ref = 1;
136 DMUSIC_LockModule();
137 hr = IDirectMusicInstrument_QueryInterface(&dminst->IDirectMusicInstrument_iface, lpcGUID,
138 ppobj);
139 IDirectMusicInstrument_Release(&dminst->IDirectMusicInstrument_iface);
141 return hr;
144 static HRESULT read_from_stream(IStream *stream, void *data, ULONG size)
146 ULONG bytes_read;
147 HRESULT hr;
149 hr = IStream_Read(stream, data, size, &bytes_read);
150 if(FAILED(hr)){
151 TRACE("IStream_Read failed: %08x\n", hr);
152 return hr;
154 if (bytes_read < size) {
155 TRACE("Didn't read full chunk: %u < %u\n", bytes_read, size);
156 return E_FAIL;
159 return S_OK;
162 static inline DWORD subtract_bytes(DWORD len, DWORD bytes)
164 if(bytes > len){
165 TRACE("Apparent mismatch in chunk lengths? %u bytes remaining, %u bytes read\n", len, bytes);
166 return 0;
168 return len - bytes;
171 static inline HRESULT advance_stream(IStream *stream, ULONG bytes)
173 LARGE_INTEGER move;
174 HRESULT ret;
176 move.QuadPart = bytes;
178 ret = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
179 if (FAILED(ret))
180 WARN("IStream_Seek failed: %08x\n", ret);
182 return ret;
185 static HRESULT load_region(IDirectMusicInstrumentImpl *This, IStream *stream, instrument_region *region, ULONG length)
187 HRESULT ret;
188 DMUS_PRIVATE_CHUNK chunk;
190 TRACE("(%p, %p, %p, %u)\n", This, stream, region, length);
192 while (length)
194 ret = read_from_stream(stream, &chunk, sizeof(chunk));
195 if (FAILED(ret))
196 return ret;
198 length = subtract_bytes(length, sizeof(chunk));
200 switch (chunk.fccID)
202 case FOURCC_RGNH:
203 TRACE("RGNH chunk (region header): %u bytes\n", chunk.dwSize);
205 ret = read_from_stream(stream, &region->header, sizeof(region->header));
206 if (FAILED(ret))
207 return ret;
209 length = subtract_bytes(length, sizeof(region->header));
210 break;
212 case FOURCC_WSMP:
213 TRACE("WSMP chunk (wave sample): %u bytes\n", chunk.dwSize);
215 ret = read_from_stream(stream, &region->wave_sample, sizeof(region->wave_sample));
216 if (FAILED(ret))
217 return ret;
218 length = subtract_bytes(length, sizeof(region->wave_sample));
220 if (!(region->loop_present = (chunk.dwSize != sizeof(region->wave_sample))))
221 break;
223 ret = read_from_stream(stream, &region->wave_loop, sizeof(region->wave_loop));
224 if (FAILED(ret))
225 return ret;
227 length = subtract_bytes(length, sizeof(region->wave_loop));
228 break;
230 case FOURCC_WLNK:
231 TRACE("WLNK chunk (wave link): %u bytes\n", chunk.dwSize);
233 ret = read_from_stream(stream, &region->wave_link, sizeof(region->wave_link));
234 if (FAILED(ret))
235 return ret;
237 length = subtract_bytes(length, sizeof(region->wave_link));
238 break;
240 default:
241 TRACE("Unknown chunk %s (skipping): %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
243 ret = advance_stream(stream, chunk.dwSize);
244 if (FAILED(ret))
245 return ret;
247 length = subtract_bytes(length, chunk.dwSize);
248 break;
252 return S_OK;
255 static HRESULT load_articulation(IDirectMusicInstrumentImpl *This, IStream *stream, ULONG length)
257 HRESULT ret;
258 instrument_articulation *articulation;
260 if (!This->articulations)
261 This->articulations = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->articulations));
262 else
263 This->articulations = HeapReAlloc(GetProcessHeap(), 0, This->articulations, sizeof(*This->articulations) * (This->nb_articulations + 1));
264 if (!This->articulations)
265 return E_OUTOFMEMORY;
267 articulation = &This->articulations[This->nb_articulations];
269 ret = read_from_stream(stream, &articulation->connections_list, sizeof(CONNECTIONLIST));
270 if (FAILED(ret))
271 return ret;
273 articulation->connections = HeapAlloc(GetProcessHeap(), 0, sizeof(CONNECTION) * articulation->connections_list.cConnections);
274 if (!articulation->connections)
275 return E_OUTOFMEMORY;
277 ret = read_from_stream(stream, articulation->connections, sizeof(CONNECTION) * articulation->connections_list.cConnections);
278 if (FAILED(ret))
280 HeapFree(GetProcessHeap(), 0, articulation->connections);
281 return ret;
284 subtract_bytes(length, sizeof(CONNECTIONLIST) + sizeof(CONNECTION) * articulation->connections_list.cConnections);
286 This->nb_articulations++;
288 return S_OK;
291 /* Function that loads all instrument data and which is called from IDirectMusicCollection_GetInstrument as in native */
292 HRESULT IDirectMusicInstrumentImpl_CustomLoad(IDirectMusicInstrument *iface, IStream *stream)
294 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
295 HRESULT hr;
296 DMUS_PRIVATE_CHUNK chunk;
297 ULONG i = 0;
298 ULONG length = This->length;
300 TRACE("(%p, %p): offset = 0x%s, length = %u)\n", This, stream, wine_dbgstr_longlong(This->liInstrumentPosition.QuadPart), This->length);
302 if (This->loaded)
303 return S_OK;
305 hr = IStream_Seek(stream, This->liInstrumentPosition, STREAM_SEEK_SET, NULL);
306 if (FAILED(hr))
308 WARN("IStream_Seek failed: %08x\n", hr);
309 return DMUS_E_UNSUPPORTED_STREAM;
312 This->regions = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->regions) * This->header.cRegions);
313 if (!This->regions)
314 return E_OUTOFMEMORY;
316 while (length)
318 hr = read_from_stream(stream, &chunk, sizeof(chunk));
319 if (FAILED(hr))
320 goto error;
322 length = subtract_bytes(length, sizeof(chunk) + chunk.dwSize);
324 switch (chunk.fccID)
326 case FOURCC_INSH:
327 case FOURCC_DLID:
328 TRACE("Chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
330 /* Instrument header and id are already set so just skip */
331 hr = advance_stream(stream, chunk.dwSize);
332 if (FAILED(hr))
333 goto error;
335 break;
337 case FOURCC_LIST: {
338 DWORD size = chunk.dwSize;
340 TRACE("LIST chunk: %u bytes\n", chunk.dwSize);
342 hr = read_from_stream(stream, &chunk.fccID, sizeof(chunk.fccID));
343 if (FAILED(hr))
344 goto error;
346 size = subtract_bytes(size, sizeof(chunk.fccID));
348 switch (chunk.fccID)
350 case FOURCC_LRGN:
351 TRACE("LRGN chunk (regions list): %u bytes\n", size);
353 while (size)
355 hr = read_from_stream(stream, &chunk, sizeof(chunk));
356 if (FAILED(hr))
357 goto error;
359 if (chunk.fccID != FOURCC_LIST)
361 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
362 goto error;
365 hr = read_from_stream(stream, &chunk.fccID, sizeof(chunk.fccID));
366 if (FAILED(hr))
367 goto error;
369 if (chunk.fccID == FOURCC_RGN)
371 TRACE("RGN chunk (region): %u bytes\n", chunk.dwSize);
372 hr = load_region(This, stream, &This->regions[i++], chunk.dwSize - sizeof(chunk.fccID));
374 else
376 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
377 hr = advance_stream(stream, chunk.dwSize - sizeof(chunk.fccID));
379 if (FAILED(hr))
380 goto error;
382 size = subtract_bytes(size, chunk.dwSize + sizeof(chunk));
384 break;
386 case FOURCC_LART:
387 TRACE("LART chunk (articulations list): %u bytes\n", size);
389 while (size)
391 hr = read_from_stream(stream, &chunk, sizeof(chunk));
392 if (FAILED(hr))
393 goto error;
395 if (chunk.fccID == FOURCC_ART1)
397 TRACE("ART1 chunk (level 1 articulation): %u bytes\n", chunk.dwSize);
398 hr = load_articulation(This, stream, chunk.dwSize);
400 else
402 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
403 hr = advance_stream(stream, chunk.dwSize);
405 if (FAILED(hr))
406 goto error;
408 size = subtract_bytes(size, chunk.dwSize + sizeof(chunk));
410 break;
412 default:
413 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
415 hr = advance_stream(stream, chunk.dwSize - sizeof(chunk.fccID));
416 if (FAILED(hr))
417 goto error;
419 size = subtract_bytes(size, chunk.dwSize - sizeof(chunk.fccID));
420 break;
422 break;
425 default:
426 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
428 hr = advance_stream(stream, chunk.dwSize);
429 if (FAILED(hr))
430 goto error;
432 break;
436 This->loaded = TRUE;
438 return S_OK;
440 error:
441 HeapFree(GetProcessHeap(), 0, This->regions);
442 This->regions = NULL;
444 return DMUS_E_UNSUPPORTED_STREAM;