author | peter1138 |
Mon, 23 Oct 2006 18:45:43 +0000 | |
changeset 4930 | 708801d486c6 |
parent 2894 | e768e08f7989 |
child 5380 | 8ea58542b6e0 |
permissions | -rw-r--r-- |
2197 | 1 |
/* $Id$ */ |
2 |
||
3 |
#include "../stdafx.h" |
|
4 |
||
5 |
#ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT |
|
6 |
||
7 |
extern "C" { |
|
8 |
#include "../openttd.h" |
|
9 |
#include "../debug.h" |
|
10 |
#include "../win32.h" |
|
11 |
#include "dmusic.h" |
|
12 |
} |
|
13 |
||
14 |
#include <windows.h> |
|
15 |
#include <dmksctrl.h> |
|
16 |
#include <dmusici.h> |
|
17 |
#include <dmusicc.h> |
|
18 |
#include <dmusicf.h> |
|
19 |
||
20 |
||
21 |
// the performance object controls manipulation of the segments |
|
22 |
static IDirectMusicPerformance* performance = NULL; |
|
23 |
||
24 |
// the loader object can load many types of DMusic related files |
|
25 |
static IDirectMusicLoader* loader = NULL; |
|
26 |
||
27 |
// the segment object is where the MIDI data is stored for playback |
|
28 |
static IDirectMusicSegment* segment = NULL; |
|
29 |
||
30 |
static bool seeking = false; |
|
31 |
||
32 |
||
33 |
#define M(x) x "\0" |
|
34 |
static const char ole_files[] = |
|
35 |
M("ole32.dll") |
|
36 |
M("CoCreateInstance") |
|
37 |
M("CoInitialize") |
|
38 |
M("CoUninitialize") |
|
39 |
M("") |
|
40 |
; |
|
41 |
#undef M |
|
42 |
||
43 |
struct ProcPtrs { |
|
44 |
unsigned long (WINAPI * CoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID* ppv); |
|
45 |
HRESULT (WINAPI * CoInitialize)(LPVOID pvReserved); |
|
46 |
void (WINAPI * CoUninitialize)(); |
|
47 |
}; |
|
48 |
||
49 |
static ProcPtrs proc; |
|
50 |
||
51 |
||
52 |
static const char* DMusicMidiStart(const char* const* parm) |
|
53 |
{ |
|
54 |
if (performance != NULL) return NULL; |
|
55 |
||
56 |
if (proc.CoCreateInstance == NULL) { |
|
57 |
if (!LoadLibraryList((Function*)&proc, ole_files)) |
|
58 |
return "ole32.dll load failed"; |
|
59 |
} |
|
60 |
||
61 |
// Initialize COM |
|
62 |
if (FAILED(proc.CoInitialize(NULL))) { |
|
63 |
return "COM initialization failed"; |
|
64 |
} |
|
65 |
||
66 |
// create the performance object |
|
67 |
if (FAILED(proc.CoCreateInstance( |
|
68 |
CLSID_DirectMusicPerformance, |
|
69 |
NULL, |
|
70 |
CLSCTX_INPROC, |
|
71 |
IID_IDirectMusicPerformance, |
|
72 |
(LPVOID*)&performance |
|
73 |
))) { |
|
74 |
proc.CoUninitialize(); |
|
75 |
return "Failed to create the performance object"; |
|
76 |
} |
|
77 |
||
78 |
// initialize it |
|
79 |
if (FAILED(performance->Init(NULL, NULL, NULL))) { |
|
80 |
performance->Release(); |
|
81 |
performance = NULL; |
|
82 |
proc.CoUninitialize(); |
|
83 |
return "Failed to initialize performance object"; |
|
84 |
} |
|
85 |
||
86 |
// choose default Windows synth |
|
87 |
if (FAILED(performance->AddPort(NULL))) { |
|
88 |
performance->CloseDown(); |
|
89 |
performance->Release(); |
|
90 |
performance = NULL; |
|
91 |
proc.CoUninitialize(); |
|
92 |
return "AddPort failed"; |
|
93 |
} |
|
94 |
||
95 |
// create the loader object; this will be used to load the MIDI file |
|
96 |
if (FAILED(proc.CoCreateInstance( |
|
97 |
CLSID_DirectMusicLoader, |
|
98 |
NULL, |
|
99 |
CLSCTX_INPROC, |
|
100 |
IID_IDirectMusicLoader, |
|
101 |
(LPVOID*)&loader |
|
102 |
))) { |
|
103 |
performance->CloseDown(); |
|
104 |
performance->Release(); |
|
105 |
performance = NULL; |
|
106 |
proc.CoUninitialize(); |
|
107 |
return "Failed to create loader object"; |
|
108 |
} |
|
109 |
||
110 |
return NULL; |
|
111 |
} |
|
112 |
||
113 |
||
114 |
static void DMusicMidiStop(void) |
|
115 |
{ |
|
116 |
seeking = false; |
|
117 |
||
2396
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
118 |
if (performance != NULL) performance->Stop(NULL, NULL, 0, 0); |
2197 | 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 (segment != NULL) { |
2894 | 121 |
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
|
122 |
segment->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
123 |
segment = NULL; |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
124 |
} |
2197 | 125 |
|
2396
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
126 |
if (performance != NULL) { |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
127 |
performance->CloseDown(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
128 |
performance->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
129 |
performance = NULL; |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
130 |
} |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
131 |
|
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
132 |
if (loader != NULL) { |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
133 |
loader->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
134 |
loader = NULL; |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
135 |
} |
2197 | 136 |
|
137 |
proc.CoUninitialize(); |
|
138 |
} |
|
139 |
||
140 |
||
141 |
static void DMusicMidiPlaySong(const char* filename) |
|
142 |
{ |
|
143 |
// set up the loader object info |
|
144 |
DMUS_OBJECTDESC obj_desc; |
|
145 |
ZeroMemory(&obj_desc, sizeof(obj_desc)); |
|
146 |
obj_desc.dwSize = sizeof(obj_desc); |
|
147 |
obj_desc.guidClass = CLSID_DirectMusicSegment; |
|
148 |
obj_desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH; |
|
149 |
MultiByteToWideChar( |
|
150 |
CP_ACP, MB_PRECOMPOSED, |
|
151 |
filename, -1, |
|
152 |
obj_desc.wszFileName, lengthof(obj_desc.wszFileName) |
|
153 |
); |
|
154 |
||
155 |
// release the existing segment if we have any |
|
156 |
if (segment != NULL) { |
|
157 |
segment->Release(); |
|
158 |
segment = NULL; |
|
159 |
} |
|
160 |
||
161 |
// make a new segment |
|
162 |
if (FAILED(loader->GetObject( |
|
163 |
&obj_desc, IID_IDirectMusicSegment, (LPVOID*)&segment |
|
164 |
))) { |
|
165 |
DEBUG(misc, 0) ("DirectMusic: Get object failed"); |
|
166 |
return; |
|
167 |
} |
|
168 |
||
169 |
// tell the segment what kind of data it contains |
|
170 |
if (FAILED(segment->SetParam( |
|
171 |
GUID_StandardMIDIFile, 0xFFFFFFFF, 0, 0, performance |
|
172 |
))) { |
|
173 |
DEBUG(misc, 0) ("DirectMusic: SetParam (MIDI file) failed"); |
|
174 |
return; |
|
175 |
} |
|
176 |
||
177 |
// tell the segment to 'download' the instruments |
|
178 |
if (FAILED(segment->SetParam(GUID_Download, 0xFFFFFFFF, 0, 0, performance))) { |
|
179 |
DEBUG(misc, 0) ("DirectMusic: Failed to download instruments"); |
|
180 |
return; |
|
181 |
} |
|
182 |
||
183 |
// start playing the MIDI file |
|
184 |
if (FAILED(performance->PlaySegment(segment, 0, 0, NULL))) { |
|
185 |
DEBUG(misc, 0) ("DirectMusic: PlaySegment failed"); |
|
186 |
return; |
|
187 |
} |
|
188 |
||
189 |
seeking = true; |
|
190 |
} |
|
191 |
||
192 |
||
193 |
static void DMusicMidiStopSong(void) |
|
194 |
{ |
|
195 |
if (FAILED(performance->Stop(segment, NULL, 0, 0))) { |
|
196 |
DEBUG(misc, 0) ("DirecMusic: StopSegment failed"); |
|
197 |
} |
|
198 |
seeking = false; |
|
199 |
} |
|
200 |
||
201 |
||
202 |
static bool DMusicMidiIsSongPlaying(void) |
|
203 |
{ |
|
204 |
/* Not the nicest code, but there is a short delay before playing actually |
|
205 |
* starts. OpenTTD makes no provision for this. */ |
|
206 |
if (performance->IsPlaying(segment, NULL) == S_OK) { |
|
207 |
seeking = false; |
|
208 |
return true; |
|
209 |
} else { |
|
210 |
return seeking; |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
||
215 |
static void DMusicMidiSetVolume(byte vol) |
|
216 |
{ |
|
217 |
// 0 - 127 -> -2000 - 0 |
|
218 |
long db = vol * 2000 / 127 - 2000; |
|
219 |
performance->SetGlobalParam(GUID_PerfMasterVolume, &db, sizeof(db)); |
|
220 |
} |
|
221 |
||
222 |
||
223 |
extern "C" const HalMusicDriver _dmusic_midi_driver = { |
|
224 |
DMusicMidiStart, |
|
225 |
DMusicMidiStop, |
|
226 |
DMusicMidiPlaySong, |
|
227 |
DMusicMidiStopSong, |
|
228 |
DMusicMidiIsSongPlaying, |
|
229 |
DMusicMidiSetVolume, |
|
230 |
}; |
|
231 |
||
232 |
#endif |