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