2 * IDirectMusicBuffer Implementation
4 * Copyright (C) 2003-2004 Rok Mandeljc
5 * Copyright (C) 2012 Christian Costa
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "dmusic_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(dmusic
);
28 static inline IDirectMusicBufferImpl
*impl_from_IDirectMusicBuffer(IDirectMusicBuffer
*iface
)
30 return CONTAINING_RECORD(iface
, IDirectMusicBufferImpl
, IDirectMusicBuffer_iface
);
33 /* IDirectMusicBufferImpl IUnknown part: */
34 static HRESULT WINAPI
IDirectMusicBufferImpl_QueryInterface(LPDIRECTMUSICBUFFER iface
, REFIID riid
, LPVOID
*ret_iface
)
36 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_dmguid(riid
), ret_iface
);
38 if (IsEqualIID(riid
, &IID_IUnknown
) ||
39 IsEqualIID(riid
, &IID_IDirectMusicBuffer
))
41 IDirectMusicBuffer_AddRef(iface
);
48 WARN("(%p)->(%s, %p): not found\n", iface
, debugstr_dmguid(riid
), ret_iface
);
53 static ULONG WINAPI
IDirectMusicBufferImpl_AddRef(LPDIRECTMUSICBUFFER iface
)
55 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
56 ULONG ref
= InterlockedIncrement(&This
->ref
);
58 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
);
73 DMUSIC_UnlockModule();
79 /* IDirectMusicBufferImpl IDirectMusicBuffer part: */
80 static HRESULT WINAPI
IDirectMusicBufferImpl_Flush(LPDIRECTMUSICBUFFER iface
)
82 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
84 TRACE("(%p)->()\n", iface
);
91 static HRESULT WINAPI
IDirectMusicBufferImpl_TotalTime(LPDIRECTMUSICBUFFER iface
, LPREFERENCE_TIME prtTime
)
93 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
95 FIXME("(%p, %p): stub\n", This
, prtTime
);
100 static HRESULT WINAPI
IDirectMusicBufferImpl_PackStructured(LPDIRECTMUSICBUFFER iface
, REFERENCE_TIME ref_time
, DWORD channel_group
, DWORD channel_message
)
102 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
103 DWORD new_write_pos
= This
->write_pos
+ sizeof(DMUS_EVENTHEADER
) + sizeof(DWORD
);
104 DMUS_EVENTHEADER header
;
106 TRACE("(%p)->(0x%s, %u, 0x%x)\n", iface
, wine_dbgstr_longlong(ref_time
), channel_group
, channel_message
);
108 if (new_write_pos
> This
->size
)
109 return DMUS_E_BUFFER_FULL
;
111 /* Channel_message 0xZZYYXX (3 bytes) is a midi message where XX = status byte, YY = byte 1 and ZZ = byte 2 */
113 if (!(channel_message
& 0x80))
115 /* Status byte MSB is always set */
116 return DMUS_E_INVALID_EVENT
;
119 if (!This
->write_pos
)
120 This
->start_time
= ref_time
;
122 header
.cbEvent
= 3; /* Midi message takes 4 bytes space but only 3 are relevant */
123 header
.dwChannelGroup
= channel_group
;
124 header
.rtDelta
= ref_time
- This
->start_time
;
125 header
.dwFlags
= DMUS_EVENT_STRUCTURED
;
127 memcpy(This
->data
+ This
->write_pos
, &header
, sizeof(header
));
128 *(DWORD
*)(This
->data
+ This
->write_pos
+ sizeof(header
)) = channel_message
;
129 This
->write_pos
= new_write_pos
;
134 static HRESULT WINAPI
IDirectMusicBufferImpl_PackUnstructured(LPDIRECTMUSICBUFFER iface
, REFERENCE_TIME rt
, DWORD dwChannelGroup
, DWORD cb
, LPBYTE lpb
)
136 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
138 FIXME("(%p, 0x%s, %d, %d, %p): stub\n", This
, wine_dbgstr_longlong(rt
), dwChannelGroup
, cb
, lpb
);
143 static HRESULT WINAPI
IDirectMusicBufferImpl_ResetReadPtr(LPDIRECTMUSICBUFFER iface
)
145 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
147 FIXME("(%p): stub\n", This
);
152 static HRESULT WINAPI
IDirectMusicBufferImpl_GetNextEvent(LPDIRECTMUSICBUFFER iface
, LPREFERENCE_TIME prt
, LPDWORD pdwChannelGroup
, LPDWORD pdwLength
, LPBYTE
* ppData
)
154 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
156 FIXME("(%p, %p, %p, %p, %p): stub\n", This
, prt
, pdwChannelGroup
, pdwLength
, ppData
);
161 static HRESULT WINAPI
IDirectMusicBufferImpl_GetRawBufferPtr(LPDIRECTMUSICBUFFER iface
, LPBYTE
* data
)
163 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
165 TRACE("(%p)->(%p)\n", iface
, data
);
175 static HRESULT WINAPI
IDirectMusicBufferImpl_GetStartTime(LPDIRECTMUSICBUFFER iface
, LPREFERENCE_TIME ref_time
)
177 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
179 TRACE("(%p)->(%p)\n", iface
, ref_time
);
183 if (!This
->write_pos
)
184 return DMUS_E_BUFFER_EMPTY
;
186 *ref_time
= This
->start_time
;
191 static HRESULT WINAPI
IDirectMusicBufferImpl_GetUsedBytes(LPDIRECTMUSICBUFFER iface
, LPDWORD used_bytes
)
193 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
195 TRACE("(%p)->(%p)\n", iface
, used_bytes
);
200 *used_bytes
= This
->write_pos
;
205 static HRESULT WINAPI
IDirectMusicBufferImpl_GetMaxBytes(LPDIRECTMUSICBUFFER iface
, LPDWORD max_bytes
)
207 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
209 TRACE("(%p)->(%p)\n", iface
, max_bytes
);
214 *max_bytes
= This
->size
;
219 static HRESULT WINAPI
IDirectMusicBufferImpl_GetBufferFormat(LPDIRECTMUSICBUFFER iface
, LPGUID format
)
221 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
223 TRACE("(%p)->(%p)\n", iface
, format
);
228 *format
= This
->format
;
232 static HRESULT WINAPI
IDirectMusicBufferImpl_SetStartTime(LPDIRECTMUSICBUFFER iface
, REFERENCE_TIME ref_time
)
234 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
236 TRACE("(%p)->(0x%s)\n", This
, wine_dbgstr_longlong(ref_time
));
238 This
->start_time
= ref_time
;
243 static HRESULT WINAPI
IDirectMusicBufferImpl_SetUsedBytes(LPDIRECTMUSICBUFFER iface
, DWORD used_bytes
)
245 IDirectMusicBufferImpl
*This
= impl_from_IDirectMusicBuffer(iface
);
247 TRACE("(%p)->(%u)\n", iface
, used_bytes
);
249 if (used_bytes
> This
->size
)
250 return DMUS_E_BUFFER_FULL
;
252 This
->write_pos
= used_bytes
;
257 static const IDirectMusicBufferVtbl DirectMusicBuffer_Vtbl
= {
258 IDirectMusicBufferImpl_QueryInterface
,
259 IDirectMusicBufferImpl_AddRef
,
260 IDirectMusicBufferImpl_Release
,
261 IDirectMusicBufferImpl_Flush
,
262 IDirectMusicBufferImpl_TotalTime
,
263 IDirectMusicBufferImpl_PackStructured
,
264 IDirectMusicBufferImpl_PackUnstructured
,
265 IDirectMusicBufferImpl_ResetReadPtr
,
266 IDirectMusicBufferImpl_GetNextEvent
,
267 IDirectMusicBufferImpl_GetRawBufferPtr
,
268 IDirectMusicBufferImpl_GetStartTime
,
269 IDirectMusicBufferImpl_GetUsedBytes
,
270 IDirectMusicBufferImpl_GetMaxBytes
,
271 IDirectMusicBufferImpl_GetBufferFormat
,
272 IDirectMusicBufferImpl_SetStartTime
,
273 IDirectMusicBufferImpl_SetUsedBytes
276 HRESULT
DMUSIC_CreateDirectMusicBufferImpl(LPDMUS_BUFFERDESC desc
, LPVOID
* ret_iface
)
278 IDirectMusicBufferImpl
* dmbuffer
;
280 TRACE("(%p, %p)\n", desc
, ret_iface
);
284 dmbuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirectMusicBufferImpl
));
286 return E_OUTOFMEMORY
;
288 dmbuffer
->IDirectMusicBuffer_iface
.lpVtbl
= &DirectMusicBuffer_Vtbl
;
291 if (IsEqualGUID(&desc
->guidBufferFormat
, &GUID_NULL
))
292 dmbuffer
->format
= KSDATAFORMAT_SUBTYPE_MIDI
;
294 dmbuffer
->format
= desc
->guidBufferFormat
;
295 dmbuffer
->size
= (desc
->cbBuffer
+ 3) & ~3; /* Buffer size must be multiple of 4 bytes */
297 dmbuffer
->data
= HeapAlloc(GetProcessHeap(), 0, dmbuffer
->size
);
298 if (!dmbuffer
->data
) {
299 HeapFree(GetProcessHeap(), 0, dmbuffer
);
300 return E_OUTOFMEMORY
;
304 *ret_iface
= &dmbuffer
->IDirectMusicBuffer_iface
;