1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 10:40:54 +00:00
psychojs/docs/sound_TonePlayer.js.html
Alain Pitiot 0f8e055542 _
2018-12-10 07:56:29 +01:00

266 lines
8.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: sound/TonePlayer.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: sound/TonePlayer.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Tone Player.
*
* @author Alain Pitiot
* @version 3.0.0b11
* @copyright (c) 2018 Ilixa Ltd. ({@link http://ilixa.com})
* @license Distributed under the terms of the MIT License
*/
import { SoundPlayer } from './SoundPlayer';
/**
* &lt;p>This class handles the playing of tones.&lt;/p>
*
* @name module:sound.TonePlayer
* @class
* @extends SoundPlayer
* @param {Object} options
* @param {PsychoJS} options.psychoJS - the PsychoJS instance
* @param {number} [options.duration_s= 0.5] - duration of the tone (in seconds)
* @param {string|number} [options.note= 'C4'] - note (if string) or frequency (if number)
* @param {number} [options.volume= 1.0] - volume of the tone (must be between 0 and 1.0)
* @param {number} [options.loops= 0] - how many times to repeat the tone after it has played once. If loops == -1, the tone will repeat indefinitely until stopped.
*/
export class TonePlayer extends SoundPlayer {
constructor({
psychoJS,
note = 'C4',
duration_s = 0.5,
volume = 1.0,
loops = 0
} = {}) {
super(psychoJS);
this._addAttributes(TonePlayer, note, duration_s, volume, loops);
// create a synth: we use a triangular oscillator with hardly any envelope:
this._synthOtions = {
oscillator: {
type: 'triangle'
},
envelope: {
attack: 0.001,
decay: 0.001,
sustain: 1,
release: 0.001
}
};
this._synth = new Tone.Synth(this._synthOtions);
// connect it to a volume node:
this._volumeNode = new Tone.Volume(-60 + volume * 66);
this._synth.connect(this._volumeNode);
// connect the volume node to the master output:
this._volumeNode.toMaster();
// tonejs Loop:
this._toneLoop = null;
}
/**
* Determine whether this player can play the given sound.
*
* @name module:sound.TonePlayer.accept
* @function
* @static
* @public
* @param {module:sound.Sound} - the sound
* @return {Object|undefined} an instance of TonePlayer that can play the given sound or undefined otherwise
*/
static accept(sound) {
// if the sound's value is an integer, we interpret it as a frequency:
if ($.isNumeric(sound.value)) {
// build the player:
const player = new TonePlayer({
psychoJS: sound.psychoJS,
note: sound.value,
duration_s: sound.secs,
volume: sound.volume,
loops: sound.loops
});
return player;
}
// if the sound's value is a string, we check whether it is a note:
if (typeof sound.value === 'string') {
// mapping between the PsychoPY notes and the standard ones:
let psychopyToToneMap = new Map();
for (const note of ['A', 'B', 'C', 'D', 'E', 'F', 'G']) {
psychopyToToneMap.set(note, note);
psychopyToToneMap.set(note + 'fl', note + 'b');
psychopyToToneMap.set(note + 'sh', note + '#');
}
// check whether the sound's value is a recognised note:
const note = psychopyToToneMap.get(sound.value);
if (typeof note !== 'undefined') {
// build the player:
const player = new TonePlayer({
psychoJS: sound.psychoJS,
note: note + sound.octave,
duration_s: sound.secs,
volume: sound.volume,
loops: sound.loops
});
return player;
}
}
// TonePlayer is not an appropriate player for the given sound:
return undefined;
}
/**
* Get the duration of the sound.
*
* @name module:sound.TonePlayer#getDuration
* @function
* @public
* @return {number} the duration of the sound, in seconds
*/
getDuration()
{
return this.duration_s;
}
/**
* Set the number of loops.
*
* @name module:sound.TonePlayer#setLoops
* @function
* @public
* @param {number} loops - how many times to repeat the track after it has played once. If loops == -1, the track will repeat indefinitely until stopped.
*/
setLoops(loops)
{
this._loops = loops;
}
/**
* Set the volume of the tone.
*
* @name module:sound.TonePlayer#setVolume
* @function
* @public
* @param {Integer} volume - the volume of the tone
* @param {booleam} [mute= false] - whether or not to mute the tone
*/
setVolume(volume, mute = false) {
this._volume = volume;
if (typeof this._volumeNode !== 'undefined') {
this._volumeNode.mute = mute;
this._synth.volume.value = -60 + volume * 66;
}
}
/**
* Start playing the sound.
*
* @name module:sound.TonePlayer#play
* @function
* @public
* @param {boolean} [loops] - how many times to repeat the sound after it has played once. If loops == -1, the sound will repeat indefinitely until stopped.
*/
play(loops) {
if (typeof loops !== 'undefined')
this._loops = loops;
const self = this;
const callback = time => { self._synth.triggerAttackRelease(self._note, self.duration_s, Tone.now()); };
if (this.loops == 0)
this._toneId = Tone.Transport.scheduleOnce(callback, Tone.now());
else if (this.loops == -1)
this._toneId = Tone.Transport.scheduleRepeat(
callback,
this.duration_s,
Tone.now(),
Tone.Infinity
);
else
this._toneId = Tone.Transport.scheduleRepeat(
callback,
this.duration_s,
Tone.now(),
this.duration_s * (this._loops+1)
);
}
/**
* Stop playing the sound immediately.
*
* @name module:sound.TonePlayer#stop
* @function
* @public
*/
stop() {
if (this._toneId)
Tone.Transport.clear(this._toneId);
}
}
// Start the Tone Transport
Tone.Transport.start(Tone.now());
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-core.html">core</a></li><li><a href="module-data.html">data</a></li><li><a href="module-sound.html">sound</a></li><li><a href="module-util.html">util</a></li><li><a href="module-visual.html">visual</a></li></ul><h3>Classes</h3><ul><li><a href="module-core.BuilderKeyResponse.html">BuilderKeyResponse</a></li><li><a href="module-core.EventManager.html">EventManager</a></li><li><a href="module-core.GUI.html">GUI</a></li><li><a href="module-core.MinimalStim.html">MinimalStim</a></li><li><a href="module-core.Mouse.html">Mouse</a></li><li><a href="module-core.PsychoJS.html">PsychoJS</a></li><li><a href="module-core.ServerManager.html">ServerManager</a></li><li><a href="module-core.Window.html">Window</a></li><li><a href="module-data.ExperimentHandler.html">ExperimentHandler</a></li><li><a href="module-data.TrialHandler.html">TrialHandler</a></li><li><a href="module-sound.Sound.html">Sound</a></li><li><a href="module-sound.TonePlayer.html">TonePlayer</a></li><li><a href="module-sound.TrackPlayer.html">TrackPlayer</a></li><li><a href="module-util.Clock.html">Clock</a></li><li><a href="module-util.Color.html">Color</a></li><li><a href="module-util.CountdownTimer.html">CountdownTimer</a></li><li><a href="module-util.EventEmitter.html">EventEmitter</a></li><li><a href="module-util.Logger.html">Logger</a></li><li><a href="module-util.MonotonicClock.html">MonotonicClock</a></li><li><a href="module-util.PsychObject.html">PsychObject</a></li><li><a href="module-util.Scheduler.html">Scheduler</a></li><li><a href="module-visual.BaseShapeStim.html">BaseShapeStim</a></li><li><a href="module-visual.BaseVisualStim.html">BaseVisualStim</a></li><li><a href="module-visual.ImageStim.html">ImageStim</a></li><li><a href="module-visual.Rect.html">Rect</a></li><li><a href="module-visual.TextStim.html">TextStim</a></li></ul><h3>Mixins</h3><ul><li><a href="module-core.WindowMixin.html">WindowMixin</a></li><li><a href="module-util.ColorMixin.html">ColorMixin</a></li></ul><h3>Interfaces</h3><ul><li><a href="module-sound.SoundPlayer.html">SoundPlayer</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Dec 10 2018 07:53:49 GMT+0100 (CET)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>