1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
//-----------------------------------------------------------------------------
// File: DMUtil.cpp
//
// Desc: DirectMusic framework classes for playing DirectMusic segments and
// DirectMusic scripts. Feel free to use this class as a starting point
// for adding extra functionality.
//
// Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <dmusicc.h>
#include <dmusici.h>
#include <dsound.h>
#include <dxerr8.h>
#include "DMUtil.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name: CMusicManager::CMusicManager()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CMusicManager::CMusicManager()
{
m_pLoader = NULL;
m_pPerformance = NULL;
// Initialize COM
HRESULT hr = CoInitialize(NULL);
m_bCleanupCOM = SUCCEEDED(hr);
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::~CMusicManager()
// Desc: Destroys the class
//-----------------------------------------------------------------------------
CMusicManager::~CMusicManager()
{
SAFE_RELEASE( m_pLoader );
if( m_pPerformance )
{
// If there is any music playing, stop it.
m_pPerformance->Stop( NULL, NULL, 0, 0 );
m_pPerformance->CloseDown();
SAFE_RELEASE( m_pPerformance );
}
if( m_bCleanupCOM )
CoUninitialize();
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::Initialize()
// Desc: Inits DirectMusic using a standard audio path
//-----------------------------------------------------------------------------
HRESULT CMusicManager::Initialize( HWND hWnd, DWORD dwPChannels, DWORD dwDefaultPathType )
{
HRESULT hr;
// Create loader object
if( FAILED( hr = CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader8, (void**)&m_pLoader ) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Create performance object
if( FAILED( hr = CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
IID_IDirectMusicPerformance8, (void**)&m_pPerformance ) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Initialize the performance with the standard audio path.
// This initializes both DirectMusic and DirectSound and
// sets up the synthesizer. Typcially its easist to use an
// audio path for playing music and sound effects.
if( FAILED( hr = m_pPerformance->InitAudio( NULL, NULL, hWnd, dwDefaultPathType,
dwPChannels, DMUS_AUDIOF_ALL, NULL ) ) )
{
if( hr == DSERR_NODRIVER )
{
DXTRACE( "Warning: No sound card found\n" );
return hr;
}
return DXTRACE_ERR( TEXT("InitAudio"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::SetSearchDirectory()
// Desc: Sets the search directory. If not called, the current working
// directory is used to load content.
//-----------------------------------------------------------------------------
HRESULT CMusicManager::SetSearchDirectory( const TCHAR* strMediaPath )
{
if( NULL == m_pLoader )
return E_UNEXPECTED;
// DMusic only takes wide strings
WCHAR wstrMediaPath[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrMediaPath, strMediaPath );
return m_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes,
wstrMediaPath, FALSE );
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::GetDefaultAudioPath()
// Desc:
//-----------------------------------------------------------------------------
IDirectMusicAudioPath8* CMusicManager::GetDefaultAudioPath()
{
IDirectMusicAudioPath8* pAudioPath = NULL;
if( NULL == m_pPerformance )
return NULL;
m_pPerformance->GetDefaultAudioPath( &pAudioPath );
return pAudioPath;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CollectGarbage()
// Desc: Tells the loader to cleanup any garbage from previously
// released objects.
//-----------------------------------------------------------------------------
VOID CMusicManager::CollectGarbage()
{
if( m_pLoader )
m_pLoader->CollectGarbage();
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateSegmentFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateSegmentFromFile( CMusicSegment** ppSegment,
TCHAR* strFileName,
BOOL bDownloadNow,
BOOL bIsMidiFile )
{
HRESULT hr;
IDirectMusicSegment8* pSegment = NULL;
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
if ( FAILED( hr = m_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
wstrFileName,
(LPVOID*) &pSegment ) ) )
{
if( hr == DMUS_E_LOADER_FAILEDOPEN )
return hr;
return DXTRACE_ERR( TEXT("LoadObjectFromFile"), hr );
}
*ppSegment = new CMusicSegment( m_pPerformance, m_pLoader, pSegment );
if (!*ppSegment)
return E_OUTOFMEMORY;
if( bIsMidiFile )
{
if( FAILED( hr = pSegment->SetParam( GUID_StandardMIDIFile,
0xFFFFFFFF, 0, 0, NULL ) ) )
return DXTRACE_ERR( TEXT("SetParam"), hr );
}
if( bDownloadNow )
{
if( FAILED( hr = (*ppSegment)->Download() ) )
return DXTRACE_ERR( TEXT("Download"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateSegmentFromResource()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateSegmentFromResource( CMusicSegment** ppSegment,
TCHAR* strResource,
TCHAR* strResourceType,
BOOL bDownloadNow,
BOOL bIsMidiFile )
{
HRESULT hr;
IDirectMusicSegment8* pSegment = NULL;
HRSRC hres = NULL;
void* pMem = NULL;
DWORD dwSize = 0;
DMUS_OBJECTDESC objdesc;
// Find the resource
hres = FindResource( NULL,strResource,strResourceType );
if( NULL == hres )
return E_FAIL;
// Load the resource
pMem = (void*)LoadResource( NULL, hres );
if( NULL == pMem )
return E_FAIL;
// Store the size of the resource
dwSize = SizeofResource( NULL, hres );
// Set up our object description
ZeroMemory(&objdesc,sizeof(DMUS_OBJECTDESC));
objdesc.dwSize = sizeof(DMUS_OBJECTDESC);
objdesc.dwValidData = DMUS_OBJ_MEMORY | DMUS_OBJ_CLASS;
objdesc.guidClass = CLSID_DirectMusicSegment;
objdesc.llMemLength =(LONGLONG)dwSize;
objdesc.pbMemData = (BYTE*)pMem;
if (FAILED ( hr = m_pLoader->GetObject( &objdesc,
IID_IDirectMusicSegment8,
(void**)&pSegment ) ) )
{
if( hr == DMUS_E_LOADER_FAILEDOPEN )
return hr;
return DXTRACE_ERR( TEXT("LoadObjectFromFile"), hr );
}
*ppSegment = new CMusicSegment( m_pPerformance, m_pLoader, pSegment );
if( NULL == *ppSegment )
return E_OUTOFMEMORY;
if( bIsMidiFile )
{
// Do this to make sure that the default General MIDI set
// is connected appropriately to the MIDI file and
// all instruments sound correct.
if( FAILED( hr = pSegment->SetParam( GUID_StandardMIDIFile,
0xFFFFFFFF, 0, 0, NULL ) ) )
return DXTRACE_ERR( TEXT("SetParam"), hr );
}
if( bDownloadNow )
{
// The segment needs to be download first before playing.
// However, some apps may want to wait before calling this
// to because the download allocates memory for the
// instruments. The more instruments currently downloaded,
// the more memory is in use by the synthesizer.
if( FAILED( hr = (*ppSegment)->Download() ) )
return DXTRACE_ERR( TEXT("Download"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateScriptFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateScriptFromFile( CMusicScript** ppScript,
TCHAR* strFileName )
{
HRESULT hr;
IDirectMusicScript* pScript = NULL;
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
if ( FAILED( hr = m_pLoader->LoadObjectFromFile( CLSID_DirectMusicScript,
IID_IDirectMusicScript8,
wstrFileName,
(LPVOID*) &pScript ) ) )
return DXTRACE_ERR_NOMSGBOX( TEXT("LoadObjectFromFile"), hr );
if ( FAILED( hr = pScript->Init( m_pPerformance, NULL ) ) )
return DXTRACE_ERR( TEXT("Init"), hr );
*ppScript = new CMusicScript( m_pPerformance, m_pLoader, pScript );
if (!*ppScript)
return E_OUTOFMEMORY;
return hr;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateChordMapFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateChordMapFromFile( IDirectMusicChordMap8** ppChordMap,
TCHAR* strFileName )
{
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
return m_pLoader->LoadObjectFromFile( CLSID_DirectMusicChordMap,
IID_IDirectMusicChordMap8,
wstrFileName, (LPVOID*) ppChordMap );
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateChordMapFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateStyleFromFile( IDirectMusicStyle8** ppStyle,
TCHAR* strFileName )
{
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
return m_pLoader->LoadObjectFromFile( CLSID_DirectMusicStyle,
IID_IDirectMusicStyle8,
wstrFileName, (LPVOID*) ppStyle );
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::GetMotifFromStyle()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::GetMotifFromStyle( IDirectMusicSegment8** ppMotif8,
TCHAR* strStyle, TCHAR* strMotif )
{
HRESULT hr;
IDirectMusicStyle8* pStyle = NULL;
IDirectMusicSegment* pMotif = NULL;
if( FAILED( hr = CreateStyleFromFile( &pStyle, strStyle ) ) )
return DXTRACE_ERR( TEXT("CreateStyleFromFile"), hr );
if( pStyle )
{
// DMusic only takes wide strings
WCHAR wstrMotif[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrMotif, strMotif );
hr = pStyle->GetMotif( wstrMotif, &pMotif );
SAFE_RELEASE( pStyle );
if( FAILED( hr ) )
return DXTRACE_ERR( TEXT("GetMotif"), hr );
pMotif->QueryInterface( IID_IDirectMusicSegment8, (LPVOID*) ppMotif8 );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::CMusicSegment()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CMusicSegment::CMusicSegment( IDirectMusicPerformance8* pPerformance,
IDirectMusicLoader8* pLoader,
IDirectMusicSegment8* pSegment )
{
m_pPerformance = pPerformance;
m_pLoader = pLoader;
m_pSegment = pSegment;
m_pEmbeddedAudioPath = NULL;
m_bDownloaded = FALSE;
// Try to pull out an audio path from the segment itself if there is one.
// This embedded audio path will be used instead of the default
// audio path if the app doesn't wish to use an overriding audio path.
IUnknown* pConfig = NULL;
if( SUCCEEDED( m_pSegment->GetAudioPathConfig( &pConfig ) ) )
{
m_pPerformance->CreateAudioPath( pConfig, TRUE, &m_pEmbeddedAudioPath );
SAFE_RELEASE( pConfig );
}
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::~CMusicSegment()
// Desc: Destroys the class
//-----------------------------------------------------------------------------
CMusicSegment::~CMusicSegment()
{
if( m_pSegment )
{
// Tell the loader that this object should now be released
if( m_pLoader )
m_pLoader->ReleaseObjectByUnknown( m_pSegment );
if( m_bDownloaded )
{
if( m_pEmbeddedAudioPath )
m_pSegment->Unload( m_pEmbeddedAudioPath );
else
m_pSegment->Unload( m_pPerformance );
}
SAFE_RELEASE( m_pEmbeddedAudioPath );
SAFE_RELEASE( m_pSegment );
}
m_pPerformance = NULL;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::Play()
// Desc: Plays the sound using voice management flags. Pass in DSBPLAY_LOOPING
// in the dwFlags to loop the sound
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::Play( DWORD dwFlags, IDirectMusicAudioPath8* pAudioPath )
{
if( m_pSegment == NULL || m_pPerformance == NULL )
return CO_E_NOTINITIALIZED;
if( !m_bDownloaded )
return E_FAIL;
// If an audio path was passed in then use it, otherwise
// use the embedded audio path if there was one.
if( pAudioPath == NULL && m_pEmbeddedAudioPath != NULL )
pAudioPath = m_pEmbeddedAudioPath;
// If pAudioPath is NULL then this plays on the default audio path.
return m_pPerformance->PlaySegmentEx( m_pSegment, 0, NULL, dwFlags,
0, 0, NULL, pAudioPath );
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::Download()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::Download( IDirectMusicAudioPath8* pAudioPath )
{
HRESULT hr;
if( m_pSegment == NULL )
return CO_E_NOTINITIALIZED;
// If no audio path was passed in, then download
// to the embedded audio path if it exists
// else download to the performance
if( pAudioPath == NULL )
{
if( m_pEmbeddedAudioPath )
hr = m_pSegment->Download( m_pEmbeddedAudioPath );
else
hr = m_pSegment->Download( m_pPerformance );
}
else
{
hr = m_pSegment->Download( pAudioPath );
}
if ( SUCCEEDED( hr ) )
m_bDownloaded = TRUE;
return hr;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::Unload()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::Unload( IDirectMusicAudioPath8* pAudioPath )
{
HRESULT hr;
if( m_pSegment == NULL )
return CO_E_NOTINITIALIZED;
// If no audio path was passed in, then unload
// from the embedded audio path if it exists
// else unload from the performance
if( pAudioPath == NULL )
{
if( m_pEmbeddedAudioPath )
hr = m_pSegment->Unload( m_pEmbeddedAudioPath );
else
hr = m_pSegment->Unload( m_pPerformance );
}
else
{
hr = m_pSegment->Unload( pAudioPath );
}
if ( SUCCEEDED( hr ) )
m_bDownloaded = FALSE;
return hr;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::IsPlaying()
// Desc:
//-----------------------------------------------------------------------------
BOOL CMusicSegment::IsPlaying()
{
if( m_pSegment == NULL || m_pPerformance == NULL )
return CO_E_NOTINITIALIZED;
return ( m_pPerformance->IsPlaying( m_pSegment, NULL ) == S_OK );
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::Stop()
// Desc: Stops the sound from playing
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::Stop( DWORD dwFlags )
{
if( m_pSegment == NULL || m_pPerformance == NULL )
return CO_E_NOTINITIALIZED;
return m_pPerformance->Stop( m_pSegment, NULL, 0, dwFlags );;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::SetRepeats()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::SetRepeats( DWORD dwRepeats )
{
if( m_pSegment == NULL )
return CO_E_NOTINITIALIZED;
return m_pSegment->SetRepeats( dwRepeats );
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::GetStyle()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::GetStyle( IDirectMusicStyle8** ppStyle, DWORD dwStyleIndex )
{
// Get the Style from the Segment by calling the Segment's GetData() with
// the data type GUID_StyleTrackStyle. 0xffffffff indicates to look at
// tracks in all TrackGroups in the segment. The first 0 indicates to
// retrieve the Style from the first Track in the indicated TrackGroup.
// The second 0 indicates to retrieve the Style from the beginning of the
// segment, i.e. time 0 in Segment time. If this Segment was loaded from a
// section file, there is only one Style and it is at time 0.
return m_pSegment->GetParam( GUID_IDirectMusicStyle, 0xffffffff, dwStyleIndex,
0, NULL, (VOID*)ppStyle );
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::CMusicScript()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CMusicScript::CMusicScript( IDirectMusicPerformance8* pPerformance,
IDirectMusicLoader8* pLoader,
IDirectMusicScript8* pScript )
{
m_pPerformance = pPerformance;
m_pLoader = pLoader;
m_pScript = pScript;
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::~CMusicScript()
// Desc: Destroys the class
//-----------------------------------------------------------------------------
CMusicScript::~CMusicScript()
{
if( m_pLoader )
{
// Tell the loader that this object should now be released
m_pLoader->ReleaseObjectByUnknown( m_pScript );
m_pLoader = NULL;
}
SAFE_RELEASE( m_pScript );
m_pPerformance = NULL;
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::Play()
// Desc: Calls a routine in the script
//-----------------------------------------------------------------------------
HRESULT CMusicScript::CallRoutine( TCHAR* strRoutine )
{
// DMusic only takes wide strings
WCHAR wstrRoutine[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrRoutine, strRoutine );
return m_pScript->CallRoutine( wstrRoutine, NULL );
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::SetVariableNumber()
// Desc: Sets the value of a variable in the script
//-----------------------------------------------------------------------------
HRESULT CMusicScript::SetVariableNumber( TCHAR* strVariable, LONG lValue )
{
// DMusic only takes wide strings
WCHAR wstrVariable[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrVariable, strVariable );
return m_pScript->SetVariableNumber( wstrVariable, lValue, NULL );
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::GetVariableNumber()
// Desc: Gets the value of a variable in the script
//-----------------------------------------------------------------------------
HRESULT CMusicScript::GetVariableNumber( TCHAR* strVariable, LONG* plValue )
{
// DMusic only takes wide strings
WCHAR wstrVariable[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrVariable, strVariable );
return m_pScript->GetVariableNumber( wstrVariable, plValue, NULL );
}
|