OpenTTD Source: src/music/win32_m.cpp Source File

OpenTTD Source 15.0-RC2

win32_m.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../string_func.h"
12#include "win32_m.h"
13#include <windows.h>
14#include <mmsystem.h>
15#include "../os/windows/win32.h"
16#include "../debug.h"
17#include "midifile.hpp"
18#include "midi.h"
19#include "../base_media_base.h"
20#include "../base_media_music.h"
21#include <mutex>
22
23#include "../safeguards.h"
24
25struct PlaybackSegment {
26 uint32_t start, end;
27 size_t start_block;
28 bool loop;
29};
30
31static struct {
32 UINT time_period;
33 HMIDIOUT midi_out;
34 UINT timer_id;
35 std::mutex lock;
36
37 bool playing;
38 int do_start;
39 bool do_stop;
40 uint8_t current_volume;
41 uint8_t new_volume;
42
43 MidiFile current_file;
44 PlaybackSegment current_segment;
45 DWORD playback_start_time;
46 size_t current_block;
47 MidiFile next_file;
48 PlaybackSegment next_segment;
49
50 std::array<uint8_t, 16> channel_volumes;
51} _midi;
52
53static FMusicDriver_Win32 iFMusicDriver_Win32;
54
55
56static uint8_t ScaleVolume(uint8_t original, uint8_t scale)
57{
58 return original * scale / 127;
59}
60
61
62void CALLBACK MidiOutProc(HMIDIOUT hmo, UINT wMsg, DWORD_PTR, DWORD_PTR dwParam1, DWORD_PTR)
63{
64 if (wMsg == MOM_DONE) {
65 MIDIHDR *hdr = (LPMIDIHDR)dwParam1;
66 midiOutUnprepareHeader(hmo, hdr, sizeof(*hdr));
67 delete hdr;
68 }
69}
70
71static void TransmitChannelMsg(uint8_t status, uint8_t p1, uint8_t p2 = 0)
72{
73 midiOutShortMsg(_midi.midi_out, status | (p1 << 8) | (p2 << 16));
74}
75
76static void TransmitSysex(const uint8_t *&msg_start, size_t &remaining)
77{
78 /* find end of message */
79 const uint8_t *msg_end = msg_start;
80 while (*msg_end != MIDIST_ENDSYSEX) msg_end++;
81 msg_end++; /* also include sysex end byte */
82
83 /* prepare header */
84 auto hdr = std::make_unique<MIDIHDR>();
85 hdr->lpData = reinterpret_cast<LPSTR>(const_cast<uint8_t *>(msg_start));
86 hdr->dwBufferLength = static_cast<DWORD>(msg_end - msg_start);
87 if (midiOutPrepareHeader(_midi.midi_out, hdr.get(), sizeof(MIDIHDR)) == MMSYSERR_NOERROR) {
88 /* transmit - just point directly into the data buffer */
89 hdr->dwBytesRecorded = hdr->dwBufferLength;
90 midiOutLongMsg(_midi.midi_out, hdr.release(), sizeof(MIDIHDR));
91 }
92
93 /* update position in buffer */
94 remaining -= msg_end - msg_start;
95 msg_start = msg_end;
96}
97
98static void TransmitStandardSysex(MidiSysexMessage msg)
99{
100 size_t length = 0;
101 const uint8_t *data = MidiGetStandardSysexMessage(msg, length);
102 TransmitSysex(data, length);
103}
104
109 void CALLBACK TimerCallback(UINT uTimerID, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR)
110{
111 static int volume_throttle = 0;
112
113 /* Ensure only one timer callback is running at once, and prevent races on status flags */
114 std::unique_lock<std::mutex> mutex_lock(_midi.lock, std::defer_lock);
115 if (!mutex_lock.try_lock()) return;
116
117 /* check for stop */
118 if (_midi.do_stop) {
119 Debug(driver, 2, "Win32-MIDI: timer: do_stop is set");
120 midiOutReset(_midi.midi_out);
121 _midi.playing = false;
122 _midi.do_stop = false;
123 return;
124 }
125
126 /* check for start/restart/change song */
127 if (_midi.do_start != 0) {
128 /* Have a delay between playback start steps, prevents jumbled-together notes at the start of song */
129 if (timeGetTime() - _midi.playback_start_time < 50) {
130 return;
131 }
132 Debug(driver, 2, "Win32-MIDI: timer: do_start step {}", _midi.do_start);
133
134 if (_midi.do_start == 1) {
135 /* Send "all notes off" */
136 midiOutReset(_midi.midi_out);
137 _midi.playback_start_time = timeGetTime();
138 _midi.do_start = 2;
139
140 return;
141 } else if (_midi.do_start == 2) {
142 /* Reset the device to General MIDI defaults */
143 TransmitStandardSysex(MidiSysexMessage::ResetGM);
144 _midi.playback_start_time = timeGetTime();
145 _midi.do_start = 3;
146
147 return;
148 } else if (_midi.do_start == 3) {
149 /* Set up device-specific effects */
150 TransmitStandardSysex(MidiSysexMessage::RolandSetReverb);
151 _midi.playback_start_time = timeGetTime();
152 _midi.do_start = 4;
153
154 return;
155 } else if (_midi.do_start == 4) {
156 /* Load the new file */
157 _midi.current_file.MoveFrom(_midi.next_file);
158 std::swap(_midi.next_segment, _midi.current_segment);
159 _midi.current_segment.start_block = 0;
160 _midi.playback_start_time = timeGetTime();
161 _midi.playing = true;
162 _midi.do_start = 0;
163 _midi.current_block = 0;
164
165 _midi.channel_volumes.fill(127);
166 /* Invalidate current volume. */
167 _midi.current_volume = UINT8_MAX;
168 volume_throttle = 0;
169 }
170 } else if (!_midi.playing) {
171 /* not playing, stop the timer */
172 Debug(driver, 2, "Win32-MIDI: timer: not playing, stopping timer");
173 timeKillEvent(uTimerID);
174 _midi.timer_id = 0;
175 return;
176 }
177
178 /* check for volume change */
179 if (_midi.current_volume != _midi.new_volume) {
180 if (volume_throttle == 0) {
181 Debug(driver, 2, "Win32-MIDI: timer: volume change");
182 _midi.current_volume = _midi.new_volume;
183 volume_throttle = 20 / _midi.time_period;
184 for (int ch = 0; ch < 16; ch++) {
185 uint8_t vol = ScaleVolume(_midi.channel_volumes[ch], _midi.current_volume);
186 TransmitChannelMsg(MIDIST_CONTROLLER | ch, MIDICT_CHANVOLUME, vol);
187 }
188 } else {
189 volume_throttle--;
190 }
191 }
192
193 /* skip beginning of file? */
194 if (_midi.current_segment.start > 0 && _midi.current_block == 0 && _midi.current_segment.start_block == 0) {
195 /* find first block after start time and pretend playback started earlier
196 * this is to allow all blocks prior to the actual start to still affect playback,
197 * as they may contain important controller and program changes */
198 size_t preload_bytes = 0;
199 for (size_t bl = 0; bl < _midi.current_file.blocks.size(); bl++) {
200 MidiFile::DataBlock &block = _midi.current_file.blocks[bl];
201 preload_bytes += block.data.size();
202 if (block.ticktime >= _midi.current_segment.start) {
203 if (_midi.current_segment.loop) {
204 Debug(driver, 2, "Win32-MIDI: timer: loop from block {} (ticktime {}, realtime {:.3f}, bytes {})", bl, block.ticktime, block.realtime / 1000.0, preload_bytes);
205 _midi.current_segment.start_block = bl;
206 break;
207 } else {
208 /* Calculate offset start time for playback.
209 * The preload_bytes are used to compensate for delay in transmission over traditional serial MIDI interfaces,
210 * which have a bitrate of 31,250 bits/sec, and transmit 1+8+1 start/data/stop bits per byte.
211 * The delay compensation is needed to avoid time-compression of following messages.
212 */
213 Debug(driver, 2, "Win32-MIDI: timer: start from block {} (ticktime {}, realtime {:.3f}, bytes {})", bl, block.ticktime, block.realtime / 1000.0, preload_bytes);
214 _midi.playback_start_time -= block.realtime / 1000 - (DWORD)(preload_bytes * 1000 / 3125);
215 break;
216 }
217 }
218 }
219 }
220
221
222 /* play pending blocks */
223 DWORD current_time = timeGetTime();
224 DWORD playback_time = current_time - _midi.playback_start_time;
225 while (_midi.current_block < _midi.current_file.blocks.size()) {
226 MidiFile::DataBlock &block = _midi.current_file.blocks[_midi.current_block];
227
228 /* check that block isn't at end-of-song override */
229 if (_midi.current_segment.end > 0 && block.ticktime >= _midi.current_segment.end) {
230 if (_midi.current_segment.loop) {
231 _midi.current_block = _midi.current_segment.start_block;
232 _midi.playback_start_time = timeGetTime() - _midi.current_file.blocks[_midi.current_block].realtime / 1000;
233 } else {
234 _midi.do_stop = true;
235 }
236 break;
237 }
238 /* check that block is not in the future */
239 if (block.realtime / 1000 > playback_time) {
240 break;
241 }
242
243 const uint8_t *data = block.data.data();
244 size_t remaining = block.data.size();
245 uint8_t last_status = 0;
246 while (remaining > 0) {
247 /* MidiFile ought to have converted everything out of running status,
248 * but handle it anyway just to be safe */
249 uint8_t status = data[0];
250 if (status & 0x80) {
251 last_status = status;
252 data++;
253 remaining--;
254 } else {
255 status = last_status;
256 }
257 switch (status & 0xF0) {
258 case MIDIST_PROGCHG:
259 case MIDIST_CHANPRESS:
260 /* 2 byte channel messages */
261 TransmitChannelMsg(status, data[0]);
262 data++;
263 remaining--;
264 break;
265 case MIDIST_NOTEOFF:
266 case MIDIST_NOTEON:
267 case MIDIST_POLYPRESS:
268 case MIDIST_PITCHBEND:
269 /* 3 byte channel messages */
270 TransmitChannelMsg(status, data[0], data[1]);
271 data += 2;
272 remaining -= 2;
273 break;
274 case MIDIST_CONTROLLER:
275 /* controller change */
276 if (data[0] == MIDICT_CHANVOLUME) {
277 /* volume controller, adjust for user volume */
278 _midi.channel_volumes[status & 0x0F] = data[1];
279 int vol = ScaleVolume(data[1], _midi.current_volume);
280 TransmitChannelMsg(status, data[0], vol);
281 } else {
282 /* handle other controllers normally */
283 TransmitChannelMsg(status, data[0], data[1]);
284 }
285 data += 2;
286 remaining -= 2;
287 break;
288 case 0xF0:
289 /* system messages */
290 switch (status) {
291 case MIDIST_SYSEX: /* system exclusive */
292 TransmitSysex(data, remaining);
293 break;
294 case MIDIST_TC_QFRAME: /* time code quarter frame */
295 case MIDIST_SONGSEL: /* song select */
296 data++;
297 remaining--;
298 break;
299 case MIDIST_SONGPOSPTR: /* song position pointer */
300 data += 2;
301 remaining -= 2;
302 break;
303 default: /* remaining have no data bytes */
304 break;
305 }
306 break;
307 }
308 }
309
310 _midi.current_block++;
311 }
312
313 /* end? */
314 if (_midi.current_block == _midi.current_file.blocks.size()) {
315 if (_midi.current_segment.loop) {
316 _midi.current_block = _midi.current_segment.start_block;
317 _midi.playback_start_time = timeGetTime() - _midi.current_file.blocks[_midi.current_block].realtime / 1000;
318 } else {
319 _midi.do_stop = true;
320 }
321 }
322}
323
324 void MusicDriver_Win32::PlaySong(const MusicSongInfo &song)
325{
326 Debug(driver, 2, "Win32-MIDI: PlaySong: entry");
327
328 MidiFile new_song;
329 if (!new_song.LoadSong(song)) return;
330 Debug(driver, 2, "Win32-MIDI: PlaySong: Loaded song");
331
332 std::lock_guard<std::mutex> mutex_lock(_midi.lock);
333
334 _midi.next_file.MoveFrom(new_song);
335 _midi.next_segment.start = song.override_start;
336 _midi.next_segment.end = song.override_end;
337 _midi.next_segment.loop = song.loop;
338
339 Debug(driver, 2, "Win32-MIDI: PlaySong: setting flag");
340 _midi.do_stop = _midi.playing;
341 _midi.do_start = 1;
342
343 if (_midi.timer_id == 0) {
344 Debug(driver, 2, "Win32-MIDI: PlaySong: starting timer");
345 _midi.timer_id = timeSetEvent(_midi.time_period, _midi.time_period, TimerCallback, (DWORD_PTR)this, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
346 }
347}
348
349 void MusicDriver_Win32::StopSong()
350{
351 Debug(driver, 2, "Win32-MIDI: StopSong: entry");
352 std::lock_guard<std::mutex> mutex_lock(_midi.lock);
353 Debug(driver, 2, "Win32-MIDI: StopSong: setting flag");
354 _midi.do_stop = true;
355}
356
357 bool MusicDriver_Win32::IsSongPlaying()
358{
359 return _midi.playing || (_midi.do_start != 0);
360}
361
362 void MusicDriver_Win32::SetVolume(uint8_t vol)
363{
364 std::lock_guard<std::mutex> mutex_lock(_midi.lock);
365 _midi.new_volume = vol;
366}
367
368std::optional<std::string_view> MusicDriver_Win32::Start(const StringList &parm)
369{
370 Debug(driver, 2, "Win32-MIDI: Start: initializing");
371
372 int resolution = GetDriverParamInt(parm, "resolution", 5);
373 uint port = (uint)GetDriverParamInt(parm, "port", UINT_MAX);
374 auto portname = GetDriverParam(parm, "portname");
375
376 /* Enumerate ports either for selecting port by name, or for debug output */
377 if (portname.has_value() || _debug_driver_level > 0) {
378 uint numports = midiOutGetNumDevs();
379 Debug(driver, 1, "Win32-MIDI: Found {} output devices:", numports);
380 for (uint tryport = 0; tryport < numports; tryport++) {
381 MIDIOUTCAPS moc{};
382 if (midiOutGetDevCaps(tryport, &moc, sizeof(moc)) == MMSYSERR_NOERROR) {
383 char tryportname[128];
384 convert_from_fs(moc.szPname, tryportname);
385
386 /* Compare requested and detected port name.
387 * If multiple ports have the same name, this will select the last matching port, and the debug output will be confusing. */
388 if (portname.has_value() && *portname == tryportname) port = tryport;
389
390 Debug(driver, 1, "MIDI port {:2d}: {}{}", tryport, tryportname, (tryport == port) ? " [selected]" : "");
391 }
392 }
393 }
394
395 UINT devid;
396 if (port == UINT_MAX) {
397 devid = MIDI_MAPPER;
398 } else {
399 devid = (UINT)port;
400 }
401
402 resolution = Clamp(resolution, 1, 20);
403
404 if (midiOutOpen(&_midi.midi_out, devid, (DWORD_PTR)&MidiOutProc, (DWORD_PTR)this, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) {
405 return "could not open midi device";
406 }
407
408 midiOutReset(_midi.midi_out);
409
410 /* prepare multimedia timer */
411 TIMECAPS timecaps;
412 if (timeGetDevCaps(&timecaps, sizeof(timecaps)) == MMSYSERR_NOERROR) {
413 _midi.time_period = std::min(std::max((UINT)resolution, timecaps.wPeriodMin), timecaps.wPeriodMax);
414 if (timeBeginPeriod(_midi.time_period) == MMSYSERR_NOERROR) {
415 /* success */
416 Debug(driver, 2, "Win32-MIDI: Start: timer resolution is {}", _midi.time_period);
417 return std::nullopt;
418 }
419 }
420 midiOutClose(_midi.midi_out);
421 return "could not set timer resolution";
422}
423
424 void MusicDriver_Win32::Stop()
425{
426 std::lock_guard<std::mutex> mutex_lock(_midi.lock);
427
428 if (_midi.timer_id) {
429 timeKillEvent(_midi.timer_id);
430 _midi.timer_id = 0;
431 }
432
433 timeEndPeriod(_midi.time_period);
434 midiOutReset(_midi.midi_out);
435 midiOutClose(_midi.midi_out);
436}
FMusicDriver_Win32
Factory for Windows' music player.
Definition win32_m.h:33
MusicDriver_Win32::PlaySong
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Definition win32_m.cpp:324
MusicDriver_Win32::StopSong
void StopSong() override
Stop playing the current song.
Definition win32_m.cpp:349
MusicDriver_Win32::IsSongPlaying
bool IsSongPlaying() override
Are we currently playing a song?
Definition win32_m.cpp:357
MusicDriver_Win32::SetVolume
void SetVolume(uint8_t vol) override
Set the volume, if possible.
Definition win32_m.cpp:362
MusicDriver_Win32::Stop
void Stop() override
Stop this driver.
Definition win32_m.cpp:424
MusicDriver_Win32::Start
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_m.cpp:368
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, std::string_view name, int def)
Get an integer parameter the list of parameters.
Definition driver.cpp:79
GetDriverParam
std::optional< std::string_view > GetDriverParam(const StringList &parm, std::string_view name)
Get a string parameter the list of parameters.
Definition driver.cpp:47
_midi
static struct @13 _midi
Metadata about the midi we're playing.
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
MidiFile::DataBlock
Definition midifile.hpp:19
MidiFile::DataBlock::data
std::vector< uint8_t > data
raw midi data contained in block
Definition midifile.hpp:22
MidiFile::DataBlock::ticktime
uint32_t ticktime
tick number since start of file this block should be triggered at
Definition midifile.hpp:20
MidiFile::DataBlock::realtime
int64_t realtime
real-time (microseconds) since start of file this block should be triggered at
Definition midifile.hpp:21
MidiFile
Definition midifile.hpp:18
MusicSongInfo
Metadata about a music track.
Definition base_media_music.h:35
MusicSongInfo::override_start
int override_start
MIDI ticks to skip over in beginning.
Definition base_media_music.h:39
MusicSongInfo::loop
bool loop
song should play in a tight loop if possible, never ending
Definition base_media_music.h:43
MusicSongInfo::override_end
int override_end
MIDI tick to end the song at (0 if no override)
Definition base_media_music.h:40
PlaybackSegment
Definition dmusic.cpp:113
convert_from_fs
std::string_view 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.
Definition win32.cpp:375
playing
bool playing
flag indicating that playback is active
Definition win32_m.cpp:37
new_volume
uint8_t new_volume
volume setting to change to
Definition win32_m.cpp:41
current_volume
uint8_t current_volume
current effective volume setting
Definition win32_m.cpp:40
current_file
MidiFile current_file
file currently being played from
Definition win32_m.cpp:43
do_stop
bool do_stop
flag for stopping playback at next opportunity
Definition win32_m.cpp:39
next_file
MidiFile next_file
upcoming file to play
Definition win32_m.cpp:47
TimerCallback
void CALLBACK TimerCallback(UINT uTimerID, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR)
Realtime MIDI playback service routine.
Definition win32_m.cpp:109
current_segment
PlaybackSegment current_segment
segment info for current playback
Definition win32_m.cpp:44
current_block
size_t current_block
next block index to send
Definition win32_m.cpp:46
channel_volumes
std::array< uint8_t, 16 > channel_volumes
last seen volume controller values in raw data
Definition win32_m.cpp:50
timer_id
UINT timer_id
ID of active multimedia timer.
Definition win32_m.cpp:34
lock
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35
next_segment
PlaybackSegment next_segment
segment info for upcoming file
Definition win32_m.cpp:48
midi_out
HMIDIOUT midi_out
handle to open midiOut
Definition win32_m.cpp:33
playback_start_time
DWORD playback_start_time
timestamp current file began playback
Definition win32_m.cpp:45
time_period
UINT time_period
obtained timer precision value
Definition win32_m.cpp:32
do_start
int do_start
flag for starting playback of next_file at next opportunity
Definition win32_m.cpp:38
win32_m.h
Base for Windows music playback.