[DMUSIC]
[reactos.git] / reactos / dll / directx / wine / dmusic / buffer.c
1 /*
2 * IDirectMusicBuffer Implementation
3 *
4 * Copyright (C) 2003-2004 Rok Mandeljc
5 * Copyright (C) 2012 Christian Costa
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "dmusic_private.h"
23
24 #include <dmksctrl.h>
25
26 static inline IDirectMusicBufferImpl *impl_from_IDirectMusicBuffer(IDirectMusicBuffer *iface)
27 {
28 return CONTAINING_RECORD(iface, IDirectMusicBufferImpl, IDirectMusicBuffer_iface);
29 }
30
31 /* IDirectMusicBufferImpl IUnknown part: */
32 static HRESULT WINAPI IDirectMusicBufferImpl_QueryInterface(LPDIRECTMUSICBUFFER iface, REFIID riid, LPVOID *ret_iface)
33 {
34 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
35
36 if (IsEqualIID(riid, &IID_IUnknown) ||
37 IsEqualIID(riid, &IID_IDirectMusicBuffer))
38 {
39 IDirectMusicBuffer_AddRef(iface);
40 *ret_iface = iface;
41 return S_OK;
42 }
43
44 *ret_iface = NULL;
45
46 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
47
48 return E_NOINTERFACE;
49 }
50
51 static ULONG WINAPI IDirectMusicBufferImpl_AddRef(LPDIRECTMUSICBUFFER iface)
52 {
53 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
54 ULONG ref = InterlockedIncrement(&This->ref);
55
56 TRACE("(%p)->(): new ref = %u\n", iface, ref);
57
58 DMUSIC_LockModule();
59
60 return ref;
61 }
62
63 static ULONG WINAPI IDirectMusicBufferImpl_Release(LPDIRECTMUSICBUFFER iface)
64 {
65 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
66 ULONG ref = InterlockedDecrement(&This->ref);
67
68 TRACE("(%p)->(): new ref = %u\n", iface, ref);
69
70 if (!ref) {
71 HeapFree(GetProcessHeap(), 0, This->data);
72 HeapFree(GetProcessHeap(), 0, This);
73 }
74
75 DMUSIC_UnlockModule();
76
77 return ref;
78 }
79
80 /* IDirectMusicBufferImpl IDirectMusicBuffer part: */
81 static HRESULT WINAPI IDirectMusicBufferImpl_Flush(LPDIRECTMUSICBUFFER iface)
82 {
83 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
84
85 TRACE("(%p)->()\n", iface);
86
87 This->write_pos = 0;
88
89 return S_OK;
90 }
91
92 static HRESULT WINAPI IDirectMusicBufferImpl_TotalTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prtTime)
93 {
94 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
95
96 FIXME("(%p, %p): stub\n", This, prtTime);
97
98 return S_OK;
99 }
100
101 static HRESULT WINAPI IDirectMusicBufferImpl_PackStructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time, DWORD channel_group, DWORD channel_message)
102 {
103 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
104 DWORD new_write_pos = This->write_pos + sizeof(DMUS_EVENTHEADER) + sizeof(DWORD);
105 DMUS_EVENTHEADER header;
106
107 TRACE("(%p)->(0x%s, %u, 0x%x)\n", iface, wine_dbgstr_longlong(ref_time), channel_group, channel_message);
108
109 if (new_write_pos > This->size)
110 return DMUS_E_BUFFER_FULL;
111
112 /* Channel_message 0xZZYYXX (3 bytes) is a midi message where XX = status byte, YY = byte 1 and ZZ = byte 2 */
113
114 if (!(channel_message & 0x80))
115 {
116 /* Status byte MSB is always set */
117 return DMUS_E_INVALID_EVENT;
118 }
119
120 if (!This->write_pos)
121 This->start_time = ref_time;
122
123 header.cbEvent = 3; /* Midi message takes 4 bytes space but only 3 are relevant */
124 header.dwChannelGroup = channel_group;
125 header.rtDelta = ref_time - This->start_time;
126 header.dwFlags = DMUS_EVENT_STRUCTURED;
127
128 memcpy(This->data + This->write_pos, &header, sizeof(header));
129 *(DWORD*)(This->data + This->write_pos + sizeof(header)) = channel_message;
130 This->write_pos = new_write_pos;
131
132 return S_OK;
133 }
134
135 static HRESULT WINAPI IDirectMusicBufferImpl_PackUnstructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME rt, DWORD dwChannelGroup, DWORD cb, LPBYTE lpb)
136 {
137 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
138
139 FIXME("(%p, 0x%s, %d, %d, %p): stub\n", This, wine_dbgstr_longlong(rt), dwChannelGroup, cb, lpb);
140
141 return S_OK;
142 }
143
144 static HRESULT WINAPI IDirectMusicBufferImpl_ResetReadPtr(LPDIRECTMUSICBUFFER iface)
145 {
146 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
147
148 FIXME("(%p): stub\n", This);
149
150 return S_OK;
151 }
152
153 static HRESULT WINAPI IDirectMusicBufferImpl_GetNextEvent(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prt, LPDWORD pdwChannelGroup, LPDWORD pdwLength, LPBYTE* ppData)
154 {
155 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
156
157 FIXME("(%p, %p, %p, %p, %p): stub\n", This, prt, pdwChannelGroup, pdwLength, ppData);
158
159 return S_OK;
160 }
161
162 static HRESULT WINAPI IDirectMusicBufferImpl_GetRawBufferPtr(LPDIRECTMUSICBUFFER iface, LPBYTE* data)
163 {
164 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
165
166 TRACE("(%p)->(%p)\n", iface, data);
167
168 if (!data)
169 return E_POINTER;
170
171 *data = This->data;
172
173 return S_OK;
174 }
175
176 static HRESULT WINAPI IDirectMusicBufferImpl_GetStartTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME ref_time)
177 {
178 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
179
180 TRACE("(%p)->(%p)\n", iface, ref_time);
181
182 if (!ref_time)
183 return E_POINTER;
184 if (!This->write_pos)
185 return DMUS_E_BUFFER_EMPTY;
186
187 *ref_time = This->start_time;
188
189 return S_OK;
190 }
191
192 static HRESULT WINAPI IDirectMusicBufferImpl_GetUsedBytes(LPDIRECTMUSICBUFFER iface, LPDWORD used_bytes)
193 {
194 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
195
196 TRACE("(%p)->(%p)\n", iface, used_bytes);
197
198 if (!used_bytes)
199 return E_POINTER;
200
201 *used_bytes = This->write_pos;
202
203 return S_OK;
204 }
205
206 static HRESULT WINAPI IDirectMusicBufferImpl_GetMaxBytes(LPDIRECTMUSICBUFFER iface, LPDWORD max_bytes)
207 {
208 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
209
210 TRACE("(%p)->(%p)\n", iface, max_bytes);
211
212 if (!max_bytes)
213 return E_POINTER;
214
215 *max_bytes = This->size;
216
217 return S_OK;
218 }
219
220 static HRESULT WINAPI IDirectMusicBufferImpl_GetBufferFormat(LPDIRECTMUSICBUFFER iface, LPGUID format)
221 {
222 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
223
224 TRACE("(%p)->(%p)\n", iface, format);
225
226 if (!format)
227 return E_POINTER;
228
229 *format = This->format;
230 return S_OK;
231 }
232
233 static HRESULT WINAPI IDirectMusicBufferImpl_SetStartTime(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time)
234 {
235 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
236
237 TRACE("(%p)->(0x%s)\n", This, wine_dbgstr_longlong(ref_time));
238
239 This->start_time = ref_time;
240
241 return S_OK;
242 }
243
244 static HRESULT WINAPI IDirectMusicBufferImpl_SetUsedBytes(LPDIRECTMUSICBUFFER iface, DWORD used_bytes)
245 {
246 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
247
248 TRACE("(%p)->(%u)\n", iface, used_bytes);
249
250 if (used_bytes > This->size)
251 return DMUS_E_BUFFER_FULL;
252
253 This->write_pos = used_bytes;
254
255 return S_OK;
256 }
257
258 static const IDirectMusicBufferVtbl DirectMusicBuffer_Vtbl = {
259 IDirectMusicBufferImpl_QueryInterface,
260 IDirectMusicBufferImpl_AddRef,
261 IDirectMusicBufferImpl_Release,
262 IDirectMusicBufferImpl_Flush,
263 IDirectMusicBufferImpl_TotalTime,
264 IDirectMusicBufferImpl_PackStructured,
265 IDirectMusicBufferImpl_PackUnstructured,
266 IDirectMusicBufferImpl_ResetReadPtr,
267 IDirectMusicBufferImpl_GetNextEvent,
268 IDirectMusicBufferImpl_GetRawBufferPtr,
269 IDirectMusicBufferImpl_GetStartTime,
270 IDirectMusicBufferImpl_GetUsedBytes,
271 IDirectMusicBufferImpl_GetMaxBytes,
272 IDirectMusicBufferImpl_GetBufferFormat,
273 IDirectMusicBufferImpl_SetStartTime,
274 IDirectMusicBufferImpl_SetUsedBytes
275 };
276
277 HRESULT DMUSIC_CreateDirectMusicBufferImpl(LPDMUS_BUFFERDESC desc, LPVOID* ret_iface)
278 {
279 IDirectMusicBufferImpl* dmbuffer;
280 HRESULT hr;
281
282 TRACE("(%p, %p)\n", desc, ret_iface);
283
284 *ret_iface = NULL;
285
286 dmbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBufferImpl));
287 if (!dmbuffer)
288 return E_OUTOFMEMORY;
289
290 dmbuffer->IDirectMusicBuffer_iface.lpVtbl = &DirectMusicBuffer_Vtbl;
291 dmbuffer->ref = 0; /* Will be inited by QueryInterface */
292
293 if (IsEqualGUID(&desc->guidBufferFormat, &GUID_NULL))
294 dmbuffer->format = KSDATAFORMAT_SUBTYPE_MIDI;
295 else
296 dmbuffer->format = desc->guidBufferFormat;
297 dmbuffer->size = (desc->cbBuffer + 3) & ~3; /* Buffer size must be multiple of 4 bytes */
298
299 dmbuffer->data = HeapAlloc(GetProcessHeap(), 0, dmbuffer->size);
300 if (!dmbuffer->data) {
301 HeapFree(GetProcessHeap(), 0, dmbuffer);
302 return E_OUTOFMEMORY;
303 }
304
305 hr = IDirectMusicBufferImpl_QueryInterface((LPDIRECTMUSICBUFFER)dmbuffer, &IID_IDirectMusicBuffer, ret_iface);
306 if (FAILED(hr))
307 {
308 HeapFree(GetProcessHeap(), 0, dmbuffer->data);
309 HeapFree(GetProcessHeap(), 0, dmbuffer);
310 }
311
312 return hr;
313 }