11 #include "../stdafx.h"
12 #ifdef WIN32_LEAN_AND_MEAN
13 # undef WIN32_LEAN_AND_MEAN // Don't exclude rarely-used stuff from Windows headers
16 #include "../os/windows/win32.h"
17 #include "../core/mem_func.hpp"
18 #include "../thread.h"
19 #include "../fileio_func.h"
20 #include "../base_media_base.h"
22 #include "midifile.hpp"
30 #include "../safeguards.h"
33 # pragma comment(lib, "ole32.lib")
40 #define FOURCC_INFO mmioFOURCC('I', 'N', 'F', 'O')
41 #define FOURCC_fmt mmioFOURCC('f', 'm', 't', ' ')
42 #define FOURCC_data mmioFOURCC('d', 'a', 't', 'a')
52 std::vector<WLOOP> wave_loops;
53 std::vector<CONNECTION> articulators;
60 std::vector<CONNECTION> articulators;
61 std::vector<DLSRegion> regions;
69 std::vector<BYTE> data;
72 std::vector<WLOOP> wave_loops;
74 bool operator ==(
long offset)
const
76 return this->file_offset == offset;
80 std::vector<DLSInstrument> instruments;
81 std::vector<POOLCUE> pool_cues;
82 std::vector<DLSWave> waves;
93 bool ReadDLSRegion(FILE *f, DWORD list_length, std::vector<DLSRegion> &out);
101 bool ReadDLSWave(FILE *f, DWORD list_length,
long offset);
105 PACK_N(
struct ChunkHeader {
111 PACK_N(
struct WAVE_DOWNLOAD {
112 DMUS_DOWNLOADINFO dlInfo;
113 ULONG ulOffsetTable[2];
115 DMUS_WAVEDATA dmWaveData;
147 static IDirectMusicPort *
_port =
nullptr;
159 while (list_length > 0) {
161 if (fread(&chunk,
sizeof(chunk), 1, f) != 1)
return false;
162 list_length -= chunk.length +
sizeof(chunk);
164 if (chunk.type == FOURCC_ART1) {
165 CONNECTIONLIST conns;
166 if (fread(&conns,
sizeof(conns), 1, f) != 1)
return false;
167 fseek(f, conns.cbSize -
sizeof(conns), SEEK_CUR);
170 for (ULONG i = 0; i < conns.cConnections; i++) {
172 if (fread(&con,
sizeof(con), 1, f) != 1)
return false;
176 fseek(f, chunk.length, SEEK_CUR);
189 region.wave_sample.cbSize = 0;
191 while (list_length > 0) {
193 if (fread(&chunk,
sizeof(chunk), 1, f) != 1)
return false;
194 list_length -= chunk.length +
sizeof(chunk);
196 if (chunk.type == FOURCC_LIST) {
198 if (fread(&chunk.type,
sizeof(chunk.type), 1, f) != 1)
return false;
199 chunk.length -=
sizeof(chunk.type);
202 switch (chunk.type) {
204 if (fread(®ion.hdr,
sizeof(region.hdr), 1, f) != 1)
return false;
208 if (fread(®ion.wave_sample,
sizeof(region.wave_sample), 1, f) != 1)
return false;
209 fseek(f, region.wave_sample.cbSize -
sizeof(region.wave_sample), SEEK_CUR);
212 for (ULONG i = 0; i < region.wave_sample.cSampleLoops; i++) {
214 if (fread(&loop,
sizeof(loop), 1, f) != 1)
return false;
215 region.wave_loops.push_back(loop);
220 if (fread(®ion.wave,
sizeof(region.wave), 1, f) != 1)
return false;
229 fseek(f, chunk.length, SEEK_CUR);
233 Debug(driver, 7,
"DLS: Ignoring unknown chunk {}{}{}{}", (
char)(chunk.type & 0xFF), (
char)((chunk.type >> 8) & 0xFF), (
char)((chunk.type >> 16) & 0xFF), (
char)((chunk.type >> 24) & 0xFF));
234 fseek(f, chunk.length, SEEK_CUR);
244 while (list_length > 0) {
246 if (fread(&chunk,
sizeof(chunk), 1, f) != 1)
return false;
247 list_length -= chunk.length +
sizeof(chunk);
249 if (chunk.type == FOURCC_LIST) {
251 if (fread(&list_type,
sizeof(list_type), 1, f) != 1)
return false;
253 if (list_type == FOURCC_RGN) {
254 this->
ReadDLSRegion(f, chunk.length -
sizeof(list_type), instrument.regions);
256 Debug(driver, 7,
"DLS: Ignoring unknown list chunk of type {}{}{}{}", (
char)(list_type & 0xFF), (
char)((list_type >> 8) & 0xFF), (
char)((list_type >> 16) & 0xFF), (
char)((list_type >> 24) & 0xFF));
257 fseek(f, chunk.length -
sizeof(list_type), SEEK_CUR);
260 Debug(driver, 7,
"DLS: Ignoring chunk {}{}{}{}", (
char)(chunk.type & 0xFF), (
char)((chunk.type >> 8) & 0xFF), (
char)((chunk.type >> 16) & 0xFF), (
char)((chunk.type >> 24) & 0xFF));
261 fseek(f, chunk.length, SEEK_CUR);
273 while (list_length > 0) {
275 if (fread(&chunk,
sizeof(chunk), 1, f) != 1)
return false;
276 list_length -= chunk.length +
sizeof(chunk);
278 if (chunk.type == FOURCC_LIST) {
280 if (fread(&chunk.type,
sizeof(chunk.type), 1, f) != 1)
return false;
281 chunk.length -=
sizeof(chunk.type);
284 switch (chunk.type) {
286 if (fread(&instrument.hdr,
sizeof(instrument.hdr), 1, f) != 1)
return false;
299 fseek(f, chunk.length, SEEK_CUR);
303 Debug(driver, 7,
"DLS: Ignoring unknown chunk {}{}{}{}", (
char)(chunk.type & 0xFF), (
char)((chunk.type >> 8) & 0xFF), (
char)((chunk.type >> 16) & 0xFF), (
char)((chunk.type >> 24) & 0xFF));
304 fseek(f, chunk.length, SEEK_CUR);
314 while (list_length > 0) {
316 if (fread(&chunk,
sizeof(chunk), 1, f) != 1)
return false;
317 list_length -= chunk.length +
sizeof(chunk);
319 if (chunk.type == FOURCC_LIST) {
321 if (fread(&list_type,
sizeof(list_type), 1, f) != 1)
return false;
323 if (list_type == FOURCC_INS) {
324 Debug(driver, 6,
"DLS: Reading instrument {}", (
int)instruments.size());
328 Debug(driver, 7,
"DLS: Ignoring unknown list chunk of type {}{}{}{}", (
char)(list_type & 0xFF), (
char)((list_type >> 8) & 0xFF), (
char)((list_type >> 16) & 0xFF), (
char)((list_type >> 24) & 0xFF));
329 fseek(f, chunk.length -
sizeof(list_type), SEEK_CUR);
332 Debug(driver, 7,
"DLS: Ignoring chunk {}{}{}{}", (
char)(chunk.type & 0xFF), (
char)((chunk.type >> 8) & 0xFF), (
char)((chunk.type >> 16) & 0xFF), (
char)((chunk.type >> 24) & 0xFF));
333 fseek(f, chunk.length, SEEK_CUR);
342 this->waves.push_back(
DLSWave());
343 DLSWave &wave = this->waves.back();
347 wave.wave_sample.cbSize =
sizeof(WSMPL);
348 wave.wave_sample.usUnityNote = 60;
349 wave.file_offset = offset;
351 while (list_length > 0) {
353 if (fread(&chunk,
sizeof(chunk), 1, f) != 1)
return false;
354 list_length -= chunk.length +
sizeof(chunk);
356 if (chunk.type == FOURCC_LIST) {
358 if (fread(&chunk.type,
sizeof(chunk.type), 1, f) != 1)
return false;
359 chunk.length -=
sizeof(chunk.type);
362 switch (chunk.type) {
364 if (fread(&wave.fmt,
sizeof(wave.fmt), 1, f) != 1)
return false;
365 if (chunk.length >
sizeof(wave.fmt)) fseek(f, chunk.length -
sizeof(wave.fmt), SEEK_CUR);
369 if (fread(&wave.wave_sample,
sizeof(wave.wave_sample), 1, f) != 1)
return false;
370 fseek(f, wave.wave_sample.cbSize -
sizeof(wave.wave_sample), SEEK_CUR);
373 for (ULONG i = 0; i < wave.wave_sample.cSampleLoops; i++) {
375 if (fread(&loop,
sizeof(loop), 1, f) != 1)
return false;
376 wave.wave_loops.push_back(loop);
381 wave.data.resize(chunk.length);
382 if (fread(&wave.data[0],
sizeof(BYTE), wave.data.size(), f) != wave.data.size())
return false;
387 fseek(f, chunk.length, SEEK_CUR);
391 Debug(driver, 7,
"DLS: Ignoring unknown chunk {}{}{}{}", (
char)(chunk.type & 0xFF), (
char)((chunk.type >> 8) & 0xFF), (
char)((chunk.type >> 16) & 0xFF), (
char)((chunk.type >> 24) & 0xFF));
392 fseek(f, chunk.length, SEEK_CUR);
402 long base_offset = ftell(f);
404 while (list_length > 0) {
405 long chunk_offset = ftell(f);
408 if (fread(&chunk,
sizeof(chunk), 1, f) != 1)
return false;
409 list_length -= chunk.length +
sizeof(chunk);
411 if (chunk.type == FOURCC_LIST) {
413 if (fread(&list_type,
sizeof(list_type), 1, f) != 1)
return false;
415 if (list_type == FOURCC_wave) {
416 Debug(driver, 6,
"DLS: Reading wave {}", waves.size());
418 if (!this->
ReadDLSWave(f, chunk.length -
sizeof(list_type), chunk_offset - base_offset))
return false;
420 Debug(driver, 7,
"DLS: Ignoring unknown list chunk of type {}{}{}{}", (
char)(list_type & 0xFF), (
char)((list_type >> 8) & 0xFF), (
char)((list_type >> 16) & 0xFF), (
char)((list_type >> 24) & 0xFF));
421 fseek(f, chunk.length -
sizeof(list_type), SEEK_CUR);
424 Debug(driver, 7,
"DLS: Ignoring chunk {}{}{}{}", (
char)(chunk.type & 0xFF), (
char)((chunk.type >> 8) & 0xFF), (
char)((chunk.type >> 16) & 0xFF), (
char)((chunk.type >> 24) & 0xFF));
425 fseek(f, chunk.length, SEEK_CUR);
434 Debug(driver, 2,
"DMusic: Try to load DLS file {}",
FS2OTTD(file));
436 FILE *f = _wfopen(file, L
"rb");
437 if (f ==
nullptr)
return false;
444 if (fread(&hdr,
sizeof(hdr), 1, f) != 1)
return false;
445 if (fread(&dls_type,
sizeof(dls_type), 1, f) != 1)
return false;
446 if (hdr.type != FOURCC_RIFF || dls_type != FOURCC_DLS)
return false;
448 hdr.length -=
sizeof(FOURCC);
450 Debug(driver, 2,
"DMusic: Parsing DLS file");
456 while (hdr.length > 0) {
458 if (fread(&chunk,
sizeof(chunk), 1, f) != 1)
return false;
459 hdr.length -= chunk.length +
sizeof(chunk);
461 if (chunk.type == FOURCC_LIST) {
463 if (fread(&chunk.type,
sizeof(chunk.type), 1, f) != 1)
return false;
464 chunk.length -=
sizeof(chunk.type);
467 switch (chunk.type) {
469 if (fread(&header,
sizeof(header), 1, f) != 1)
return false;
482 if (fread(&ptbl,
sizeof(ptbl), 1, f) != 1)
return false;
483 fseek(f, ptbl.cbSize -
sizeof(ptbl), SEEK_CUR);
486 for (ULONG i = 0; i < ptbl.cCues; i++) {
488 if (fread(&cue,
sizeof(cue), 1, f) != 1)
return false;
489 this->pool_cues.push_back(cue);
495 fseek(f, chunk.length, SEEK_CUR);
499 Debug(driver, 7,
"DLS: Ignoring unknown chunk {}{}{}{}", (
char)(chunk.type & 0xFF), (
char)((chunk.type >> 8) & 0xFF), (
char)((chunk.type >> 16) & 0xFF), (
char)((chunk.type >> 24) & 0xFF));
500 fseek(f, chunk.length, SEEK_CUR);
506 if (header.cInstruments != this->instruments.size())
return false;
509 for (std::vector<POOLCUE>::iterator cue = this->pool_cues.begin(); cue != this->pool_cues.end(); cue++) {
510 std::vector<DLSWave>::iterator w = std::find(this->waves.begin(), this->waves.end(), cue->ulOffset);
511 if (w != this->waves.end()) {
512 cue->ulOffset = (ULONG)(w - this->waves.begin());
522 static byte ScaleVolume(
byte original,
byte scale)
524 return original * scale / 127;
527 static void TransmitChannelMsg(IDirectMusicBuffer *buffer, REFERENCE_TIME rt,
byte status,
byte p1,
byte p2 = 0)
529 if (buffer->PackStructured(rt, 0, status | (p1 << 8) | (p2 << 16)) == E_OUTOFMEMORY) {
531 _port->PlayBuffer(buffer);
534 buffer->PackStructured(rt, 0, status | (p1 << 8) | (p2 << 16));
538 static void TransmitSysex(IDirectMusicBuffer *buffer, REFERENCE_TIME rt,
const byte *&msg_start,
size_t &remaining)
541 const byte *msg_end = msg_start;
542 while (*msg_end != MIDIST_ENDSYSEX) msg_end++;
545 if (buffer->PackUnstructured(rt, 0, msg_end - msg_start,
const_cast<LPBYTE
>(msg_start)) == E_OUTOFMEMORY) {
547 _port->PlayBuffer(buffer);
550 buffer->PackUnstructured(rt, 0, msg_end - msg_start,
const_cast<LPBYTE
>(msg_start));
554 remaining -= msg_end - msg_start;
558 static void TransmitStandardSysex(IDirectMusicBuffer *buffer, REFERENCE_TIME rt, MidiSysexMessage msg)
561 const byte *data = MidiGetStandardSysexMessage(msg, length);
562 TransmitSysex(buffer, rt, data, length);
566 static void TransmitNotesOff(IDirectMusicBuffer *buffer, REFERENCE_TIME block_time, REFERENCE_TIME cur_time)
568 for (
int ch = 0; ch < 16; ch++) {
569 TransmitChannelMsg(buffer, block_time + 10, MIDIST_CONTROLLER | ch, MIDICT_MODE_ALLNOTESOFF, 0);
570 TransmitChannelMsg(buffer, block_time + 10, MIDIST_CONTROLLER | ch, MIDICT_SUSTAINSW, 0);
571 TransmitChannelMsg(buffer, block_time + 10, MIDIST_CONTROLLER | ch, MIDICT_MODE_RESETALLCTRL, 0);
575 TransmitStandardSysex(buffer, block_time + 20, MidiSysexMessage::ResetGM);
576 TransmitStandardSysex(buffer, block_time + 30, MidiSysexMessage::RolandSetReverb);
580 _port->PlayBuffer(buffer);
587 static void MidiThreadProc()
589 Debug(driver, 2,
"DMusic: Entering playback thread");
591 REFERENCE_TIME last_volume_time = 0;
592 REFERENCE_TIME block_time = 0;
601 IReferenceClock *clock;
602 _port->GetLatencyClock(&clock);
604 REFERENCE_TIME cur_time;
605 clock->GetTime(&cur_time);
610 DWORD next_timeout = 1000;
613 DWORD wfso = WaitForSingleObject(
_thread_event, next_timeout);
615 if (_playback.shutdown) {
616 _playback.playing =
false;
620 if (_playback.do_stop) {
621 Debug(driver, 2,
"DMusic thread: Stopping playback");
624 clock->GetTime(&cur_time);
627 _playback.playing =
false;
628 _playback.do_stop =
false;
634 if (wfso == WAIT_OBJECT_0) {
635 if (_playback.do_start) {
636 Debug(driver, 2,
"DMusic thread: Starting playback");
645 _playback.playing =
true;
646 _playback.do_start =
false;
650 clock->GetTime(&cur_time);
656 last_volume_time = 0;
664 if (_playback.playing) {
670 size_t preload_bytes = 0;
673 preload_bytes += block.
data.size();
676 Debug(driver, 2,
"DMusic: timer: loop from block {} (ticktime {}, realtime {:.3f}, bytes {})", bl, block.
ticktime, ((
int)block.
realtime) / 1000.0, preload_bytes);
683 Debug(driver, 2,
"DMusic: timer: start from block {} (ticktime {}, realtime {:.3f}, bytes {})", bl, block.
ticktime, ((
int)block.
realtime) / 1000.0, preload_bytes);
692 REFERENCE_TIME current_time;
693 clock->GetTime(¤t_time);
698 Debug(driver, 2,
"DMusic thread: volume change");
700 last_volume_time = current_time;
701 for (
int ch = 0; ch < 16; ch++) {
703 TransmitChannelMsg(
_buffer, block_time + 1, MIDIST_CONTROLLER | ch, MIDICT_CHANVOLUME, vol);
716 Debug(driver, 2,
"DMusic thread: Looping song");
720 _playback.do_stop =
true;
738 const byte *data = block.
data.data();
739 size_t remaining = block.
data.size();
740 byte last_status = 0;
741 while (remaining > 0) {
744 byte status = data[0];
746 last_status = status;
750 status = last_status;
752 switch (status & 0xF0) {
754 case MIDIST_CHANPRESS:
756 TransmitChannelMsg(
_buffer, block_time, status, data[0]);
762 case MIDIST_POLYPRESS:
763 case MIDIST_PITCHBEND:
765 TransmitChannelMsg(
_buffer, block_time, status, data[0], data[1]);
769 case MIDIST_CONTROLLER:
771 if (data[0] == MIDICT_CHANVOLUME) {
775 TransmitChannelMsg(
_buffer, block_time, status, data[0], vol);
778 TransmitChannelMsg(
_buffer, block_time, status, data[0], data[1]);
787 TransmitSysex(
_buffer, block_time, data, remaining);
789 case MIDIST_TC_QFRAME:
794 case MIDIST_SONGPOSPTR:
809 DWORD used_buffer = 0;
810 _buffer->GetUsedBytes(&used_buffer);
811 if (used_buffer > 0) {
822 _playback.do_stop =
true;
829 Debug(driver, 2,
"DMusic: Exiting playback thread");
832 clock->GetTime(&cur_time);
834 Sleep(_playback.preload_time * 4);
839 static void * DownloadArticulationData(
int base_offset,
void *data,
const std::vector<CONNECTION> &artic)
841 DMUS_ARTICULATION2 *art = (DMUS_ARTICULATION2 *)data;
842 art->ulArtIdx = base_offset + 1;
843 art->ulFirstExtCkIdx = 0;
844 art->ulNextArtIdx = 0;
846 CONNECTIONLIST *con_list = (CONNECTIONLIST *)(art + 1);
847 con_list->cbSize =
sizeof(CONNECTIONLIST);
848 con_list->cConnections = (ULONG)artic.size();
849 MemCpyT((CONNECTION *)(con_list + 1), &artic.front(), artic.size());
851 return (CONNECTION *)(con_list + 1) + artic.size();
854 static const char *LoadDefaultDLSFile(
const char *user_dls)
858 caps.dwSize =
sizeof(DMUS_PORTCAPS);
859 _port->GetCaps(&caps);
862 if ((caps.dwFlags & (DMUS_PC_DLS | DMUS_PC_DLS2)) != 0 && (caps.dwFlags & DMUS_PC_GMINHARDWARE) == 0) {
865 if (user_dls ==
nullptr) {
868 if (SUCCEEDED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L
"Software\\Microsoft\\DirectMusic", 0, KEY_READ, &hkDM))) {
869 wchar_t dls_path[MAX_PATH];
870 DWORD buf_size =
sizeof(dls_path);
871 if (SUCCEEDED(RegQueryValueEx(hkDM, L
"GMFilePath",
nullptr,
nullptr, (LPBYTE)dls_path, &buf_size))) {
872 wchar_t expand_path[MAX_PATH * 2];
873 ExpandEnvironmentStrings(dls_path, expand_path,
lengthof(expand_path));
874 if (!dls_file.
LoadFile(expand_path))
Debug(driver, 1,
"Failed to load default GM DLS file from registry");
880 if (dls_file.instruments.empty()) {
881 static const wchar_t *DLS_GM_FILE = L
"%windir%\\System32\\drivers\\gm.dls";
882 wchar_t path[MAX_PATH];
883 ExpandEnvironmentStrings(DLS_GM_FILE, path,
lengthof(path));
885 if (!dls_file.
LoadFile(path))
return "Can't load GM DLS collection";
888 if (!dls_file.
LoadFile(
OTTD2FS(user_dls).c_str()))
return "Can't load GM DLS collection";
892 IDirectMusicPortDownload *download_port =
nullptr;
893 if (FAILED(
_port->QueryInterface(IID_IDirectMusicPortDownload, (LPVOID *)&download_port)))
return "Can't get download port";
895 DWORD dlid_wave = 0, dlid_inst = 0;
896 if (FAILED(download_port->GetDLId(&dlid_wave, (DWORD)dls_file.waves.size())) || FAILED(download_port->GetDLId(&dlid_inst, (DWORD)dls_file.instruments.size()))) {
897 download_port->Release();
898 return "Can't get enough DLS ids";
902 download_port->GetAppend(&dwAppend);
905 for (DWORD i = 0; i < dls_file.waves.size(); i++) {
906 IDirectMusicDownload *dl_wave =
nullptr;
907 if (FAILED(download_port->AllocateBuffer((DWORD)(
sizeof(WAVE_DOWNLOAD) + dwAppend * dls_file.waves[i].fmt.wf.nBlockAlign + dls_file.waves[i].data.size()), &dl_wave))) {
908 download_port->Release();
909 return "Can't allocate wave download buffer";
914 if (FAILED(dl_wave->GetBuffer((LPVOID *)&wave, &wave_size))) {
916 download_port->Release();
917 return "Can't get wave download buffer";
922 wave->dlInfo.dwDLType = DMUS_DOWNLOADINFO_WAVE;
923 wave->dlInfo.cbSize = wave_size;
924 wave->dlInfo.dwDLId = dlid_wave + i;
925 wave->dlInfo.dwNumOffsetTableEntries = 2;
926 wave->ulOffsetTable[0] = offsetof(WAVE_DOWNLOAD, dmWave);
927 wave->ulOffsetTable[1] = offsetof(WAVE_DOWNLOAD, dmWaveData);
928 wave->dmWave.ulWaveDataIdx = 1;
929 MemCpyT((PCMWAVEFORMAT *)&wave->dmWave.WaveformatEx, &dls_file.waves[i].fmt, 1);
930 wave->dmWaveData.cbSize = (DWORD)dls_file.waves[i].data.size();
931 MemCpyT(wave->dmWaveData.byData, &dls_file.waves[i].data[0], dls_file.waves[i].data.size());
934 if (FAILED(download_port->Download(dl_wave))) {
935 download_port->Release();
936 return "Downloading DLS wave failed";
941 for (DWORD i = 0; i < dls_file.instruments.size(); i++) {
942 DWORD offsets = 1 + (DWORD)dls_file.instruments[i].regions.size();
945 size_t i_size =
sizeof(DMUS_DOWNLOADINFO) +
sizeof(DMUS_INSTRUMENT);
946 if (!dls_file.instruments[i].articulators.empty()) {
949 i_size +=
sizeof(DMUS_ARTICULATION2) +
sizeof(CONNECTIONLIST) +
sizeof(CONNECTION) * dls_file.instruments[i].articulators.size();
952 for (std::vector<DLSFile::DLSRegion>::iterator rgn = dls_file.instruments[i].regions.begin(); rgn != dls_file.instruments[i].regions.end(); rgn++) {
953 if (!rgn->articulators.empty()) {
955 i_size +=
sizeof(DMUS_ARTICULATION2) +
sizeof(CONNECTIONLIST) +
sizeof(CONNECTION) * rgn->articulators.size();
960 if (rgn->wave_sample.cbSize != 0) {
961 i_size +=
sizeof(DMUS_REGION) -
sizeof(DMUS_REGION::WLOOP) +
sizeof(WLOOP) * rgn->wave_loops.size();
963 i_size +=
sizeof(DMUS_REGION) -
sizeof(DMUS_REGION::WLOOP) +
sizeof(WLOOP) * dls_file.waves[dls_file.pool_cues[rgn->wave.ulTableIndex].ulOffset].wave_loops.size();
967 i_size += offsets *
sizeof(ULONG);
970 IDirectMusicDownload *dl_inst =
nullptr;
971 if (FAILED(download_port->AllocateBuffer((DWORD)i_size, &dl_inst))) {
972 download_port->Release();
973 return "Can't allocate instrument download buffer";
978 if (FAILED(dl_inst->GetBuffer((LPVOID *)&instrument, &inst_size))) {
980 download_port->Release();
981 return "Can't get instrument download buffer";
983 char *inst_base = (
char *)instrument;
986 DMUS_DOWNLOADINFO *d_info = (DMUS_DOWNLOADINFO *)instrument;
987 d_info->dwDLType = DMUS_DOWNLOADINFO_INSTRUMENT2;
988 d_info->cbSize = inst_size;
989 d_info->dwDLId = dlid_inst + i;
990 d_info->dwNumOffsetTableEntries = offsets;
991 instrument = d_info + 1;
994 ULONG *offset_table = (ULONG *)instrument;
995 instrument = offset_table + offsets;
999 DMUS_INSTRUMENT *inst_data = (DMUS_INSTRUMENT *)instrument;
1001 offset_table[last_offset++] = (
char *)inst_data - inst_base;
1002 inst_data->ulPatch = (dls_file.instruments[i].hdr.Locale.ulBank & F_INSTRUMENT_DRUMS) | ((dls_file.instruments[i].hdr.Locale.ulBank & 0x7F7F) << 8) | (dls_file.instruments[i].hdr.Locale.ulInstrument & 0x7F);
1003 instrument = inst_data + 1;
1006 if (!dls_file.instruments[i].articulators.empty()) {
1007 inst_data->ulGlobalArtIdx = last_offset;
1008 offset_table[last_offset++] = (
char *)instrument - inst_base;
1009 offset_table[last_offset++] = (
char *)instrument +
sizeof(DMUS_ARTICULATION2) - inst_base;
1011 instrument = DownloadArticulationData(inst_data->ulGlobalArtIdx, instrument, dls_file.instruments[i].articulators);
1012 assert((
char *)instrument - inst_base <= (ptrdiff_t)inst_size);
1016 inst_data->ulFirstRegionIdx = last_offset;
1017 for (uint j = 0; j < dls_file.instruments[i].regions.size(); j++) {
1020 DMUS_REGION *inst_region = (DMUS_REGION *)instrument;
1021 offset_table[last_offset++] = (
char *)inst_region - inst_base;
1022 inst_region->RangeKey = rgn.hdr.RangeKey;
1023 inst_region->RangeVelocity = rgn.hdr.RangeVelocity;
1024 inst_region->fusOptions = rgn.hdr.fusOptions;
1025 inst_region->usKeyGroup = rgn.hdr.usKeyGroup;
1026 inst_region->ulFirstExtCkIdx = 0;
1028 ULONG wave_id = dls_file.pool_cues[rgn.wave.ulTableIndex].ulOffset;
1029 inst_region->WaveLink = rgn.wave;
1030 inst_region->WaveLink.ulTableIndex = wave_id + dlid_wave;
1033 if (rgn.wave_sample.cbSize != 0) {
1034 inst_region->WSMP = rgn.wave_sample;
1035 if (!rgn.wave_loops.empty())
MemCpyT(inst_region->WLOOP, &rgn.wave_loops.front(), rgn.wave_loops.size());
1037 instrument = (
char *)(inst_region + 1) -
sizeof(DMUS_REGION::WLOOP) +
sizeof(WLOOP) * rgn.wave_loops.size();
1039 inst_region->WSMP = rgn.wave_sample;
1040 if (!dls_file.waves[wave_id].wave_loops.empty())
MemCpyT(inst_region->WLOOP, &dls_file.waves[wave_id].wave_loops.front(), dls_file.waves[wave_id].wave_loops.size());
1042 instrument = (
char *)(inst_region + 1) -
sizeof(DMUS_REGION::WLOOP) +
sizeof(WLOOP) * dls_file.waves[wave_id].wave_loops.size();
1046 if (!rgn.articulators.empty()) {
1047 inst_region->ulRegionArtIdx = last_offset;
1048 offset_table[last_offset++] = (
char *)instrument - inst_base;
1049 offset_table[last_offset++] = (
char *)instrument +
sizeof(DMUS_ARTICULATION2) - inst_base;
1051 instrument = DownloadArticulationData(inst_region->ulRegionArtIdx, instrument, rgn.articulators);
1053 inst_region->ulRegionArtIdx = 0;
1055 assert((
char *)instrument - inst_base <= (ptrdiff_t)inst_size);
1058 inst_region->ulNextRegionIdx = j < dls_file.instruments[i].regions.size() - 1 ? last_offset : 0;
1062 if (FAILED(download_port->Download(dl_inst))) {
1063 download_port->Release();
1064 return "Downloading DLS instrument failed";
1068 download_port->Release();
1078 if (FAILED(CoInitializeEx(
nullptr, COINIT_MULTITHREADED)))
return "COM initialization failed";
1081 if (FAILED(CoCreateInstance(
1088 return "Failed to create the music object";
1092 if (FAILED(
_music->SetDirectSound(
nullptr,
nullptr)))
return "Can't set DirectSound interface";
1099 if (_debug_driver_level > 0) {
1101 char desc[DMUS_MAX_DESCRIPTION];
1105 caps.dwSize =
sizeof(DMUS_PORTCAPS);
1107 Debug(driver, 1,
"Detected DirectMusic ports:");
1108 for (
int i = 0;
_music->EnumPort(i, &caps) == S_OK; i++) {
1109 if (caps.dwClass == DMUS_PC_OUTPUTCLASS) {
1120 caps.dwSize =
sizeof(DMUS_PORTCAPS);
1121 if (FAILED(
_music->EnumPort(pIdx, &caps)))
return "Supplied port parameter is not a valid port";
1122 if (caps.dwClass != DMUS_PC_OUTPUTCLASS)
return "Supplied port parameter is not an output port";
1123 guidPort = caps.guidPort;
1125 if (FAILED(
_music->GetDefaultPort(&guidPort)))
return "Can't query default music port";
1129 DMUS_PORTPARAMS params;
1131 params.dwSize =
sizeof(DMUS_PORTPARAMS);
1132 params.dwValidParams = DMUS_PORTPARAMS_CHANNELGROUPS;
1133 params.dwChannelGroups = 1;
1134 if (FAILED(
_music->CreatePort(guidPort, ¶ms, &
_port,
nullptr)))
return "Failed to create port";
1136 if (FAILED(
_port->Activate(TRUE)))
return "Failed to activate port";
1139 DMUS_BUFFERDESC desc;
1141 desc.dwSize =
sizeof(DMUS_BUFFERDESC);
1142 desc.guidBufferFormat = KSDATAFORMAT_SUBTYPE_DIRECTMUSIC;
1143 desc.cbBuffer = 1024;
1144 if (FAILED(
_music->CreateMusicBuffer(&desc, &
_buffer,
nullptr)))
return "Failed to create music buffer";
1147 const char *dls = LoadDefaultDLSFile(
GetDriverParam(parm,
"dls"));
1148 if (dls !=
nullptr)
return dls;
1151 _thread_event = CreateEvent(
nullptr, FALSE, FALSE,
nullptr);
1152 if (
_thread_event ==
nullptr)
return "Can't create thread shutdown event";
1160 MusicDriver_DMusic::~MusicDriver_DMusic()
1168 if (_dmusic_thread.joinable()) {
1169 _playback.shutdown =
true;
1176 IDirectMusicPortDownload *download_port =
nullptr;
1177 _port->QueryInterface(IID_IDirectMusicPortDownload, (LPVOID *)&download_port);
1181 for (std::vector<IDirectMusicDownload *>::reverse_iterator i =
_dls_downloads.rbegin(); download_port !=
nullptr && i !=
_dls_downloads.rend(); i++) {
1182 download_port->Unload(*i);
1187 if (download_port !=
nullptr) download_port->Release();
1195 if (
_port !=
nullptr) {
1196 _port->Activate(FALSE);
1216 if (!_playback.next_file.LoadSong(song))
return;
1220 _playback.next_segment.loop = song.
loop;
1222 _playback.do_start =
true;
1229 _playback.do_stop =
true;
1236 return _playback.playing || _playback.do_start;
1242 _playback.new_volume = vol;