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"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
25 static const GUID IID_IDirectMusicInstrumentPRIVATE = { 0xbcb20080, 0xa40c, 0x11d1, { 0x86, 0xbc, 0x00, 0xc0, 0x4f, 0xbf, 0x8f, 0xef } };
27 /* IDirectMusicInstrument IUnknown part: */
28 static HRESULT WINAPI IDirectMusicInstrumentImpl_QueryInterface(LPDIRECTMUSICINSTRUMENT iface, REFIID riid, LPVOID *ret_iface)
30 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
32 if (IsEqualIID(riid, &IID_IUnknown) ||
33 IsEqualIID(riid, &IID_IDirectMusicInstrument))
36 IDirectMusicInstrument_AddRef(iface);
39 else if (IsEqualIID(riid, &IID_IDirectMusicInstrumentPRIVATE))
41 /* it seems to me that this interface is only basic IUnknown, without any
42 * other inherited functions... *sigh* this is the worst scenario, since it means
43 * that whoever calls it knows the layout of original implementation table and therefore
44 * tries to get data by direct access... expect crashes
46 FIXME("*sigh*... requested private/unspecified interface\n");
49 IDirectMusicInstrument_AddRef(iface);
53 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
58 static ULONG WINAPI IDirectMusicInstrumentImpl_AddRef(LPDIRECTMUSICINSTRUMENT iface)
60 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
61 ULONG refCount = InterlockedIncrement(&This->ref);
63 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
70 static ULONG WINAPI IDirectMusicInstrumentImpl_Release(LPDIRECTMUSICINSTRUMENT iface)
72 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
73 ULONG refCount = InterlockedDecrement(&This->ref);
75 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
78 HeapFree(GetProcessHeap(), 0, This);
81 DMUSIC_UnlockModule();
86 /* IDirectMusicInstrumentImpl IDirectMusicInstrument part: */
87 static HRESULT WINAPI IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD* pdwPatch)
89 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
91 TRACE("(%p)->(%p)\n", This, pdwPatch);
93 *pdwPatch = MIDILOCALE2Patch(&This->pHeader->Locale);
98 static HRESULT WINAPI IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD dwPatch)
100 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
102 TRACE("(%p)->(%d): stub\n", This, dwPatch);
104 Patch2MIDILOCALE(dwPatch, &This->pHeader->Locale);
109 static const IDirectMusicInstrumentVtbl DirectMusicInstrument_Vtbl =
111 IDirectMusicInstrumentImpl_QueryInterface,
112 IDirectMusicInstrumentImpl_AddRef,
113 IDirectMusicInstrumentImpl_Release,
114 IDirectMusicInstrumentImpl_GetPatch,
115 IDirectMusicInstrumentImpl_SetPatch
118 /* for ClassFactory */
119 HRESULT DMUSIC_CreateDirectMusicInstrumentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
120 IDirectMusicInstrumentImpl* dminst;
122 dminst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicInstrumentImpl));
123 if (NULL == dminst) {
125 return E_OUTOFMEMORY;
127 dminst->IDirectMusicInstrument_iface.lpVtbl = &DirectMusicInstrument_Vtbl;
128 dminst->ref = 0; /* will be inited by QueryInterface */
130 return IDirectMusicInstrument_QueryInterface(&dminst->IDirectMusicInstrument_iface, lpcGUID, ppobj);
133 static HRESULT read_from_stream(IStream *stream, void *data, ULONG size)
138 hr = IStream_Read(stream, data, size, &bytes_read);
140 TRACE("IStream_Read failed: %08x\n", hr);
143 if (bytes_read < size) {
144 TRACE("Didn't read full chunk: %u < %u\n", bytes_read, size);
151 static inline DWORD subtract_bytes(DWORD len, DWORD bytes)
154 TRACE("Apparent mismatch in chunk lengths? %u bytes remaining, %u bytes read\n", len, bytes);
160 static HRESULT load_instrument(IDirectMusicInstrumentImpl *This, IStream *stream, DWORD length)
168 hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
172 hr = read_from_stream(stream, &bytes, sizeof(bytes));
176 length = subtract_bytes(length, sizeof(fourcc) + sizeof(bytes));
180 TRACE("INSH chunk: %u bytes\n", bytes);
181 hr = read_from_stream(stream, This->pHeader, sizeof(*This->pHeader));
185 move.QuadPart = bytes - sizeof(*This->pHeader);
186 hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
188 WARN("IStream_Seek failed: %08x\n", hr);
192 length = subtract_bytes(length, bytes);
196 TRACE("DLID chunk: %u bytes\n", bytes);
197 hr = read_from_stream(stream, This->pInstrumentID, sizeof(*This->pInstrumentID));
201 move.QuadPart = bytes - sizeof(*This->pInstrumentID);
202 hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
204 WARN("IStream_Seek failed: %08x\n", hr);
208 length = subtract_bytes(length, bytes);
212 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(fourcc), bytes);
214 move.QuadPart = bytes;
215 hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
217 WARN("IStream_Seek failed: %08x\n", hr);
221 length = subtract_bytes(length, bytes);
229 /* aux. function that completely loads instrument; my tests indicate that it's
230 called somewhere around IDirectMusicCollection_GetInstrument */
231 HRESULT IDirectMusicInstrumentImpl_Custom_Load(LPDIRECTMUSICINSTRUMENT iface, LPSTREAM stream)
233 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
239 TRACE("(%p, %p, offset = %s)\n", This, stream, wine_dbgstr_longlong(This->liInstrumentPosition.QuadPart));
241 hr = IStream_Seek(stream, This->liInstrumentPosition, STREAM_SEEK_SET, NULL);
243 WARN("IStream_Seek failed: %08x\n", hr);
247 hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
251 if(fourcc != FOURCC_LIST){
252 WARN("Loading failed: Expected LIST chunk, got: %s\n", debugstr_fourcc(fourcc));
256 hr = read_from_stream(stream, &bytes, sizeof(bytes));
260 TRACE("LIST chunk: %u bytes\n", bytes);
262 hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
268 TRACE("INS chunk: (no byte count)\n");
269 hr = load_instrument(This, stream, bytes - sizeof(FOURCC));
275 hr = read_from_stream(stream, &bytes, sizeof(bytes));
279 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(fourcc), bytes);
281 move.QuadPart = bytes;
282 hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
284 WARN("IStream_Seek failed: %08x\n", hr);
295 return DMUS_E_UNSUPPORTED_STREAM;