Git Product home page Git Product logo

Comments (9)

dingram avatar dingram commented on August 30, 2024 1

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.

from jsmidgen.

flesler avatar flesler commented on August 30, 2024

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

from jsmidgen.

flesler avatar flesler commented on August 30, 2024

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

from jsmidgen.

dingram avatar dingram commented on August 30, 2024

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?

from jsmidgen.

dingram avatar dingram commented on August 30, 2024

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.

from jsmidgen.

flesler avatar flesler commented on August 30, 2024

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?

from jsmidgen.

dingram avatar dingram commented on August 30, 2024

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

from jsmidgen.

dingram avatar dingram commented on August 30, 2024

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

from jsmidgen.

flesler avatar flesler commented on August 30, 2024

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.

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.