author | KUDr |
Wed, 10 Jan 2007 18:12:09 +0000 | |
changeset 5835 | e0ff603ae0b7 |
parent 5726 | src/music/win32_m.c@8f399788f6c9 |
child 6268 | 4b5241e5dd10 |
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 "../openttd.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
|
5 |
#include "win32_m.h" |
0 | 6 |
#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
|
7 |
#include <mmsystem.h> |
0 | 8 |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
9 |
static struct { |
0 | 10 |
bool stop_song; |
11 |
bool terminate; |
|
12 |
bool playing; |
|
13 |
int new_vol; |
|
14 |
HANDLE wait_obj; |
|
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
15 |
UINT_PTR devid; |
0 | 16 |
char start_song[260]; |
17 |
} _midi; |
|
18 |
||
19 |
static void Win32MidiPlaySong(const char *filename) |
|
20 |
{ |
|
21 |
strcpy(_midi.start_song, filename); |
|
22 |
_midi.playing = true; |
|
23 |
_midi.stop_song = false; |
|
24 |
SetEvent(_midi.wait_obj); |
|
25 |
} |
|
26 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
27 |
static void Win32MidiStopSong(void) |
0 | 28 |
{ |
29 |
if (_midi.playing) { |
|
30 |
_midi.stop_song = true; |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
31 |
_midi.start_song[0] = '\0'; |
0 | 32 |
SetEvent(_midi.wait_obj); |
33 |
} |
|
34 |
} |
|
35 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
36 |
static bool Win32MidiIsSongPlaying(void) |
0 | 37 |
{ |
38 |
return _midi.playing; |
|
39 |
} |
|
40 |
||
41 |
static void Win32MidiSetVolume(byte vol) |
|
42 |
{ |
|
43 |
_midi.new_vol = vol; |
|
44 |
SetEvent(_midi.wait_obj); |
|
45 |
} |
|
46 |
||
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
|
47 |
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
|
48 |
{ |
0 | 49 |
va_list va; |
50 |
char buf[512]; |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
51 |
|
0 | 52 |
va_start(va, cmd); |
53 |
vsprintf(buf, cmd, va); |
|
54 |
va_end(va); |
|
55 |
return mciSendStringA(buf, NULL, 0, 0); |
|
56 |
} |
|
57 |
||
58 |
static bool MidiIntPlaySong(const char *filename) |
|
59 |
{ |
|
60 |
MidiSendCommand("close all"); |
|
2054
e313dd080bd3
(svn r2563) -Fix: [1209084] Spaces in the path to the MIDI files caused the win32 MIDI player to fail
tron
parents:
2026
diff
changeset
|
61 |
if (MidiSendCommand("open \"%s\" type sequencer alias song", filename) != 0) |
0 | 62 |
return false; |
63 |
||
64 |
if (MidiSendCommand("play song from 0") != 0) |
|
65 |
return false; |
|
66 |
return true; |
|
67 |
} |
|
68 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
69 |
static void MidiIntStopSong(void) |
0 | 70 |
{ |
71 |
MidiSendCommand("close all"); |
|
72 |
} |
|
73 |
||
74 |
static void MidiIntSetVolume(int vol) |
|
75 |
{ |
|
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
76 |
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
|
77 |
midiOutSetVolume((HMIDIOUT)_midi.devid, v + (v << 16)); |
0 | 78 |
} |
79 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
80 |
static bool MidiIntIsSongPlaying(void) |
0 | 81 |
{ |
82 |
char buf[16]; |
|
83 |
mciSendStringA("status song mode", buf, sizeof(buf), 0); |
|
84 |
return strcmp(buf, "playing") == 0 || strcmp(buf, "seeking") == 0; |
|
85 |
} |
|
86 |
||
87 |
static DWORD WINAPI MidiThread(LPVOID arg) |
|
88 |
{ |
|
89 |
_midi.wait_obj = CreateEvent(NULL, FALSE, FALSE, NULL); |
|
90 |
||
91 |
do { |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
92 |
char *s; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
93 |
int vol; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
94 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
95 |
vol = _midi.new_vol; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
96 |
if (vol != -1) { |
0 | 97 |
_midi.new_vol = -1; |
98 |
MidiIntSetVolume(vol); |
|
99 |
} |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
100 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
101 |
s = _midi.start_song; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
102 |
if (s[0] != '\0') { |
0 | 103 |
_midi.playing = MidiIntPlaySong(s); |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
104 |
s[0] = '\0'; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
105 |
|
0 | 106 |
// Delay somewhat in case we don't manage to play. |
107 |
if (!_midi.playing) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
108 |
Sleep(5000); |
0 | 109 |
} |
110 |
} |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
111 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
112 |
if (_midi.stop_song && _midi.playing) { |
0 | 113 |
_midi.stop_song = false; |
114 |
_midi.playing = false; |
|
115 |
MidiIntStopSong(); |
|
116 |
} |
|
117 |
||
118 |
if (_midi.playing && !MidiIntIsSongPlaying()) |
|
119 |
_midi.playing = false; |
|
120 |
||
121 |
WaitForMultipleObjects(1, &_midi.wait_obj, FALSE, 1000); |
|
122 |
} while (!_midi.terminate); |
|
123 |
||
124 |
DeleteObject(_midi.wait_obj); |
|
125 |
return 0; |
|
126 |
} |
|
127 |
||
1301
313804601383
(svn r1805) Teach the driver layer a few things about const correctness
tron
parents:
1230
diff
changeset
|
128 |
static const char *Win32MidiStart(const char * const *parm) |
0 | 129 |
{ |
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
130 |
MIDIOUTCAPS midicaps; |
0 | 131 |
DWORD threadId; |
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
132 |
UINT nbdev; |
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
133 |
UINT_PTR dev; |
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
134 |
char buf[16]; |
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
135 |
|
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
136 |
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
|
137 |
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
|
138 |
|
0 | 139 |
memset(&_midi, 0, sizeof(_midi)); |
140 |
_midi.new_vol = -1; |
|
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
141 |
|
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
142 |
/* Get midi device */ |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
143 |
_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
|
144 |
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
|
145 |
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
|
146 |
_midi.devid = dev; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
147 |
break; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
148 |
} |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
149 |
} |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
150 |
|
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
151 |
if (CreateThread(NULL, 8192, MidiThread, 0, 0, &threadId) == NULL) |
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
152 |
return "Failed to create thread"; |
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
153 |
|
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
154 |
return NULL; |
0 | 155 |
} |
156 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
157 |
static void Win32MidiStop(void) |
0 | 158 |
{ |
159 |
_midi.terminate = true; |
|
160 |
SetEvent(_midi.wait_obj); |
|
161 |
} |
|
162 |
||
163 |
const HalMusicDriver _win32_music_driver = { |
|
164 |
Win32MidiStart, |
|
165 |
Win32MidiStop, |
|
166 |
Win32MidiPlaySong, |
|
167 |
Win32MidiStopSong, |
|
168 |
Win32MidiIsSongPlaying, |
|
169 |
Win32MidiSetVolume, |
|
170 |
}; |