Instrument.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /* *
  2. *
  3. * (c) 2009-2020 Øystein Moseng
  4. *
  5. * Instrument class for sonification module.
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import H from '../../Core/Globals.js';
  14. import U from '../../Core/Utilities.js';
  15. var error = U.error, merge = U.merge, pick = U.pick, uniqueKey = U.uniqueKey;
  16. /**
  17. * A set of options for the Instrument class.
  18. *
  19. * @requires module:modules/sonification
  20. *
  21. * @interface Highcharts.InstrumentOptionsObject
  22. */ /**
  23. * The type of instrument. Currently only `oscillator` is supported. Defaults
  24. * to `oscillator`.
  25. * @name Highcharts.InstrumentOptionsObject#type
  26. * @type {string|undefined}
  27. */ /**
  28. * The unique ID of the instrument. Generated if not supplied.
  29. * @name Highcharts.InstrumentOptionsObject#id
  30. * @type {string|undefined}
  31. */ /**
  32. * The master volume multiplier to apply to the instrument, regardless of other
  33. * volume changes. Defaults to 1.
  34. * @name Highcharts.InstrumentPlayOptionsObject#masterVolume
  35. * @type {number|undefined}
  36. */ /**
  37. * When using functions to determine frequency or other parameters during
  38. * playback, this options specifies how often to call the callback functions.
  39. * Number given in milliseconds. Defaults to 20.
  40. * @name Highcharts.InstrumentOptionsObject#playCallbackInterval
  41. * @type {number|undefined}
  42. */ /**
  43. * A list of allowed frequencies for this instrument. If trying to play a
  44. * frequency not on this list, the closest frequency will be used. Set to `null`
  45. * to allow all frequencies to be used. Defaults to `null`.
  46. * @name Highcharts.InstrumentOptionsObject#allowedFrequencies
  47. * @type {Array<number>|undefined}
  48. */ /**
  49. * Options specific to oscillator instruments.
  50. * @name Highcharts.InstrumentOptionsObject#oscillator
  51. * @type {Highcharts.OscillatorOptionsObject|undefined}
  52. */
  53. /**
  54. * Options for playing an instrument.
  55. *
  56. * @requires module:modules/sonification
  57. *
  58. * @interface Highcharts.InstrumentPlayOptionsObject
  59. */ /**
  60. * The frequency of the note to play. Can be a fixed number, or a function. The
  61. * function receives one argument: the relative time of the note playing (0
  62. * being the start, and 1 being the end of the note). It should return the
  63. * frequency number for each point in time. The poll interval of this function
  64. * is specified by the Instrument.playCallbackInterval option.
  65. * @name Highcharts.InstrumentPlayOptionsObject#frequency
  66. * @type {number|Function}
  67. */ /**
  68. * The duration of the note in milliseconds.
  69. * @name Highcharts.InstrumentPlayOptionsObject#duration
  70. * @type {number}
  71. */ /**
  72. * The minimum frequency to allow. If the instrument has a set of allowed
  73. * frequencies, the closest frequency is used by default. Use this option to
  74. * stop too low frequencies from being used.
  75. * @name Highcharts.InstrumentPlayOptionsObject#minFrequency
  76. * @type {number|undefined}
  77. */ /**
  78. * The maximum frequency to allow. If the instrument has a set of allowed
  79. * frequencies, the closest frequency is used by default. Use this option to
  80. * stop too high frequencies from being used.
  81. * @name Highcharts.InstrumentPlayOptionsObject#maxFrequency
  82. * @type {number|undefined}
  83. */ /**
  84. * The volume of the instrument. Can be a fixed number between 0 and 1, or a
  85. * function. The function receives one argument: the relative time of the note
  86. * playing (0 being the start, and 1 being the end of the note). It should
  87. * return the volume for each point in time. The poll interval of this function
  88. * is specified by the Instrument.playCallbackInterval option. Defaults to 1.
  89. * @name Highcharts.InstrumentPlayOptionsObject#volume
  90. * @type {number|Function|undefined}
  91. */ /**
  92. * The panning of the instrument. Can be a fixed number between -1 and 1, or a
  93. * function. The function receives one argument: the relative time of the note
  94. * playing (0 being the start, and 1 being the end of the note). It should
  95. * return the panning value for each point in time. The poll interval of this
  96. * function is specified by the Instrument.playCallbackInterval option.
  97. * Defaults to 0.
  98. * @name Highcharts.InstrumentPlayOptionsObject#pan
  99. * @type {number|Function|undefined}
  100. */ /**
  101. * Callback function to be called when the play is completed.
  102. * @name Highcharts.InstrumentPlayOptionsObject#onEnd
  103. * @type {Function|undefined}
  104. */
  105. /**
  106. * @requires module:modules/sonification
  107. *
  108. * @interface Highcharts.OscillatorOptionsObject
  109. */ /**
  110. * The waveform shape to use for oscillator instruments. Defaults to `sine`.
  111. * @name Highcharts.OscillatorOptionsObject#waveformShape
  112. * @type {string|undefined}
  113. */
  114. // Default options for Instrument constructor
  115. var defaultOptions = {
  116. type: 'oscillator',
  117. playCallbackInterval: 20,
  118. masterVolume: 1,
  119. oscillator: {
  120. waveformShape: 'sine'
  121. }
  122. };
  123. /* eslint-disable no-invalid-this, valid-jsdoc */
  124. /**
  125. * The Instrument class. Instrument objects represent an instrument capable of
  126. * playing a certain pitch for a specified duration.
  127. *
  128. * @sample highcharts/sonification/instrument/
  129. * Using Instruments directly
  130. * @sample highcharts/sonification/instrument-advanced/
  131. * Using callbacks for instrument parameters
  132. *
  133. * @requires module:modules/sonification
  134. *
  135. * @class
  136. * @name Highcharts.Instrument
  137. *
  138. * @param {Highcharts.InstrumentOptionsObject} options
  139. * Options for the instrument instance.
  140. */
  141. function Instrument(options) {
  142. this.init(options);
  143. }
  144. Instrument.prototype.init = function (options) {
  145. if (!this.initAudioContext()) {
  146. error(29);
  147. return;
  148. }
  149. this.options = merge(defaultOptions, options);
  150. this.id = this.options.id = options && options.id || uniqueKey();
  151. this.masterVolume = this.options.masterVolume || 0;
  152. // Init the audio nodes
  153. var ctx = H.audioContext;
  154. this.gainNode = ctx.createGain();
  155. this.setGain(0);
  156. this.panNode = ctx.createStereoPanner && ctx.createStereoPanner();
  157. if (this.panNode) {
  158. this.setPan(0);
  159. this.gainNode.connect(this.panNode);
  160. this.panNode.connect(ctx.destination);
  161. }
  162. else {
  163. this.gainNode.connect(ctx.destination);
  164. }
  165. // Oscillator initialization
  166. if (this.options.type === 'oscillator') {
  167. this.initOscillator(this.options.oscillator);
  168. }
  169. // Init timer list
  170. this.playCallbackTimers = [];
  171. };
  172. /**
  173. * Return a copy of an instrument. Only one instrument instance can play at a
  174. * time, so use this to get a new copy of the instrument that can play alongside
  175. * it. The new instrument copy will receive a new ID unless one is supplied in
  176. * options.
  177. *
  178. * @function Highcharts.Instrument#copy
  179. *
  180. * @param {Highcharts.InstrumentOptionsObject} [options]
  181. * Options to merge in for the copy.
  182. *
  183. * @return {Highcharts.Instrument}
  184. * A new Instrument instance with the same options.
  185. */
  186. Instrument.prototype.copy = function (options) {
  187. return new Instrument(merge(this.options, { id: null }, options));
  188. };
  189. /**
  190. * Init the audio context, if we do not have one.
  191. * @private
  192. * @return {boolean} True if successful, false if not.
  193. */
  194. Instrument.prototype.initAudioContext = function () {
  195. var Context = H.win.AudioContext || H.win.webkitAudioContext, hasOldContext = !!H.audioContext;
  196. if (Context) {
  197. H.audioContext = H.audioContext || new Context();
  198. if (!hasOldContext &&
  199. H.audioContext &&
  200. H.audioContext.state === 'running') {
  201. H.audioContext.suspend(); // Pause until we need it
  202. }
  203. return !!(H.audioContext &&
  204. H.audioContext.createOscillator &&
  205. H.audioContext.createGain);
  206. }
  207. return false;
  208. };
  209. /**
  210. * Init an oscillator instrument.
  211. * @private
  212. * @param {Highcharts.OscillatorOptionsObject} oscillatorOptions
  213. * The oscillator options passed to Highcharts.Instrument#init.
  214. * @return {void}
  215. */
  216. Instrument.prototype.initOscillator = function (options) {
  217. var ctx = H.audioContext;
  218. this.oscillator = ctx.createOscillator();
  219. this.oscillator.type = options.waveformShape;
  220. this.oscillator.connect(this.gainNode);
  221. this.oscillatorStarted = false;
  222. };
  223. /**
  224. * Set pan position.
  225. * @private
  226. * @param {number} panValue
  227. * The pan position to set for the instrument.
  228. * @return {void}
  229. */
  230. Instrument.prototype.setPan = function (panValue) {
  231. if (this.panNode) {
  232. this.panNode.pan.setValueAtTime(panValue, H.audioContext.currentTime);
  233. }
  234. };
  235. /**
  236. * Set gain level. A maximum of 1.2 is allowed before we emit a warning. The
  237. * actual volume is not set above this level regardless of input. This function
  238. * also handles the Instrument's master volume.
  239. * @private
  240. * @param {number} gainValue
  241. * The gain level to set for the instrument.
  242. * @param {number} [rampTime=0]
  243. * Gradually change the gain level, time given in milliseconds.
  244. * @return {void}
  245. */
  246. Instrument.prototype.setGain = function (gainValue, rampTime) {
  247. var gainNode = this.gainNode;
  248. var newVal = gainValue * this.masterVolume;
  249. if (gainNode) {
  250. if (newVal > 1.2) {
  251. console.warn(// eslint-disable-line
  252. 'Highcharts sonification warning: ' +
  253. 'Volume of instrument set too high.');
  254. newVal = 1.2;
  255. }
  256. if (rampTime) {
  257. gainNode.gain.setValueAtTime(gainNode.gain.value, H.audioContext.currentTime);
  258. gainNode.gain.linearRampToValueAtTime(newVal, H.audioContext.currentTime + rampTime / 1000);
  259. }
  260. else {
  261. gainNode.gain.setValueAtTime(newVal, H.audioContext.currentTime);
  262. }
  263. }
  264. };
  265. /**
  266. * Cancel ongoing gain ramps.
  267. * @private
  268. * @return {void}
  269. */
  270. Instrument.prototype.cancelGainRamp = function () {
  271. if (this.gainNode) {
  272. this.gainNode.gain.cancelScheduledValues(0);
  273. }
  274. };
  275. /**
  276. * Set the master volume multiplier of the instrument after creation.
  277. * @param {number} volumeMultiplier
  278. * The gain level to set for the instrument.
  279. * @return {void}
  280. */
  281. Instrument.prototype.setMasterVolume = function (volumeMultiplier) {
  282. this.masterVolume = volumeMultiplier || 0;
  283. };
  284. /**
  285. * Get the closest valid frequency for this instrument.
  286. * @private
  287. * @param {number} frequency - The target frequency.
  288. * @param {number} [min] - Minimum frequency to return.
  289. * @param {number} [max] - Maximum frequency to return.
  290. * @return {number} The closest valid frequency to the input frequency.
  291. */
  292. Instrument.prototype.getValidFrequency = function (frequency, min, max) {
  293. var validFrequencies = this.options.allowedFrequencies, maximum = pick(max, Infinity), minimum = pick(min, -Infinity);
  294. return !validFrequencies || !validFrequencies.length ?
  295. // No valid frequencies for this instrument, return the target
  296. frequency :
  297. // Use the valid frequencies and return the closest match
  298. validFrequencies.reduce(function (acc, cur) {
  299. // Find the closest allowed value
  300. return Math.abs(cur - frequency) < Math.abs(acc - frequency) &&
  301. cur < maximum && cur > minimum ?
  302. cur : acc;
  303. }, Infinity);
  304. };
  305. /**
  306. * Clear existing play callback timers.
  307. * @private
  308. * @return {void}
  309. */
  310. Instrument.prototype.clearPlayCallbackTimers = function () {
  311. this.playCallbackTimers.forEach(function (timer) {
  312. clearInterval(timer);
  313. });
  314. this.playCallbackTimers = [];
  315. };
  316. /**
  317. * Set the current frequency being played by the instrument. The closest valid
  318. * frequency between the frequency limits is used.
  319. * @param {number} frequency
  320. * The frequency to set.
  321. * @param {Highcharts.Dictionary<number>} [frequencyLimits]
  322. * Object with maxFrequency and minFrequency
  323. * @return {void}
  324. */
  325. Instrument.prototype.setFrequency = function (frequency, frequencyLimits) {
  326. var limits = frequencyLimits || {}, validFrequency = this.getValidFrequency(frequency, limits.min, limits.max);
  327. if (this.options.type === 'oscillator') {
  328. this.oscillatorPlay(validFrequency);
  329. }
  330. };
  331. /**
  332. * Play oscillator instrument.
  333. * @private
  334. * @param {number} frequency - The frequency to play.
  335. */
  336. Instrument.prototype.oscillatorPlay = function (frequency) {
  337. if (!this.oscillatorStarted) {
  338. this.oscillator.start();
  339. this.oscillatorStarted = true;
  340. }
  341. this.oscillator.frequency.setValueAtTime(frequency, H.audioContext.currentTime);
  342. };
  343. /**
  344. * Prepare instrument before playing. Resumes the audio context and starts the
  345. * oscillator.
  346. * @private
  347. */
  348. Instrument.prototype.preparePlay = function () {
  349. this.setGain(0.001);
  350. if (H.audioContext.state === 'suspended') {
  351. H.audioContext.resume();
  352. }
  353. if (this.oscillator && !this.oscillatorStarted) {
  354. this.oscillator.start();
  355. this.oscillatorStarted = true;
  356. }
  357. };
  358. /**
  359. * Play the instrument according to options.
  360. *
  361. * @sample highcharts/sonification/instrument/
  362. * Using Instruments directly
  363. * @sample highcharts/sonification/instrument-advanced/
  364. * Using callbacks for instrument parameters
  365. *
  366. * @function Highcharts.Instrument#play
  367. *
  368. * @param {Highcharts.InstrumentPlayOptionsObject} options
  369. * Options for the playback of the instrument.
  370. *
  371. * @return {void}
  372. */
  373. Instrument.prototype.play = function (options) {
  374. var instrument = this, duration = options.duration || 0,
  375. // Set a value, or if it is a function, set it continously as a timer.
  376. // Pass in the value/function to set, the setter function, and any
  377. // additional data to pass through to the setter function.
  378. setOrStartTimer = function (value, setter, setterData) {
  379. var target = options.duration, currentDurationIx = 0, callbackInterval = instrument.options.playCallbackInterval;
  380. if (typeof value === 'function') {
  381. var timer = setInterval(function () {
  382. currentDurationIx++;
  383. var curTime = (currentDurationIx * callbackInterval / target);
  384. if (curTime >= 1) {
  385. instrument[setter](value(1), setterData);
  386. clearInterval(timer);
  387. }
  388. else {
  389. instrument[setter](value(curTime), setterData);
  390. }
  391. }, callbackInterval);
  392. instrument.playCallbackTimers.push(timer);
  393. }
  394. else {
  395. instrument[setter](value, setterData);
  396. }
  397. };
  398. if (!instrument.id) {
  399. // No audio support - do nothing
  400. return;
  401. }
  402. // If the AudioContext is suspended we have to resume it before playing
  403. if (H.audioContext.state === 'suspended' ||
  404. this.oscillator && !this.oscillatorStarted) {
  405. instrument.preparePlay();
  406. // Try again in 10ms
  407. setTimeout(function () {
  408. instrument.play(options);
  409. }, 10);
  410. return;
  411. }
  412. // Clear any existing play timers
  413. if (instrument.playCallbackTimers.length) {
  414. instrument.clearPlayCallbackTimers();
  415. }
  416. // Clear any gain ramps
  417. instrument.cancelGainRamp();
  418. // Clear stop oscillator timer
  419. if (instrument.stopOscillatorTimeout) {
  420. clearTimeout(instrument.stopOscillatorTimeout);
  421. delete instrument.stopOscillatorTimeout;
  422. }
  423. // If a note is playing right now, clear the stop timeout, and call the
  424. // callback.
  425. if (instrument.stopTimeout) {
  426. clearTimeout(instrument.stopTimeout);
  427. delete instrument.stopTimeout;
  428. if (instrument.stopCallback) {
  429. // We have a callback for the play we are interrupting. We do not
  430. // allow this callback to start a new play, because that leads to
  431. // chaos. We pass in 'cancelled' to indicate that this note did not
  432. // finish, but still stopped.
  433. instrument._play = instrument.play;
  434. instrument.play = function () { };
  435. instrument.stopCallback('cancelled');
  436. instrument.play = instrument._play;
  437. }
  438. }
  439. // Stop the note without fadeOut if the duration is too short to hear the
  440. // note otherwise.
  441. var immediate = duration < H.sonification.fadeOutDuration + 20;
  442. // Stop the instrument after the duration of the note
  443. instrument.stopCallback = options.onEnd;
  444. var onStop = function () {
  445. delete instrument.stopTimeout;
  446. instrument.stop(immediate);
  447. };
  448. if (duration) {
  449. instrument.stopTimeout = setTimeout(onStop, immediate ? duration :
  450. duration - H.sonification.fadeOutDuration);
  451. // Play the note
  452. setOrStartTimer(options.frequency, 'setFrequency', {
  453. minFrequency: options.minFrequency,
  454. maxFrequency: options.maxFrequency
  455. });
  456. // Set the volume and panning
  457. setOrStartTimer(pick(options.volume, 1), 'setGain', 4); // Slight ramp
  458. setOrStartTimer(pick(options.pan, 0), 'setPan');
  459. }
  460. else {
  461. // No note duration, so just stop immediately
  462. onStop();
  463. }
  464. };
  465. /**
  466. * Mute an instrument that is playing. If the instrument is not currently
  467. * playing, this function does nothing.
  468. *
  469. * @function Highcharts.Instrument#mute
  470. */
  471. Instrument.prototype.mute = function () {
  472. this.setGain(0.0001, H.sonification.fadeOutDuration * 0.8);
  473. };
  474. /**
  475. * Stop the instrument playing.
  476. *
  477. * @function Highcharts.Instrument#stop
  478. *
  479. * @param {boolean} immediately
  480. * Whether to do the stop immediately or fade out.
  481. *
  482. * @param {Function} [onStopped]
  483. * Callback function to be called when the stop is completed.
  484. *
  485. * @param {*} [callbackData]
  486. * Data to send to the onEnd callback functions.
  487. *
  488. * @return {void}
  489. */
  490. Instrument.prototype.stop = function (immediately, onStopped, callbackData) {
  491. var instr = this, reset = function () {
  492. // Remove timeout reference
  493. if (instr.stopOscillatorTimeout) {
  494. delete instr.stopOscillatorTimeout;
  495. }
  496. // The oscillator may have stopped in the meantime here, so allow
  497. // this function to fail if so.
  498. try {
  499. instr.oscillator.stop();
  500. }
  501. catch (e) {
  502. // silent error
  503. }
  504. instr.oscillator.disconnect(instr.gainNode);
  505. // We need a new oscillator in order to restart it
  506. instr.initOscillator(instr.options.oscillator);
  507. // Done stopping, call the callback from the stop
  508. if (onStopped) {
  509. onStopped(callbackData);
  510. }
  511. // Call the callback for the play we finished
  512. if (instr.stopCallback) {
  513. instr.stopCallback(callbackData);
  514. }
  515. };
  516. // Clear any existing timers
  517. if (instr.playCallbackTimers.length) {
  518. instr.clearPlayCallbackTimers();
  519. }
  520. if (instr.stopTimeout) {
  521. clearTimeout(instr.stopTimeout);
  522. }
  523. if (immediately) {
  524. instr.setGain(0);
  525. reset();
  526. }
  527. else {
  528. instr.mute();
  529. // Stop the oscillator after the mute fade-out has finished
  530. instr.stopOscillatorTimeout =
  531. setTimeout(reset, H.sonification.fadeOutDuration + 100);
  532. }
  533. };
  534. export default Instrument;