dmusic: Variable spelling fix.
[wine] / dlls / dmusic / instrument.c
1 /*
2  * IDirectMusicInstrument Implementation
3  *
4  * Copyright (C) 2003-2004 Rok Mandeljc
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "dmusic_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
24
25 static const GUID IID_IDirectMusicInstrumentPRIVATE = { 0xbcb20080, 0xa40c, 0x11d1, { 0x86, 0xbc, 0x00, 0xc0, 0x4f, 0xbf, 0x8f, 0xef } };
26
27 /* IDirectMusicInstrument IUnknown part: */
28 static HRESULT WINAPI IDirectMusicInstrumentImpl_QueryInterface(LPDIRECTMUSICINSTRUMENT iface, REFIID riid, LPVOID *ret_iface)
29 {
30     TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
31
32     if (IsEqualIID(riid, &IID_IUnknown) ||
33         IsEqualIID(riid, &IID_IDirectMusicInstrument))
34     {
35         *ret_iface = iface;
36         IDirectMusicInstrument_AddRef(iface);
37         return S_OK;
38     }
39     else if (IsEqualIID(riid, &IID_IDirectMusicInstrumentPRIVATE))
40     {
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
45          */
46         FIXME("*sigh*... requested private/unspecified interface\n");
47
48         *ret_iface = iface;
49         IDirectMusicInstrument_AddRef(iface);
50         return S_OK;
51     }
52
53     WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
54
55     return E_NOINTERFACE;
56 }
57
58 static ULONG WINAPI IDirectMusicInstrumentImpl_AddRef(LPDIRECTMUSICINSTRUMENT iface)
59 {
60         IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
61         ULONG refCount = InterlockedIncrement(&This->ref);
62
63         TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
64
65         DMUSIC_LockModule();
66
67         return refCount;
68 }
69
70 static ULONG WINAPI IDirectMusicInstrumentImpl_Release(LPDIRECTMUSICINSTRUMENT iface)
71 {
72         IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
73         ULONG refCount = InterlockedDecrement(&This->ref);
74
75         TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
76
77         if (!refCount) {
78                 HeapFree(GetProcessHeap(), 0, This);
79         }
80
81         DMUSIC_UnlockModule();
82         
83         return refCount;
84 }
85
86 /* IDirectMusicInstrumentImpl IDirectMusicInstrument part: */
87 static HRESULT WINAPI IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD* pdwPatch)
88 {
89     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
90
91     TRACE("(%p)->(%p)\n", This, pdwPatch);
92
93     *pdwPatch = MIDILOCALE2Patch(&This->pHeader->Locale);
94
95     return S_OK;
96 }
97
98 static HRESULT WINAPI IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD dwPatch)
99 {
100     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
101
102     TRACE("(%p)->(%d): stub\n", This, dwPatch);
103
104     Patch2MIDILOCALE(dwPatch, &This->pHeader->Locale);
105
106     return S_OK;
107 }
108
109 static const IDirectMusicInstrumentVtbl DirectMusicInstrument_Vtbl =
110 {
111     IDirectMusicInstrumentImpl_QueryInterface,
112     IDirectMusicInstrumentImpl_AddRef,
113     IDirectMusicInstrumentImpl_Release,
114     IDirectMusicInstrumentImpl_GetPatch,
115     IDirectMusicInstrumentImpl_SetPatch
116 };
117
118 /* for ClassFactory */
119 HRESULT DMUSIC_CreateDirectMusicInstrumentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
120         IDirectMusicInstrumentImpl* dminst;
121         
122         dminst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicInstrumentImpl));
123         if (NULL == dminst) {
124                 *ppobj = NULL;
125                 return E_OUTOFMEMORY;
126         }
127         dminst->IDirectMusicInstrument_iface.lpVtbl = &DirectMusicInstrument_Vtbl;
128         dminst->ref = 0; /* will be inited by QueryInterface */
129         
130         return IDirectMusicInstrument_QueryInterface(&dminst->IDirectMusicInstrument_iface, lpcGUID, ppobj);
131 }
132
133 static HRESULT read_from_stream(IStream *stream, void *data, ULONG size)
134 {
135     ULONG bytes_read;
136     HRESULT hr;
137
138     hr = IStream_Read(stream, data, size, &bytes_read);
139     if(FAILED(hr)){
140         TRACE("IStream_Read failed: %08x\n", hr);
141         return hr;
142     }
143     if (bytes_read < size) {
144         TRACE("Didn't read full chunk: %u < %u\n", bytes_read, size);
145         return E_FAIL;
146     }
147
148     return S_OK;
149 }
150
151 static inline DWORD subtract_bytes(DWORD len, DWORD bytes)
152 {
153     if(bytes > len){
154         TRACE("Apparent mismatch in chunk lengths? %u bytes remaining, %u bytes read\n", len, bytes);
155         return 0;
156     }
157     return len - bytes;
158 }
159
160 static HRESULT load_instrument(IDirectMusicInstrumentImpl *This, IStream *stream, DWORD length)
161 {
162     HRESULT hr;
163     FOURCC fourcc;
164     DWORD bytes;
165     LARGE_INTEGER move;
166
167     while(length){
168         hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
169         if(FAILED(hr))
170             return hr;
171
172         hr = read_from_stream(stream, &bytes, sizeof(bytes));
173         if(FAILED(hr))
174             return hr;
175
176         length = subtract_bytes(length, sizeof(fourcc) + sizeof(bytes));
177
178         switch(fourcc){
179         case FOURCC_INSH:
180             TRACE("INSH chunk: %u bytes\n", bytes);
181             hr = read_from_stream(stream, This->pHeader, sizeof(*This->pHeader));
182             if(FAILED(hr))
183                 return hr;
184
185             move.QuadPart = bytes - sizeof(*This->pHeader);
186             hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
187             if(FAILED(hr)){
188                 WARN("IStream_Seek failed: %08x\n", hr);
189                 return hr;
190             }
191
192             length = subtract_bytes(length, bytes);
193             break;
194
195         case FOURCC_DLID:
196             TRACE("DLID chunk: %u bytes\n", bytes);
197             hr = read_from_stream(stream, This->pInstrumentID, sizeof(*This->pInstrumentID));
198             if(FAILED(hr))
199                 return hr;
200
201             move.QuadPart = bytes - sizeof(*This->pInstrumentID);
202             hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
203             if(FAILED(hr)){
204                 WARN("IStream_Seek failed: %08x\n", hr);
205                 return hr;
206             }
207
208             length = subtract_bytes(length, bytes);
209             break;
210
211         default:
212             TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(fourcc), bytes);
213
214             move.QuadPart = bytes;
215             hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
216             if(FAILED(hr)){
217                 WARN("IStream_Seek failed: %08x\n", hr);
218                 return hr;
219             }
220
221             length = subtract_bytes(length, bytes);
222             break;
223         }
224     }
225
226     return S_OK;
227 }
228
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)
232 {
233     IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
234     LARGE_INTEGER move;
235     FOURCC fourcc;
236     DWORD bytes;
237     HRESULT hr;
238
239     TRACE("(%p, %p, offset = %s)\n", This, stream, wine_dbgstr_longlong(This->liInstrumentPosition.QuadPart));
240
241     hr = IStream_Seek(stream, This->liInstrumentPosition, STREAM_SEEK_SET, NULL);
242     if(FAILED(hr)){
243         WARN("IStream_Seek failed: %08x\n", hr);
244         goto load_failure;
245     }
246
247     hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
248     if(FAILED(hr))
249         goto load_failure;
250
251     if(fourcc != FOURCC_LIST){
252         WARN("Loading failed: Expected LIST chunk, got: %s\n", debugstr_fourcc(fourcc));
253         goto load_failure;
254     }
255
256     hr = read_from_stream(stream, &bytes, sizeof(bytes));
257     if(FAILED(hr))
258         goto load_failure;
259
260     TRACE("LIST chunk: %u bytes\n", bytes);
261     while(1){
262         hr = read_from_stream(stream, &fourcc, sizeof(fourcc));
263         if(FAILED(hr))
264             goto load_failure;
265
266         switch(fourcc){
267         case FOURCC_INS:
268             TRACE("INS  chunk: (no byte count)\n");
269             hr = load_instrument(This, stream, bytes - sizeof(FOURCC));
270             if(FAILED(hr))
271                 goto load_failure;
272             break;
273
274         default:
275             hr = read_from_stream(stream, &bytes, sizeof(bytes));
276             if(FAILED(hr))
277                 goto load_failure;
278
279             TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(fourcc), bytes);
280
281             move.QuadPart = bytes;
282             hr = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
283             if(FAILED(hr)){
284                 WARN("IStream_Seek failed: %08x\n", hr);
285                 return hr;
286             }
287
288             break;
289         }
290     }
291
292     return S_OK;
293
294 load_failure:
295     return DMUS_E_UNSUPPORTED_STREAM;
296 }