Skip to content
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

Closed
flesler opened this issue Nov 19, 2015 · 9 comments
Closed

Support browsers and data URI #10

flesler opened this issue Nov 19, 2015 · 9 comments

Comments

@flesler
Copy link

flesler commented Nov 19, 2015

This would allow front-ends to generate MIDIs and allow download from a link without any need for a server.

@dingram
Copy link
Owner

dingram commented Nov 19, 2015

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 toDataUri() method on Midi.File as a convenience.

@flesler
Copy link
Author

flesler commented Nov 19, 2015

Indeed it does, nice! I'll get started on a little idea of mine, thanks!

@flesler flesler closed this as completed Nov 19, 2015
@flesler
Copy link
Author

flesler commented Nov 19, 2015

Sorry, may I bother you with you quick questions?
I'm trying to create a midi based on mouse click holding, using the same note.

  • I have a note duration in milliseconds, how do I translate that to ticks precisely?
  • How do I add silences between notes, using the same duration strategy?
  • Can I pass numbers instead of strings as notes? what is the valid range?

Thanks

@dingram
Copy link
Owner

dingram commented Nov 19, 2015

Currently, jsmidgen hardcodes a rate of 128 ticks per beat, and you can set the number of beats per minute by calling setTempo(120) (or whatever value) on a Track object.

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:

// Override files to write 1000 ticks per beat
Midi.File.HDR_SPEED = "\x03\xe8";

Then you would want to set the tempo to 60 BPM:

track.setTempo(60);

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?

@dingram
Copy link
Owner

dingram commented Nov 19, 2015

Oh, and if you're looking to add silences, you may want to use addNoteOn() and addNoteOff(). Simply call addNoteOn() on click and addNoteOff() on release, passing the time since the last release/click event as appropriate.

@flesler
Copy link
Author

flesler commented Nov 19, 2015

But what note should be used for a silent note?
Or I may have misunderstood the API
You mean I pass the silence ticks in the addNoteOn of the next note?

@dingram
Copy link
Owner

dingram commented Nov 19, 2015

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);
});

@dingram
Copy link
Owner

dingram commented Nov 19, 2015

(If you saw the comment above immediately, be aware that I've edited it slightly as I realized it had an error.)

@flesler
Copy link
Author

flesler commented Nov 19, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants