Enterprise MIDI English

From Enterprise Wiki
Jump to: navigation, search

Introduction

A *.mid file doesn't contain any wav sample sound but only the "notes" themselves. The notes can be associated to musical instrument sounds. These instrument sounds are built into the operating system or synthesizer. Midi can sound even 16 layers (channels) at the same time and within one channel even more voices, chords. A special midi channel is the 10th where drums can sound. Our aim is to sound midi files through the EP Dave sound chip. As Dave can handle 3+1 channels altogether most midi files from the net need conversion to get suitable sounding.

Midi hardware also exists for the Enterprise but there are not many pieces. Now a PC midi player/editor application can be connected to the EP128Emu to get the EP sounding midi. To do this you need:

  • Installing a midi port e.g. loopMIDI. In Linux the Midi Through port can be used or editor programs (e.g. Qtractor) also can create a port that can be directly connected to the emulator. A hardware midi keyboard can also be used, in this case loopMIDI is not needed.
  • An EP128Emu version that supports midi input, the latest emulator can be downloaded from GitHub.

If using Windows is the case, you need to configure the midi input (loopMIDI port (I MMSystem)) in the Options -> Sound -> Configure menu item. After that, start a midi player, editor, or virtual keyboard on the PC where output the loopMIDI OUT is set. Start the emulator, load the application called Midiplay on it. After that, the midi player's will sound on the emulated EP, using DAVE's capabilities.

To minimize audio delay and timing errors, you should reduce the default latency setting (70 ms) and select an output device using a low audio API (ALSA or WASAPI), especially when using a MIDI keyboard. Also, it is advisable to avoid OpenGL video emulator modes requiring high CPUs or waiting (eg "resample to monitor refresh rate").

The EP application, Midiplay, includes several files, for example, envelope.txt, see below.

How to use Midiplay in the emulator?

With this application you can play the midi file with the emulated EP as the instrument. If you are using a midi editor (eg Cakewalk, Sonar, etc.), you can also hear the notes through the emulator. It is possible to convert an edited midi file into a loadable and playable EP file (midiconv, see below). Midiplay will automatically check if the default folder contains a converted file called mididata.bin (the maximum size is 1040 bytes of header + 8192 bytes of envelope + 31743 bytes of MIDI data) and, if it is found, the song will be played indefinitely in a loop. If there is no such file, the app will wait for notes received via loopMIDI. While the program is running, the screen will stay completely dark, but newer releases also include mididisp.com with support for simple visualization. It is desirable to run the app on a turbocharged (eg 10 MHz) configuration, but it also works at 4 MHz.

Once started the app, you can use these buttons:

F1: Restarts the program, rereads envelope.txt and (if available) mididata.bin .

F4: Switches off all sounds, useful for "stuck" sounds. The MIDI controller 123 can also be used for similar purposes.

F6: Full MIDI reset, all sounds, controllers, bend and instruments are reset, and the playback position is also returned to the start of the file. The controls and bending can also be reset with the 121 controller.

F8: Exit.


Which MIDI channel (1-16) correspond to each Dave channel? Midi channels are managed by the player as "logical" channels. So each of the sixteen stores their entire status separately:

  • from the 128 keys, which one is pressed and, if so, the assigned DAVE channel.
  • the program (instrument).
  • 4 controllers (7, 10, 76, 77)
  • bend (only the top 8 bits are taken into account, the range is fixed at +/- 2 semitones)

This information is independent of the DAVE channels in which the MIDI sounds are played. This is determined by the following algorithm:

  • in case of an incoming note on the percussion Midi channel (10), it will be played unconditionally on the DAVE channel 3, immediately interrupting the previous sound.
  • otherwise, from the 0-2 Dave channels, one is better choice than the other two when they are through the envelope process (off> release> attack / decay> sustain), then
  • if you have not been able to make a decision and you need to interrupt the active sound because all 3 music channels are busy, the older (or previously released) sound will be replaced
  • if they are equal then a fixed order will be followed:
    • MIDI channel 1: 0 > 2 > 1
    • MIDI channel 2: 2 > 0 > 1
    • MIDI channel 3: 0 > 2 > 1
    • MIDI channel 4: 2 > 0 > 1
    • ...etc.

