1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 18:50:54 +00:00
psychojs/docs/sound_TonePlayer.js.html
2022-05-23 12:31:01 +02:00

428 lines
14 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 2021.2.0
* @copyright (c) 2017-2020 Ilixa Ltd. (http://ilixa.com) (c) 2020-2021 Open Science Tools Ltd. (https://opensciencetools.org)
* @license Distributed under the terms of the MIT License
*/
import * as Tone from "tone";
import { isNumeric } from "../util/Util.js";
import { SoundPlayer } from "./SoundPlayer.js";
/**
* &lt;p>This class handles the playing of tones.&lt;/p>
*
* @name module:sound.TonePlayer
* @class
* @extends SoundPlayer
* @param {Object} options
* @param {module:core.PsychoJS} options.psychoJS - the PsychoJS instance
* @param {number} [options.duration_s= 0.5] - duration of the tone (in seconds). If duration_s == -1, the sound will play indefinitely.
* @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,
soundLibrary = TonePlayer.SoundLibrary.TONE_JS,
autoLog = true,
} = {})
{
super(psychoJS);
this._addAttribute("note", note);
this._addAttribute("duration_s", duration_s);
this._addAttribute("volume", volume);
this._addAttribute("loops", loops);
this._addAttribute("soundLibrary", soundLibrary);
this._addAttribute("autoLog", autoLog);
// initialise the sound library:
this._initSoundLibrary();
// Tone.js Loop:
this._toneLoop = null;
if (this._autoLog)
{
this._psychoJS.experimentLogger.exp(`Created ${this.name} = ${this.toString()}`);
}
}
/**
* Determine whether this player can play the given sound.
*
* &lt;p>Note: if TonePlayer accepts the sound but Tone.js is not available, e.g. if the browser is IE11,
* we throw an exception.&lt;/p>
*
* @name module:sound.TonePlayer.accept
* @function
* @static
* @public
* @param {module:sound.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))
{
return new TonePlayer({
psychoJS: sound.psychoJS,
note: sound.value,
duration_s: sound.secs,
volume: sound.volume,
loops: sound.loops,
});
}
// 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")
{
return new TonePlayer({
psychoJS: sound.psychoJS,
note: note + sound.octave,
duration_s: sound.secs,
volume: sound.volume,
loops: sound.loops,
});
}
}
// 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 duration of the tone.
*
* @name module:sound.TonePlayer#setDuration
* @function
* @public
* @param {number} duration_s - the duration of the tone (in seconds) If duration_s == -1, the sound will play indefinitely.
*/
setDuration(duration_s)
{
this.duration_s = 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 {boolean} [mute= false] - whether or not to mute the tone
*/
setVolume(volume, mute = false)
{
this._volume = volume;
if (this._soundLibrary === TonePlayer.SoundLibrary.TONE_JS)
{
if (typeof this._volumeNode !== "undefined")
{
this._volumeNode.mute = mute;
this._volumeNode.volume.value = -60 + volume * 66;
// this._synth.volume.value = -60 + volume * 66;
}
else
{
// TODO
}
}
else
{
// TODO
}
}
/**
* 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;
}
// if duration_s == -1, the sound should play indefinitely, therefore we use an arbitrarily long playing time
const actualDuration_s = (this._duration_s === -1) ? 1000000 : this._duration_s;
const self = this;
let playToneCallback;
if (this._soundLibrary === TonePlayer.SoundLibrary.TONE_JS)
{
playToneCallback = () =>
{
self._synth.triggerAttackRelease(self._note, actualDuration_s, Tone.context.currentTime);
};
}
else
{
playToneCallback = () =>
{
self._webAudioOscillator = self._audioContext.createOscillator();
self._webAudioOscillator.type = "sine";
self._webAudioOscillator.frequency.value = 440;
self._webAudioOscillator.connect(self._audioContext.destination);
const contextCurrentTime = self._audioContext.currentTime;
self._webAudioOscillator.start(contextCurrentTime);
self._webAudioOscillator.stop(contextCurrentTime + actualDuration_s);
};
}
// play just once:
if (this.loops === 0)
{
playToneCallback();
}
// repeat forever:
else if (this.loops === -1)
{
this._toneId = Tone.Transport.scheduleRepeat(
playToneCallback,
this.duration_s,
Tone.now(),
Infinity,
);
}
// repeat this._loops times:
else
{
this._toneId = Tone.Transport.scheduleRepeat(
playToneCallback,
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._soundLibrary === TonePlayer.SoundLibrary.TONE_JS)
{
// trigger the release of the sound, immediately:
this._synth.triggerRelease();
// clear the repeat event if need be:
if (this._toneId)
{
Tone.Transport.clear(this._toneId);
}
}
else
{
const contextCurrentTime = this._audioContext.currentTime;
this._webAudioOscillator.stop(contextCurrentTime);
}
}
/**
* Initialise the sound library.
*
* &lt;p>Note: if TonePlayer accepts the sound but Tone.js is not available, e.g. if the browser is IE11,
* we throw an exception.&lt;/p>
*
* @name module:sound.TonePlayer._initSoundLibrary
* @function
* @protected
*/
_initSoundLibrary()
{
const response = {
origin: "TonePlayer._initSoundLibrary",
context: "when initialising the sound library",
};
if (this._soundLibrary === TonePlayer.SoundLibrary.TONE_JS)
{
// check that Tone.js is available:
if (typeof Tone === "undefined")
{
throw Object.assign(response, {
error: "Tone.js is not available. A different sound library must be selected. Please contact the experiment designer.",
});
}
// start the Tone Transport if it has not started already:
if (typeof Tone !== "undefined" &amp;&amp; Tone.Transport.state !== "started")
{
this.psychoJS.logger.info("[PsychoJS] start Tone Transport");
Tone.Transport.start(Tone.now());
// this is necessary to prevent Tone from introducing a delay when triggering a note
// ( see https://github.com/Tonejs/Tone.js/issues/306#issuecomment-365989984 )
Tone.context.lookAhead = 0;
}
// create a synth: we use a triangular oscillator with hardly any envelope:
this._synthOtions = {
oscillator: {
type: "square", // 'triangle'
},
envelope: {
attack: 0.001, // 1ms
decay: 0.001, // 1ms
sustain: 1,
release: 0.001, // 1ms
},
};
this._synth = new Tone.Synth(this._synthOtions);
// connect it to a volume node:
this._volumeNode = new Tone.Volume(-60 + this._volume * 66);
this._synth.connect(this._volumeNode);
// connect the volume node to the master output:
if (typeof this._volumeNode.toDestination === "function")
{
this._volumeNode.toDestination();
}
else
{
this._volumeNode.toMaster();
}
}
else
{
// create an AudioContext:
if (typeof this._audioContext === "undefined")
{
const AudioContext = window.AudioContext || window.webkitAudioContext;
// if AudioContext is not available (e.g. on IE), we throw an exception:
if (typeof AudioContext === "undefined")
{
throw Object.assign(response, {
error: `AudioContext is not available on your browser, ${this._psychoJS.browser}, please contact the experiment designer.`,
});
}
this._audioContext = new AudioContext();
}
}
}
}
/**
*
* @type {{TONE_JS: *, AUDIO_CONTEXT: *}}
*/
TonePlayer.SoundLibrary = {
AUDIO_CONTEXT: Symbol.for("AUDIO_CONTEXT"),
TONE_JS: Symbol.for("TONE_JS"),
};
</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="FaceDetector_FaceDetector.html">FaceDetector</a></li><li><a href="module.data.MultiStairHandler.html">MultiStairHandler</a></li><li><a href="module.data.QuestHandler.html">QuestHandler</a></li><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.Keyboard.html">Keyboard</a></li><li><a href="module-core.KeyPress.html">KeyPress</a></li><li><a href="module-core.Logger.html">Logger</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.MultiStairHandler.html">MultiStairHandler</a></li><li><a href="module-data.QuestHandler.html">QuestHandler</a></li><li><a href="module-data.Shelf.html">Shelf</a></li><li><a href="module-data.TrialHandler.html">TrialHandler</a></li><li><a href="module-sound.AudioClip.html">AudioClip</a></li><li><a href="module-sound.AudioClipPlayer.html">AudioClipPlayer</a></li><li><a href="module-sound.Microphone.html">Microphone</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-sound.Transcriber.html">Transcriber</a></li><li><a href="module-sound.Transcript.html">Transcript</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.MixinBuilder.html">MixinBuilder</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.ButtonStim.html">ButtonStim</a></li><li><a href="module-visual.Camera.html">Camera</a></li><li><a href="module-visual.FaceDetector.html">FaceDetector</a></li><li><a href="module-visual.Form.html">Form</a></li><li><a href="module-visual.GratingStim.html">GratingStim</a></li><li><a href="module-visual.ImageStim.html">ImageStim</a></li><li><a href="module-visual.MovieStim.html">MovieStim</a></li><li><a href="module-visual.Polygon.html">Polygon</a></li><li><a href="module-visual.Rect.html">Rect</a></li><li><a href="module-visual.ShapeStim.html">ShapeStim</a></li><li><a href="module-visual.Slider.html">Slider</a></li><li><a href="module-visual.TextBox.html">TextBox</a></li><li><a href="module-visual.TextStim.html">TextStim</a></li><li><a href="module-visual.VisualStim.html">VisualStim</a></li></ul><h3>Interfaces</h3><ul><li><a href="module-sound.SoundPlayer.html">SoundPlayer</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>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Mon May 23 2022 12:29:28 GMT+0200 (Central European Summer Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>