author | peter1138 |
Thu, 05 Jul 2007 12:23:54 +0000 | |
changeset 7170 | 923946ec324f |
parent 6481 | 85ad87daf4b0 |
child 8262 | 45ec878cee4d |
permissions | -rw-r--r-- |
2197 | 1 |
/* $Id$ */ |
2 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
3 |
/** @file dmusic.cpp */ |
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
4 |
|
2197 | 5 |
#ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT |
6 |
||
6124
9f822ae6c086
(svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater
parents:
5988
diff
changeset
|
7 |
#include "../stdafx.h" |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5475
diff
changeset
|
8 |
#include "../debug.h" |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5475
diff
changeset
|
9 |
#include "../win32.h" |
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5475
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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
19 |
/** the performance object controls manipulation of the segments */ |
2197 | 20 |
static IDirectMusicPerformance* performance = NULL; |
21 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
22 |
/** the loader object can load many types of DMusic related files */ |
2197 | 23 |
static IDirectMusicLoader* loader = NULL; |
24 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
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 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
50 |
const char *MusicDriver_DMusic::Start(const char * const *parm) |
2197 | 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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
59 |
/* Initialize COM */ |
2197 | 60 |
if (FAILED(proc.CoInitialize(NULL))) { |
61 |
return "COM initialization failed"; |
|
62 |
} |
|
63 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
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 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
112 |
void MusicDriver_DMusic::Stop() |
2197 | 113 |
{ |
114 |
seeking = false; |
|
115 |
||
2396
3c03cb7e81bb
(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
3c03cb7e81bb
(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
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
120 |
segment->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
121 |
segment = NULL; |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
122 |
} |
2197 | 123 |
|
2396
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
124 |
if (performance != NULL) { |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
125 |
performance->CloseDown(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
126 |
performance->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
127 |
performance = NULL; |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
128 |
} |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
129 |
|
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
130 |
if (loader != NULL) { |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
131 |
loader->Release(); |
3c03cb7e81bb
(svn r2922) Fix crash with directmusic if no music files could be found to play
Darkvater
parents:
2385
diff
changeset
|
132 |
loader = NULL; |
3c03cb7e81bb
(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 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
139 |
void MusicDriver_DMusic::PlaySong(const char* filename) |
2197 | 140 |
{ |
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
153 |
/* release the existing segment if we have any */ |
2197 | 154 |
if (segment != NULL) { |
155 |
segment->Release(); |
|
156 |
segment = NULL; |
|
157 |
} |
|
158 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
159 |
/* make a new segment */ |
2197 | 160 |
if (FAILED(loader->GetObject( |
161 |
&obj_desc, IID_IDirectMusicSegment, (LPVOID*)&segment |
|
162 |
))) { |
|
5380
8ea58542b6e0
(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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
167 |
/* tell the segment what kind of data it contains */ |
2197 | 168 |
if (FAILED(segment->SetParam( |
169 |
GUID_StandardMIDIFile, 0xFFFFFFFF, 0, 0, performance |
|
170 |
))) { |
|
5380
8ea58542b6e0
(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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
175 |
/* tell the segment to 'download' the instruments */ |
2197 | 176 |
if (FAILED(segment->SetParam(GUID_Download, 0xFFFFFFFF, 0, 0, performance))) { |
5380
8ea58542b6e0
(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 |
||
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
181 |
/* start playing the MIDI file */ |
2197 | 182 |
if (FAILED(performance->PlaySegment(segment, 0, 0, NULL))) { |
5380
8ea58542b6e0
(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 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
191 |
void MusicDriver_DMusic::StopSong() |
2197 | 192 |
{ |
193 |
if (FAILED(performance->Stop(segment, NULL, 0, 0))) { |
|
5380
8ea58542b6e0
(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 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
200 |
bool MusicDriver_DMusic::IsSongPlaying() |
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 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6481
diff
changeset
|
213 |
void MusicDriver_DMusic::SetVolume(byte vol) |
2197 | 214 |
{ |
6481
85ad87daf4b0
(svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents:
6247
diff
changeset
|
215 |
long db = vol * 2000 / 127 - 2000; ///< 0 - 127 -> -2000 - 0 |
2197 | 216 |
performance->SetGlobalParam(GUID_PerfMasterVolume, &db, sizeof(db)); |
217 |
} |
|
218 |
||
219 |
||
5988
1aabf94612c6
(svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents:
5587
diff
changeset
|
220 |
#endif /* WIN32_ENABLE_DIRECTMUSIC_SUPPORT */ |