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