wined3d: Rename create/destroy helpers dealing with Vulkan objects.
[wine/zf.git] / dlls / dmusic / buffer.c
blobe17bad90dc31d69e235aa1144c70e331ab42a6cb
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 "dmobject.h"
24 #include "initguid.h"
25 #include "dmksctrl.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
29 static inline IDirectMusicBufferImpl *impl_from_IDirectMusicBuffer(IDirectMusicBuffer *iface)
31 return CONTAINING_RECORD(iface, IDirectMusicBufferImpl, IDirectMusicBuffer_iface);
34 /* IDirectMusicBufferImpl IUnknown part: */
35 static HRESULT WINAPI IDirectMusicBufferImpl_QueryInterface(LPDIRECTMUSICBUFFER iface, REFIID riid, LPVOID *ret_iface)
37 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
39 if (IsEqualIID(riid, &IID_IUnknown) ||
40 IsEqualIID(riid, &IID_IDirectMusicBuffer))
42 IDirectMusicBuffer_AddRef(iface);
43 *ret_iface = iface;
44 return S_OK;
47 *ret_iface = NULL;
49 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
51 return E_NOINTERFACE;
54 static ULONG WINAPI IDirectMusicBufferImpl_AddRef(LPDIRECTMUSICBUFFER iface)
56 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
57 ULONG ref = InterlockedIncrement(&This->ref);
59 TRACE("(%p)->(): new ref = %u\n", iface, ref);
61 return ref;
64 static ULONG WINAPI IDirectMusicBufferImpl_Release(LPDIRECTMUSICBUFFER iface)
66 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
67 ULONG ref = InterlockedDecrement(&This->ref);
69 TRACE("(%p)->(): new ref = %u\n", iface, ref);
71 if (!ref) {
72 HeapFree(GetProcessHeap(), 0, This->data);
73 HeapFree(GetProcessHeap(), 0, This);
74 DMUSIC_UnlockModule();
77 return ref;
80 /* IDirectMusicBufferImpl IDirectMusicBuffer part: */
81 static HRESULT WINAPI IDirectMusicBufferImpl_Flush(LPDIRECTMUSICBUFFER iface)
83 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
85 TRACE("(%p)->()\n", iface);
87 This->write_pos = 0;
89 return S_OK;
92 static HRESULT WINAPI IDirectMusicBufferImpl_TotalTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prtTime)
94 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
96 FIXME("(%p, %p): stub\n", This, prtTime);
98 return S_OK;
101 static HRESULT WINAPI IDirectMusicBufferImpl_PackStructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time, DWORD channel_group, DWORD channel_message)
103 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
104 DWORD new_write_pos = This->write_pos + DMUS_EVENT_SIZE(sizeof(channel_message));
105 DMUS_EVENTHEADER *header;
107 TRACE("(%p)->(0x%s, %u, 0x%x)\n", iface, wine_dbgstr_longlong(ref_time), channel_group, channel_message);
109 if (new_write_pos > This->size)
110 return DMUS_E_BUFFER_FULL;
112 /* Channel_message 0xZZYYXX (3 bytes) is a midi message where XX = status byte, YY = byte 1 and ZZ = byte 2 */
114 if (!(channel_message & 0x80))
116 /* Status byte MSB is always set */
117 return DMUS_E_INVALID_EVENT;
120 if (!This->write_pos)
121 This->start_time = ref_time;
123 header = (DMUS_EVENTHEADER*)&This->data[This->write_pos];
124 header->cbEvent = 3; /* Midi message takes 4 bytes space but only 3 are relevant */
125 header->dwChannelGroup = channel_group;
126 header->rtDelta = ref_time - This->start_time;
127 header->dwFlags = DMUS_EVENT_STRUCTURED;
129 *(DWORD*)&header[1] = channel_message;
130 This->write_pos = new_write_pos;
132 return S_OK;
135 static HRESULT WINAPI IDirectMusicBufferImpl_PackUnstructured(IDirectMusicBuffer *iface,
136 REFERENCE_TIME ref_time, DWORD channel_group, DWORD len, BYTE *data)
138 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
139 DWORD new_write_pos = This->write_pos + DMUS_EVENT_SIZE(len);
140 DMUS_EVENTHEADER *header;
142 TRACE("(%p, 0x%s, %d, %d, %p)\n", This, wine_dbgstr_longlong(ref_time), channel_group, len, data);
144 if (new_write_pos > This->size)
145 return DMUS_E_BUFFER_FULL;
147 if (!This->write_pos)
148 This->start_time = ref_time;
150 header = (DMUS_EVENTHEADER*)&This->data[This->write_pos];
151 header->cbEvent = len;
152 header->dwChannelGroup = channel_group;
153 header->rtDelta = ref_time - This->start_time;
154 header->dwFlags = 0;
156 memcpy(&header[1], data, len);
157 This->write_pos = new_write_pos;
159 return S_OK;
162 static HRESULT WINAPI IDirectMusicBufferImpl_ResetReadPtr(LPDIRECTMUSICBUFFER iface)
164 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
166 FIXME("(%p): stub\n", This);
168 return S_OK;
171 static HRESULT WINAPI IDirectMusicBufferImpl_GetNextEvent(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prt, LPDWORD pdwChannelGroup, LPDWORD pdwLength, LPBYTE* ppData)
173 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
175 FIXME("(%p, %p, %p, %p, %p): stub\n", This, prt, pdwChannelGroup, pdwLength, ppData);
177 return S_OK;
180 static HRESULT WINAPI IDirectMusicBufferImpl_GetRawBufferPtr(LPDIRECTMUSICBUFFER iface, LPBYTE* data)
182 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
184 TRACE("(%p)->(%p)\n", iface, data);
186 if (!data)
187 return E_POINTER;
189 *data = This->data;
191 return S_OK;
194 static HRESULT WINAPI IDirectMusicBufferImpl_GetStartTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME ref_time)
196 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
198 TRACE("(%p)->(%p)\n", iface, ref_time);
200 if (!ref_time)
201 return E_POINTER;
202 if (!This->write_pos)
203 return DMUS_E_BUFFER_EMPTY;
205 *ref_time = This->start_time;
207 return S_OK;
210 static HRESULT WINAPI IDirectMusicBufferImpl_GetUsedBytes(LPDIRECTMUSICBUFFER iface, LPDWORD used_bytes)
212 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
214 TRACE("(%p)->(%p)\n", iface, used_bytes);
216 if (!used_bytes)
217 return E_POINTER;
219 *used_bytes = This->write_pos;
221 return S_OK;
224 static HRESULT WINAPI IDirectMusicBufferImpl_GetMaxBytes(LPDIRECTMUSICBUFFER iface, LPDWORD max_bytes)
226 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
228 TRACE("(%p)->(%p)\n", iface, max_bytes);
230 if (!max_bytes)
231 return E_POINTER;
233 *max_bytes = This->size;
235 return S_OK;
238 static HRESULT WINAPI IDirectMusicBufferImpl_GetBufferFormat(LPDIRECTMUSICBUFFER iface, LPGUID format)
240 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
242 TRACE("(%p)->(%p)\n", iface, format);
244 if (!format)
245 return E_POINTER;
247 *format = This->format;
248 return S_OK;
251 static HRESULT WINAPI IDirectMusicBufferImpl_SetStartTime(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time)
253 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
255 TRACE("(%p)->(0x%s)\n", This, wine_dbgstr_longlong(ref_time));
257 This->start_time = ref_time;
259 return S_OK;
262 static HRESULT WINAPI IDirectMusicBufferImpl_SetUsedBytes(LPDIRECTMUSICBUFFER iface, DWORD used_bytes)
264 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
266 TRACE("(%p)->(%u)\n", iface, used_bytes);
268 if (used_bytes > This->size)
269 return DMUS_E_BUFFER_FULL;
271 This->write_pos = used_bytes;
273 return S_OK;
276 static const IDirectMusicBufferVtbl DirectMusicBuffer_Vtbl = {
277 IDirectMusicBufferImpl_QueryInterface,
278 IDirectMusicBufferImpl_AddRef,
279 IDirectMusicBufferImpl_Release,
280 IDirectMusicBufferImpl_Flush,
281 IDirectMusicBufferImpl_TotalTime,
282 IDirectMusicBufferImpl_PackStructured,
283 IDirectMusicBufferImpl_PackUnstructured,
284 IDirectMusicBufferImpl_ResetReadPtr,
285 IDirectMusicBufferImpl_GetNextEvent,
286 IDirectMusicBufferImpl_GetRawBufferPtr,
287 IDirectMusicBufferImpl_GetStartTime,
288 IDirectMusicBufferImpl_GetUsedBytes,
289 IDirectMusicBufferImpl_GetMaxBytes,
290 IDirectMusicBufferImpl_GetBufferFormat,
291 IDirectMusicBufferImpl_SetStartTime,
292 IDirectMusicBufferImpl_SetUsedBytes
295 HRESULT DMUSIC_CreateDirectMusicBufferImpl(LPDMUS_BUFFERDESC desc, LPVOID* ret_iface)
297 IDirectMusicBufferImpl* dmbuffer;
299 TRACE("(%p, %p)\n", desc, ret_iface);
301 *ret_iface = NULL;
303 dmbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBufferImpl));
304 if (!dmbuffer)
305 return E_OUTOFMEMORY;
307 dmbuffer->IDirectMusicBuffer_iface.lpVtbl = &DirectMusicBuffer_Vtbl;
308 dmbuffer->ref = 1;
310 if (IsEqualGUID(&desc->guidBufferFormat, &GUID_NULL))
311 dmbuffer->format = KSDATAFORMAT_SUBTYPE_MIDI;
312 else
313 dmbuffer->format = desc->guidBufferFormat;
314 dmbuffer->size = (desc->cbBuffer + 3) & ~3; /* Buffer size must be multiple of 4 bytes */
316 dmbuffer->data = HeapAlloc(GetProcessHeap(), 0, dmbuffer->size);
317 if (!dmbuffer->data) {
318 HeapFree(GetProcessHeap(), 0, dmbuffer);
319 return E_OUTOFMEMORY;
322 DMUSIC_LockModule();
323 *ret_iface = &dmbuffer->IDirectMusicBuffer_iface;
325 return S_OK;