Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 30fafcd964
Sergi Mansilla November 11, 2010
file 214 lines (171 sloc) 6.461 kb
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>JSMidi test suite</title>

<script type="text/javascript" src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script>
<script type="text/javascript" src="../midi.js"></script>

</head>

<body class="yui3-skin-sam yui-skin-sam">
<div id="testLogger"></div>

<script type="text/javascript">
YUI().use("console", "test",function (Y) {

    var A = Y.Assert;

    var HDR_CHUNKID = "MThd";
    var HDR_CHUNK_SIZE = "\x00\x00\x00\x06"; // Header size for SMF
    var HDR_TYPE0 = "\x00\x00"; // Midi Type 0 id
    var HDR_TYPE1 = "\x00\x01"; // Midi Type 1 id
    var HDR_SPEED = "\x00\x80"; // Defaults to 128 ticks per beat

    // Midi event codes
    var EVT_NOTE_OFF = 0x8;
    var EVT_NOTE_ON = 0x9;
    var EVT_AFTER_TOUCH = 0xA;
    var EVT_CONTROLLER = 0xB;
    var EVT_PROGRAM_CHANGE = 0xC;
    var EVT_CHANNEL_AFTERTOUCH = 0xD;
    var EVT_PITCH_BEND = 0xE;

    Y.namespace("jsmidi.test");

    Y.jsmidi.test.MidiTestCase = new Y.Test.Case({

        name: "Midi Class testcase",

        setUp : function () {
            this.evt1 = new MidiEvent({
                    time: 0,
                    type: EVT_NOTE_ON,
                    channel: 0,
                    param1: 60,
                    param2: 90
                });
            this.evt2 = new MidiEvent({
                    time: 128,
                    type: EVT_NOTE_OFF,
                    channel: 0,
                    param1: 60,
                    param2: 90
                });
        },
        /*
testTranslateTickTime: function() {
var ttt = translateTickTime;

A.areEqual(127, ttt(127)[0]);

var v2 = 255;
A.areEqual(0x81, ttt(v2)[0]);
A.areEqual(0x7f, ttt(v2)[1]);

var v3 = 32768;
A.areEqual(0x82, ttt(v3)[0]);
A.areEqual(0x80, ttt(v3)[1]);
A.areEqual(0x00, ttt(v3)[2]);

},
*/
        testMidiWriter: function() {
            var track = new MidiTrack({ events: [this.evt1, this.evt2] });
            var midiHex = MidiWriter({ tracks: [track] });
            //midiHex.save();
            //a.src = uri//"http://www.a1freesoundeffects.com/popular12008/sword.mp3";
        }
    });

    Y.jsmidi.test.EventTestCase = new Y.Test.Case({

        name : "Midi events testcase",

        _should: {
            error: {
                testCreateEmpty: "Not enough parameters to create an event."
            }
        },


        testCreateEmpty : function () {
            var event = new MidiEvent();

            /*
A.isObject(event);
A.isInstanceOf(MidiEvent, event);
A.isArray(event.timeStamp);
A.areEqual(0, event.timeStamp.length);
*/
        },

        testCreateWithParams : function () {
            var event = new MidiEvent({
                    time: 128,
                    type: EVT_NOTE_ON,
                    channel: 0,
                    param1: 0x3c,
                    param2: 90
                });

            A.isObject(event);
            A.isInstanceOf(MidiEvent, event);
            A.areEqual(EVT_NOTE_ON, event.type);
            A.areEqual(0, event.channel);
            A.areEqual(0x3c, event.param1);
            A.areEqual(90, event.param2);
            A.areEqual([0x81, 0x00, 0x90, 0x3c, 0x5a].toString(), event.toBytes().toString());

            var event2 = new MidiEvent({
                    time: 127,
                    type: EVT_NOTE_ON,
                    channel: 0,
                    param1: 0x3E,
                    param2: 0x60
                    })
            A.areEqual([0x7f, 0x90, 0x3e, 0x60].toString(), event2.toBytes().toString());
        },


    });

    Y.jsmidi.test.TrackTestCase = new Y.Test.Case({

        name : "Midi track testcase",

        setUp : function () {
            this.evt1 = new MidiEvent({
                    time: 0,
                    type: EVT_NOTE_ON,
                    channel: 0,
                    param1: 60,
                    param2: 90
                });
            this.evt2 = new MidiEvent({
                    time: 128,
                    type: EVT_NOTE_OFF,
                    channel: 0,
                    param1: 60,
                    param2: 90
                });
        },

        tearDown: function () {
            delete this.evt1;
        },

        testCreateEmpty: function () {
            var track = new MidiTrack();

            A.isObject(track);
            A.isInstanceOf(MidiTrack, track);
            A.isObject(track.events);
            A.areEqual(0, track.events.length);
            A.areEqual([0x4d, 0x54, 0x72, 0x6b, 0x00, 0x00, 0x00, 0x4, 0x00, 0xff, 0x2f, 0x00].toString()
                        ,track.toBytes().toString());
        },
        testCreateWithEvents: function() {
            var track = new MidiTrack({events: [this.evt1, this.evt2] });
            //A.areEqual("4d54726b0000000f00903c0a8100803c0a00ff2f00", ArrayToHexString(track.toBytes()));
        }
    });

    Y.jsmidi.test.RealWorldTestCase = new Y.Test.Case({

        name : "Real Examples testcase",

        testReal1: function() {

            var noteEvents = [];
            ["C4", "E4", "G4"].forEach(function(note) {
                Array.prototype.push.apply(noteEvents, MidiEvent.createNote(note));
            });
            var track = new MidiTrack({ events: noteEvents });
            var song = MidiWriter({ tracks: [track] });

            A.areEqual("TVRoZAAAAAYAAAABAIBNVHJrAAAAHwCQPFqBAIA8WgCQQFqBAIBAWgCQQ1qBAIBDWgD/LwA=", song.b64);
            // song.play();
            song.save();
        }
    });

    Y.jsmidi.test.Suite = new Y.Test.Suite("JSMidi Suite");
    Y.jsmidi.test.Suite.add(Y.jsmidi.test.MidiTestCase);
    Y.jsmidi.test.Suite.add(Y.jsmidi.test.EventTestCase);
    Y.jsmidi.test.Suite.add(Y.jsmidi.test.TrackTestCase);
    Y.jsmidi.test.Suite.add(Y.jsmidi.test.RealWorldTestCase);

    var r = new Y.Console({
        newestOnTop : false,
        style: 'block'
    });

    r.render('#testLogger');

    Y.Test.Runner.add(Y.jsmidi.test.Suite);
    Y.Test.Runner.run();
});
</script>

</body>
</html>
Something went wrong with that request. Please try again.