author | glx |
Sun, 13 Jan 2008 00:14:29 +0000 | |
changeset 8758 | 91a623686e07 |
parent 7666 | a5fccd76176a |
child 10429 | 1b99254f9607 |
child 10724 | 68a692eacf22 |
permissions | -rw-r--r-- |
2197 | 1 |
/* $Id$ */ |
2 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
3 |
/** @file dmusic.cpp */ |
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
4 |
|
2197 | 5 |
#ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT |
6 |
||
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
|
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 |
||
8758
91a623686e07
(svn r11826) -Fix (r10444): at least one instance of dmusic driver is needed for it to be registered and usable
glx
parents:
7666
diff
changeset
|
18 |
static FMusicDriver_DMusic iFMusicDriver_DMusic; |
2197 | 19 |
|
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
20 |
/** the performance object controls manipulation of the segments */ |
2197 | 21 |
static IDirectMusicPerformance* performance = NULL; |
22 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
23 |
/** the loader object can load many types of DMusic related files */ |
2197 | 24 |
static IDirectMusicLoader* loader = NULL; |
25 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
26 |
/** the segment object is where the MIDI data is stored for playback */ |
2197 | 27 |
static IDirectMusicSegment* segment = NULL; |
28 |
||
29 |
static bool seeking = false; |
|
30 |
||
31 |
||
32 |
#define M(x) x "\0" |
|
33 |
static const char ole_files[] = |
|
34 |
M("ole32.dll") |
|
35 |
M("CoCreateInstance") |
|
36 |
M("CoInitialize") |
|
37 |
M("CoUninitialize") |
|
38 |
M("") |
|
39 |
; |
|
40 |
#undef M |
|
41 |
||
42 |
struct ProcPtrs { |
|
43 |
unsigned long (WINAPI * CoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID* ppv); |
|
44 |
HRESULT (WINAPI * CoInitialize)(LPVOID pvReserved); |
|
45 |
void (WINAPI * CoUninitialize)(); |
|
46 |
}; |
|
47 |
||
48 |
static ProcPtrs proc; |
|
49 |
||
50 |
||
7666
a5fccd76176a
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6977
diff
changeset
|
51 |
const char *MusicDriver_DMusic::Start(const char * const *parm) |
2197 | 52 |
{ |
53 |
if (performance != NULL) return NULL; |
|
54 |
||
55 |
if (proc.CoCreateInstance == NULL) { |
|
56 |
if (!LoadLibraryList((Function*)&proc, ole_files)) |
|
57 |
return "ole32.dll load failed"; |
|
58 |
} |
|
59 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
60 |
/* Initialize COM */ |
2197 | 61 |
if (FAILED(proc.CoInitialize(NULL))) { |
62 |
return "COM initialization failed"; |
|
63 |
} |
|
64 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
65 |
/* create the performance object */ |
2197 | 66 |
if (FAILED(proc.CoCreateInstance( |
67 |
CLSID_DirectMusicPerformance, |
|
68 |
NULL, |
|
69 |
CLSCTX_INPROC, |
|
70 |
IID_IDirectMusicPerformance, |
|
71 |
(LPVOID*)&performance |
|
72 |
))) { |
|
73 |
proc.CoUninitialize(); |
|
74 |
return "Failed to create the performance object"; |
|
75 |
} |
|
76 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
77 |
/* initialize it */ |
2197 | 78 |
if (FAILED(performance->Init(NULL, NULL, NULL))) { |
79 |
performance->Release(); |
|
80 |
performance = NULL; |
|
81 |
proc.CoUninitialize(); |
|
82 |
return "Failed to initialize performance object"; |
|
83 |
} |
|
84 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
85 |
/* choose default Windows synth */ |
2197 | 86 |
if (FAILED(performance->AddPort(NULL))) { |
87 |
performance->CloseDown(); |
|
88 |
performance->Release(); |
|
89 |
performance = NULL; |
|
90 |
proc.CoUninitialize(); |
|
91 |
return "AddPort failed"; |
|
92 |
} |
|
93 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
94 |
/* create the loader object; this will be used to load the MIDI file */ |
2197 | 95 |
if (FAILED(proc.CoCreateInstance( |
96 |
CLSID_DirectMusicLoader, |
|
97 |
NULL, |
|
98 |
CLSCTX_INPROC, |
|
99 |
IID_IDirectMusicLoader, |
|
100 |
(LPVOID*)&loader |
|
101 |
))) { |
|
102 |
performance->CloseDown(); |
|
103 |
performance->Release(); |
|
104 |
performance = NULL; |
|
105 |
proc.CoUninitialize(); |
|
106 |
return "Failed to create loader object"; |
|
107 |
} |
|
108 |
||
109 |
return NULL; |
|
110 |
} |
|
111 |
||
112 |
||
7666
a5fccd76176a
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6977
diff
changeset
|
113 |
void MusicDriver_DMusic::Stop() |
2197 | 114 |
{ |
115 |
seeking = false; |
|
116 |
||
2396
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
117 |
if (performance != NULL) performance->Stop(NULL, NULL, 0, 0); |
2197 | 118 |
|
2396
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
119 |
if (segment != NULL) { |
2894 | 120 |
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
|
121 |
segment->Release(); |
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
122 |
segment = NULL; |
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
123 |
} |
2197 | 124 |
|
2396
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
125 |
if (performance != NULL) { |
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
126 |
performance->CloseDown(); |
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
127 |
performance->Release(); |
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
128 |
performance = NULL; |
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 |
|
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
131 |
if (loader != NULL) { |
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
132 |
loader->Release(); |
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
133 |
loader = NULL; |
6c136d04ed31
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
134 |
} |
2197 | 135 |
|
136 |
proc.CoUninitialize(); |
|
137 |
} |
|
138 |
||
139 |
||
7666
a5fccd76176a
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6977
diff
changeset
|
140 |
void MusicDriver_DMusic::PlaySong(const char* filename) |
2197 | 141 |
{ |
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
142 |
/* set up the loader object info */ |
2197 | 143 |
DMUS_OBJECTDESC obj_desc; |
144 |
ZeroMemory(&obj_desc, sizeof(obj_desc)); |
|
145 |
obj_desc.dwSize = sizeof(obj_desc); |
|
146 |
obj_desc.guidClass = CLSID_DirectMusicSegment; |
|
147 |
obj_desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME | DMUS_OBJ_FULLPATH; |
|
148 |
MultiByteToWideChar( |
|
149 |
CP_ACP, MB_PRECOMPOSED, |
|
150 |
filename, -1, |
|
151 |
obj_desc.wszFileName, lengthof(obj_desc.wszFileName) |
|
152 |
); |
|
153 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
154 |
/* release the existing segment if we have any */ |
2197 | 155 |
if (segment != NULL) { |
156 |
segment->Release(); |
|
157 |
segment = NULL; |
|
158 |
} |
|
159 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
160 |
/* make a new segment */ |
2197 | 161 |
if (FAILED(loader->GetObject( |
162 |
&obj_desc, IID_IDirectMusicSegment, (LPVOID*)&segment |
|
163 |
))) { |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
2894
diff
changeset
|
164 |
DEBUG(driver, 0, "DirectMusic: GetObject failed"); |
2197 | 165 |
return; |
166 |
} |
|
167 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
168 |
/* tell the segment what kind of data it contains */ |
2197 | 169 |
if (FAILED(segment->SetParam( |
170 |
GUID_StandardMIDIFile, 0xFFFFFFFF, 0, 0, performance |
|
171 |
))) { |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
2894
diff
changeset
|
172 |
DEBUG(driver, 0, "DirectMusic: SetParam (MIDI file) failed"); |
2197 | 173 |
return; |
174 |
} |
|
175 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
176 |
/* tell the segment to 'download' the instruments */ |
2197 | 177 |
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
|
178 |
DEBUG(driver, 0, "DirectMusic: failed to download instruments"); |
2197 | 179 |
return; |
180 |
} |
|
181 |
||
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
182 |
/* start playing the MIDI file */ |
2197 | 183 |
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
|
184 |
DEBUG(driver, 0, "DirectMusic: PlaySegment failed"); |
2197 | 185 |
return; |
186 |
} |
|
187 |
||
188 |
seeking = true; |
|
189 |
} |
|
190 |
||
191 |
||
7666
a5fccd76176a
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6977
diff
changeset
|
192 |
void MusicDriver_DMusic::StopSong() |
2197 | 193 |
{ |
194 |
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
|
195 |
DEBUG(driver, 0, "DirectMusic: StopSegment failed"); |
2197 | 196 |
} |
197 |
seeking = false; |
|
198 |
} |
|
199 |
||
200 |
||
7666
a5fccd76176a
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6977
diff
changeset
|
201 |
bool MusicDriver_DMusic::IsSongPlaying() |
2197 | 202 |
{ |
203 |
/* Not the nicest code, but there is a short delay before playing actually |
|
204 |
* starts. OpenTTD makes no provision for this. */ |
|
205 |
if (performance->IsPlaying(segment, NULL) == S_OK) { |
|
206 |
seeking = false; |
|
207 |
return true; |
|
208 |
} else { |
|
209 |
return seeking; |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
||
7666
a5fccd76176a
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6977
diff
changeset
|
214 |
void MusicDriver_DMusic::SetVolume(byte vol) |
2197 | 215 |
{ |
6977
67b989528f3d
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6573
diff
changeset
|
216 |
long db = vol * 2000 / 127 - 2000; ///< 0 - 127 -> -2000 - 0 |
2197 | 217 |
performance->SetGlobalParam(GUID_PerfMasterVolume, &db, sizeof(db)); |
218 |
} |
|
219 |
||
220 |
||
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
|
221 |
#endif /* WIN32_ENABLE_DIRECTMUSIC_SUPPORT */ |