soundfont-player icon indicating copy to clipboard operation
soundfont-player copied to clipboard

Start event listener triggered when scheduled, should be when the note starts

Open paulrosen opened this issue 7 years ago • 2 comments

(I could use more documentation on how to use the player.on(event, callback) call. What are the acceptable values for "event", for instance?)

I'm trying to get a callback when a note is about to sound. When I use the player.on() method along with the player.schedule() method, I get all the start and started callbacks at once. The ended callbacks happen at the correct time, though.

As a test case, I created a new vue-cli app and replaced the Hello.vue file with this:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
<button @click="play">Play</button>
  </div>
</template>

<script>
  import Soundfont from "soundfont-player";
export default {
  name: 'hello',
  mounted() {
  	console.log("loading piano...");
  	const self = this;
  	self.ac = new AudioContext();
    Soundfont.instrument(self.ac, 'acoustic_grand_piano').then(function (piano) {
      self.piano = piano;
      self.piano.on('event', self.progress);
      console.log("piano loaded");
    });

  },
  data () {
    return {
    	piano: null,
      ac: null,
      msg: 'soundfont player test'
    }
  },
  methods: {
  	play: function(ev) {
  		console.log("play pushed");
  		//this.piano.play("C4");
      this.piano.schedule(this.ac.currentTime, [
        { note: 60, time: 0.2 },
        { note: 64, time: 0.2 },
        { note: 68, time: 1.4 },
        { note: 72, time: 2.6 },
        { note: 76, time: 2.6 },
        { note: 78, time: 6.6 },
      ]);
    },
    progress: function(ev, time, obj, opts) {
  		console.log("progress", this.ac.currentTime, ev, time);
    }
  }
}
</script>

My console looks like this:

loading piano...
piano loaded
play pushed
progress 6.88 schedule 6.88 
progress 6.88 start 7.08 60 
progress 6.88 started 7.08 0 
progress 6.88 start 7.08 64 
progress 6.88 started 7.08 1 
progress 6.88 start 8.28 68 
progress 6.88 started 8.28 2 
progress 6.88 start 9.48 72 
progress 6.88 started 9.48 3 
progress 6.88 start 9.48 76 
progress 6.88 started 9.48 4 
progress 6.88 start 13.48 78 
progress 6.88 started 13.48 5 
progress 10.112 ended 10.112 1 
progress 10.112 ended 10.112 0 
progress 11.296 ended 11.296 2 
progress 12.512 ended 12.512 4 
progress 12.512 ended 12.512 3 
progress 16.512 ended 16.512 5 

This first number is the time according to AudioContext and the second is the time the note should be scheduled.

I'd like to build a "bouncing ball" type application where I show something right when a note is started. Do I need to figure out the timing myself?

paulrosen avatar Apr 24 '17 07:04 paulrosen

@paulrosen did you ever find a solution or workaround to this ? I am trying to play a simple scale with a call back to highlight the note on fretboard when it is actually played

Aduffy avatar Dec 17 '20 00:12 Aduffy

@Aduffy I'm actually in a very similar predicament right now, with a piano. Did you ever figure out how to do this using Soundfont Player?

john-np avatar Jul 24 '21 07:07 john-np

Same here! Trying to highlight a note on a staff as it plays...would be great if I could attach a callback to each object in the schedule.

drscottlobo avatar May 08 '23 17:05 drscottlobo

Sorry, I ended up creating my own soundfont player, so I haven't kept up with this.

paulrosen avatar May 08 '23 18:05 paulrosen

No worries, thanks for the reply!

drscottlobo avatar May 08 '23 19:05 drscottlobo

I'm going to archive this repo because the lack of maintenance. I'm currently using this: https://github.com/danigb/smplr but there are other alternatives

danigb avatar May 16 '23 16:05 danigb