1 /* IDirectMusicBuffer 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 "dmusic_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
26 static inline IDirectMusicBufferImpl *impl_from_IDirectMusicBuffer(IDirectMusicBuffer *iface)
28 return CONTAINING_RECORD(iface, IDirectMusicBufferImpl, IDirectMusicBuffer_iface);
31 /* IDirectMusicBufferImpl IUnknown part: */
32 static HRESULT WINAPI IDirectMusicBufferImpl_QueryInterface(LPDIRECTMUSICBUFFER iface, REFIID riid, LPVOID *ret_iface)
34 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
36 if (IsEqualIID(riid, &IID_IUnknown) ||
37 IsEqualIID(riid, &IID_IDirectMusicBuffer))
39 IDirectMusicBuffer_AddRef(iface);
46 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
51 static ULONG WINAPI IDirectMusicBufferImpl_AddRef(LPDIRECTMUSICBUFFER iface)
53 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
54 ULONG ref = InterlockedIncrement(&This->ref);
56 TRACE("(%p)->(): new ref = %u\n", iface, ref);
63 static ULONG WINAPI IDirectMusicBufferImpl_Release(LPDIRECTMUSICBUFFER iface)
65 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
66 ULONG ref = InterlockedDecrement(&This->ref);
68 TRACE("(%p)->(): new ref = %u\n", iface, ref);
71 HeapFree(GetProcessHeap(), 0, This->data);
72 HeapFree(GetProcessHeap(), 0, This);
75 DMUSIC_UnlockModule();
80 /* IDirectMusicBufferImpl IDirectMusicBuffer part: */
81 static HRESULT WINAPI IDirectMusicBufferImpl_Flush(LPDIRECTMUSICBUFFER iface)
83 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
85 FIXME("(%p): stub\n", This);
90 static HRESULT WINAPI IDirectMusicBufferImpl_TotalTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prtTime)
92 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
94 FIXME("(%p, %p): stub\n", This, prtTime);
99 static HRESULT WINAPI IDirectMusicBufferImpl_PackStructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time, DWORD channel_group, DWORD channel_message)
101 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
102 DWORD new_write_pos = This->write_pos + sizeof(DMUS_EVENTHEADER) + sizeof(DWORD);
103 DMUS_EVENTHEADER header;
105 TRACE("(%p)->(0x%s, %u, 0x%x)\n", iface, wine_dbgstr_longlong(ref_time), channel_group, channel_message);
107 if (new_write_pos > This->size)
108 return DMUS_E_BUFFER_FULL;
110 /* Channel_message 0xZZYYXX is a midi message where XX = status byte, YY = byte 1 and ZZ = byte 2 */
112 if (!(channel_message & 0x80))
114 /* Status byte MSB is always set */
115 return DMUS_E_INVALID_EVENT;
118 header.cbEvent = sizeof(channel_message);
119 header.dwChannelGroup = channel_group;
120 header.rtDelta = ref_time;
121 header.dwFlags = DMUS_EVENT_STRUCTURED;
123 memcpy(This->data + This->write_pos, &header, sizeof(header));
124 *(DWORD*)(This->data + This->write_pos + sizeof(header)) = channel_message;
125 This->write_pos = new_write_pos;
130 static HRESULT WINAPI IDirectMusicBufferImpl_PackUnstructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME rt, DWORD dwChannelGroup, DWORD cb, LPBYTE lpb)
132 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
134 FIXME("(%p, 0x%s, %d, %d, %p): stub\n", This, wine_dbgstr_longlong(rt), dwChannelGroup, cb, lpb);
139 static HRESULT WINAPI IDirectMusicBufferImpl_ResetReadPtr(LPDIRECTMUSICBUFFER iface)
141 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
143 FIXME("(%p): stub\n", This);
148 static HRESULT WINAPI IDirectMusicBufferImpl_GetNextEvent(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prt, LPDWORD pdwChannelGroup, LPDWORD pdwLength, LPBYTE* ppData)
150 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
152 FIXME("(%p, %p, %p, %p, %p): stub\n", This, prt, pdwChannelGroup, pdwLength, ppData);
157 static HRESULT WINAPI IDirectMusicBufferImpl_GetRawBufferPtr(LPDIRECTMUSICBUFFER iface, LPBYTE* data)
159 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
161 TRACE("(%p)->(%p)\n", iface, data);
171 static HRESULT WINAPI IDirectMusicBufferImpl_GetStartTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prt)
173 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
175 FIXME("(%p, %p): stub\n", This, prt);
180 static HRESULT WINAPI IDirectMusicBufferImpl_GetUsedBytes(LPDIRECTMUSICBUFFER iface, LPDWORD pcb)
182 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
184 FIXME("(%p, %p): stub\n", This, pcb);
189 static HRESULT WINAPI IDirectMusicBufferImpl_GetMaxBytes(LPDIRECTMUSICBUFFER iface, LPDWORD max_bytes)
191 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
193 TRACE("(%p)->(%p)\n", iface, max_bytes);
198 *max_bytes = This->size;
203 static HRESULT WINAPI IDirectMusicBufferImpl_GetBufferFormat(LPDIRECTMUSICBUFFER iface, LPGUID format)
205 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
207 TRACE("(%p)->(%p)\n", iface, format);
212 *format = This->format;
216 static HRESULT WINAPI IDirectMusicBufferImpl_SetStartTime(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME rt)
218 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
220 FIXME("(%p, 0x%s): stub\n", This, wine_dbgstr_longlong(rt));
225 static HRESULT WINAPI IDirectMusicBufferImpl_SetUsedBytes (LPDIRECTMUSICBUFFER iface, DWORD cb)
227 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
229 FIXME("(%p, %d): stub\n", This, cb);
234 static const IDirectMusicBufferVtbl DirectMusicBuffer_Vtbl = {
235 IDirectMusicBufferImpl_QueryInterface,
236 IDirectMusicBufferImpl_AddRef,
237 IDirectMusicBufferImpl_Release,
238 IDirectMusicBufferImpl_Flush,
239 IDirectMusicBufferImpl_TotalTime,
240 IDirectMusicBufferImpl_PackStructured,
241 IDirectMusicBufferImpl_PackUnstructured,
242 IDirectMusicBufferImpl_ResetReadPtr,
243 IDirectMusicBufferImpl_GetNextEvent,
244 IDirectMusicBufferImpl_GetRawBufferPtr,
245 IDirectMusicBufferImpl_GetStartTime,
246 IDirectMusicBufferImpl_GetUsedBytes,
247 IDirectMusicBufferImpl_GetMaxBytes,
248 IDirectMusicBufferImpl_GetBufferFormat,
249 IDirectMusicBufferImpl_SetStartTime,
250 IDirectMusicBufferImpl_SetUsedBytes
253 HRESULT DMUSIC_CreateDirectMusicBufferImpl(LPDMUS_BUFFERDESC desc, LPVOID* ret_iface)
255 IDirectMusicBufferImpl* dmbuffer;
258 TRACE("(%p, %p)\n", desc, ret_iface);
262 dmbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBufferImpl));
264 return E_OUTOFMEMORY;
266 dmbuffer->IDirectMusicBuffer_iface.lpVtbl = &DirectMusicBuffer_Vtbl;
267 dmbuffer->ref = 0; /* Will be inited by QueryInterface */
269 if (IsEqualGUID(&desc->guidBufferFormat, &GUID_NULL))
270 dmbuffer->format = KSDATAFORMAT_SUBTYPE_MIDI;
272 dmbuffer->format = desc->guidBufferFormat;
273 dmbuffer->size = (desc->cbBuffer + 3) & ~3; /* Buffer size must be multiple of 4 bytes */
275 dmbuffer->data = HeapAlloc(GetProcessHeap(), 0, dmbuffer->size);
276 if (!dmbuffer->data) {
277 HeapFree(GetProcessHeap(), 0, dmbuffer);
278 return E_OUTOFMEMORY;
281 hr = IDirectMusicBufferImpl_QueryInterface((LPDIRECTMUSICBUFFER)dmbuffer, &IID_IDirectMusicBuffer, ret_iface);
284 HeapFree(GetProcessHeap(), 0, dmbuffer->data);
285 HeapFree(GetProcessHeap(), 0, dmbuffer);