netprofm: Implement INetworkListManager::GetNetwork.
[wine/multimedia.git] / dlls / dmusic / buffer.c
blobbd0ad1ac0f5e6d78314bdd69735b794dfbc68998
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 return 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);
70 if (!ref) {
71 HeapFree(GetProcessHeap(), 0, This->data);
72 HeapFree(GetProcessHeap(), 0, This);
73 DMUSIC_UnlockModule();
76 return ref;
79 /* IDirectMusicBufferImpl IDirectMusicBuffer part: */
80 static HRESULT WINAPI IDirectMusicBufferImpl_Flush(LPDIRECTMUSICBUFFER iface)
82 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
84 TRACE("(%p)->()\n", iface);
86 This->write_pos = 0;
88 return S_OK;
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);
97 return S_OK;
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;
131 return S_OK;
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);
140 return S_OK;
143 static HRESULT WINAPI IDirectMusicBufferImpl_ResetReadPtr(LPDIRECTMUSICBUFFER iface)
145 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
147 FIXME("(%p): stub\n", This);
149 return S_OK;
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);
158 return S_OK;
161 static HRESULT WINAPI IDirectMusicBufferImpl_GetRawBufferPtr(LPDIRECTMUSICBUFFER iface, LPBYTE* data)
163 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
165 TRACE("(%p)->(%p)\n", iface, data);
167 if (!data)
168 return E_POINTER;
170 *data = This->data;
172 return S_OK;
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);
181 if (!ref_time)
182 return E_POINTER;
183 if (!This->write_pos)
184 return DMUS_E_BUFFER_EMPTY;
186 *ref_time = This->start_time;
188 return S_OK;
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);
197 if (!used_bytes)
198 return E_POINTER;
200 *used_bytes = This->write_pos;
202 return S_OK;
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);
211 if (!max_bytes)
212 return E_POINTER;
214 *max_bytes = This->size;
216 return S_OK;
219 static HRESULT WINAPI IDirectMusicBufferImpl_GetBufferFormat(LPDIRECTMUSICBUFFER iface, LPGUID format)
221 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
223 TRACE("(%p)->(%p)\n", iface, format);
225 if (!format)
226 return E_POINTER;
228 *format = This->format;
229 return S_OK;
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;
240 return S_OK;
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;
254 return S_OK;
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);
282 *ret_iface = NULL;
284 dmbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBufferImpl));
285 if (!dmbuffer)
286 return E_OUTOFMEMORY;
288 dmbuffer->IDirectMusicBuffer_iface.lpVtbl = &DirectMusicBuffer_Vtbl;
289 dmbuffer->ref = 1;
291 if (IsEqualGUID(&desc->guidBufferFormat, &GUID_NULL))
292 dmbuffer->format = KSDATAFORMAT_SUBTYPE_MIDI;
293 else
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;
303 DMUSIC_LockModule();
304 *ret_iface = &dmbuffer->IDirectMusicBuffer_iface;
306 return S_OK;