Files @ r28800:381a234fb097
Branch filter:

Location: cpp/openttd-patchpack/source/src/music/win32_m.cpp

translators
Update: Translations from eints
english (au): 2 changes by krysclarke
spanish (mexican): 149 changes by Can202
estonian: 11 changes by RM87
chinese (simplified): 18 changes by WenSimEHRP
hungarian: 2 changes by PstasDev
italian: 195 changes by Rivarossi
serbian: 42 changes by nkrs
german: 2 changes by Wuzzy2
belarusian: 537 changes by KorneySan
russian: 25 changes by KorneySan
ukrainian: 21 changes by StepanIvasyn
turkish: 14 changes by jnmbk
latvian: 2 changes by lexuslatvia
dutch: 1 change by iamthedutchdude
spanish: 15 changes by MontyMontana
french: 2 changes by ottdfevr
portuguese: 2 changes by jcteotonio, 2 changes by azulcosta
portuguese (brazilian): 149 changes by pasantoro
polish: 2 changes by pAter-exe
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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 <http://www.gnu.org/licenses/>.
 */

/** @file win32_m.cpp Music playback for Windows. */

#include "../stdafx.h"
#include "../string_func.h"
#include "win32_m.h"
#include <windows.h>
#include <mmsystem.h>
#include "../os/windows/win32.h"
#include "../debug.h"
#include "midifile.hpp"
#include "midi.h"
#include "../base_media_base.h"
#include "../core/mem_func.hpp"
#include <mutex>

#include "../safeguards.h"

struct PlaybackSegment {
	uint32_t start, end;
	size_t start_block;
	bool loop;
};

static struct {
	UINT time_period;    ///< obtained timer precision value
	HMIDIOUT midi_out;   ///< handle to open midiOut
	UINT timer_id;       ///< ID of active multimedia timer
	std::mutex lock;     ///< synchronization for playback status fields

	bool playing;        ///< flag indicating that playback is active
	int do_start;        ///< flag for starting playback of next_file at next opportunity
	bool do_stop;        ///< flag for stopping playback at next opportunity
	byte current_volume; ///< current effective volume setting
	byte new_volume;     ///< volume setting to change to

	MidiFile current_file;           ///< file currently being played from
	PlaybackSegment current_segment; ///< segment info for current playback
	DWORD playback_start_time;       ///< timestamp current file began playback
	size_t current_block;            ///< next block index to send
	MidiFile next_file;              ///< upcoming file to play
	PlaybackSegment next_segment;    ///< segment info for upcoming file

	byte channel_volumes[16]; ///< last seen volume controller values in raw data
} _midi;

static FMusicDriver_Win32 iFMusicDriver_Win32;


static byte ScaleVolume(byte original, byte scale)
{
	return original * scale / 127;
}


void CALLBACK MidiOutProc(HMIDIOUT hmo, UINT wMsg, DWORD_PTR, DWORD_PTR dwParam1, DWORD_PTR)
{
	if (wMsg == MOM_DONE) {
		MIDIHDR *hdr = (LPMIDIHDR)dwParam1;
		midiOutUnprepareHeader(hmo, hdr, sizeof(*hdr));
		free(hdr);
	}
}

static void TransmitChannelMsg(byte status, byte p1, byte p2 = 0)
{
	midiOutShortMsg(_midi.midi_out, status | (p1 << 8) | (p2 << 16));
}

static void TransmitSysex(const byte *&msg_start, size_t &remaining)
{
	/* find end of message */
	const byte *msg_end = msg_start;
	while (*msg_end != MIDIST_ENDSYSEX) msg_end++;
	msg_end++; /* also include sysex end byte */

	/* prepare header */
	MIDIHDR *hdr = CallocT<MIDIHDR>(1);
	hdr->lpData = reinterpret_cast<LPSTR>(const_cast<byte *>(msg_start));
	hdr->dwBufferLength = msg_end - msg_start;
	if (midiOutPrepareHeader(_midi.midi_out, hdr, sizeof(*hdr)) == MMSYSERR_NOERROR) {
		/* transmit - just point directly into the data buffer */
		hdr->dwBytesRecorded = hdr->dwBufferLength;
		midiOutLongMsg(_midi.midi_out, hdr, sizeof(*hdr));
	} else {
		free(hdr);
	}

	/* update position in buffer */
	remaining -= msg_end - msg_start;
	msg_start = msg_end;
}

