Comments (5)
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.
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
- Event: noteOn (144) | Note: C4(60) | Note velocity: 36
- Event: noteOff (128) | Note: C4(60) | Note velocity: 30
- Event: noteOn (144) | Note: C3(48) | Note velocity: 111
- 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.
Thank you for your answer ! I'm not really good at writing code but that will (hopefully) really help !
from jsmidgen.
@Littlaura you are welcome :)
from jsmidgen.
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)
- Notes are rendered an octave lower HOT 4
- How to add note pauses? HOT 3
- License file
- Can jsmidgen work in the browser ?? HOT 2
- Support for MIDI Track Names ??? HOT 2
- Overlapping notes HOT 1
- set tempo not working?
- Drums HOT 3
- timeSignature in header and controlChanges in Tracks missing after encoding
- translateTickTime() hangs when provided with wrong input
- Large MIDI files cause `RangeError: Maximum call stack size exceeded` when writing to bytes
- What's the max of duration and time of the note?
- How to apply bending for notes?
- Is that a bug with the durations in noteOn? HOT 1
- How to get notes from MIDI file?
- How to set sustain-on mode?
- Unable to save .mid file as blob HOT 1
- Allow Add System Exclusive Messages to Track HOT 5
- [feature request] - jsmidgen - idea for a project based in jsmidgen (automatic music generation)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsmidgen.