author | rubidium |
Fri, 28 Mar 2008 10:32:55 +0000 | |
branch | noai |
changeset 9826 | 9707ad4c9b60 |
parent 9704 | 197cb8c6ae17 |
child 10455 | 22c441f5adf9 |
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; |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9631
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; |
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
16 |
char start_song[MAX_PATH]; |
0 | 17 |
} _midi; |
18 |
||
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
6573
diff
changeset
|
19 |
static FMusicDriver_Win32 iFMusicDriver_Win32; |
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
6573
diff
changeset
|
20 |
|
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
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 |
||
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
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 |
||
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
6573
diff
changeset
|
38 |
bool MusicDriver_Win32::IsSongPlaying() |
0 | 39 |
{ |
40 |
return _midi.playing; |
|
41 |
} |
|
42 |
||
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
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 |
||
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
49 |
static MCIERROR CDECL MidiSendCommand(const TCHAR* cmd, ...) |
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
|
50 |
{ |
0 | 51 |
va_list va; |
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
52 |
TCHAR 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); |
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
55 |
_vsntprintf(buf, lengthof(buf), cmd, va); |
0 | 56 |
va_end(va); |
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
57 |
return mciSendString(buf, NULL, 0, 0); |
0 | 58 |
} |
59 |
||
60 |
static bool MidiIntPlaySong(const char *filename) |
|
61 |
{ |
|
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
62 |
MidiSendCommand(_T("close all")); |
0 | 63 |
|
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
64 |
if (MidiSendCommand(_T("open \"%s\" type sequencer alias song"), OTTD2FS(filename)) != 0) { |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
65 |
/* Let's try the "short name" */ |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
66 |
TCHAR buf[MAX_PATH]; |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
67 |
if (GetShortPathName(OTTD2FS(filename), buf, MAX_PATH) == 0) return false; |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
68 |
if (MidiSendCommand(_T("open \"%s\" type sequencer alias song"), buf) != 0) return false; |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
69 |
} |
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
70 |
|
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
71 |
return MidiSendCommand(_T("play song from 0")) == 0; |
0 | 72 |
} |
73 |
||
6573 | 74 |
static void MidiIntStopSong() |
0 | 75 |
{ |
9826
9707ad4c9b60
(svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents:
9704
diff
changeset
|
76 |
MidiSendCommand(_T("close all")); |
0 | 77 |
} |
78 |
||
79 |
static void MidiIntSetVolume(int vol) |
|
80 |
{ |
|
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
81 |
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
|
82 |
midiOutSetVolume((HMIDIOUT)_midi.devid, v + (v << 16)); |
0 | 83 |
} |
84 |
||
6573 | 85 |
static bool MidiIntIsSongPlaying() |
0 | 86 |
{ |
87 |
char buf[16]; |
|
88 |
mciSendStringA("status song mode", buf, sizeof(buf), 0); |
|
89 |
return strcmp(buf, "playing") == 0 || strcmp(buf, "seeking") == 0; |
|
90 |
} |
|
91 |
||
92 |
static DWORD WINAPI MidiThread(LPVOID arg) |
|
93 |
{ |
|
94 |
do { |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
95 |
char *s; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
96 |
int vol; |
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 |
vol = _midi.new_vol; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
99 |
if (vol != -1) { |
0 | 100 |
_midi.new_vol = -1; |
101 |
MidiIntSetVolume(vol); |
|
102 |
} |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
103 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
104 |
s = _midi.start_song; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
105 |
if (s[0] != '\0') { |
0 | 106 |
_midi.playing = MidiIntPlaySong(s); |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
107 |
s[0] = '\0'; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
108 |
|
0 | 109 |
// Delay somewhat in case we don't manage to play. |
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9631
diff
changeset
|
110 |
if (!_midi.playing) WaitForMultipleObjects(1, &_midi.wait_obj, FALSE, 5000); |
0 | 111 |
} |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
112 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
113 |
if (_midi.stop_song && _midi.playing) { |
0 | 114 |
_midi.stop_song = false; |
115 |
_midi.playing = false; |
|
116 |
MidiIntStopSong(); |
|
117 |
} |
|
118 |
||
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
|
119 |
if (_midi.playing && !MidiIntIsSongPlaying()) _midi.playing = false; |
0 | 120 |
|
121 |
WaitForMultipleObjects(1, &_midi.wait_obj, FALSE, 1000); |
|
122 |
} while (!_midi.terminate); |
|
123 |
||
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9631
diff
changeset
|
124 |
MidiIntStopSong(); |
0 | 125 |
return 0; |
126 |
} |
|
127 |
||
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
6573
diff
changeset
|
128 |
const char *MusicDriver_Win32::Start(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; |
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
131 |
UINT nbdev; |
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
132 |
UINT_PTR dev; |
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
133 |
char buf[16]; |
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
134 |
|
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
135 |
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
|
136 |
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
|
137 |
|
0 | 138 |
memset(&_midi, 0, sizeof(_midi)); |
139 |
_midi.new_vol = -1; |
|
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
140 |
|
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
141 |
/* Get midi device */ |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
142 |
_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
|
143 |
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
|
144 |
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
|
145 |
_midi.devid = dev; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
146 |
break; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
147 |
} |
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 |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9631
diff
changeset
|
150 |
if (NULL == (_midi.wait_obj = CreateEvent(NULL, FALSE, FALSE, NULL))) return "Failed to create event"; |
9704 | 151 |
|
152 |
/* The lpThreadId parameter of CreateThread (the last parameter) |
|
153 |
* may NOT be NULL on Windows 95, 98 and ME. */ |
|
154 |
DWORD threadId; |
|
155 |
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
|
156 |
|
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
157 |
return NULL; |
0 | 158 |
} |
159 |
||
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
6573
diff
changeset
|
160 |
void MusicDriver_Win32::Stop() |
0 | 161 |
{ |
162 |
_midi.terminate = true; |
|
163 |
SetEvent(_midi.wait_obj); |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9631
diff
changeset
|
164 |
WaitForMultipleObjects(1, &_midi.thread, true, INFINITE); |
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9631
diff
changeset
|
165 |
CloseHandle(_midi.wait_obj); |
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9631
diff
changeset
|
166 |
CloseHandle(_midi.thread); |
0 | 167 |
} |