|
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 |
/* release everything but the segment, which the performance
|
|
|
117 |
* will release automatically (and it'll crash if it's been
|
|
|
118 |
* released already) */
|
|
|
119 |
|
|
|
120 |
seeking = false;
|
|
|
121 |
|
|
|
122 |
loader->Release();
|
|
|
123 |
loader = NULL;
|
|
|
124 |
|
|
|
125 |
performance->CloseDown();
|
|
|
126 |
performance->Release();
|
|
|
127 |
performance = NULL;
|
|
|
128 |
|
|
|
129 |
segment = NULL;
|
|
|
130 |
|
|
|
131 |
proc.CoUninitialize();
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
static void DMusicMidiPlaySong(const char* filename)
|
|
|
136 |
{
|
|
|
137 |
// set up the loader object info
|
|
|
138 |
DMUS_OBJECTDESC obj_desc;
|
|
|
139 |
ZeroMemory(&obj_desc, sizeof(obj_desc));
|
|
|
140 |
obj_desc.dwSize = sizeof(obj_desc);
|
|
|
141 |
obj_desc.guidClass = CLSID_DirectMusicSegment;
|
|
|
142 |
obj_desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH;
|
|
|
143 |
MultiByteToWideChar(
|
|
|
144 |
CP_ACP, MB_PRECOMPOSED,
|
|
|
145 |
filename, -1,
|
|
|
146 |
obj_desc.wszFileName, lengthof(obj_desc.wszFileName)
|
|
|
147 |
);
|
|
|
148 |
|
|
|
149 |
// release the existing segment if we have any
|
|
|
150 |
if (segment != NULL) {
|
|
|
151 |
segment->Release();
|
|
|
152 |
segment = NULL;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
// make a new segment
|
|
|
156 |
if (FAILED(loader->GetObject(
|
|
|
157 |
&obj_desc, IID_IDirectMusicSegment, (LPVOID*)&segment
|
|
|
158 |
))) {
|
|
|
159 |
DEBUG(misc, 0) ("DirectMusic: Get object failed");
|
|
|
160 |
return;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// tell the segment what kind of data it contains
|
|
|
164 |
if (FAILED(segment->SetParam(
|
|
|
165 |
GUID_StandardMIDIFile, 0xFFFFFFFF, 0, 0, performance
|
|
|
166 |
))) {
|
|
|
167 |
DEBUG(misc, 0) ("DirectMusic: SetParam (MIDI file) failed");
|
|
|
168 |
return;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
// tell the segment to 'download' the instruments
|
|
|
172 |
if (FAILED(segment->SetParam(GUID_Download, 0xFFFFFFFF, 0, 0, performance))) {
|
|
|
173 |
DEBUG(misc, 0) ("DirectMusic: Failed to download instruments");
|
|
|
174 |
return;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
// start playing the MIDI file
|
|
|
178 |
if (FAILED(performance->PlaySegment(segment, 0, 0, NULL))) {
|
|
|
179 |
DEBUG(misc, 0) ("DirectMusic: PlaySegment failed");
|
|
|
180 |
return;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
seeking = true;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
static void DMusicMidiStopSong(void)
|
|
|
188 |
{
|
|
|
189 |
if (FAILED(performance->Stop(segment, NULL, 0, 0))) {
|
|
|
190 |
DEBUG(misc, 0) ("DirecMusic: StopSegment failed");
|
|
|
191 |
}
|
|
|
192 |
seeking = false;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
static bool DMusicMidiIsSongPlaying(void)
|
|
|
197 |
{
|
|
|
198 |
/* Not the nicest code, but there is a short delay before playing actually
|
|
|
199 |
* starts. OpenTTD makes no provision for this. */
|
|
|
200 |
if (performance->IsPlaying(segment, NULL) == S_OK) {
|
|
|
201 |
seeking = false;
|
|
|
202 |
return true;
|
|
|
203 |
} else {
|
|
|
204 |
return seeking;
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
static void DMusicMidiSetVolume(byte vol)
|
|
|
210 |
{
|
|
|
211 |
// 0 - 127 -> -2000 - 0
|
|
|
212 |
long db = vol * 2000 / 127 - 2000;
|
|
|
213 |
performance->SetGlobalParam(GUID_PerfMasterVolume, &db, sizeof(db));
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
|
|
|
217 |
extern "C" const HalMusicDriver _dmusic_midi_driver = {
|
|
|
218 |
DMusicMidiStart,
|
|
|
219 |
DMusicMidiStop,
|
|
|
220 |
DMusicMidiPlaySong,
|
|
|
221 |
DMusicMidiStopSong,
|
|
|
222 |
DMusicMidiIsSongPlaying,
|
|
|
223 |
DMusicMidiSetVolume,
|
|
|
224 |
};
|
|
|
225 |
|
|
|
226 |
#endif
|