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