static void TransmitStandardSysex(MidiSysexMessage msg)
{
	size_t length = 0;
	const byte *data = MidiGetStandardSysexMessage(msg, length);
	TransmitSysex(data, length);
}

/**
 * Realtime MIDI playback service routine.
 * This is called by the multimedia timer.
 */
void CALLBACK TimerCallback(UINT uTimerID, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR)
{
	static int volume_throttle = 0;

	/* Ensure only one timer callback is running at once, and prevent races on status flags */
	std::unique_lock<std::mutex> mutex_lock(_midi.lock, std::defer_lock);
	if (!mutex_lock.try_lock()) return;

	/* check for stop */
	if (_midi.do_stop) {
		Debug(driver, 2, "Win32-MIDI: timer: do_stop is set");
		midiOutReset(_midi.midi_out);
		_midi.playing = false;
		_midi.do_stop = false;
		return;
	}

	/* check for start/restart/change song */
	if (_midi.do_start != 0) {
		/* Have a delay between playback start steps, prevents jumbled-together notes at the start of song */
		if (timeGetTime() - _midi.playback_start_time < 50) {
			return;
		}
		Debug(driver, 2, "Win32-MIDI: timer: do_start step {}", _midi.do_start);

		if (_midi.do_start == 1) {
			/* Send "all notes off" */
			midiOutReset(_midi.midi_out);
			_midi.playback_start_time = timeGetTime();
			_midi.do_start = 2;

			return;
		} else if (_midi.do_start == 2) {
			/* Reset the device to General MIDI defaults */
			TransmitStandardSysex(MidiSysexMessage::ResetGM);
			_midi.playback_start_time = timeGetTime();
			_midi.do_start = 3;

			return;
		} else if (_midi.do_start == 3) {
			/* Set up device-specific effects */
			TransmitStandardSysex(MidiSysexMessage::RolandSetReverb);
			_midi.playback_start_time = timeGetTime();
			_midi.do_start = 4;

			return;
		} else if (_midi.do_start == 4) {
			/* Load the new file */
			_midi.current_file.MoveFrom(_midi.next_file);
			std::swap(_midi.next_segment, _midi.current_segment);
			_midi.current_segment.start_block = 0;
			_midi.playback_start_time = timeGetTime();
			_midi.playing = true;
			_midi.do_start = 0;
			_midi.current_block = 0;

			MemSetT<byte>(_midi.channel_volumes, 127, lengthof(_midi.channel_volumes));
			/* Invalidate current volume. */
			_midi.current_volume = UINT8_MAX;
			volume_throttle = 0;
		}
	} else if (!_midi.playing) {
		/* not playing, stop the timer */
		Debug(driver, 2, "Win32-MIDI: timer: not playing, stopping timer");
		timeKillEvent(uTimerID);
		_midi.timer_id = 0;
		return;
	}

	/* check for volume change */
	if (_midi.current_volume != _midi.new_volume) {
		if (volume_throttle == 0) {
			Debug(driver, 2, "Win32-MIDI: timer: volume change");
			_midi.current_volume = _midi.new_volume;
			volume_throttle = 20 / _midi.time_period;
			for (int ch = 0; ch < 16; ch++) {
				byte vol = ScaleVolume(_midi.channel_volumes[ch], _midi.current_volume);
				TransmitChannelMsg(MIDIST_CONTROLLER | ch, MIDICT_CHANVOLUME, vol);
			}
		} else {
			volume_throttle--;
		}
	}

	/* skip beginning of file? */
	if (_midi.current_segment.start > 0 && _midi.current_block == 0 && _midi.current_segment.start_block == 0) {
		/* find first block after start time and pretend playback started earlier
		 * this is to allow all blocks prior to the actual start to still affect playback,
		 * as they may contain important controller and program changes */
		size_t preload_bytes = 0;
		for (size_t bl = 0; bl < _midi.current_file.blocks.size(); bl++) {
			MidiFile::DataBlock &block = _midi.current_file.blocks[bl];
			preload_bytes += block.data.size();
			if (block.ticktime >= _midi.current_segment.start) {
				if (_midi.current_segment.loop) {
					Debug(driver, 2, "Win32-MIDI: timer: loop from block {} (ticktime {}, realtime {:.3f}, bytes {})", bl, block.ticktime, ((int)block.realtime)/1000.0, preload_bytes);
					_midi.current_segment.start_block = bl;
					break;
				} else {
					/* Calculate offset start time for playback.
					 * The preload_bytes are used to compensate for delay in transmission over traditional serial MIDI interfaces,
					 * which have a bitrate of 31,250 bits/sec, and transmit 1+8+1 start/data/stop bits per byte.
					 * The delay compensation is needed to avoid time-compression of following messages.
					 */
					Debug(driver, 2, "Win32-MIDI: timer: start from block {} (ticktime {}, realtime {:.3f}, bytes {})", bl, block.ticktime, ((int)block.realtime) / 1000.0, preload_bytes);
					_midi.playback_start_time -= block.realtime / 1000 - (DWORD)(preload_bytes * 1000 / 3125);
					break;
				}
			}
		}
	}


	/* play pending blocks */
	DWORD current_time = timeGetTime();
	DWORD playback_time = current_time - _midi.playback_start_time;
	while (_midi.current_block < _midi.current_file.blocks.size()) {
		MidiFile::DataBlock &block = _midi.current_file.blocks[_midi.current_block];

		/* check that block isn't at end-of-song override */
		if (_midi.current_segment.end > 0 && block.ticktime >= _midi.current_segment.end) {
			if (_midi.current_segment.loop) {
				_midi.current_block = _midi.current_segment.start_block;
				_midi.playback_start_time = timeGetTime() - _midi.current_file.blocks[_midi.current_block].realtime / 1000;
			} else {
				_midi.do_stop = true;
			}
			break;
		}
		/* check that block is not in the future */
		if (block.realtime / 1000 > playback_time) {
			break;
		}

		const byte *data = block.data.data();
		size_t remaining = block.data.size();
		byte last_status = 0;
		while (remaining > 0) {
			/* MidiFile ought to have converted everything out of running status,
			 * but handle it anyway just to be safe */
			byte status = data[0];
			if (status & 0x80) {
				last_status = status;
				data++;
				remaining--;
			} else {
				status = last_status;
			}
			switch (status & 0xF0) {
				case MIDIST_PROGCHG:
				case MIDIST_CHANPRESS:
					/* 2 byte channel messages */
					TransmitChannelMsg(status, data[0]);
					data++;
					remaining--;
					break;
				case MIDIST_NOTEOFF:
				case MIDIST_NOTEON:
				case MIDIST_POLYPRESS:
				case MIDIST_PITCHBEND:
					/* 3 byte channel messages */
					TransmitChannelMsg(status, data[0], data[1]);
					data += 2;
					remaining -= 2;
					break;
				case MIDIST_CONTROLLER:
					/* controller change */
					if (data[0] == MIDICT_CHANVOLUME) {
						/* volume controller, adjust for user volume */
						_midi.channel_volumes[status & 0x0F] = data[1];
						int vol = ScaleVolume(data[1], _midi.current_volume);
						TransmitChannelMsg(status, data[0], vol);
					} else {
						/* handle other controllers normally */
						TransmitChannelMsg(status, data[0], data[1]);
					}
					data += 2;
					remaining -= 2;
					break;
				case 0xF0:
					/* system messages */
					switch (status) {
						case MIDIST_SYSEX: /* system exclusive */
							TransmitSysex(data, remaining);
							break;
						case MIDIST_TC_QFRAME: /* time code quarter frame */
						case MIDIST_SONGSEL: /* song select */
							data++;
							remaining--;
							break;
						case MIDIST_SONGPOSPTR: /* song position pointer */
							data += 2;
							remaining -= 2;
							break;
						default: /* remaining have no data bytes */
							break;
					}
					break;
			}
		}

		_midi.current_block++;
	}

	/* end? */
	if (_midi.current_block == _midi.current_file.blocks.size()) {
		if (_midi.current_segment.loop) {
			_midi.current_block = _midi.current_segment.start_block;
			_midi.playback_start_time = timeGetTime() - _midi.current_file.blocks[_midi.current_block].realtime / 1000;
		} else {
			_midi.do_stop = true;
		}
	}
}

