mohayonao / old-neume.js

Neume.js is a Web Audio API library for developing browser music.
http://mohayonao.github.io/neume.js/examples/
MIT License
78 stars 6 forks source link

"Sched" example not working #60

Closed echo66 closed 9 years ago

echo66 commented 9 years ago

Greetings!

I'm trying to run the following code:

function Foo($, freq, dur) {
  var out;

  out = $("sin", { freq: freq, mul: 0.25 });
  out = $("xline", { dur: dur }, out).on("end", function(e) {
    this.stop(e.playbackTime);
  });

  return out;
}

neu.Sched(function(e) {
  var freq = e.count ? 440 : 880;
  Neume.Synth(Foo, freq, 0.75).start(e.playbackTime);
  return { next: "+" + (e.count + 2) + "n" };
}).start().stop("+10");

but, unfortunately, the browser throws the following error: neume.js:1216 Uncaught TypeError: undefined is not a function

mohayonao commented 9 years ago

neu.Sched receives a function that returns an iterator.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols

neu.Sched(function(e) {
  var freq = e.count ? 440 : 880;
  neu.Synth(Foo, freq, 0.75).start(e.playbackTime);
  return { 
    next: function() {
      return {
        value: "+" + (e.count + 2) + "n",
        done: false
      };
    };
  };
}).start().stop("+10");

I'm going to redesign neume.js in the near future. Maybe, the specification for scheduling will be changed.

echo66 commented 9 years ago

thanks for the clarification, mohayonao!

echo66 commented 9 years ago

I just noticed one thing: the function that returns the "next" does not have access to the "e.count".

In sched.js, we have this in line 87:

var result = _this._schedIter.next();

There is no object passed as argument to both "next" and "_schedIter".

mohayonao commented 9 years ago

_schedIter.next() should return an iteration object that contains duration for next schedule.

e.g.

var result = _this._schedIter.next();

// result
{ value: 0.5, done: false } // run next schedule after 500ms

For sure, this specification is awkward to use. Maybe, I will change this specification.

echo66 commented 9 years ago

Well, it is awkward. Maybe that is the reason why I got confuse.

Still, thanks for the quick reply! :)