10 #include "../stdafx.h"
11 #include "../string_func.h"
15 #include "../os/windows/win32.h"
17 #include "midifile.hpp"
19 #include "../base_media_base.h"
20 #include "../core/mem_func.hpp"
23 #include "../safeguards.h"
56 static uint8_t ScaleVolume(uint8_t original, uint8_t scale)
58 return original * scale / 127;
62 void CALLBACK MidiOutProc(HMIDIOUT hmo, UINT wMsg, DWORD_PTR, DWORD_PTR dwParam1, DWORD_PTR)
64 if (wMsg == MOM_DONE) {
65 MIDIHDR *hdr = (LPMIDIHDR)dwParam1;
66 midiOutUnprepareHeader(hmo, hdr,
sizeof(*hdr));
71 static void TransmitChannelMsg(uint8_t status, uint8_t p1, uint8_t p2 = 0)
73 midiOutShortMsg(
_midi.midi_out, status | (p1 << 8) | (p2 << 16));
76 static void TransmitSysex(
const uint8_t *&msg_start,
size_t &remaining)
79 const uint8_t *msg_end = msg_start;
80 while (*msg_end != MIDIST_ENDSYSEX) msg_end++;
84 MIDIHDR *hdr = CallocT<MIDIHDR>(1);
85 hdr->lpData =
reinterpret_cast<LPSTR
>(
const_cast<uint8_t *
>(msg_start));
86 hdr->dwBufferLength = msg_end - msg_start;
87 if (midiOutPrepareHeader(
_midi.midi_out, hdr,
sizeof(*hdr)) == MMSYSERR_NOERROR) {
89 hdr->dwBytesRecorded = hdr->dwBufferLength;
90 midiOutLongMsg(
_midi.midi_out, hdr,
sizeof(*hdr));
96 remaining -= msg_end - msg_start;
100 static void TransmitStandardSysex(MidiSysexMessage msg)
103 const uint8_t *data = MidiGetStandardSysexMessage(msg, length);
104 TransmitSysex(data, length);
111 void CALLBACK
TimerCallback(UINT uTimerID, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR)
113 static int volume_throttle = 0;
116 std::unique_lock<std::mutex> mutex_lock(
_midi.lock, std::defer_lock);
117 if (!mutex_lock.try_lock())
return;
121 Debug(driver, 2,
"Win32-MIDI: timer: do_stop is set");
122 midiOutReset(
_midi.midi_out);
123 _midi.playing =
false;
124 _midi.do_stop =
false;
129 if (
_midi.do_start != 0) {
131 if (timeGetTime() -
_midi.playback_start_time < 50) {
134 Debug(driver, 2,
"Win32-MIDI: timer: do_start step {}",
_midi.do_start);
136 if (
_midi.do_start == 1) {
138 midiOutReset(
_midi.midi_out);
139 _midi.playback_start_time = timeGetTime();
143 }
else if (
_midi.do_start == 2) {
145 TransmitStandardSysex(MidiSysexMessage::ResetGM);
146 _midi.playback_start_time = timeGetTime();
150 }
else if (
_midi.do_start == 3) {
152 TransmitStandardSysex(MidiSysexMessage::RolandSetReverb);
153 _midi.playback_start_time = timeGetTime();
157 }
else if (
_midi.do_start == 4) {
160 std::swap(
_midi.next_segment,
_midi.current_segment);
161 _midi.current_segment.start_block = 0;
162 _midi.playback_start_time = timeGetTime();
163 _midi.playing =
true;
165 _midi.current_block = 0;
169 _midi.current_volume = UINT8_MAX;
172 }
else if (!
_midi.playing) {
174 Debug(driver, 2,
"Win32-MIDI: timer: not playing, stopping timer");
175 timeKillEvent(uTimerID);
181 if (
_midi.current_volume !=
_midi.new_volume) {
182 if (volume_throttle == 0) {
183 Debug(driver, 2,
"Win32-MIDI: timer: volume change");
185 volume_throttle = 20 /
_midi.time_period;
186 for (
int ch = 0; ch < 16; ch++) {
187 uint8_t vol = ScaleVolume(
_midi.channel_volumes[ch],
_midi.current_volume);
188 TransmitChannelMsg(MIDIST_CONTROLLER | ch, MIDICT_CHANVOLUME, vol);
196 if (
_midi.current_segment.start > 0 &&
_midi.current_block == 0 &&
_midi.current_segment.start_block == 0) {
200 size_t preload_bytes = 0;
201 for (
size_t bl = 0; bl <
_midi.current_file.blocks.size(); bl++) {
203 preload_bytes += block.
data.size();
205 if (
_midi.current_segment.loop) {
206 Debug(driver, 2,
"Win32-MIDI: timer: loop from block {} (ticktime {}, realtime {:.3f}, bytes {})", bl, block.
ticktime, ((
int)block.
realtime)/1000.0, preload_bytes);
207 _midi.current_segment.start_block = bl;
215 Debug(driver, 2,
"Win32-MIDI: timer: start from block {} (ticktime {}, realtime {:.3f}, bytes {})", bl, block.
ticktime, ((
int)block.
realtime) / 1000.0, preload_bytes);
216 _midi.playback_start_time -= block.
realtime / 1000 - (DWORD)(preload_bytes * 1000 / 3125);
225 DWORD current_time = timeGetTime();
226 DWORD playback_time = current_time -
_midi.playback_start_time;
227 while (
_midi.current_block <
_midi.current_file.blocks.size()) {
232 if (
_midi.current_segment.loop) {
233 _midi.current_block =
_midi.current_segment.start_block;
234 _midi.playback_start_time = timeGetTime() -
_midi.current_file.blocks[
_midi.current_block].realtime / 1000;
236 _midi.do_stop =
true;
241 if (block.
realtime / 1000 > playback_time) {
245 const uint8_t *data = block.
data.data();
246 size_t remaining = block.
data.size();
247 uint8_t last_status = 0;
248 while (remaining > 0) {
251 uint8_t status = data[0];
253 last_status = status;
257 status = last_status;
259 switch (status & 0xF0) {
261 case MIDIST_CHANPRESS:
263 TransmitChannelMsg(status, data[0]);
269 case MIDIST_POLYPRESS:
270 case MIDIST_PITCHBEND:
272 TransmitChannelMsg(status, data[0], data[1]);
276 case MIDIST_CONTROLLER:
278 if (data[0] == MIDICT_CHANVOLUME) {
280 _midi.channel_volumes[status & 0x0F] = data[1];
281 int vol = ScaleVolume(data[1],
_midi.current_volume);
282 TransmitChannelMsg(status, data[0], vol);
285 TransmitChannelMsg(status, data[0], data[1]);
294 TransmitSysex(data, remaining);
296 case MIDIST_TC_QFRAME:
301 case MIDIST_SONGPOSPTR:
312 _midi.current_block++;
316 if (
_midi.current_block ==
_midi.current_file.blocks.size()) {
317 if (
_midi.current_segment.loop) {
318 _midi.current_block =
_midi.current_segment.start_block;
319 _midi.playback_start_time = timeGetTime() -
_midi.current_file.blocks[
_midi.current_block].realtime / 1000;
321 _midi.do_stop =
true;
328 Debug(driver, 2,
"Win32-MIDI: PlaySong: entry");
331 if (!new_song.LoadSong(song))
return;
332 Debug(driver, 2,
"Win32-MIDI: PlaySong: Loaded song");
334 std::lock_guard<std::mutex> mutex_lock(
_midi.lock);
336 _midi.next_file.MoveFrom(new_song);
341 Debug(driver, 2,
"Win32-MIDI: PlaySong: setting flag");
345 if (
_midi.timer_id == 0) {
346 Debug(driver, 2,
"Win32-MIDI: PlaySong: starting timer");
347 _midi.timer_id = timeSetEvent(
_midi.time_period,
_midi.time_period,
TimerCallback, (DWORD_PTR)
this, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
353 Debug(driver, 2,
"Win32-MIDI: StopSong: entry");
354 std::lock_guard<std::mutex> mutex_lock(
_midi.lock);
355 Debug(driver, 2,
"Win32-MIDI: StopSong: setting flag");
356 _midi.do_stop =
true;
361 return _midi.playing || (
_midi.do_start != 0);
366 std::lock_guard<std::mutex> mutex_lock(
_midi.lock);
367 _midi.new_volume = vol;
372 Debug(driver, 2,
"Win32-MIDI: Start: initializing");
379 if (portname !=
nullptr || _debug_driver_level > 0) {
380 uint numports = midiOutGetNumDevs();
381 Debug(driver, 1,
"Win32-MIDI: Found {} output devices:", numports);
382 for (uint tryport = 0; tryport < numports; tryport++) {
384 if (midiOutGetDevCaps(tryport, &moc,
sizeof(moc)) == MMSYSERR_NOERROR) {
385 char tryportname[128];
390 if (portname !=
nullptr && strncmp(tryportname, portname,
lengthof(tryportname)) == 0) port = tryport;
392 Debug(driver, 1,
"MIDI port {:2d}: {}{}", tryport, tryportname, (tryport == port) ?
" [selected]" :
"");
398 if (port == UINT_MAX) {
404 resolution =
Clamp(resolution, 1, 20);
406 if (midiOutOpen(&
_midi.midi_out, devid, (DWORD_PTR)&MidiOutProc, (DWORD_PTR)
this, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) {
407 return "could not open midi device";
410 midiOutReset(
_midi.midi_out);
414 if (timeGetDevCaps(&timecaps,
sizeof(timecaps)) == MMSYSERR_NOERROR) {
415 _midi.time_period = std::min(std::max((UINT)resolution, timecaps.wPeriodMin), timecaps.wPeriodMax);
416 if (timeBeginPeriod(
_midi.time_period) == MMSYSERR_NOERROR) {
418 Debug(driver, 2,
"Win32-MIDI: Start: timer resolution is {}",
_midi.time_period);
422 midiOutClose(
_midi.midi_out);
423 return "could not set timer resolution";
428 std::lock_guard<std::mutex> mutex_lock(
_midi.lock);
430 if (
_midi.timer_id) {
431 timeKillEvent(
_midi.timer_id);
435 timeEndPeriod(
_midi.time_period);
436 midiOutReset(
_midi.midi_out);
437 midiOutClose(
_midi.midi_out);
Factory for Windows' music player.
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
void StopSong() override
Stop playing the current song.
bool IsSongPlaying() override
Are we currently playing a song?
void SetVolume(uint8_t vol) override
Set the volume, if possible.
void Stop() override
Stop this driver.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
static struct @11 _midi
Metadata about the midi we're playing.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
#define lengthof(array)
Return the length of an fixed size array.
std::vector< std::string > StringList
Type for a list of strings.
std::vector< uint8_t > data
raw midi data contained in block
uint32_t realtime
real-time (microseconds) since start of file this block should be triggered at
uint32_t ticktime
tick number since start of file this block should be triggered at
Metadata about a music track.
int override_start
MIDI ticks to skip over in beginning.
bool loop
song should play in a tight loop if possible, never ending
int override_end
MIDI tick to end the song at (0 if no override)
char * convert_from_fs(const std::wstring_view src, std::span< char > dst_buf)
Convert to OpenTTD's encoding from that of the environment in UNICODE.
bool playing
flag indicating that playback is active
uint8_t new_volume
volume setting to change to
uint8_t current_volume
current effective volume setting
MidiFile current_file
file currently being played from
bool do_stop
flag for stopping playback at next opportunity
MidiFile next_file
upcoming file to play
void CALLBACK TimerCallback(UINT uTimerID, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR)
Realtime MIDI playback service routine.
PlaybackSegment current_segment
segment info for current playback
size_t current_block
next block index to send
UINT timer_id
ID of active multimedia timer.
std::mutex lock
synchronization for playback status fields
uint8_t channel_volumes[16]
last seen volume controller values in raw data
PlaybackSegment next_segment
segment info for upcoming file
HMIDIOUT midi_out
handle to open midiOut
DWORD playback_start_time
timestamp current file began playback
UINT time_period
obtained timer precision value
int do_start
flag for starting playback of next_file at next opportunity
Base for Windows music playback.