dingram / jsmidgen

A pure-JavaScript MIDI generator
228 stars 33 forks source link

Question: Writing file directly from MIDI device #25

Open tristangodfrey opened 8 years ago

tristangodfrey commented 8 years ago

Hope it's okay for me to ask this here. I'm using node-midi to catch input from usb devices, am I able to use this tool to directly write everything from device to file? I believe delta time is important here and I didn't notice it being mentioned in the readme, I suppose calling addNote automatically adds a timestamp. Would this be an accurate way to get a 1/1 recording of what is being played on a USB instrument?

Thanks in advance.

Littlaura commented 7 years ago

Hi, I know you wrote this a few months ago, but for a project, I have the same question as you did. Did you, by chance, get to do what you wanted with this code ? Thanks for you answer !

walmik commented 7 years ago

This is certainly an interesting idea and from what I understand, you ll need to

For instance, using node-midi, let's say I lightly press the C4 note on my MIDI keyboard, release it, wait for a moment and then press the C3 note with a bit of higher velocity and then release it, I end up getting this incoming data:

m:144,60,36 d:0
m:128,60,30 d:0.10576977500000001
m:144,48,111 d:1.542220928
m:128,48,120 d:0.15237455500000002

Those ^ 4 lines roughly mean

  1. Event: noteOn (144) | Note: C4(60) | Note velocity: 36
  2. Event: noteOff (128) | Note: C4(60) | Note velocity: 30
  3. Event: noteOn (144) | Note: C3(48) | Note velocity: 111
  4. Event: noteOff (128) | Note: C3(48) | Note velocity: 120

Now you ll need to write a function to take this data and convert it to something that jsmidgen can understand:

track.addNote(0, 'c4', <convert delta press and release timestamps to duration>);
track.addNote(0, 'c3', <convert delta press and release timestamps to duration>);

And then save this with

fs.writeFileSync('test.mid', file.toBytes(), 'binary');

Obviously that is way over simplified, but hopefully it's a start. jsmidgen doesnt have this out of the box and you ll need to write this middleman parser thing yourself - I m not sure if this fits the bill for making it a jsmdigen feature - @dingram can address that better - but it's certainly something that folks making music with Node.js can make use of.

Littlaura commented 7 years ago

Thank you for your answer ! I'm not really good at writing code but that will (hopefully) really help !

walmik commented 7 years ago

@Littlaura you are welcome :)

jasonmcaffee commented 5 years ago

Yes, it's possible, but a tad bit inconvenient. You cannot do this with addNote. You must use noteOn and noteOff. addNote won't work because the 4th param is the time (in ticks) since the last midi event fired. addNote fires two midi events: noteOn and noteOff.
You couldn't play overlapping notes with addNote, as the second note's time is the time since the last event, which is the first note's noteOff event. Time cannot be a negative number (this library will peg your cpu at 100% indefinitely if you use a negative time)

Using noteOn and noteOff works though, you just need to keep track of the last fired event time, and convert it into ticks.

class Recorder{
...
    notePlayed({note, bpm=this.bpm, track=this.jsmidgenTrack}){
        if(!this.isRecording){ return; }
        const {noteSymbol, octave, velocity} = note;
        const pitch = `${noteSymbol}${octave}`;
        //jsmidgen requires 'ticks' since the last midi event.
        const startTimeMsInRelationToLastRecordingEvent = Date.now() - this.lastRecordingEventTime;
        const timeSinceLastEventInTicks = convertTimeInMsToTicks({timeInMs: startTimeMsInRelationToLastRecordingEvent, bpm});
        track.addNoteOn(0, pitch, timeSinceLastEventInTicks, velocity); //channel, pitch, time, velocity
        //set our last event time as now.
        this.lastRecordingEventTime = Date.now();
    }
    noteStopped({note, track=this.jsmidgenTrack, bpm=this.bpm}){
        if(!this.isRecording){ return; }
        const {noteSymbol, octave, velocity} = note;
        const pitch = `${noteSymbol}${octave}`;
        const startTimeMsInRelationToLastRecordingEvent = Date.now() - this.lastRecordingEventTime;
        const timeSinceLastEventInTicks = convertTimeInMsToTicks({timeInMs: startTimeMsInRelationToLastRecordingEvent, bpm});
        track.addNoteOff(0, pitch, timeSinceLastEventInTicks, velocity); //channel, pitch, time, velocity
        this.lastRecordingEventTime = Date.now();
    }
...
}

function convertTimeInMsToTicks({timeInMs, bpm, ticksPerMeasure=256}){
    const secondsPerMeaure = bpm / 60;  // 2
    const millisecondsPerMeasure = secondsPerMeaure * 1000; // 2000
    const durationInMeasures = timeInMs / millisecondsPerMeasure;
    const numberOfTicks = durationInMeasures * ticksPerMeasure;
    return numberOfTicks;
}

I was able to get it working using that approach, and validated the midi file in ableton live.