OpenTTD Source 20241224-master-gf74b0cf984
dmusic.cpp File Reference

Playing music via DirectMusic. More...

#include "../stdafx.h"
#include "../debug.h"
#include "../os/windows/win32.h"
#include "../core/mem_func.hpp"
#include "../thread.h"
#include "../fileio_func.h"
#include "../base_media_base.h"
#include "dmusic.h"
#include "midifile.hpp"
#include "midi.h"
#include <windows.h>
#include <dmksctrl.h>
#include <dmusicc.h>
#include <mutex>
#include "../safeguards.h"

Go to the source code of this file.

Data Structures

struct  DLSFile
 A DLS file. More...
 
struct  DLSFile::DLSRegion
 An instrument region maps a note range to wave data. More...
 
struct  DLSFile::DLSInstrument
 Instrument definition read from a DLS file. More...
 
struct  DLSFile::DLSWave
 Wave data definition from a DLS file. More...
 
struct  PlaybackSegment
 

Macros

#define INITGUID
 
#define FOURCC_INFO   mmioFOURCC('I', 'N', 'F', 'O')
 
#define FOURCC_fmt   mmioFOURCC('f', 'm', 't', ' ')
 
#define FOURCC_data   mmioFOURCC('d', 'a', 't', 'a')
 

Functions

 PACK_N (struct ChunkHeader { FOURCC type;DWORD length;}, 2)
 A RIFF chunk header.
 
 PACK_N (struct WAVE_DOWNLOAD { DMUS_DOWNLOADINFO dlInfo;ULONG ulOffsetTable[2];DMUS_WAVE dmWave;DMUS_WAVEDATA dmWaveData;}, 2)
 Buffer format for a DLS wave download.
 
static uint8_t ScaleVolume (uint8_t original, uint8_t scale)
 
static void TransmitChannelMsg (IDirectMusicBuffer *buffer, REFERENCE_TIME rt, uint8_t status, uint8_t p1, uint8_t p2=0)
 
static void TransmitSysex (IDirectMusicBuffer *buffer, REFERENCE_TIME rt, const uint8_t *&msg_start, size_t &remaining)
 
static void TransmitStandardSysex (IDirectMusicBuffer *buffer, REFERENCE_TIME rt, MidiSysexMessage msg)
 
static void TransmitNotesOff (IDirectMusicBuffer *buffer, REFERENCE_TIME block_time, REFERENCE_TIME cur_time)
 Transmit 'Note off' messages to all MIDI channels.
 
static void MidiThreadProc ()
 
static void * DownloadArticulationData (int base_offset, void *data, const std::vector< CONNECTION > &artic)
 
static const char * LoadDefaultDLSFile (const char *user_dls)
 

Variables

static const int MS_TO_REFTIME = 1000 * 10
 DirectMusic time base is 100 ns.
 
static const int MIDITIME_TO_REFTIME = 10
 Time base of the midi file reader is 1 us.
 
struct { 
 
   bool   shutdown 
 flag to indicate playback thread shutdown More...
 
   bool   playing 
 flag indicating that playback is active More...
 
   bool   do_start 
 flag for starting playback of next_file at next opportunity More...
 
   bool   do_stop 
 flag for stopping playback at next opportunity More...
 
   int   preload_time 
 preload time for music blocks. More...
 
   uint8_t   new_volume 
 volume setting to change to More...
 
   MidiFile   next_file 
 upcoming file to play More...
 
   PlaybackSegment   next_segment 
 segment info for upcoming file More...
 
_playback 
 
static std::thread _dmusic_thread
 Handle to our worker thread.
 
static HANDLE _thread_event = nullptr
 Event to signal the thread that it should look at a state change.
 
static std::mutex _thread_mutex
 Lock access to playback data that is not thread-safe.
 
static IDirectMusic * _music = nullptr
 The direct music object manages buffers and ports.
 
static IDirectMusicPort * _port = nullptr
 The port object lets us send MIDI data to the synthesizer.
 
static IDirectMusicBuffer * _buffer = nullptr
 The buffer object collects the data to sent.
 
static std::vector< IDirectMusicDownload * > _dls_downloads
 List of downloaded DLS instruments.
 
static FMusicDriver_DMusic iFMusicDriver_DMusic
 

Detailed Description

Playing music via DirectMusic.

Definition in file dmusic.cpp.

Macro Definition Documentation

◆ FOURCC_data

#define FOURCC_data   mmioFOURCC('d', 'a', 't', 'a')

Definition at line 42 of file dmusic.cpp.

◆ FOURCC_fmt

#define FOURCC_fmt   mmioFOURCC('f', 'm', 't', ' ')

Definition at line 41 of file dmusic.cpp.

◆ FOURCC_INFO

#define FOURCC_INFO   mmioFOURCC('I', 'N', 'F', 'O')

Definition at line 40 of file dmusic.cpp.

◆ INITGUID

#define INITGUID

Definition at line 10 of file dmusic.cpp.

Function Documentation

◆ DownloadArticulationData()

static void * DownloadArticulationData ( int  base_offset,
void *  data,
const std::vector< CONNECTION > &  artic 
)
static

Definition at line 830 of file dmusic.cpp.

◆ LoadDefaultDLSFile()

static const char * LoadDefaultDLSFile ( const char *  user_dls)
static

Definition at line 845 of file dmusic.cpp.

◆ MidiThreadProc()

static void MidiThreadProc ( )
static

Definition at line 578 of file dmusic.cpp.

◆ ScaleVolume()

static uint8_t ScaleVolume ( uint8_t  original,
uint8_t  scale 
)
static

Definition at line 513 of file dmusic.cpp.

◆ TransmitChannelMsg()

static void TransmitChannelMsg ( IDirectMusicBuffer *  buffer,
REFERENCE_TIME  rt,
uint8_t  status,
uint8_t  p1,
uint8_t  p2 = 0 
)
static

Definition at line 518 of file dmusic.cpp.

◆ TransmitNotesOff()

static void TransmitNotesOff ( IDirectMusicBuffer *  buffer,
REFERENCE_TIME  block_time,
REFERENCE_TIME  cur_time 
)
static

Transmit 'Note off' messages to all MIDI channels.

Definition at line 557 of file dmusic.cpp.

References _port, Clamp(), and MS_TO_REFTIME.

◆ TransmitStandardSysex()

static void TransmitStandardSysex ( IDirectMusicBuffer *  buffer,
REFERENCE_TIME  rt,
MidiSysexMessage  msg 
)
static

Definition at line 549 of file dmusic.cpp.

◆ TransmitSysex()

static void TransmitSysex ( IDirectMusicBuffer *  buffer,
REFERENCE_TIME  rt,
const uint8_t *&  msg_start,
size_t &  remaining 
)
static

Definition at line 529 of file dmusic.cpp.

Variable Documentation

◆ _buffer

IDirectMusicBuffer* _buffer = nullptr
static

The buffer object collects the data to sent.

Definition at line 144 of file dmusic.cpp.

Referenced by MusicDriver_DMusic::Start(), and MusicDriver_DMusic::Stop().

◆ _dls_downloads

std::vector<IDirectMusicDownload *> _dls_downloads
static

List of downloaded DLS instruments.

Definition at line 146 of file dmusic.cpp.

Referenced by MusicDriver_DMusic::Stop().

◆ _dmusic_thread

std::thread _dmusic_thread
static

Handle to our worker thread.

Definition at line 133 of file dmusic.cpp.

Referenced by MusicDriver_DMusic::Start(), and MusicDriver_DMusic::Stop().

◆ _music

IDirectMusic* _music = nullptr
static

The direct music object manages buffers and ports.

Definition at line 140 of file dmusic.cpp.

Referenced by MusicDriver_DMusic::Start(), and MusicDriver_DMusic::Stop().

◆ _port

IDirectMusicPort* _port = nullptr
static

The port object lets us send MIDI data to the synthesizer.

Definition at line 142 of file dmusic.cpp.

Referenced by MusicDriver_DMusic::Start(), MusicDriver_DMusic::Stop(), and TransmitNotesOff().

◆ _thread_event

HANDLE _thread_event = nullptr
static

Event to signal the thread that it should look at a state change.

Definition at line 135 of file dmusic.cpp.

Referenced by MusicDriver_DMusic::PlaySong(), MusicDriver_DMusic::Start(), MusicDriver_DMusic::Stop(), and MusicDriver_DMusic::StopSong().

◆ _thread_mutex

std::mutex _thread_mutex
static

Lock access to playback data that is not thread-safe.

Definition at line 137 of file dmusic.cpp.

Referenced by MusicDriver_DMusic::PlaySong().

◆ do_start

bool do_start

flag for starting playback of next_file at next opportunity

Definition at line 122 of file dmusic.cpp.

Referenced by CmdMassStartStopVehicle().

◆ do_stop

bool do_stop

flag for stopping playback at next opportunity

Definition at line 123 of file dmusic.cpp.

◆ iFMusicDriver_DMusic

FMusicDriver_DMusic iFMusicDriver_DMusic
static

Definition at line 149 of file dmusic.cpp.

◆ MIDITIME_TO_REFTIME

const int MIDITIME_TO_REFTIME = 10
static

Time base of the midi file reader is 1 us.

Definition at line 37 of file dmusic.cpp.

◆ MS_TO_REFTIME

const int MS_TO_REFTIME = 1000 * 10
static

DirectMusic time base is 100 ns.

Definition at line 36 of file dmusic.cpp.

Referenced by TransmitNotesOff().

◆ new_volume

uint8_t new_volume

volume setting to change to

Definition at line 126 of file dmusic.cpp.

◆ next_file

MidiFile next_file

upcoming file to play

Definition at line 128 of file dmusic.cpp.

◆ next_segment

PlaybackSegment next_segment

segment info for upcoming file

Definition at line 129 of file dmusic.cpp.

◆ playing

bool playing

flag indicating that playback is active

Definition at line 121 of file dmusic.cpp.

◆ preload_time

int preload_time

preload time for music blocks.

Definition at line 125 of file dmusic.cpp.

◆ shutdown

bool shutdown

flag to indicate playback thread shutdown

Definition at line 120 of file dmusic.cpp.