diff --git a/src/music/dmusic.cpp b/src/music/dmusic.cpp index 91f0684..454d886 100644 --- a/src/music/dmusic.cpp +++ b/src/music/dmusic.cpp @@ -416,6 +416,19 @@ void MusicDriver_DMusic::PlaySong(const char *filename, int time_start, int time return; } + /* All original TTD music has a timedivision of 384, while DirectMusic internally + * works with a timedivision of 768 i.e. double. Not hardcoding this would require + * either more addendum data in the baseset files, or reading a couple bytes from + * the MIDI files to get the actual timedivision, and scale accordingly. */ + time_start *= 2; + time_end *= 2; + segment->SetStartPoint(time_start); + /* Enable looping if required */ + if (time_end > time_start && loop) { + segment->SetLoopPoints(time_start, time_end); + segment->SetRepeats(DMUS_SEG_REPEAT_INFINITE); + } + /* tell the segment to 'download' the instruments */ if (FAILED(segment->SetParam(GUID_Download, 0xFFFFFFFF, 0, 0, performance))) { DEBUG(driver, 0, "DirectMusic: failed to download instruments"); @@ -423,11 +436,18 @@ void MusicDriver_DMusic::PlaySong(const char *filename, int time_start, int time } /* start playing the MIDI file */ + MUSIC_TIME perf_time_start = 0; + performance->GetTime(NULL, &perf_time_start); if (FAILED(performance->PlaySegment(segment, 0, 0, NULL))) { DEBUG(driver, 0, "DirectMusic: PlaySegment failed"); return; } + /* If an ending time is given, request a stop at that point */ + if (time_end > time_start && !loop) { + performance->Stop(segment, NULL, perf_time_start + time_end, DMUS_SEGF_DEFAULT); + } + seeking = true; }