author | KUDr |
Sat, 21 Apr 2007 08:23:57 +0000 | |
branch | cpp_gui |
changeset 6308 | 646711c5feaa |
parent 6298 | c30fe89622df |
child 6720 | 35756db7e577 |
permissions | -rw-r--r-- |
2197 | 1 |
/* $Id$ */ |
2 |
||
6308 | 3 |
/** @file dmusic.cpp */ |
4 |
||
2197 | 5 |
#ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT |
6 |
||
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6253
diff
changeset
|
7 |
#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
|
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 |
||
6308 | 19 |
/** the performance object controls manipulation of the segments */ |
2197 | 20 |
static IDirectMusicPerformance* performance = NULL; |
21 |
||
6308 | 22 |
/** the loader object can load many types of DMusic related files */ |
2197 | 23 |
static IDirectMusicLoader* loader = NULL; |
24 |
||
6308 | 25 |
/** the segment object is where the MIDI data is stored for playback */ |
2197 | 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 |
||
6308 | 59 |
/* Initialize COM */ |
2197 | 60 |
if (FAILED(proc.CoInitialize(NULL))) { |
61 |
return "COM initialization failed"; |
|
62 |
} |
|
63 |
||
6308 | 64 |
/* create the performance object */ |
2197 | 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 |
||
6308 | 76 |
/* initialize it */ |
2197 | 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 |
||
6308 | 84 |
/* choose default Windows synth */ |
2197 | 85 |
if (FAILED(performance->AddPort(NULL))) { |
86 |
performance->CloseDown(); |
|
87 |
performance->Release(); |
|
88 |
performance = NULL; |
|
89 |
proc.CoUninitialize(); |
|
90 |
return "AddPort failed"; |
|
91 |
} |
|
92 |
||
6308 | 93 |
/* create the loader object; this will be used to load the MIDI file */ |
2197 | 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 |
||
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6268
diff
changeset
|
112 |
static void DMusicMidiStop() |
2197 | 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 |
{ |
|
6308 | 141 |
/* set up the loader object info */ |
2197 | 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 |
||
6308 | 153 |
/* release the existing segment if we have any */ |
2197 | 154 |
if (segment != NULL) { |
155 |
segment->Release(); |
|
156 |
segment = NULL; |
|
157 |
} |
|
158 |
||
6308 | 159 |
/* make a new segment */ |
2197 | 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 |
||
6308 | 167 |
/* tell the segment what kind of data it contains */ |
2197 | 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 |
||
6308 | 175 |
/* tell the segment to 'download' the instruments */ |
2197 | 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 |
||
6308 | 181 |
/* start playing the MIDI file */ |
2197 | 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 |
||
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6268
diff
changeset
|
191 |
static void DMusicMidiStopSong() |
2197 | 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 |
||
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6268
diff
changeset
|
200 |
static bool DMusicMidiIsSongPlaying() |
2197 | 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 |
{ |
|
6308 | 215 |
long db = vol * 2000 / 127 - 2000; ///< 0 - 127 -> -2000 - 0 |
2197 | 216 |
performance->SetGlobalParam(GUID_PerfMasterVolume, &db, sizeof(db)); |
217 |
} |
|
218 |
||
219 |
||
6253 | 220 |
const HalMusicDriver _dmusic_midi_driver = { |
2197 | 221 |
DMusicMidiStart, |
222 |
DMusicMidiStop, |
|
223 |
DMusicMidiPlaySong, |
|
224 |
DMusicMidiStopSong, |
|
225 |
DMusicMidiIsSongPlaying, |
|
226 |
DMusicMidiSetVolume, |
|
227 |
}; |
|
228 |
||
6253 | 229 |
#endif /* WIN32_ENABLE_DIRECTMUSIC_SUPPORT */ |