void MusicDriver_Win32::PlaySong(const MusicSongInfo &song)
{
	Debug(driver, 2, "Win32-MIDI: PlaySong: entry");

	MidiFile new_song;
	if (!new_song.LoadSong(song)) return;
	Debug(driver, 2, "Win32-MIDI: PlaySong: Loaded song");

	std::lock_guard<std::mutex> mutex_lock(_midi.lock);

	_midi.next_file.MoveFrom(new_song);
	_midi.next_segment.start = song.override_start;
	_midi.next_segment.end = song.override_end;
	_midi.next_segment.loop = song.loop;

	Debug(driver, 2, "Win32-MIDI: PlaySong: setting flag");
	_midi.do_stop = _midi.playing;
	_midi.do_start = 1;

	if (_midi.timer_id == 0) {
		Debug(driver, 2, "Win32-MIDI: PlaySong: starting timer");
		_midi.timer_id = timeSetEvent(_midi.time_period, _midi.time_period, TimerCallback, (DWORD_PTR)this, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
	}
}

void MusicDriver_Win32::StopSong()
{
	Debug(driver, 2, "Win32-MIDI: StopSong: entry");
	std::lock_guard<std::mutex> mutex_lock(_midi.lock);
	Debug(driver, 2, "Win32-MIDI: StopSong: setting flag");
	_midi.do_stop = true;
}

bool MusicDriver_Win32::IsSongPlaying()
{
	return _midi.playing || (_midi.do_start != 0);
}

void MusicDriver_Win32::SetVolume(byte vol)
{
	std::lock_guard<std::mutex> mutex_lock(_midi.lock);
	_midi.new_volume = vol;
}

const char *MusicDriver_Win32::Start(const StringList &parm)
{
	Debug(driver, 2, "Win32-MIDI: Start: initializing");

	int resolution = GetDriverParamInt(parm, "resolution", 5);
	uint port = (uint)GetDriverParamInt(parm, "port", UINT_MAX);
	const char *portname = GetDriverParam(parm, "portname");

	/* Enumerate ports either for selecting port by name, or for debug output */
	if (portname != nullptr || _debug_driver_level > 0) {
		uint numports = midiOutGetNumDevs();
		Debug(driver, 1, "Win32-MIDI: Found {} output devices:", numports);
		for (uint tryport = 0; tryport < numports; tryport++) {
			MIDIOUTCAPS moc{};
			if (midiOutGetDevCaps(tryport, &moc, sizeof(moc)) == MMSYSERR_NOERROR) {
				char tryportname[128];
				convert_from_fs(moc.szPname, tryportname, lengthof(tryportname));

				/* Compare requested and detected port name.
				 * If multiple ports have the same name, this will select the last matching port, and the debug output will be confusing. */
				if (portname != nullptr && strncmp(tryportname, portname, lengthof(tryportname)) == 0) port = tryport;

				Debug(driver, 1, "MIDI port {:2d}: {}{}", tryport, tryportname, (tryport == port) ? " [selected]" : "");
			}
		}
	}

	UINT devid;
	if (port == UINT_MAX) {
		devid = MIDI_MAPPER;
	} else {
		devid = (UINT)port;
	}

	resolution = Clamp(resolution, 1, 20);

	if (midiOutOpen(&_midi.midi_out, devid, (DWORD_PTR)&MidiOutProc, (DWORD_PTR)this, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) {
		return "could not open midi device";
	}

	midiOutReset(_midi.midi_out);

	/* prepare multimedia timer */
	TIMECAPS timecaps;
	if (timeGetDevCaps(&timecaps, sizeof(timecaps)) == MMSYSERR_NOERROR) {
		_midi.time_period = std::min(std::max((UINT)resolution, timecaps.wPeriodMin), timecaps.wPeriodMax);
		if (timeBeginPeriod(_midi.time_period) == MMSYSERR_NOERROR) {
			/* success */
			Debug(driver, 2, "Win32-MIDI: Start: timer resolution is {}", _midi.time_period);
			return nullptr;
		}
	}
	midiOutClose(_midi.midi_out);
	return "could not set timer resolution";
}

void MusicDriver_Win32::Stop()
{
	std::lock_guard<std::mutex> mutex_lock(_midi.lock);

	if (_midi.timer_id) {
		timeKillEvent(_midi.timer_id);
		_midi.timer_id = 0;
	}

	timeEndPeriod(_midi.time_period);
	midiOutReset(_midi.midi_out);
	midiOutClose(_midi.midi_out);
}