msvcp90: Fix basic_ios<char>::imbue implementation.
[wine/multimedia.git] / dlls / dmusic / buffer.c
blob35cbd88d7651fd7656f2ff4deee322d9d7614642
1 /*
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"
23 #include "initguid.h"
24 #include "dmksctrl.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);
42 *ret_iface = iface;
43 return S_OK;
46 *ret_iface = NULL;
48 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
50 return E_NOINTERFACE;
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);
60 DMUSIC_LockModule();
62 return ref;
65 static ULONG WINAPI IDirectMusicBufferImpl_Release(LPDIRECTMUSICBUFFER iface)
67 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
68 ULONG ref = InterlockedDecrement(&This->ref);
70 TRACE("(%p)->(): new ref = %u\n", iface, ref);
72 if (!ref) {
73 HeapFree(GetProcessHeap(), 0, This->data);
74 HeapFree(GetProcessHeap(), 0, This);
77 DMUSIC_UnlockModule();
79 return ref;
82 /* IDirectMusicBufferImpl IDirectMusicBuffer part: */
83 static HRESULT WINAPI IDirectMusicBufferImpl_Flush(LPDIRECTMUSICBUFFER iface)
85 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
87 TRACE("(%p)->()\n", iface);
89 This->write_pos = 0;
91 return S_OK;
94 static HRESULT WINAPI IDirectMusicBufferImpl_TotalTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prtTime)
96 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
98 FIXME("(%p, %p): stub\n", This, prtTime);
100 return S_OK;
103 static HRESULT WINAPI IDirectMusicBufferImpl_PackStructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time, DWORD channel_group, DWORD channel_message)
105 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
106 DWORD new_write_pos = This->write_pos + sizeof(DMUS_EVENTHEADER) + sizeof(DWORD);
107 DMUS_EVENTHEADER header;
109 TRACE("(%p)->(0x%s, %u, 0x%x)\n", iface, wine_dbgstr_longlong(ref_time), channel_group, channel_message);
111 if (new_write_pos > This->size)
112 return DMUS_E_BUFFER_FULL;
114 /* Channel_message 0xZZYYXX (3 bytes) is a midi message where XX = status byte, YY = byte 1 and ZZ = byte 2 */
116 if (!(channel_message & 0x80))
118 /* Status byte MSB is always set */
119 return DMUS_E_INVALID_EVENT;
122 if (!This->write_pos)
123 This->start_time = ref_time;
125 header.cbEvent = 3; /* Midi message takes 4 bytes space but only 3 are relevant */
126 header.dwChannelGroup = channel_group;
127 header.rtDelta = ref_time - This->start_time;
128 header.dwFlags = DMUS_EVENT_STRUCTURED;
130 memcpy(This->data + This->write_pos, &header, sizeof(header));
131 *(DWORD*)(This->data + This->write_pos + sizeof(header)) = channel_message;
132 This->write_pos = new_write_pos;
134 return S_OK;
137 static HRESULT WINAPI IDirectMusicBufferImpl_PackUnstructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME rt, DWORD dwChannelGroup, DWORD cb, LPBYTE lpb)
139 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
141 FIXME("(%p, 0x%s, %d, %d, %p): stub\n", This, wine_dbgstr_longlong(rt), dwChannelGroup, cb, lpb);
143 return S_OK;
146 static HRESULT WINAPI IDirectMusicBufferImpl_ResetReadPtr(LPDIRECTMUSICBUFFER iface)
148 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
150 FIXME("(%p): stub\n", This);
152 return S_OK;
155 static HRESULT WINAPI IDirectMusicBufferImpl_GetNextEvent(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prt, LPDWORD pdwChannelGroup, LPDWORD pdwLength, LPBYTE* ppData)
157 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
159 FIXME("(%p, %p, %p, %p, %p): stub\n", This, prt, pdwChannelGroup, pdwLength, ppData);
161 return S_OK;
164 static HRESULT WINAPI IDirectMusicBufferImpl_GetRawBufferPtr(LPDIRECTMUSICBUFFER iface, LPBYTE* data)
166 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
168 TRACE("(%p)->(%p)\n", iface, data);
170 if (!data)
171 return E_POINTER;
173 *data = This->data;
175 return S_OK;
178 static HRESULT WINAPI IDirectMusicBufferImpl_GetStartTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME ref_time)
180 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
182 TRACE("(%p)->(%p)\n", iface, ref_time);
184 if (!ref_time)
185 return E_POINTER;
186 if (!This->write_pos)
187 return DMUS_E_BUFFER_EMPTY;
189 *ref_time = This->start_time;
191 return S_OK;
194 static HRESULT WINAPI IDirectMusicBufferImpl_GetUsedBytes(LPDIRECTMUSICBUFFER iface, LPDWORD used_bytes)
196 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
198 TRACE("(%p)->(%p)\n", iface, used_bytes);
200 if (!used_bytes)
201 return E_POINTER;
203 *used_bytes = This->write_pos;
205 return S_OK;
208 static HRESULT WINAPI IDirectMusicBufferImpl_GetMaxBytes(LPDIRECTMUSICBUFFER iface, LPDWORD max_bytes)
210 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
212 TRACE("(%p)->(%p)\n", iface, max_bytes);
214 if (!max_bytes)
215 return E_POINTER;
217 *max_bytes = This->size;
219 return S_OK;
222 static HRESULT WINAPI IDirectMusicBufferImpl_GetBufferFormat(LPDIRECTMUSICBUFFER iface, LPGUID format)
224 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
226 TRACE("(%p)->(%p)\n", iface, format);
228 if (!format)
229 return E_POINTER;
231 *format = This->format;
232 return S_OK;
235 static HRESULT WINAPI IDirectMusicBufferImpl_SetStartTime(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time)
237 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
239 TRACE("(%p)->(0x%s)\n", This, wine_dbgstr_longlong(ref_time));
241 This->start_time = ref_time;
243 return S_OK;
246 static HRESULT WINAPI IDirectMusicBufferImpl_SetUsedBytes(LPDIRECTMUSICBUFFER iface, DWORD used_bytes)
248 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
250 TRACE("(%p)->(%u)\n", iface, used_bytes);
252 if (used_bytes > This->size)
253 return DMUS_E_BUFFER_FULL;
255 This->write_pos = used_bytes;
257 return S_OK;
260 static const IDirectMusicBufferVtbl DirectMusicBuffer_Vtbl = {
261 IDirectMusicBufferImpl_QueryInterface,
262 IDirectMusicBufferImpl_AddRef,
263 IDirectMusicBufferImpl_Release,
264 IDirectMusicBufferImpl_Flush,
265 IDirectMusicBufferImpl_TotalTime,
266 IDirectMusicBufferImpl_PackStructured,
267 IDirectMusicBufferImpl_PackUnstructured,
268 IDirectMusicBufferImpl_ResetReadPtr,
269 IDirectMusicBufferImpl_GetNextEvent,
270 IDirectMusicBufferImpl_GetRawBufferPtr,
271 IDirectMusicBufferImpl_GetStartTime,
272 IDirectMusicBufferImpl_GetUsedBytes,
273 IDirectMusicBufferImpl_GetMaxBytes,
274 IDirectMusicBufferImpl_GetBufferFormat,
275 IDirectMusicBufferImpl_SetStartTime,
276 IDirectMusicBufferImpl_SetUsedBytes
279 HRESULT DMUSIC_CreateDirectMusicBufferImpl(LPDMUS_BUFFERDESC desc, LPVOID* ret_iface)
281 IDirectMusicBufferImpl* dmbuffer;
282 HRESULT hr;
284 TRACE("(%p, %p)\n", desc, ret_iface);
286 *ret_iface = NULL;
288 dmbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBufferImpl));
289 if (!dmbuffer)
290 return E_OUTOFMEMORY;
292 dmbuffer->IDirectMusicBuffer_iface.lpVtbl = &DirectMusicBuffer_Vtbl;
293 dmbuffer->ref = 0; /* Will be inited by QueryInterface */
295 if (IsEqualGUID(&desc->guidBufferFormat, &GUID_NULL))
296 dmbuffer->format = KSDATAFORMAT_SUBTYPE_MIDI;
297 else
298 dmbuffer->format = desc->guidBufferFormat;
299 dmbuffer->size = (desc->cbBuffer + 3) & ~3; /* Buffer size must be multiple of 4 bytes */
301 dmbuffer->data = HeapAlloc(GetProcessHeap(), 0, dmbuffer->size);
302 if (!dmbuffer->data) {
303 HeapFree(GetProcessHeap(), 0, dmbuffer);
304 return E_OUTOFMEMORY;
307 hr = IDirectMusicBufferImpl_QueryInterface((LPDIRECTMUSICBUFFER)dmbuffer, &IID_IDirectMusicBuffer, ret_iface);
308 if (FAILED(hr))
310 HeapFree(GetProcessHeap(), 0, dmbuffer->data);
311 HeapFree(GetProcessHeap(), 0, dmbuffer);
314 return hr;