author | peter1138 |
Sun, 03 Aug 2008 17:35:08 +0000 | |
changeset 9834 | 7474149531c1 |
parent 9261 | e344e55e5cb2 |
permissions | -rw-r--r-- |
2197 | 1 |
/* $Id$ */ |
2 |
||
9111
48ce04029fe4
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
8262
diff
changeset
|
3 |
/** @file dmusic.cpp Playing music via DirectMusic. */ |
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
4 |
|
2197 | 5 |
#ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT |
6 |
||
6124
9f822ae6c086
(svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater
parents:
5988
diff
changeset
|
7 |
#include "../stdafx.h" |
9261
e344e55e5cb2
(svn r13127) -Fix (r13122): of course WIN32_LEAN_AND_MEAN excludes too much stuff
glx
parents:
9111
diff
changeset
|
8 |
#ifdef WIN32_LEAN_AND_MEAN |
e344e55e5cb2
(svn r13127) -Fix (r13122): of course WIN32_LEAN_AND_MEAN excludes too much stuff
glx
parents:
9111
diff
changeset
|
9 |
#undef WIN32_LEAN_AND_MEAN // Don't exclude rarely-used stuff from Windows headers |
e344e55e5cb2
(svn r13127) -Fix (r13122): of course WIN32_LEAN_AND_MEAN excludes too much stuff
glx
parents:
9111
diff
changeset
|
10 |
#endif |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5475
diff
changeset
|
11 |
#include "../debug.h" |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5475
diff
changeset
|
12 |
#include "../win32.h" |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5475
diff
changeset
|
13 |
#include "dmusic.h" |
2197 | 14 |
|
15 |
#include <windows.h> |
|
16 |
#include <dmksctrl.h> |
|
17 |
#include <dmusici.h> |
|
18 |
#include <dmusicc.h> |
|
19 |
#include <dmusicf.h> |
|
20 |
||
8262
45ec878cee4d
(svn r11826) -Fix (r10444): at least one instance of dmusic driver is needed for it to be registered and usable
glx
parents:
7170
diff
changeset
|
21 |
static FMusicDriver_DMusic iFMusicDriver_DMusic; |
2197 | 22 |
|
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
23 |
/** the performance object controls manipulation of the segments */ |
2197 | 24 |
static IDirectMusicPerformance* performance = NULL; |
25 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
26 |
/** the loader object can load many types of DMusic related files */ |
2197 | 27 |
static IDirectMusicLoader* loader = NULL; |
28 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
29 |
/** the segment object is where the MIDI data is stored for playback */ |
2197 | 30 |
static IDirectMusicSegment* segment = NULL; |
31 |
||
32 |
static bool seeking = false; |
|
33 |
||
34 |
||
35 |
#define M(x) x "\0" |
|
36 |
static const char ole_files[] = |
|
37 |
M("ole32.dll") |
|
38 |
M("CoCreateInstance") |
|
39 |
M("CoInitialize") |
|
40 |
M("CoUninitialize") |
|
41 |
M("") |
|
42 |
; |
|
43 |
#undef M |
|
44 |
||
45 |
struct ProcPtrs { |
|
46 |
unsigned long (WINAPI * CoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID* ppv); |
|
47 |
HRESULT (WINAPI * CoInitialize)(LPVOID pvReserved); |
|
48 |
void (WINAPI * CoUninitialize)(); |
|
49 |
}; |
|
50 |
||
51 |
static ProcPtrs proc; |
|
52 |
||
53 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
54 |
const char *MusicDriver_DMusic::Start(const char * const *parm) |
2197 | 55 |
{ |
56 |
if (performance != NULL) return NULL; |
|
57 |
||
58 |
if (proc.CoCreateInstance == NULL) { |
|
59 |
if (!LoadLibraryList((Function*)&proc, ole_files)) |
|
60 |
return "ole32.dll load failed"; |
|
61 |
} |
|
62 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
63 |
/* Initialize COM */ |
2197 | 64 |
if (FAILED(proc.CoInitialize(NULL))) { |
65 |
return "COM initialization failed"; |
|
66 |
} |
|
67 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
68 |
/* create the performance object */ |
2197 | 69 |
if (FAILED(proc.CoCreateInstance( |
70 |
CLSID_DirectMusicPerformance, |
|
71 |
NULL, |
|
72 |
CLSCTX_INPROC, |
|
73 |
IID_IDirectMusicPerformance, |
|
74 |
(LPVOID*)&performance |
|
75 |
))) { |
|
76 |
proc.CoUninitialize(); |
|
77 |
return "Failed to create the performance object"; |
|
78 |
} |
|
79 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
80 |
/* initialize it */ |
2197 | 81 |
if (FAILED(performance->Init(NULL, NULL, NULL))) { |
82 |
performance->Release(); |
|
83 |
performance = NULL; |
|
84 |
proc.CoUninitialize(); |
|
85 |
return "Failed to initialize performance object"; |
|
86 |
} |
|
87 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
88 |
/* choose default Windows synth */ |
2197 | 89 |
if (FAILED(performance->AddPort(NULL))) { |
90 |
performance->CloseDown(); |
|
91 |
performance->Release(); |
|
92 |
performance = NULL; |
|
93 |
proc.CoUninitialize(); |
|
94 |
return "AddPort failed"; |
|
95 |
} |
|
96 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
97 |
/* create the loader object; this will be used to load the MIDI file */ |
2197 | 98 |
if (FAILED(proc.CoCreateInstance( |
99 |
CLSID_DirectMusicLoader, |
|
100 |
NULL, |
|
101 |
CLSCTX_INPROC, |
|
102 |
IID_IDirectMusicLoader, |
|
103 |
(LPVOID*)&loader |
|
104 |
))) { |
|
105 |
performance->CloseDown(); |
|
106 |
performance->Release(); |
|
107 |
performance = NULL; |
|
108 |
proc.CoUninitialize(); |
|
109 |
return "Failed to create loader object"; |
|
110 |
} |
|
111 |
||
112 |
return NULL; |
|
113 |
} |
|
114 |
||
115 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
116 |
void MusicDriver_DMusic::Stop() |
2197 | 117 |
{ |
118 |
seeking = false; |
|
119 |
||
2396
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
120 |
if (performance != NULL) performance->Stop(NULL, NULL, 0, 0); |
2197 | 121 |
|
2396
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
122 |
if (segment != NULL) { |
2894 | 123 |
segment->SetParam(GUID_Unload, 0xFFFFFFFF, 0, 0, performance); |
2396
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
124 |
segment->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
125 |
segment = NULL; |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
126 |
} |
2197 | 127 |
|
2396
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
128 |
if (performance != NULL) { |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
129 |
performance->CloseDown(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
130 |
performance->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
131 |
performance = NULL; |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
132 |
} |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
133 |
|
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
134 |
if (loader != NULL) { |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
135 |
loader->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
136 |
loader = NULL; |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
137 |
} |
2197 | 138 |
|
139 |
proc.CoUninitialize(); |
|
140 |
} |
|
141 |
||
142 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
143 |
void MusicDriver_DMusic::PlaySong(const char* filename) |
2197 | 144 |
{ |
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
145 |
/* set up the loader object info */ |
2197 | 146 |
DMUS_OBJECTDESC obj_desc; |
147 |
ZeroMemory(&obj_desc, sizeof(obj_desc)); |
|
148 |
obj_desc.dwSize = sizeof(obj_desc); |
|
149 |
obj_desc.guidClass = CLSID_DirectMusicSegment; |
|
150 |
obj_desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH; |
|
151 |
MultiByteToWideChar( |
|
152 |
CP_ACP, MB_PRECOMPOSED, |
|
153 |
filename, -1, |
|
154 |
obj_desc.wszFileName, lengthof(obj_desc.wszFileName) |
|
155 |
); |
|
156 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
157 |
/* release the existing segment if we have any */ |
2197 | 158 |
if (segment != NULL) { |
159 |
segment->Release(); |
|
160 |
segment = NULL; |
|
161 |
} |
|
162 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
163 |
/* make a new segment */ |
2197 | 164 |
if (FAILED(loader->GetObject( |
165 |
&obj_desc, IID_IDirectMusicSegment, (LPVOID*)&segment |
|
166 |
))) { |
|
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
2894
diff
changeset
|
167 |
DEBUG(driver, 0, "DirectMusic: GetObject failed"); |
2197 | 168 |
return; |
169 |
} |
|
170 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
171 |
/* tell the segment what kind of data it contains */ |
2197 | 172 |
if (FAILED(segment->SetParam( |
173 |
GUID_StandardMIDIFile, 0xFFFFFFFF, 0, 0, performance |
|
174 |
))) { |
|
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
2894
diff
changeset
|
175 |
DEBUG(driver, 0, "DirectMusic: SetParam (MIDI file) failed"); |
2197 | 176 |
return; |
177 |
} |
|
178 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
179 |
/* tell the segment to 'download' the instruments */ |
2197 | 180 |
if (FAILED(segment->SetParam(GUID_Download, 0xFFFFFFFF, 0, 0, performance))) { |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
2894
diff
changeset
|
181 |
DEBUG(driver, 0, "DirectMusic: failed to download instruments"); |
2197 | 182 |
return; |
183 |
} |
|
184 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
185 |
/* start playing the MIDI file */ |
2197 | 186 |
if (FAILED(performance->PlaySegment(segment, 0, 0, NULL))) { |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
2894
diff
changeset
|
187 |
DEBUG(driver, 0, "DirectMusic: PlaySegment failed"); |
2197 | 188 |
return; |
189 |
} |
|
190 |
||
191 |
seeking = true; |
|
192 |
} |
|
193 |
||
194 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
195 |
void MusicDriver_DMusic::StopSong() |
2197 | 196 |
{ |
197 |
if (FAILED(performance->Stop(segment, NULL, 0, 0))) { |
|
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
2894
diff
changeset
|
198 |
DEBUG(driver, 0, "DirectMusic: StopSegment failed"); |
2197 | 199 |
} |
200 |
seeking = false; |
|
201 |
} |
|
202 |
||
203 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
204 |
bool MusicDriver_DMusic::IsSongPlaying() |
2197 | 205 |
{ |
206 |
/* Not the nicest code, but there is a short delay before playing actually |
|
207 |
* starts. OpenTTD makes no provision for this. */ |
|
208 |
if (performance->IsPlaying(segment, NULL) == S_OK) { |
|
209 |
seeking = false; |
|
210 |
return true; |
|
211 |
} else { |
|
212 |
return seeking; |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
217 |
void MusicDriver_DMusic::SetVolume(byte vol) |
2197 | 218 |
{ |
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
219 |
long db = vol * 2000 / 127 - 2000; ///< 0 - 127 -> -2000 - 0 |
2197 | 220 |
performance->SetGlobalParam(GUID_PerfMasterVolume, &db, sizeof(db)); |
221 |
} |
|
222 |
||
223 |
||
5988
1aabf94612c6
(svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents:
5587
diff
changeset
|
224 |
#endif /* WIN32_ENABLE_DIRECTMUSIC_SUPPORT */ |