File size: 3,470 Bytes
b0bfea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { messageTypes } from "./midi_message.js";
import { writeVariableLengthQuantity } from "../utils/byte_functions/variable_length_quantity.js";
import { writeBytesAsUintBigEndian } from "../utils/byte_functions/big_endian.js";

/**
 * Exports the midi as a standard MIDI file
 * @this {BasicMIDI}
 */
export function writeMIDI()
{
    const midi = this;
    if (!midi.tracks)
    {
        throw new Error("MIDI has no tracks!");
    }
    /**
     * @type {Uint8Array[]}
     */
    const binaryTrackData = [];
    for (const track of midi.tracks)
    {
        const binaryTrack = [];
        let currentTick = 0;
        let runningByte = undefined;
        for (const event of track)
        {
            // Ticks stored in MIDI are absolute, but SMF wants relative. Convert them here.
            const deltaTicks = event.ticks - currentTick;
            /**
             * @type {number[]}
             */
            let messageData;
            // determine the message
            if (event.messageStatusByte <= messageTypes.sequenceSpecific)
            {
                // this is a meta-message
                // syntax is FF<type><length><data>
                messageData = [0xff, event.messageStatusByte, ...writeVariableLengthQuantity(event.messageData.length), ...event.messageData];
            }
            else if (event.messageStatusByte === messageTypes.systemExclusive)
            {
                // this is a system exclusive message
                // syntax is F0<length><data>
                messageData = [0xf0, ...writeVariableLengthQuantity(event.messageData.length), ...event.messageData];
            }
            else
            {
                // this is a midi message
                messageData = [];
                if (runningByte !== event.messageStatusByte)
                {
                    // Running byte was not the byte we want. Add the byte here.
                    runningByte = event.messageStatusByte;
                    // add the status byte to the midi
                    messageData.push(event.messageStatusByte);
                }
                // add the data
                messageData.push(...event.messageData);
            }
            // write VLQ
            binaryTrack.push(...writeVariableLengthQuantity(deltaTicks));
            // write the message
            binaryTrack.push(...messageData);
            currentTick += deltaTicks;
        }
        binaryTrackData.push(new Uint8Array(binaryTrack));
    }
    
    /**
     * @param text {string}
     * @param arr {number[]}
     */
    function writeText(text, arr)
    {
        for (let i = 0; i < text.length; i++)
        {
            arr.push(text.charCodeAt(i));
        }
    }
    
    // write the file
    const binaryData = [];
    // write header
    writeText("MThd", binaryData); // MThd
    binaryData.push(...writeBytesAsUintBigEndian(6, 4)); // length
    binaryData.push(0, midi.format); // format
    binaryData.push(...writeBytesAsUintBigEndian(midi.tracksAmount, 2)); // num tracks
    binaryData.push(...writeBytesAsUintBigEndian(midi.timeDivision, 2)); // time division
    
    // write tracks
    for (const track of binaryTrackData)
    {
        // write track header
        writeText("MTrk", binaryData); // MTrk
        binaryData.push(...writeBytesAsUintBigEndian(track.length, 4)); // length
        binaryData.push(...track); // write data
    }
    return new Uint8Array(binaryData);
}