author | glx |
Fri, 14 Mar 2008 18:31:37 +0000 | |
changeset 9189 | 40f723073fef |
parent 8119 | 9505a65c30d5 |
child 9195 | b83118d81d4b |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
2189
d240b9097139
(svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents:
2186
diff
changeset
|
3 |
#include "../stdafx.h" |
d240b9097139
(svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents:
2186
diff
changeset
|
4 |
#include "win32_m.h" |
0 | 5 |
#include <windows.h> |
2189
d240b9097139
(svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents:
2186
diff
changeset
|
6 |
#include <mmsystem.h> |
0 | 7 |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
8 |
static struct { |
0 | 9 |
bool stop_song; |
10 |
bool terminate; |
|
11 |
bool playing; |
|
12 |
int new_vol; |
|
13 |
HANDLE wait_obj; |
|
8010
a924bdae75ab
(svn r11029) -Fix [FS#1164]: win32 midi doesn't stop when closing openttd
glx
parents:
7666
diff
changeset
|
14 |
HANDLE thread; |
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
15 |
UINT_PTR devid; |
9189
40f723073fef
(svn r12366) -Codechange: use MAX_PATH instead hardcoded value
glx
parents:
8119
diff
changeset
|
16 |
char start_song[MAX_PATH]; |
0 | 17 |
} _midi; |
18 |
||
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:
6573
diff
changeset
|
19 |
static FMusicDriver_Win32 iFMusicDriver_Win32; |
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:
6573
diff
changeset
|
20 |
|
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:
6573
diff
changeset
|
21 |
void MusicDriver_Win32::PlaySong(const char *filename) |
0 | 22 |
{ |
23 |
strcpy(_midi.start_song, filename); |
|
24 |
_midi.playing = true; |
|
25 |
_midi.stop_song = false; |
|
26 |
SetEvent(_midi.wait_obj); |
|
27 |
} |
|
28 |
||
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:
6573
diff
changeset
|
29 |
void MusicDriver_Win32::StopSong() |
0 | 30 |
{ |
31 |
if (_midi.playing) { |
|
32 |
_midi.stop_song = true; |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
33 |
_midi.start_song[0] = '\0'; |
0 | 34 |
SetEvent(_midi.wait_obj); |
35 |
} |
|
36 |
} |
|
37 |
||
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:
6573
diff
changeset
|
38 |
bool MusicDriver_Win32::IsSongPlaying() |
0 | 39 |
{ |
40 |
return _midi.playing; |
|
41 |
} |
|
42 |
||
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:
6573
diff
changeset
|
43 |
void MusicDriver_Win32::SetVolume(byte vol) |
0 | 44 |
{ |
45 |
_midi.new_vol = vol; |
|
46 |
SetEvent(_midi.wait_obj); |
|
47 |
} |
|
48 |
||
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2458
diff
changeset
|
49 |
static MCIERROR CDECL MidiSendCommand(const char* cmd, ...) |
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2458
diff
changeset
|
50 |
{ |
0 | 51 |
va_list va; |
52 |
char buf[512]; |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
53 |
|
0 | 54 |
va_start(va, cmd); |
6450
1c2016673250
(svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater
parents:
5835
diff
changeset
|
55 |
vsnprintf(buf, lengthof(buf), cmd, va); |
0 | 56 |
va_end(va); |
57 |
return mciSendStringA(buf, NULL, 0, 0); |
|
58 |
} |
|
59 |
||
60 |
static bool MidiIntPlaySong(const char *filename) |
|
61 |
{ |
|
62 |
MidiSendCommand("close all"); |
|
6450
1c2016673250
(svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater
parents:
5835
diff
changeset
|
63 |
if (MidiSendCommand("open \"%s\" type sequencer alias song", filename) != 0) return false; |
0 | 64 |
|
6450
1c2016673250
(svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater
parents:
5835
diff
changeset
|
65 |
return MidiSendCommand("play song from 0") == 0; |
0 | 66 |
} |
67 |
||
6573 | 68 |
static void MidiIntStopSong() |
0 | 69 |
{ |
70 |
MidiSendCommand("close all"); |
|
71 |
} |
|
72 |
||
73 |
static void MidiIntSetVolume(int vol) |
|
74 |
{ |
|
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
75 |
DWORD v = (vol * 65535 / 127); |
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
76 |
midiOutSetVolume((HMIDIOUT)_midi.devid, v + (v << 16)); |
0 | 77 |
} |
78 |
||
6573 | 79 |
static bool MidiIntIsSongPlaying() |
0 | 80 |
{ |
81 |
char buf[16]; |
|
82 |
mciSendStringA("status song mode", buf, sizeof(buf), 0); |
|
83 |
return strcmp(buf, "playing") == 0 || strcmp(buf, "seeking") == 0; |
|
84 |
} |
|
85 |
||
86 |
static DWORD WINAPI MidiThread(LPVOID arg) |
|
87 |
{ |
|
88 |
do { |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
89 |
char *s; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
90 |
int vol; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
91 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
92 |
vol = _midi.new_vol; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
93 |
if (vol != -1) { |
0 | 94 |
_midi.new_vol = -1; |
95 |
MidiIntSetVolume(vol); |
|
96 |
} |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
97 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
98 |
s = _midi.start_song; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
99 |
if (s[0] != '\0') { |
0 | 100 |
_midi.playing = MidiIntPlaySong(s); |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
101 |
s[0] = '\0'; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
102 |
|
0 | 103 |
// Delay somewhat in case we don't manage to play. |
8010
a924bdae75ab
(svn r11029) -Fix [FS#1164]: win32 midi doesn't stop when closing openttd
glx
parents:
7666
diff
changeset
|
104 |
if (!_midi.playing) WaitForMultipleObjects(1, &_midi.wait_obj, FALSE, 5000); |
0 | 105 |
} |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
106 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
107 |
if (_midi.stop_song && _midi.playing) { |
0 | 108 |
_midi.stop_song = false; |
109 |
_midi.playing = false; |
|
110 |
MidiIntStopSong(); |
|
111 |
} |
|
112 |
||
6450
1c2016673250
(svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater
parents:
5835
diff
changeset
|
113 |
if (_midi.playing && !MidiIntIsSongPlaying()) _midi.playing = false; |
0 | 114 |
|
115 |
WaitForMultipleObjects(1, &_midi.wait_obj, FALSE, 1000); |
|
116 |
} while (!_midi.terminate); |
|
117 |
||
8010
a924bdae75ab
(svn r11029) -Fix [FS#1164]: win32 midi doesn't stop when closing openttd
glx
parents:
7666
diff
changeset
|
118 |
MidiIntStopSong(); |
0 | 119 |
return 0; |
120 |
} |
|
121 |
||
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:
6573
diff
changeset
|
122 |
const char *MusicDriver_Win32::Start(const char * const *parm) |
0 | 123 |
{ |
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
124 |
MIDIOUTCAPS midicaps; |
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
125 |
UINT nbdev; |
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
126 |
UINT_PTR dev; |
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
127 |
char buf[16]; |
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
128 |
|
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
129 |
mciSendStringA("capability sequencer has audio", buf, lengthof(buf), 0); |
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
130 |
if (strcmp(buf, "true") != 0) return "MCI sequencer can't play audio"; |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
131 |
|
0 | 132 |
memset(&_midi, 0, sizeof(_midi)); |
133 |
_midi.new_vol = -1; |
|
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
134 |
|
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
135 |
/* Get midi device */ |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
136 |
_midi.devid = MIDI_MAPPER; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
137 |
for (dev = 0, nbdev = midiOutGetNumDevs(); dev < nbdev; dev++) { |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
138 |
if (midiOutGetDevCaps(dev, &midicaps, sizeof(midicaps)) == 0 && (midicaps.dwSupport & MIDICAPS_VOLUME)) { |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
139 |
_midi.devid = dev; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
140 |
break; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
141 |
} |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
142 |
} |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
143 |
|
8010
a924bdae75ab
(svn r11029) -Fix [FS#1164]: win32 midi doesn't stop when closing openttd
glx
parents:
7666
diff
changeset
|
144 |
if (NULL == (_midi.wait_obj = CreateEvent(NULL, FALSE, FALSE, NULL))) return "Failed to create event"; |
8119
9505a65c30d5
(svn r11154) -Fix [FS#1239]: MIDI failing to play under Windows 95, 98 and ME.
rubidium
parents:
8010
diff
changeset
|
145 |
|
9505a65c30d5
(svn r11154) -Fix [FS#1239]: MIDI failing to play under Windows 95, 98 and ME.
rubidium
parents:
8010
diff
changeset
|
146 |
/* The lpThreadId parameter of CreateThread (the last parameter) |
9505a65c30d5
(svn r11154) -Fix [FS#1239]: MIDI failing to play under Windows 95, 98 and ME.
rubidium
parents:
8010
diff
changeset
|
147 |
* may NOT be NULL on Windows 95, 98 and ME. */ |
9505a65c30d5
(svn r11154) -Fix [FS#1239]: MIDI failing to play under Windows 95, 98 and ME.
rubidium
parents:
8010
diff
changeset
|
148 |
DWORD threadId; |
9505a65c30d5
(svn r11154) -Fix [FS#1239]: MIDI failing to play under Windows 95, 98 and ME.
rubidium
parents:
8010
diff
changeset
|
149 |
if (NULL == (_midi.thread = CreateThread(NULL, 8192, MidiThread, 0, 0, &threadId))) return "Failed to create thread"; |
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
150 |
|
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
151 |
return NULL; |
0 | 152 |
} |
153 |
||
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:
6573
diff
changeset
|
154 |
void MusicDriver_Win32::Stop() |
0 | 155 |
{ |
156 |
_midi.terminate = true; |
|
157 |
SetEvent(_midi.wait_obj); |
|
8010
a924bdae75ab
(svn r11029) -Fix [FS#1164]: win32 midi doesn't stop when closing openttd
glx
parents:
7666
diff
changeset
|
158 |
WaitForMultipleObjects(1, &_midi.thread, true, INFINITE); |
a924bdae75ab
(svn r11029) -Fix [FS#1164]: win32 midi doesn't stop when closing openttd
glx
parents:
7666
diff
changeset
|
159 |
CloseHandle(_midi.wait_obj); |
a924bdae75ab
(svn r11029) -Fix [FS#1164]: win32 midi doesn't stop when closing openttd
glx
parents:
7666
diff
changeset
|
160 |
CloseHandle(_midi.thread); |
0 | 161 |
} |