Therefore, Dave channel 1 will be in use only when 3 music sounds are active at the same time. Exactly the other two are certainly 0 and 2. Any of the logical MIDI channels can have any number of voices, but always have a maximum of 3 + 1 percussion.

The volume of the left and right channels can be approximated in the following way:

Left = ENV_L * ((VELOCITY + AFTERTOUCH + 1) / 128) * ((VOLUME + 1) / 128) * cos(PAN * (PI / 2) / 127.5) * sqrt(2)
Right = ENV_R * ((VELOCITY + AFTERTOUCH + 1) / 128) * ((VOLUME + 1) / 128) * sin(PAN * (PI / 2) / 127.5) * sqrt(2)

Where ENV_L = left envelope, ENV_R = right envelope, VELOCITY = strength of the key hit, AFTERTOUCH = extra pressure strength, VOLUME = volume (control 7), PAN = stereo position (control 10). If ENV_L or ENV_R would be multiplied by greater than 1, the multiplier is limited to 1. So, adjusting the stereo position only works correctly if VELOCITY and VOLUME together result in a minimum 3 dB reduction in the volume. The volume multiplication is actually only 1/128 resolution. The AFTERTOUCH parameter is set by the Axh and Dxh MIDI events, the former separately for each key, while the latter for all active notes on the same channel. For a new sound (event 9xh), the initial aftertouch value is always zero.

Special settings for the MIDI files

The Midiplay player therefore pairs the individual midi channels with a certain logic to the Dave channels. However, we can also specify that a given MIDI channel must match a given Dave channel. In addition to the dynamic reservation, you can also set a fixed channel reservation. In fact, in most cases, it is necessary to use Dave's capabilities and convert the midi file downloaded from the net to a meaningful sound on EP.

For this, in the midi file, different controls can be used for different Dave specific settings. (Controllers are generally available in midi editing programs in the Controllers menu.)

Using MIDI Controllers

71 (Harmonic Content) and 76 Controllers: STYLE Parameter / 4

The Style parameter is the same as the one after the Basic command SOUND STYLE, only divided by 4. So, for example, if 16 (low distortion) is desired, then 16/4 = 4 must be entered. If 144 (ring modulation + low distortion)must be applied, then 144/4 = 36 should be specified.

When using 4 or 5 bit distortion, the frequency is automatically corrected to avoid "bad" pitch values where there is no sound or the pitch would be much higher than expected. These occur because of how polynomial counter distortion is implemented on DAVE channels 0 to 2: the counters run constantly at a clock frequency of 250 kHz, and are looping a pseudo-random pattern of 2^N-1 (15, 31 and 127 for 4, 5 and 7 bits, respectively) samples. These counters are then sampled at a frequency of 250000 / (F + 1) Hz, where F is the pitch value written into DAVE registers A0h to A5h. Therefore, if F+1 and the length of the polynomial counter pattern can be divided evenly by the same integer number, then a shorter pattern will be output, possibly no sound if the length is reduced to 1.

In the case of 4-bit distortion, if bit 1 of the controller is set (2 is added to the value), then the frequency is corrected so that the pattern length will be 5 instead of the maximum (15).

The correction works by adding an offset to the DAVE pitch, depending on the remainder of dividing the original value by 15 (4-bit) or 31 (5-bit). For 5-bit distortion, 1 is added (slightly lower frequency) if the remainder is 30. 4-bit distortion uses the following tables:

Pattern length = 15:  0,  0, -1,  0, -1,  1,  0,  0, -1,  1,  0,  1,  0,  0,  1
Pattern length = 5:   2,  1,  0, -1,  1,  0, -1,  1,  0, -1,  1,  0, -1, -2,  3

The latter can overflow if the pitch is very low.

Sound Variation (70) and Control 77 (Channel Control) only affect the bits 2 to 4:

- 0 = dynamic reservation

- 4 = fixed channel 0 (one note only)

- 8 = fixed channel 1

- 12 = fixed channel 2

- 16, 24 = channel 0, up to 3 simultaneous voices system (the notes are alternated on the same channel at a frequency of 50 Hz)

- 20, 28 = Channel 1, up to 3 voices may alternate in the same way

Fixed assignment of DAVE channels is not exclusive, the dynamic MIDI channels can continue in use.

How to use ENVELOPE.TXT

Midi files play the voices of various instruments. Envelope.txt will associate the sound (envelope curve) with these instruments, using this to use Dave's capabilities on EP. It is only necessary to use it for playback on a real-time input (MIDI port) and header-free mididata.bin, newer-format converted files also include the instruments.

The definition of an "instrument" looks like this:

INSTRUMENT [D] [P] [: + CHANNEL, + PITCH] [, INSTRUMENT2 ...] {
  [L | R | S] DURATION, LEFT, RIGHT, BEND, DISTORTION;
  ...
}

Multiple instruments can use the same envelope, multiple numbers separated by commas. The number of instruments is between 0 and 127 for normal channels, or between -1 and -127 for drums. Optionally, the number can be followed by the 'D' or 'P' characters (see below), or select two additional parameters separated by a colon. When using the latter, each note on the instrument will be duplicated with the specified offsets added to the MIDI channel and pitch (the values wrap around if they overflow). For example, with 1:2,12, Instrument 1 also plays a second note an octave higher on the MIDI channel 2 higher (or 14 lower if it would overflow) than the current one.

The 'D' character after the instrument number forces the full envelope curvature to be released even when the key is released early, for drums and 'R' envelope curves, this is the default, so there is no use for it, and combining 'D' with 'L' or 'S' results in an error. 'P' changes the stereo position of the sounds depending on the pitch, no effect on the drum. By default, the ranges between 48 to 79 are set to 1..125, this can be changed with controller 10. With the 9 instrument (Glockenspiel), the effect is enabled by default and is turned off by 'P'.

The duration (which can be set in video interrupt units, 0 changes the parameters immediately), the character 'S' means that the beginning of the segment is a "sustain" position from which the player will only continue when the key is released. It does not make any sense for the drums. A section of the envelope can be repeated using the 'L' character, which will play in a loop infinite the rest of the envelope without adding a release phase. The effect of 'R' is similar to 'S', but in contrast, the envelope always runs to the end with 'R', but with 'S' it immediately stops if the key is released before reaching the specified point. When using 'L' and 'R' together, the interval between them is repeated (from the beginning of the L segment to the end of the segment before R), and at the time of release, the repeat disappears and the envelope runs to the end.

Unlike EXOS, the volume is an absolute between 0 and 63, it is given the new volume at the beginning of the next segment that is reached by linear interpolation. However, it is also possible to define exponential envelope curves if the volume value is '*' before the volume value, then the number / 128 is multiplied by volume at each interruption. For example * 96, 0.75 is the multiplier, * 128 does not change the volume, etc. Bending is between -2048 and +2047, 64 corresponding to a musical semitone. Linear interpolation is also done here and the specified value reaches the beginning of the next segment. Drums can not be bent.

Distortion (STYLE) can not be interpolated in contrast with the other parameters, but its value is immediately changed, and it needs to be watched when it changes in sound.

Spaces, Tab characters and new rows can be used arbitrarily, the program ignores them. In the case of the '#' character, the rest of the line is considered a comment and ignored. The player program automatically stores the compiled (binary) envelope data into a file called envelope.bin. The format is:

  • 256 bytes define the duplication of notes for each instrument, the first byte is +CHANNEL and the second is +PITCH, or both are FFh if the feature is not used by the instrument
  • the following 256 bytes are the same for the drums, in the same format
  • 256 bytes envelope start position for normal instruments: bottom, top byte alternately, or 8080h if the instrument is not defined. The start position is relative to the beginning of the envelope data and is divided by 2, and the top 4 bits have a special function, so the maximum size of the addressable envelope data is 8192 bytes. The flag bits default to 2000h, which is not changed by using 'S'.
    • bit 15: disables the instrument
    • bit 14: pitch-dependent stereo position (P)
    • bit 13: indicates the absence of a repeating stage (L)
    • bit 12: releases the full envelope curve (D or R)
  • 256 bytes envelope position for drums
  • up to 8192 bytes envelope data, every 50 Hz interrupt 4 bytes:
    • left volume between 0 and 63. Setting bit 6 is the beginning of a repeating phase (L), and 8 is the end that no longer repeats. The two bits are set together to hold the current sample and then indicate a release phase (only R or S). It is a special case when the left volume has bit 7 set and the right volume is FFh, this indicates the end of the envelope and turning off the sound.
    • right volume between 0 and 63, or 255 at the end of the envelope
    • bend lower 8 bits, its resolution is half a 1/64 part. Not used in drums
    • distortion and (for normal instruments only) the upper 4 bits of the bend are stored. In the case of a percussion instrument, it defines the full byte distortion

Converting MIDI files to midiplay format

Once we have finished editing a MIDI file, and it sounds correctly with DAVE instruments, it can be converted so it can be listened on the EP standalone. midiconv (older version 1, 2, 3) is a command line application. On Windows, the command line interface (cmd.exe) can be accessed via the Start Menu. After entering the directory where midiconv.exe is with the CD command, it can be used as follows:

midiconv INFILE.MID OUTFILE.BIN ENVELOPE.BIN|ENVELOPE.TXT|-raw [IRQFREQ] [-optsort] [-renumber] [-quantN] [-biasN] [-render] [-1..-9]
midiconv ENVELOPE.TXT ENVELOPE.BIN -env [-1..-9]

The second form of the command converts text format instrument definitions to binary format. When converting MIDI a file, the following options can be used:

  • IRQFREQ (defaults to 50.0363 Hz): the assumed interrupt frequency of the playback, currently this can only be the 50 Hz video interrupt generated by the NICK chip, but other values may be useful to speed up or slow down the music. If the original tempo is 125 or 150 BPM, or other value where the duration of a quarter note is an exact integer multiple of 20 ms, it is recommended to set IRQFREQ to 50
  • -optsort: sorts note events that occur at the same time in a way that attempts to minimize the size of the output MIDI data, otherwise the sorting is done by channel number
  • -renumber: renumbers instruments starting from 0, but does not change the numbering of percussive instruments
  • -quantN: if N is not 0 (it can be an integer number in the range 1 to 9), then the tempo is adjusted automatically so that the duration of a quarter note becomes an exact integer multiple of N interrupts
  • -biasN: adjusts the rounding of MIDI event times when converted to Enterprise interrupts, N is an integer from 0 to 99 (the default is 25), 0 rounds down, 99 rounds up, and 50 rounds to the nearest integer
  • -N: enables compression if N is not 0, the allowed range is 0 to 9. Greater values may reduce the size of the output file, but this is not guaranteed, N above 6 will sometimes increase the file size by a small amount
  • -render: generates raw DAVE register data (16 bytes per frame) by emulating midiplay

After running the command, the .bin file immediately appears in the same folder, and it can be played with the midiplay.com or midi_asm.com application on the Enterprise.

The format of the converted files:

  • 16 bytes EXOS header:
    • 00h, 6Dh
    • 2 bytes: file size without the header (LSB first), in the case of future compressed formats, this would be the total size of the compressed data
    • 2 bytes: uncompressed envelope data size
    • 2 bytes: uncompressed MIDI data size
    • 1 byte: reserved for future versions, currently always 00h
  • 1 byte: compression type, 0 means no compression, 2 is epcompress -m2, others are currently not supported by the player. Compressed files can only be played with midi_asm.com or mididisp.com, the C version of the player (midiplay.com) does not implement this feature yet. To unpack a compressed MIDI file, use midipack, which is a modified version of epcompress
    • 2 bytes: compressed envelope data size, or 0 if the file is uncompressed
    • 4 bytes: reserved for future versions, currently always 00h
  • envelope data (envelope.bin) in the above described format, midiconv automatically removes any unused envelopes
  • MIDI data, the format is similar to standard MIDI files (SMF), but it contains only channel messages on a single track without header, and the delta time between events has a fixed resolution of 1/IRQFREQ seconds. For example, the following sequence of bytes plays the 440 Hz "A" note on the first MIDI channel for one second, using instrument 11:
    • 00 C0 0B 00 90 45 7F 32 45 00

When using the -raw parameter, the program converts to a simple headerless format that includes only the raw MIDI data. In this case, it is not necessary to specify a path to an envelope file on the command line.

Other

Name and program number of the midi instruments

Name and program number of the midi drums