简体   繁体   中英

How to Correctly Clean Up timbre.js

I am running Timbre.js successfully on iOS 9.2 by coupling it with AudioContextMonkeyPatch, and I am trying to use a slightly modified version of code found here: http://mohayonao.github.io/timbre.js/interval.html . The code (to save lookup) is:

var freqs = T(function(count) {
  return [220, 440, 660, 880][count % 4];
});

var osc = T("sin", {freq:freqs, mul:0.5});
var env = T("perc", {a:50, r:500}, osc).bang();

var interval = T("param", {value:500}).linTo(50, "30sec");

T("interval", {interval:interval}, freqs, env).start();

env.play();

What I am trying to figure out is how to start and then stop and then re-start the sound. I'm trying to see how the developer's example 'Pause' button works, but I can't seem to locate that code example. I do basic things tike "T().stop(); env.pause();" followed by "env.play();" (in separate onClick events), and I end up with multiple signals on the second play event. Really frustrating. The documentation suggests a 'removeAll' will remove all items loaded into Timbre() (or T()?), but applying this in my stop function does not provide a satisfactory result either. Anyone know the correct way to pause and restart this script snippet?

In order to start and stop the sequence you need to assign the last T function to a variable. You can then use .start() and .stop() as you like:

code....
var interval = T("param", {value:500}).linTo(50, "30sec");
var audioSequence = T("interval", {interval:interval}, freqs, env);

env.play(); 

audioSequence.start(); // Start the Sequence 

interval.stop(); // Stop the interval
audioSequence.stop();  // Stop the Sequence
interval.start(); // Restart the Interval
audioSequence.start();  // Restart the Sequence

Its a little confusing at first as play()/pause() is used for oscillators or drums for example and start()/stop() is used for intervals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM