12 #include "../stdafx.h"
13 #include "../string_func.h"
18 #include "../safeguards.h"
28 char start_song[MAX_PATH];
35 assert(filename != NULL);
38 _midi.stop_song =
false;
39 SetEvent(
_midi.wait_obj);
45 _midi.stop_song =
true;
46 _midi.start_song[0] =
'\0';
47 SetEvent(
_midi.wait_obj);
59 SetEvent(
_midi.wait_obj);
68 _vsntprintf(buf,
lengthof(buf), cmd, va);
70 return mciSendString(buf, NULL, 0, 0);
73 static bool MidiIntPlaySong(
const char *filename)
80 if (GetShortPathName(
OTTD2FS(filename), buf, MAX_PATH) == 0)
return false;
81 if (
MidiSendCommand(_T(
"open \"%s\" type sequencer alias song"), buf) != 0)
return false;
88 static void MidiIntStopSong()
93 static void MidiIntSetVolume(
int vol)
95 DWORD v = (vol * 65535 / 127);
96 midiOutSetVolume((HMIDIOUT)
_midi.devid, v + (v << 16));
99 static bool MidiIntIsSongPlaying()
102 mciSendStringA(
"status song mode", buf,
sizeof(buf), 0);
103 return strcmp(buf,
"playing") == 0 || strcmp(buf,
"seeking") == 0;
106 static DWORD WINAPI MidiThread(LPVOID arg)
115 MidiIntSetVolume(vol);
118 s =
_midi.start_song;
120 _midi.playing = MidiIntPlaySong(s);
124 if (!
_midi.playing) WaitForMultipleObjects(1, &
_midi.wait_obj, FALSE, 5000);
128 _midi.stop_song =
false;
129 _midi.playing =
false;
133 if (
_midi.playing && !MidiIntIsSongPlaying())
_midi.playing =
false;
135 WaitForMultipleObjects(1, &
_midi.wait_obj, FALSE, 1000);
136 }
while (!
_midi.terminate);
144 MIDIOUTCAPS midicaps;
149 mciSendStringA(
"capability sequencer has audio", buf,
lengthof(buf), 0);
150 if (strcmp(buf,
"true") != 0)
return "MCI sequencer can't play audio";
156 _midi.devid = MIDI_MAPPER;
157 for (dev = 0, nbdev = midiOutGetNumDevs(); dev < nbdev; dev++) {
158 if (midiOutGetDevCaps(dev, &midicaps,
sizeof(midicaps)) == 0 && (midicaps.dwSupport & MIDICAPS_VOLUME)) {
164 if (NULL == (
_midi.wait_obj = CreateEvent(NULL, FALSE, FALSE, NULL)))
return "Failed to create event";
169 if (NULL == (
_midi.thread = CreateThread(NULL, 8192, MidiThread, 0, 0, &threadId)))
return "Failed to create thread";
176 _midi.terminate =
true;
177 SetEvent(
_midi.wait_obj);
178 WaitForMultipleObjects(1, &
_midi.thread,
true, INFINITE);
179 CloseHandle(
_midi.wait_obj);
180 CloseHandle(
_midi.thread);