MIDI

View the source. Import with require('v1/midi');

This library does not support MIDI controllers. It's an adapter for the Tone.js MIDI file converter.

This library imports v1/instruments

Global Functions

parseMidi({transport, parts})

Arguments

parseMidi uses named arguments.

  • parts Required. The parts object returned by the MIDI converter.
  • transport Optional. Required for track to run at correct BPM. Pass the transport object returned by the MIDI converter.
  • masterGain Optional. Number value. Default -20. Set the track's master gain, useful if the track goes into the red.
  • instrumentBuilders Optional. { track_key: (note) -> Mixable } (See "Usage.") The function accepts one argument, an object containing:
    • frequency: Number. Frequency value of the note in Hz.
    • start: Number. Time in seconds. When the note starts playing.
    • duration: Number. Length of the note in seconds.
    • velocity: Number. The MIDI velocity of the note. Value between 0 - 1.
    • instrument: String. The track key from parts.
Returns

A closure that will schedule all note events when run. Set buildTrack to this value. E.g. var buildTrack = parseMidi({transport: t, parts: p});

For more advanced use you can schedule the call to parseMidi(). See "Usage."

Usage (JavaScript)

require("v1/midi");

// From http://tonejs.github.io/MidiConvert/
var transport = {
  "bpm": 125
};
var parts = {
  "Track 1": [
    {
      "time": "144i",
      "midiNote": 60,
      "noteName": "C4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "168i",
      "midiNote": 60,
      "noteName": "C4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "192i",
      "midiNote": 69,
      "noteName": "A4",
      "velocity": 0.5590551181102362,
      "duration": "72i"
    },
    {
      "time": "312i",
      "midiNote": 65,
      "noteName": "F4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "336i",
      "midiNote": 67,
      "noteName": "G4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "360i",
      "midiNote": 65,
      "noteName": "F4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "384i",
      "midiNote": 62,
      "noteName": "D4",
      "velocity": 0.5590551181102362,
      "duration": "72i"
    }
  ]
};

var instrumentBuilders = {
  "Track 1": function(note){
    var o1 = new Oscillator({type: Oscillator.SAWTOOTH, frequency: note.frequency});
    var o2 = new Oscillator({type: Oscillator.SAWTOOTH, frequency: (note.frequency / 2)});
    var e = new Envelope({delayTime: note.duration});
    var m = new Mixer({gain: 3});
    return e.process(m.process(o1, o2))
  }
};

var buildTrack = function(){
  // If we don't do this now, the call to parseMidi() will do it later
  this.setMasterGain(-20);
  this.setBPM(transport.bpm * 48); // Transport uses a multiplier of 48 "parts per quarter"

  this.every((96).beats(), function(){
    // Just a periodic "ping" to demonstrate the timer running in parallel
    this.play((new Envelope).process(new Oscillator));
  });

  this.after((192).beats(), function(){
    // Run the returned closure in the scope of `this`
    parseMidi({transport: transport, parts: parts, instrumentBuilders: instrumentBuilders}).call(this);
  });
}

Usage (CoffeeScript)

require "v1/midi"

# From http://tonejs.github.io/MidiConvert/
transport = {
  "bpm": 125
}
parts = {
  "Track 1": [
    {
      "time": "144i",
      "midiNote": 60,
      "noteName": "C4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "168i",
      "midiNote": 60,
      "noteName": "C4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "192i",
      "midiNote": 69,
      "noteName": "A4",
      "velocity": 0.5590551181102362,
      "duration": "72i"
    },
    {
      "time": "312i",
      "midiNote": 65,
      "noteName": "F4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "336i",
      "midiNote": 67,
      "noteName": "G4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "360i",
      "midiNote": 65,
      "noteName": "F4",
      "velocity": 0.5590551181102362,
      "duration": "24i"
    },
    {
      "time": "384i",
      "midiNote": 62,
      "noteName": "D4",
      "velocity": 0.5590551181102362,
      "duration": "72i"
    },
  ]
}

instrumentBuilders = {
  "Track 1": (note) ->
    o1 = new Oscillator type: Oscillator.SAWTOOTH, frequency: note.frequency
    o2 = new Oscillator type: Oscillator.SAWTOOTH, frequency: note.frequency / 2
    e = new Envelope delayTime: note.duration
    m = new Mixer gain: 3
    e.process(m.process(o1, o2))
}

buildTrack = ->
  # If we don't do this now, the call to parseMidi() will do it later
  @setMasterGain -20
  @setBPM transport.bpm * 48

  @every 96.beats(), ->
    # Just a periodic "ping" to demonstrate the timer running in parallel
    @play (new Envelope).process(new Oscillator)

  @after 192.beats(), ->
    # Run the returned closure in the scope of `this`
    parseMidi({transport: transport, parts: parts, instrumentBuilders: instrumentBuilders}).call(@)

results matching ""

    No results matching ""