A class to control the MIDI device is located in the IncMIDI.mqh file. The example of its use is represented in the eMIDI.mq5 file.
The order of using the CMIDI class
- Connect a file with a class:
- Declare a class:
- Transfer music program using the SetMusic(string & aMusic) method.
- Enable playback using the Play(bool aSynch=true) method. If aSynch is true, play is performed in a synchronous mode in the cycle with pauses using the Sleep() function. If aSynch is false, play is performed in a synchronous mode, besides you can stop play using the Stop() method. To work in a synchronous mode from the OnChartEvent() function of the Expert Advisor the ChartEvent() method must be called.
- Call the Stop() method upon completion of work. It is necessary for the MIDI device closing, otherwise to rerun it you should restart the terminal.
Music program is recorded in the string array. In one array element is written a list of actions. Actions are written by a comma. At first a command, an equal sign and a command value are written. The following commands are available:
- Tick - time in milliseconds through which all the commands of one array element will be performed. It defines the tempo with which the play is performed.
- Volume - the volume level, from 0 to 100.
- VolumeL - the left channel volume level, from 0 to 100.
- VolumeR - the right channel volume level, from 0 to 100.
- Instrument - selection of the music instrument, from 0 to 127.
- NoteDN - press a key. Number 60 corresponds to "do" note of the main octave, 61 - to "C sharp", 62 - to "re", etc.
- NoteUP - release a key. For several instruments you cannot use release a key, the sound fades away itself, and for some keys it is obligatory to press.
It is possible to take chords, for example, "NoteDN=57, NoteDN=60, NoteDN=64". A possible parallel play on several instruments, for example, "Instrument=1, NoteDN=60, Instrument=123, NoteDN=60". Different instruments' volume can not be changed, all is played on the same volume, but you can change the volume of different parts of playing, for example:
"Tick=1500, Instrument=123, Volume=100, NoteDN=60",
"VolumeL=0, VolumeR=100, NoteDN=64",
"VolumeL=100, VolumeR=0, NoteDN=57",
"Volume=50, NoteDN=67, NoneDN=57",
"Volume=25, NoteDN=64, NoneDN=67"
The CMIDI class methods
- void ChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam) - called from the function when using play in an asynchronous mode.
- void Play(bool aSynch=true) - enable play. If aSynch is false, from the OnChartEvent function the ChartEvent() method must be called.
- void Stop() - stop play in an asynchronous play. Stop play and close the device.
- void SetMusic(string & aMusic) - sending an array with a music program.
- void SetEventID(ushort aID) - setting a number of a custom event. It is necessary when using play in an asynchronous mode. It is not necessary to call, default value is 1.