Git Product home page Git Product logo

Comments (5)

Littlaura avatar Littlaura commented on July 25, 2024

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 !

from jsmidgen.

walmik avatar walmik commented on July 25, 2024

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

  • Store incoming messages with their delta times
  • Transcribe the data to jsmidgen's noteOn/noteOff methods on the Track object
  • Write a midi file by saving the bytes returned by jsmidgen's toBytes function on the File object

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.

from jsmidgen.

Littlaura avatar Littlaura commented on July 25, 2024

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

from jsmidgen.

walmik avatar walmik commented on July 25, 2024

@Littlaura you are welcome :)

from jsmidgen.

jasonmcaffee avatar jasonmcaffee commented on July 25, 2024

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.

from jsmidgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.