-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support browsers and data URI #10
Comments
It should be possible to support browsers as things stand right now. Something like: <script src="jsmidgen.js"></script>
<script>
var file = new Midi.File();
var track = new Midi.Track();
file.addTrack(track);
track.addNote(0, 'c4', 64);
track.addNote(0, 'd4', 64);
track.addNote(0, 'e4', 64);
track.addNote(0, 'f4', 64);
track.addNote(0, 'g4', 64);
track.addNote(0, 'a4', 64);
track.addNote(0, 'b4', 64);
track.addNote(0, 'c5', 64);
var dataUri = 'data:audio/mid;base64,' + btoa(file.toBytes());
</script> should do the job, I think. I haven't tested that, of course 😄 If that does work, I'll add a |
Indeed it does, nice! I'll get started on a little idea of mine, thanks! |
Sorry, may I bother you with you quick questions?
Thanks |
Currently, jsmidgen hardcodes a rate of 128 ticks per beat, and you can set the number of beats per minute by calling If you want ticks to translate to milliseconds, you could perhaps do this by changing the structure of the file. Firstly, you'd need to override the hardcoded rate of ticks per beat:
Then you would want to set the tempo to 60 BPM:
This way, there are 1000 ticks per beat, and 60 beats per minute. Could you please open an issue about adding the ability to set the number of ticks per beat? |
Oh, and if you're looking to add silences, you may want to use |
But what note should be used for a silent note? |
Something like: // time of the last mouse click/release
var lastEventTime = 0;
thing.on('mousedown', function(){
// time of this midi event (in ms since the last event)
// defaults to zero for the first ever note
var time = 0;
// current ms since epoch
var now = +new Date();
if (lastEventTime) {
// calculate the ms since the last event
time = now - lastEventTime;
}
// record the current time
lastEventTime = now;
// add the note
track.addNoteOn(0, 'c4', time);
});
thing.on('mouseup', function(){
if (!lastEventTime) {
throw new Error('Cannot process an end-of-note if there was no start!');
}
var now = +new Date();
var time = now - lastEventTime;
lastEventTime = now;
track.addNoteOff(0, 'c4', time);
}); |
(If you saw the comment above immediately, be aware that I've edited it slightly as I realized it had an error.) |
Perfect, I actually made a very similar piece of code, assumed had to pass it on addNoteOn. Mind you it now hangs for several seconds when I generate the midi, I supposed due to the change to ticks per beat. I brought it back to 128 (also adjusting the ticks) and it's fast again. |
This would allow front-ends to generate MIDIs and allow download from a link without any need for a server.
The text was updated successfully, but these errors were encountered: