sonification.src.js 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966
  1. /**
  2. * @license Highcharts JS v8.0.0 (2019-12-10)
  3. *
  4. * Sonification module
  5. *
  6. * (c) 2012-2019 Øystein Moseng
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/sonification', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'modules/sonification/Instrument.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * (c) 2009-2019 Øystein Moseng
  35. *
  36. * Instrument class for sonification module.
  37. *
  38. * License: www.highcharts.com/license
  39. *
  40. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  41. *
  42. * */
  43. var pick = U.pick;
  44. /**
  45. * A set of options for the Instrument class.
  46. *
  47. * @requires module:modules/sonification
  48. *
  49. * @interface Highcharts.InstrumentOptionsObject
  50. */ /**
  51. * The type of instrument. Currently only `oscillator` is supported. Defaults
  52. * to `oscillator`.
  53. * @name Highcharts.InstrumentOptionsObject#type
  54. * @type {string|undefined}
  55. */ /**
  56. * The unique ID of the instrument. Generated if not supplied.
  57. * @name Highcharts.InstrumentOptionsObject#id
  58. * @type {string|undefined}
  59. */ /**
  60. * When using functions to determine frequency or other parameters during
  61. * playback, this options specifies how often to call the callback functions.
  62. * Number given in milliseconds. Defaults to 20.
  63. * @name Highcharts.InstrumentOptionsObject#playCallbackInterval
  64. * @type {number|undefined}
  65. */ /**
  66. * A list of allowed frequencies for this instrument. If trying to play a
  67. * frequency not on this list, the closest frequency will be used. Set to `null`
  68. * to allow all frequencies to be used. Defaults to `null`.
  69. * @name Highcharts.InstrumentOptionsObject#allowedFrequencies
  70. * @type {Array<number>|undefined}
  71. */ /**
  72. * Options specific to oscillator instruments.
  73. * @name Highcharts.InstrumentOptionsObject#oscillator
  74. * @type {Highcharts.OscillatorOptionsObject|undefined}
  75. */
  76. /**
  77. * Options for playing an instrument.
  78. *
  79. * @requires module:modules/sonification
  80. *
  81. * @interface Highcharts.InstrumentPlayOptionsObject
  82. */ /**
  83. * The frequency of the note to play. Can be a fixed number, or a function. The
  84. * function receives one argument: the relative time of the note playing (0
  85. * being the start, and 1 being the end of the note). It should return the
  86. * frequency number for each point in time. The poll interval of this function
  87. * is specified by the Instrument.playCallbackInterval option.
  88. * @name Highcharts.InstrumentPlayOptionsObject#frequency
  89. * @type {number|Function}
  90. */ /**
  91. * The duration of the note in milliseconds.
  92. * @name Highcharts.InstrumentPlayOptionsObject#duration
  93. * @type {number}
  94. */ /**
  95. * The minimum frequency to allow. If the instrument has a set of allowed
  96. * frequencies, the closest frequency is used by default. Use this option to
  97. * stop too low frequencies from being used.
  98. * @name Highcharts.InstrumentPlayOptionsObject#minFrequency
  99. * @type {number|undefined}
  100. */ /**
  101. * The maximum frequency to allow. If the instrument has a set of allowed
  102. * frequencies, the closest frequency is used by default. Use this option to
  103. * stop too high frequencies from being used.
  104. * @name Highcharts.InstrumentPlayOptionsObject#maxFrequency
  105. * @type {number|undefined}
  106. */ /**
  107. * The volume of the instrument. Can be a fixed number between 0 and 1, or a
  108. * function. The function receives one argument: the relative time of the note
  109. * playing (0 being the start, and 1 being the end of the note). It should
  110. * return the volume for each point in time. The poll interval of this function
  111. * is specified by the Instrument.playCallbackInterval option. Defaults to 1.
  112. * @name Highcharts.InstrumentPlayOptionsObject#volume
  113. * @type {number|Function|undefined}
  114. */ /**
  115. * The panning of the instrument. Can be a fixed number between -1 and 1, or a
  116. * function. The function receives one argument: the relative time of the note
  117. * playing (0 being the start, and 1 being the end of the note). It should
  118. * return the panning value for each point in time. The poll interval of this
  119. * function is specified by the Instrument.playCallbackInterval option.
  120. * Defaults to 0.
  121. * @name Highcharts.InstrumentPlayOptionsObject#pan
  122. * @type {number|Function|undefined}
  123. */ /**
  124. * Callback function to be called when the play is completed.
  125. * @name Highcharts.InstrumentPlayOptionsObject#onEnd
  126. * @type {Function|undefined}
  127. */
  128. /**
  129. * @requires module:modules/sonification
  130. *
  131. * @interface Highcharts.OscillatorOptionsObject
  132. */ /**
  133. * The waveform shape to use for oscillator instruments. Defaults to `sine`.
  134. * @name Highcharts.OscillatorOptionsObject#waveformShape
  135. * @type {string|undefined}
  136. */
  137. // Default options for Instrument constructor
  138. var defaultOptions = {
  139. type: 'oscillator',
  140. playCallbackInterval: 20,
  141. oscillator: {
  142. waveformShape: 'sine'
  143. }
  144. };
  145. /* eslint-disable no-invalid-this, valid-jsdoc */
  146. /**
  147. * The Instrument class. Instrument objects represent an instrument capable of
  148. * playing a certain pitch for a specified duration.
  149. *
  150. * @sample highcharts/sonification/instrument/
  151. * Using Instruments directly
  152. * @sample highcharts/sonification/instrument-advanced/
  153. * Using callbacks for instrument parameters
  154. *
  155. * @requires module:modules/sonification
  156. *
  157. * @class
  158. * @name Highcharts.Instrument
  159. *
  160. * @param {Highcharts.InstrumentOptionsObject} options
  161. * Options for the instrument instance.
  162. */
  163. function Instrument(options) {
  164. this.init(options);
  165. }
  166. Instrument.prototype.init = function (options) {
  167. if (!this.initAudioContext()) {
  168. H.error(29);
  169. return;
  170. }
  171. this.options = H.merge(defaultOptions, options);
  172. this.id = this.options.id = options && options.id || H.uniqueKey();
  173. // Init the audio nodes
  174. var ctx = H.audioContext;
  175. this.gainNode = ctx.createGain();
  176. this.setGain(0);
  177. this.panNode = ctx.createStereoPanner && ctx.createStereoPanner();
  178. if (this.panNode) {
  179. this.setPan(0);
  180. this.gainNode.connect(this.panNode);
  181. this.panNode.connect(ctx.destination);
  182. }
  183. else {
  184. this.gainNode.connect(ctx.destination);
  185. }
  186. // Oscillator initialization
  187. if (this.options.type === 'oscillator') {
  188. this.initOscillator(this.options.oscillator);
  189. }
  190. // Init timer list
  191. this.playCallbackTimers = [];
  192. };
  193. /**
  194. * Return a copy of an instrument. Only one instrument instance can play at a
  195. * time, so use this to get a new copy of the instrument that can play alongside
  196. * it. The new instrument copy will receive a new ID unless one is supplied in
  197. * options.
  198. *
  199. * @function Highcharts.Instrument#copy
  200. *
  201. * @param {Highcharts.InstrumentOptionsObject} [options]
  202. * Options to merge in for the copy.
  203. *
  204. * @return {Highcharts.Instrument}
  205. * A new Instrument instance with the same options.
  206. */
  207. Instrument.prototype.copy = function (options) {
  208. return new Instrument(H.merge(this.options, { id: null }, options));
  209. };
  210. /**
  211. * Init the audio context, if we do not have one.
  212. * @private
  213. * @return {boolean} True if successful, false if not.
  214. */
  215. Instrument.prototype.initAudioContext = function () {
  216. var Context = H.win.AudioContext || H.win.webkitAudioContext, hasOldContext = !!H.audioContext;
  217. if (Context) {
  218. H.audioContext = H.audioContext || new Context();
  219. if (!hasOldContext &&
  220. H.audioContext &&
  221. H.audioContext.state === 'running') {
  222. H.audioContext.suspend(); // Pause until we need it
  223. }
  224. return !!(H.audioContext &&
  225. H.audioContext.createOscillator &&
  226. H.audioContext.createGain);
  227. }
  228. return false;
  229. };
  230. /**
  231. * Init an oscillator instrument.
  232. * @private
  233. * @param {Highcharts.OscillatorOptionsObject} oscillatorOptions
  234. * The oscillator options passed to Highcharts.Instrument#init.
  235. * @return {void}
  236. */
  237. Instrument.prototype.initOscillator = function (options) {
  238. var ctx = H.audioContext;
  239. this.oscillator = ctx.createOscillator();
  240. this.oscillator.type = options.waveformShape;
  241. this.oscillator.connect(this.gainNode);
  242. this.oscillatorStarted = false;
  243. };
  244. /**
  245. * Set pan position.
  246. * @private
  247. * @param {number} panValue
  248. * The pan position to set for the instrument.
  249. * @return {void}
  250. */
  251. Instrument.prototype.setPan = function (panValue) {
  252. if (this.panNode) {
  253. this.panNode.pan.setValueAtTime(panValue, H.audioContext.currentTime);
  254. }
  255. };
  256. /**
  257. * Set gain level. A maximum of 1.2 is allowed before we emit a warning. The
  258. * actual volume is not set above this level regardless of input.
  259. * @private
  260. * @param {number} gainValue
  261. * The gain level to set for the instrument.
  262. * @param {number} [rampTime=0]
  263. * Gradually change the gain level, time given in milliseconds.
  264. * @return {void}
  265. */
  266. Instrument.prototype.setGain = function (gainValue, rampTime) {
  267. if (this.gainNode) {
  268. if (gainValue > 1.2) {
  269. console.warn(// eslint-disable-line
  270. 'Highcharts sonification warning: ' +
  271. 'Volume of instrument set too high.');
  272. gainValue = 1.2;
  273. }
  274. if (rampTime) {
  275. this.gainNode.gain.setValueAtTime(this.gainNode.gain.value, H.audioContext.currentTime);
  276. this.gainNode.gain.linearRampToValueAtTime(gainValue, H.audioContext.currentTime + rampTime / 1000);
  277. }
  278. else {
  279. this.gainNode.gain.setValueAtTime(gainValue, H.audioContext.currentTime);
  280. }
  281. }
  282. };
  283. /**
  284. * Cancel ongoing gain ramps.
  285. * @private
  286. * @return {void}
  287. */
  288. Instrument.prototype.cancelGainRamp = function () {
  289. if (this.gainNode) {
  290. this.gainNode.gain.cancelScheduledValues(0);
  291. }
  292. };
  293. /**
  294. * Get the closest valid frequency for this instrument.
  295. * @private
  296. * @param {number} frequency - The target frequency.
  297. * @param {number} [min] - Minimum frequency to return.
  298. * @param {number} [max] - Maximum frequency to return.
  299. * @return {number} The closest valid frequency to the input frequency.
  300. */
  301. Instrument.prototype.getValidFrequency = function (frequency, min, max) {
  302. var validFrequencies = this.options.allowedFrequencies, maximum = pick(max, Infinity), minimum = pick(min, -Infinity);
  303. return !validFrequencies || !validFrequencies.length ?
  304. // No valid frequencies for this instrument, return the target
  305. frequency :
  306. // Use the valid frequencies and return the closest match
  307. validFrequencies.reduce(function (acc, cur) {
  308. // Find the closest allowed value
  309. return Math.abs(cur - frequency) < Math.abs(acc - frequency) &&
  310. cur < maximum && cur > minimum ?
  311. cur : acc;
  312. }, Infinity);
  313. };
  314. /**
  315. * Clear existing play callback timers.
  316. * @private
  317. * @return {void}
  318. */
  319. Instrument.prototype.clearPlayCallbackTimers = function () {
  320. this.playCallbackTimers.forEach(function (timer) {
  321. clearInterval(timer);
  322. });
  323. this.playCallbackTimers = [];
  324. };
  325. /**
  326. * Set the current frequency being played by the instrument. The closest valid
  327. * frequency between the frequency limits is used.
  328. * @param {number} frequency
  329. * The frequency to set.
  330. * @param {Highcharts.Dictionary<number>} [frequencyLimits]
  331. * Object with maxFrequency and minFrequency
  332. * @return {void}
  333. */
  334. Instrument.prototype.setFrequency = function (frequency, frequencyLimits) {
  335. var limits = frequencyLimits || {}, validFrequency = this.getValidFrequency(frequency, limits.min, limits.max);
  336. if (this.options.type === 'oscillator') {
  337. this.oscillatorPlay(validFrequency);
  338. }
  339. };
  340. /**
  341. * Play oscillator instrument.
  342. * @private
  343. * @param {number} frequency - The frequency to play.
  344. */
  345. Instrument.prototype.oscillatorPlay = function (frequency) {
  346. if (!this.oscillatorStarted) {
  347. this.oscillator.start();
  348. this.oscillatorStarted = true;
  349. }
  350. this.oscillator.frequency.setValueAtTime(frequency, H.audioContext.currentTime);
  351. };
  352. /**
  353. * Prepare instrument before playing. Resumes the audio context and starts the
  354. * oscillator.
  355. * @private
  356. */
  357. Instrument.prototype.preparePlay = function () {
  358. this.setGain(0.001);
  359. if (H.audioContext.state === 'suspended') {
  360. H.audioContext.resume();
  361. }
  362. if (this.oscillator && !this.oscillatorStarted) {
  363. this.oscillator.start();
  364. this.oscillatorStarted = true;
  365. }
  366. };
  367. /**
  368. * Play the instrument according to options.
  369. *
  370. * @sample highcharts/sonification/instrument/
  371. * Using Instruments directly
  372. * @sample highcharts/sonification/instrument-advanced/
  373. * Using callbacks for instrument parameters
  374. *
  375. * @function Highcharts.Instrument#play
  376. *
  377. * @param {Highcharts.InstrumentPlayOptionsObject} options
  378. * Options for the playback of the instrument.
  379. *
  380. * @return {void}
  381. */
  382. Instrument.prototype.play = function (options) {
  383. var instrument = this, duration = options.duration || 0,
  384. // Set a value, or if it is a function, set it continously as a timer.
  385. // Pass in the value/function to set, the setter function, and any
  386. // additional data to pass through to the setter function.
  387. setOrStartTimer = function (value, setter, setterData) {
  388. var target = options.duration, currentDurationIx = 0, callbackInterval = instrument.options.playCallbackInterval;
  389. if (typeof value === 'function') {
  390. var timer = setInterval(function () {
  391. currentDurationIx++;
  392. var curTime = (currentDurationIx * callbackInterval / target);
  393. if (curTime >= 1) {
  394. instrument[setter](value(1), setterData);
  395. clearInterval(timer);
  396. }
  397. else {
  398. instrument[setter](value(curTime), setterData);
  399. }
  400. }, callbackInterval);
  401. instrument.playCallbackTimers.push(timer);
  402. }
  403. else {
  404. instrument[setter](value, setterData);
  405. }
  406. };
  407. if (!instrument.id) {
  408. // No audio support - do nothing
  409. return;
  410. }
  411. // If the AudioContext is suspended we have to resume it before playing
  412. if (H.audioContext.state === 'suspended' ||
  413. this.oscillator && !this.oscillatorStarted) {
  414. instrument.preparePlay();
  415. // Try again in 10ms
  416. setTimeout(function () {
  417. instrument.play(options);
  418. }, 10);
  419. return;
  420. }
  421. // Clear any existing play timers
  422. if (instrument.playCallbackTimers.length) {
  423. instrument.clearPlayCallbackTimers();
  424. }
  425. // Clear any gain ramps
  426. instrument.cancelGainRamp();
  427. // Clear stop oscillator timer
  428. if (instrument.stopOscillatorTimeout) {
  429. clearTimeout(instrument.stopOscillatorTimeout);
  430. delete instrument.stopOscillatorTimeout;
  431. }
  432. // If a note is playing right now, clear the stop timeout, and call the
  433. // callback.
  434. if (instrument.stopTimeout) {
  435. clearTimeout(instrument.stopTimeout);
  436. delete instrument.stopTimeout;
  437. if (instrument.stopCallback) {
  438. // We have a callback for the play we are interrupting. We do not
  439. // allow this callback to start a new play, because that leads to
  440. // chaos. We pass in 'cancelled' to indicate that this note did not
  441. // finish, but still stopped.
  442. instrument._play = instrument.play;
  443. instrument.play = function () { };
  444. instrument.stopCallback('cancelled');
  445. instrument.play = instrument._play;
  446. }
  447. }
  448. // Stop the note without fadeOut if the duration is too short to hear the
  449. // note otherwise.
  450. var immediate = duration < H.sonification.fadeOutDuration + 20;
  451. // Stop the instrument after the duration of the note
  452. instrument.stopCallback = options.onEnd;
  453. var onStop = function () {
  454. delete instrument.stopTimeout;
  455. instrument.stop(immediate);
  456. };
  457. if (duration) {
  458. instrument.stopTimeout = setTimeout(onStop, immediate ? duration :
  459. duration - H.sonification.fadeOutDuration);
  460. // Play the note
  461. setOrStartTimer(options.frequency, 'setFrequency', {
  462. minFrequency: options.minFrequency,
  463. maxFrequency: options.maxFrequency
  464. });
  465. // Set the volume and panning
  466. setOrStartTimer(pick(options.volume, 1), 'setGain', 4); // Slight ramp
  467. setOrStartTimer(pick(options.pan, 0), 'setPan');
  468. }
  469. else {
  470. // No note duration, so just stop immediately
  471. onStop();
  472. }
  473. };
  474. /**
  475. * Mute an instrument that is playing. If the instrument is not currently
  476. * playing, this function does nothing.
  477. *
  478. * @function Highcharts.Instrument#mute
  479. */
  480. Instrument.prototype.mute = function () {
  481. this.setGain(0.0001, H.sonification.fadeOutDuration * 0.8);
  482. };
  483. /**
  484. * Stop the instrument playing.
  485. *
  486. * @function Highcharts.Instrument#stop
  487. *
  488. * @param {boolean} immediately
  489. * Whether to do the stop immediately or fade out.
  490. *
  491. * @param {Function} [onStopped]
  492. * Callback function to be called when the stop is completed.
  493. *
  494. * @param {*} [callbackData]
  495. * Data to send to the onEnd callback functions.
  496. *
  497. * @return {void}
  498. */
  499. Instrument.prototype.stop = function (immediately, onStopped, callbackData) {
  500. var instr = this, reset = function () {
  501. // Remove timeout reference
  502. if (instr.stopOscillatorTimeout) {
  503. delete instr.stopOscillatorTimeout;
  504. }
  505. // The oscillator may have stopped in the meantime here, so allow
  506. // this function to fail if so.
  507. try {
  508. instr.oscillator.stop();
  509. }
  510. catch (e) {
  511. // silent error
  512. }
  513. instr.oscillator.disconnect(instr.gainNode);
  514. // We need a new oscillator in order to restart it
  515. instr.initOscillator(instr.options.oscillator);
  516. // Done stopping, call the callback from the stop
  517. if (onStopped) {
  518. onStopped(callbackData);
  519. }
  520. // Call the callback for the play we finished
  521. if (instr.stopCallback) {
  522. instr.stopCallback(callbackData);
  523. }
  524. };
  525. // Clear any existing timers
  526. if (instr.playCallbackTimers.length) {
  527. instr.clearPlayCallbackTimers();
  528. }
  529. if (instr.stopTimeout) {
  530. clearTimeout(instr.stopTimeout);
  531. }
  532. if (immediately) {
  533. instr.setGain(0);
  534. reset();
  535. }
  536. else {
  537. instr.mute();
  538. // Stop the oscillator after the mute fade-out has finished
  539. instr.stopOscillatorTimeout =
  540. setTimeout(reset, H.sonification.fadeOutDuration + 100);
  541. }
  542. };
  543. return Instrument;
  544. });
  545. _registerModule(_modules, 'modules/sonification/musicalFrequencies.js', [], function () {
  546. /* *
  547. *
  548. * (c) 2009-2019 Øystein Moseng
  549. *
  550. * List of musical frequencies from C0 to C8.
  551. *
  552. * License: www.highcharts.com/license
  553. *
  554. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  555. *
  556. * */
  557. var frequencies = [
  558. 16.351597831287414,
  559. 17.323914436054505,
  560. 18.354047994837977,
  561. 19.445436482630058,
  562. 20.601722307054366,
  563. 21.826764464562746,
  564. 23.12465141947715,
  565. 24.499714748859326,
  566. 25.956543598746574,
  567. 27.5,
  568. 29.13523509488062,
  569. 30.86770632850775,
  570. 32.70319566257483,
  571. 34.64782887210901,
  572. 36.70809598967594,
  573. 38.890872965260115,
  574. 41.20344461410875,
  575. 43.653528929125486,
  576. 46.2493028389543,
  577. 48.999429497718666,
  578. 51.91308719749314,
  579. 55,
  580. 58.27047018976124,
  581. 61.7354126570155,
  582. 65.40639132514966,
  583. 69.29565774421802,
  584. 73.41619197935188,
  585. 77.78174593052023,
  586. 82.4068892282175,
  587. 87.30705785825097,
  588. 92.4986056779086,
  589. 97.99885899543733,
  590. 103.82617439498628,
  591. 110,
  592. 116.54094037952248,
  593. 123.47082531403103,
  594. 130.8127826502993,
  595. 138.59131548843604,
  596. 146.8323839587038,
  597. 155.56349186104046,
  598. 164.81377845643496,
  599. 174.61411571650194,
  600. 184.9972113558172,
  601. 195.99771799087463,
  602. 207.65234878997256,
  603. 220,
  604. 233.08188075904496,
  605. 246.94165062806206,
  606. 261.6255653005986,
  607. 277.1826309768721,
  608. 293.6647679174076,
  609. 311.1269837220809,
  610. 329.6275569128699,
  611. 349.2282314330039,
  612. 369.9944227116344,
  613. 391.99543598174927,
  614. 415.3046975799451,
  615. 440,
  616. 466.1637615180899,
  617. 493.8833012561241,
  618. 523.2511306011972,
  619. 554.3652619537442,
  620. 587.3295358348151,
  621. 622.2539674441618,
  622. 659.2551138257398,
  623. 698.4564628660078,
  624. 739.9888454232688,
  625. 783.9908719634985,
  626. 830.6093951598903,
  627. 880,
  628. 932.3275230361799,
  629. 987.7666025122483,
  630. 1046.5022612023945,
  631. 1108.7305239074883,
  632. 1174.6590716696303,
  633. 1244.5079348883237,
  634. 1318.5102276514797,
  635. 1396.9129257320155,
  636. 1479.9776908465376,
  637. 1567.981743926997,
  638. 1661.2187903197805,
  639. 1760,
  640. 1864.6550460723597,
  641. 1975.533205024496,
  642. 2093.004522404789,
  643. 2217.4610478149766,
  644. 2349.31814333926,
  645. 2489.0158697766474,
  646. 2637.02045530296,
  647. 2793.825851464031,
  648. 2959.955381693075,
  649. 3135.9634878539946,
  650. 3322.437580639561,
  651. 3520,
  652. 3729.3100921447194,
  653. 3951.066410048992,
  654. 4186.009044809578 // C8
  655. ];
  656. return frequencies;
  657. });
  658. _registerModule(_modules, 'modules/sonification/utilities.js', [_modules['modules/sonification/musicalFrequencies.js'], _modules['parts/Utilities.js']], function (musicalFrequencies, U) {
  659. /* *
  660. *
  661. * (c) 2009-2019 Øystein Moseng
  662. *
  663. * Utility functions for sonification.
  664. *
  665. * License: www.highcharts.com/license
  666. *
  667. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  668. *
  669. * */
  670. var clamp = U.clamp;
  671. /* eslint-disable no-invalid-this, valid-jsdoc */
  672. /**
  673. * The SignalHandler class. Stores signal callbacks (event handlers), and
  674. * provides an interface to register them, and emit signals. The word "event" is
  675. * not used to avoid confusion with TimelineEvents.
  676. *
  677. * @requires module:modules/sonification
  678. *
  679. * @private
  680. * @class
  681. * @name Highcharts.SignalHandler
  682. *
  683. * @param {Array<string>} supportedSignals
  684. * List of supported signal names.
  685. */
  686. function SignalHandler(supportedSignals) {
  687. this.init(supportedSignals || []);
  688. }
  689. SignalHandler.prototype.init = function (supportedSignals) {
  690. this.supportedSignals = supportedSignals;
  691. this.signals = {};
  692. };
  693. /**
  694. * Register a set of signal callbacks with this SignalHandler.
  695. * Multiple signal callbacks can be registered for the same signal.
  696. * @private
  697. * @param {Highcharts.Dictionary<(Function|undefined)>} signals
  698. * An object that contains a mapping from the signal name to the callbacks. Only
  699. * supported events are considered.
  700. * @return {void}
  701. */
  702. SignalHandler.prototype.registerSignalCallbacks = function (signals) {
  703. var signalHandler = this;
  704. signalHandler.supportedSignals.forEach(function (supportedSignal) {
  705. var signal = signals[supportedSignal];
  706. if (signal) {
  707. (signalHandler.signals[supportedSignal] =
  708. signalHandler.signals[supportedSignal] || []).push(signal);
  709. }
  710. });
  711. };
  712. /**
  713. * Clear signal callbacks, optionally by name.
  714. * @private
  715. * @param {Array<string>} [signalNames] - A list of signal names to clear. If
  716. * not supplied, all signal callbacks are removed.
  717. * @return {void}
  718. */
  719. SignalHandler.prototype.clearSignalCallbacks = function (signalNames) {
  720. var signalHandler = this;
  721. if (signalNames) {
  722. signalNames.forEach(function (signalName) {
  723. if (signalHandler.signals[signalName]) {
  724. delete signalHandler.signals[signalName];
  725. }
  726. });
  727. }
  728. else {
  729. signalHandler.signals = {};
  730. }
  731. };
  732. /**
  733. * Emit a signal. Does nothing if the signal does not exist, or has no
  734. * registered callbacks.
  735. * @private
  736. * @param {string} signalNames
  737. * Name of signal to emit.
  738. * @param {*} [data]
  739. * Data to pass to the callback.
  740. * @return {*}
  741. */
  742. SignalHandler.prototype.emitSignal = function (signalName, data) {
  743. var retval;
  744. if (this.signals[signalName]) {
  745. this.signals[signalName].forEach(function (handler) {
  746. var result = handler(data);
  747. retval = typeof result !== 'undefined' ? result : retval;
  748. });
  749. }
  750. return retval;
  751. };
  752. var utilities = {
  753. // List of musical frequencies from C0 to C8
  754. musicalFrequencies: musicalFrequencies,
  755. // SignalHandler class
  756. SignalHandler: SignalHandler,
  757. /**
  758. * Get a musical scale by specifying the semitones from 1-12 to include.
  759. * 1: C, 2: C#, 3: D, 4: D#, 5: E, 6: F,
  760. * 7: F#, 8: G, 9: G#, 10: A, 11: Bb, 12: B
  761. * @private
  762. * @param {Array<number>} semitones
  763. * Array of semitones from 1-12 to include in the scale. Duplicate entries
  764. * are ignored.
  765. * @return {Array<number>}
  766. * Array of frequencies from C0 to C8 that are included in this scale.
  767. */
  768. getMusicalScale: function (semitones) {
  769. return musicalFrequencies.filter(function (freq, i) {
  770. var interval = i % 12 + 1;
  771. return semitones.some(function (allowedInterval) {
  772. return allowedInterval === interval;
  773. });
  774. });
  775. },
  776. /**
  777. * Calculate the extreme values in a chart for a data prop.
  778. * @private
  779. * @param {Highcharts.Chart} chart - The chart
  780. * @param {string} prop - The data prop to find extremes for
  781. * @return {Highcharts.RangeObject} Object with min and max properties
  782. */
  783. calculateDataExtremes: function (chart, prop) {
  784. return chart.series.reduce(function (extremes, series) {
  785. // We use cropped points rather than series.data here, to allow
  786. // users to zoom in for better fidelity.
  787. series.points.forEach(function (point) {
  788. var val = typeof point[prop] !== 'undefined' ?
  789. point[prop] : point.options[prop];
  790. extremes.min = Math.min(extremes.min, val);
  791. extremes.max = Math.max(extremes.max, val);
  792. });
  793. return extremes;
  794. }, {
  795. min: Infinity,
  796. max: -Infinity
  797. });
  798. },
  799. /**
  800. * Translate a value on a virtual axis. Creates a new, virtual, axis with a
  801. * min and max, and maps the relative value onto this axis.
  802. * @private
  803. * @param {number} value
  804. * The relative data value to translate.
  805. * @param {Highcharts.RangeObject} dataExtremes
  806. * The possible extremes for this value.
  807. * @param {object} limits
  808. * Limits for the virtual axis.
  809. * @return {number}
  810. * The value mapped to the virtual axis.
  811. */
  812. virtualAxisTranslate: function (value, dataExtremes, limits) {
  813. var lenValueAxis = dataExtremes.max - dataExtremes.min, lenVirtualAxis = limits.max - limits.min, virtualAxisValue = limits.min +
  814. lenVirtualAxis * (value - dataExtremes.min) / lenValueAxis;
  815. return lenValueAxis > 0 ?
  816. clamp(virtualAxisValue, limits.min, limits.max) :
  817. limits.min;
  818. }
  819. };
  820. return utilities;
  821. });
  822. _registerModule(_modules, 'modules/sonification/instrumentDefinitions.js', [_modules['modules/sonification/Instrument.js'], _modules['modules/sonification/utilities.js']], function (Instrument, utilities) {
  823. /* *
  824. *
  825. * (c) 2009-2019 Øystein Moseng
  826. *
  827. * Instrument definitions for sonification module.
  828. *
  829. * License: www.highcharts.com/license
  830. *
  831. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  832. *
  833. * */
  834. var instruments = {};
  835. ['sine', 'square', 'triangle', 'sawtooth'].forEach(function (waveform) {
  836. // Add basic instruments
  837. instruments[waveform] = new Instrument({
  838. oscillator: { waveformShape: waveform }
  839. });
  840. // Add musical instruments
  841. instruments[waveform + 'Musical'] = new Instrument({
  842. allowedFrequencies: utilities.musicalFrequencies,
  843. oscillator: { waveformShape: waveform }
  844. });
  845. // Add scaled instruments
  846. instruments[waveform + 'Major'] = new Instrument({
  847. allowedFrequencies: utilities.getMusicalScale([1, 3, 5, 6, 8, 10, 12]),
  848. oscillator: { waveformShape: waveform }
  849. });
  850. });
  851. return instruments;
  852. });
  853. _registerModule(_modules, 'modules/sonification/Earcon.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  854. /* *
  855. *
  856. * (c) 2009-2019 Øystein Moseng
  857. *
  858. * Earcons for the sonification module in Highcharts.
  859. *
  860. * License: www.highcharts.com/license
  861. *
  862. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  863. *
  864. * */
  865. var pick = U.pick;
  866. /**
  867. * Define an Instrument and the options for playing it.
  868. *
  869. * @requires module:modules/sonification
  870. *
  871. * @interface Highcharts.EarconInstrument
  872. */ /**
  873. * An instrument instance or the name of the instrument in the
  874. * Highcharts.sonification.instruments map.
  875. * @name Highcharts.EarconInstrument#instrument
  876. * @type {string|Highcharts.Instrument}
  877. */ /**
  878. * The options to pass to Instrument.play.
  879. * @name Highcharts.EarconInstrument#playOptions
  880. * @type {Highcharts.InstrumentPlayOptionsObject}
  881. */
  882. /**
  883. * Options for an Earcon.
  884. *
  885. * @requires module:modules/sonification
  886. *
  887. * @interface Highcharts.EarconOptionsObject
  888. */ /**
  889. * The instruments and their options defining this earcon.
  890. * @name Highcharts.EarconOptionsObject#instruments
  891. * @type {Array<Highcharts.EarconInstrument>}
  892. */ /**
  893. * The unique ID of the Earcon. Generated if not supplied.
  894. * @name Highcharts.EarconOptionsObject#id
  895. * @type {string|undefined}
  896. */ /**
  897. * Global panning of all instruments. Overrides all panning on individual
  898. * instruments. Can be a number between -1 and 1.
  899. * @name Highcharts.EarconOptionsObject#pan
  900. * @type {number|undefined}
  901. */ /**
  902. * Master volume for all instruments. Volume settings on individual instruments
  903. * can still be used for relative volume between the instruments. This setting
  904. * does not affect volumes set by functions in individual instruments. Can be a
  905. * number between 0 and 1. Defaults to 1.
  906. * @name Highcharts.EarconOptionsObject#volume
  907. * @type {number|undefined}
  908. */ /**
  909. * Callback function to call when earcon has finished playing.
  910. * @name Highcharts.EarconOptionsObject#onEnd
  911. * @type {Function|undefined}
  912. */
  913. /* eslint-disable no-invalid-this, valid-jsdoc */
  914. /**
  915. * The Earcon class. Earcon objects represent a certain sound consisting of
  916. * one or more instruments playing a predefined sound.
  917. *
  918. * @sample highcharts/sonification/earcon/
  919. * Using earcons directly
  920. *
  921. * @requires module:modules/sonification
  922. *
  923. * @class
  924. * @name Highcharts.Earcon
  925. *
  926. * @param {Highcharts.EarconOptionsObject} options
  927. * Options for the Earcon instance.
  928. */
  929. function Earcon(options) {
  930. this.init(options || {});
  931. }
  932. Earcon.prototype.init = function (options) {
  933. this.options = options;
  934. if (!this.options.id) {
  935. this.options.id = this.id = H.uniqueKey();
  936. }
  937. this.instrumentsPlaying = {};
  938. };
  939. /**
  940. * Play the earcon, optionally overriding init options.
  941. *
  942. * @sample highcharts/sonification/earcon/
  943. * Using earcons directly
  944. *
  945. * @function Highcharts.Earcon#sonify
  946. *
  947. * @param {Highcharts.EarconOptionsObject} options
  948. * Override existing options.
  949. *
  950. * @return {void}
  951. */
  952. Earcon.prototype.sonify = function (options) {
  953. var playOptions = H.merge(this.options, options);
  954. // Find master volume/pan settings
  955. var masterVolume = pick(playOptions.volume, 1), masterPan = playOptions.pan, earcon = this, playOnEnd = options && options.onEnd, masterOnEnd = earcon.options.onEnd;
  956. // Go through the instruments and play them
  957. playOptions.instruments.forEach(function (opts) {
  958. var instrument = typeof opts.instrument === 'string' ?
  959. H.sonification.instruments[opts.instrument] : opts.instrument, instrumentOpts = H.merge(opts.playOptions), instrOnEnd, instrumentCopy, copyId = '';
  960. if (instrument && instrument.play) {
  961. if (opts.playOptions) {
  962. // Handle master pan/volume
  963. if (typeof opts.playOptions.volume !== 'function') {
  964. instrumentOpts.volume = pick(masterVolume, 1) *
  965. pick(opts.playOptions.volume, 1);
  966. }
  967. instrumentOpts.pan = pick(masterPan, instrumentOpts.pan);
  968. // Handle onEnd
  969. instrOnEnd = instrumentOpts.onEnd;
  970. instrumentOpts.onEnd = function () {
  971. delete earcon.instrumentsPlaying[copyId];
  972. if (instrOnEnd) {
  973. instrOnEnd.apply(this, arguments);
  974. }
  975. if (!Object.keys(earcon.instrumentsPlaying).length) {
  976. if (playOnEnd) {
  977. playOnEnd.apply(this, arguments);
  978. }
  979. if (masterOnEnd) {
  980. masterOnEnd.apply(this, arguments);
  981. }
  982. }
  983. };
  984. // Play the instrument. Use a copy so we can play multiple at
  985. // the same time.
  986. instrumentCopy = instrument.copy();
  987. copyId = instrumentCopy.id;
  988. earcon.instrumentsPlaying[copyId] = instrumentCopy;
  989. instrumentCopy.play(instrumentOpts);
  990. }
  991. }
  992. else {
  993. H.error(30);
  994. }
  995. });
  996. };
  997. /**
  998. * Cancel any current sonification of the Earcon. Calls onEnd functions.
  999. *
  1000. * @function Highcharts.Earcon#cancelSonify
  1001. *
  1002. * @param {boolean} [fadeOut=false]
  1003. * Whether or not to fade out as we stop. If false, the earcon is
  1004. * cancelled synchronously.
  1005. *
  1006. * @return {void}
  1007. */
  1008. Earcon.prototype.cancelSonify = function (fadeOut) {
  1009. var playing = this.instrumentsPlaying, instrIds = playing && Object.keys(playing);
  1010. if (instrIds && instrIds.length) {
  1011. instrIds.forEach(function (instr) {
  1012. playing[instr].stop(!fadeOut, null, 'cancelled');
  1013. });
  1014. this.instrumentsPlaying = {};
  1015. }
  1016. };
  1017. return Earcon;
  1018. });
  1019. _registerModule(_modules, 'modules/sonification/pointSonify.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['modules/sonification/utilities.js']], function (H, U, utilities) {
  1020. /* *
  1021. *
  1022. * (c) 2009-2019 Øystein Moseng
  1023. *
  1024. * Code for sonifying single points.
  1025. *
  1026. * License: www.highcharts.com/license
  1027. *
  1028. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1029. *
  1030. * */
  1031. var pick = U.pick;
  1032. /**
  1033. * Define the parameter mapping for an instrument.
  1034. *
  1035. * @requires module:modules/sonification
  1036. *
  1037. * @interface Highcharts.PointInstrumentMappingObject
  1038. */ /**
  1039. * Define the volume of the instrument. This can be a string with a data
  1040. * property name, e.g. `'y'`, in which case this data property is used to define
  1041. * the volume relative to the `y`-values of the other points. A higher `y` value
  1042. * would then result in a higher volume. This option can also be a fixed number
  1043. * or a function. If it is a function, this function is called in regular
  1044. * intervals while the note is playing. It receives three arguments: The point,
  1045. * the dataExtremes, and the current relative time - where 0 is the beginning of
  1046. * the note and 1 is the end. The function should return the volume of the note
  1047. * as a number between 0 and 1.
  1048. * @name Highcharts.PointInstrumentMappingObject#volume
  1049. * @type {string|number|Function}
  1050. */ /**
  1051. * Define the duration of the notes for this instrument. This can be a string
  1052. * with a data property name, e.g. `'y'`, in which case this data property is
  1053. * used to define the duration relative to the `y`-values of the other points. A
  1054. * higher `y` value would then result in a longer duration. This option can also
  1055. * be a fixed number or a function. If it is a function, this function is called
  1056. * once before the note starts playing, and should return the duration in
  1057. * milliseconds. It receives two arguments: The point, and the dataExtremes.
  1058. * @name Highcharts.PointInstrumentMappingObject#duration
  1059. * @type {string|number|Function}
  1060. */ /**
  1061. * Define the panning of the instrument. This can be a string with a data
  1062. * property name, e.g. `'x'`, in which case this data property is used to define
  1063. * the panning relative to the `x`-values of the other points. A higher `x`
  1064. * value would then result in a higher panning value (panned further to the
  1065. * right). This option can also be a fixed number or a function. If it is a
  1066. * function, this function is called in regular intervals while the note is
  1067. * playing. It receives three arguments: The point, the dataExtremes, and the
  1068. * current relative time - where 0 is the beginning of the note and 1 is the
  1069. * end. The function should return the panning of the note as a number between
  1070. * -1 and 1.
  1071. * @name Highcharts.PointInstrumentMappingObject#pan
  1072. * @type {string|number|Function|undefined}
  1073. */ /**
  1074. * Define the frequency of the instrument. This can be a string with a data
  1075. * property name, e.g. `'y'`, in which case this data property is used to define
  1076. * the frequency relative to the `y`-values of the other points. A higher `y`
  1077. * value would then result in a higher frequency. This option can also be a
  1078. * fixed number or a function. If it is a function, this function is called in
  1079. * regular intervals while the note is playing. It receives three arguments:
  1080. * The point, the dataExtremes, and the current relative time - where 0 is the
  1081. * beginning of the note and 1 is the end. The function should return the
  1082. * frequency of the note as a number (in Hz).
  1083. * @name Highcharts.PointInstrumentMappingObject#frequency
  1084. * @type {string|number|Function}
  1085. */
  1086. /**
  1087. * @requires module:modules/sonification
  1088. *
  1089. * @interface Highcharts.PointInstrumentOptionsObject
  1090. */ /**
  1091. * The minimum duration for a note when using a data property for duration. Can
  1092. * be overridden by using either a fixed number or a function for
  1093. * instrumentMapping.duration. Defaults to 20.
  1094. * @name Highcharts.PointInstrumentOptionsObject#minDuration
  1095. * @type {number|undefined}
  1096. */ /**
  1097. * The maximum duration for a note when using a data property for duration. Can
  1098. * be overridden by using either a fixed number or a function for
  1099. * instrumentMapping.duration. Defaults to 2000.
  1100. * @name Highcharts.PointInstrumentOptionsObject#maxDuration
  1101. * @type {number|undefined}
  1102. */ /**
  1103. * The minimum pan value for a note when using a data property for panning. Can
  1104. * be overridden by using either a fixed number or a function for
  1105. * instrumentMapping.pan. Defaults to -1 (fully left).
  1106. * @name Highcharts.PointInstrumentOptionsObject#minPan
  1107. * @type {number|undefined}
  1108. */ /**
  1109. * The maximum pan value for a note when using a data property for panning. Can
  1110. * be overridden by using either a fixed number or a function for
  1111. * instrumentMapping.pan. Defaults to 1 (fully right).
  1112. * @name Highcharts.PointInstrumentOptionsObject#maxPan
  1113. * @type {number|undefined}
  1114. */ /**
  1115. * The minimum volume for a note when using a data property for volume. Can be
  1116. * overridden by using either a fixed number or a function for
  1117. * instrumentMapping.volume. Defaults to 0.1.
  1118. * @name Highcharts.PointInstrumentOptionsObject#minVolume
  1119. * @type {number|undefined}
  1120. */ /**
  1121. * The maximum volume for a note when using a data property for volume. Can be
  1122. * overridden by using either a fixed number or a function for
  1123. * instrumentMapping.volume. Defaults to 1.
  1124. * @name Highcharts.PointInstrumentOptionsObject#maxVolume
  1125. * @type {number|undefined}
  1126. */ /**
  1127. * The minimum frequency for a note when using a data property for frequency.
  1128. * Can be overridden by using either a fixed number or a function for
  1129. * instrumentMapping.frequency. Defaults to 220.
  1130. * @name Highcharts.PointInstrumentOptionsObject#minFrequency
  1131. * @type {number|undefined}
  1132. */ /**
  1133. * The maximum frequency for a note when using a data property for frequency.
  1134. * Can be overridden by using either a fixed number or a function for
  1135. * instrumentMapping.frequency. Defaults to 2200.
  1136. * @name Highcharts.PointInstrumentOptionsObject#maxFrequency
  1137. * @type {number|undefined}
  1138. */
  1139. /**
  1140. * An instrument definition for a point, specifying the instrument to play and
  1141. * how to play it.
  1142. *
  1143. * @interface Highcharts.PointInstrumentObject
  1144. */ /**
  1145. * An Instrument instance or the name of the instrument in the
  1146. * Highcharts.sonification.instruments map.
  1147. * @name Highcharts.PointInstrumentObject#instrument
  1148. * @type {Highcharts.Instrument|string}
  1149. */ /**
  1150. * Mapping of instrument parameters for this instrument.
  1151. * @name Highcharts.PointInstrumentObject#instrumentMapping
  1152. * @type {Highcharts.PointInstrumentMappingObject}
  1153. */ /**
  1154. * Options for this instrument.
  1155. * @name Highcharts.PointInstrumentObject#instrumentOptions
  1156. * @type {Highcharts.PointInstrumentOptionsObject|undefined}
  1157. */ /**
  1158. * Callback to call when the instrument has stopped playing.
  1159. * @name Highcharts.PointInstrumentObject#onEnd
  1160. * @type {Function|undefined}
  1161. */
  1162. /**
  1163. * Options for sonifying a point.
  1164. * @interface Highcharts.PointSonifyOptionsObject
  1165. */ /**
  1166. * The instrument definitions for this point.
  1167. * @name Highcharts.PointSonifyOptionsObject#instruments
  1168. * @type {Array<Highcharts.PointInstrumentObject>}
  1169. */ /**
  1170. * Optionally provide the minimum/maximum values for the points. If this is not
  1171. * supplied, it is calculated from the points in the chart on demand. This
  1172. * option is supplied in the following format, as a map of point data properties
  1173. * to objects with min/max values:
  1174. * ```js
  1175. * dataExtremes: {
  1176. * y: {
  1177. * min: 0,
  1178. * max: 100
  1179. * },
  1180. * z: {
  1181. * min: -10,
  1182. * max: 10
  1183. * }
  1184. * // Properties used and not provided are calculated on demand
  1185. * }
  1186. * ```
  1187. * @name Highcharts.PointSonifyOptionsObject#dataExtremes
  1188. * @type {object|undefined}
  1189. */ /**
  1190. * Callback called when the sonification has finished.
  1191. * @name Highcharts.PointSonifyOptionsObject#onEnd
  1192. * @type {Function|undefined}
  1193. */
  1194. // Defaults for the instrument options
  1195. // NOTE: Also change defaults in Highcharts.PointInstrumentOptionsObject if
  1196. // making changes here.
  1197. var defaultInstrumentOptions = {
  1198. minDuration: 20,
  1199. maxDuration: 2000,
  1200. minVolume: 0.1,
  1201. maxVolume: 1,
  1202. minPan: -1,
  1203. maxPan: 1,
  1204. minFrequency: 220,
  1205. maxFrequency: 2200
  1206. };
  1207. /* eslint-disable no-invalid-this, valid-jsdoc */
  1208. /**
  1209. * Sonify a single point.
  1210. *
  1211. * @sample highcharts/sonification/point-basic/
  1212. * Click on points to sonify
  1213. * @sample highcharts/sonification/point-advanced/
  1214. * Sonify bubbles
  1215. *
  1216. * @requires module:modules/sonification
  1217. *
  1218. * @function Highcharts.Point#sonify
  1219. *
  1220. * @param {Highcharts.PointSonifyOptionsObject} options
  1221. * Options for the sonification of the point.
  1222. *
  1223. * @return {void}
  1224. */
  1225. function pointSonify(options) {
  1226. var point = this, chart = point.series.chart, dataExtremes = options.dataExtremes || {},
  1227. // Get the value to pass to instrument.play from the mapping value
  1228. // passed in.
  1229. getMappingValue = function (value, makeFunction, allowedExtremes) {
  1230. // Function. Return new function if we try to use callback,
  1231. // otherwise call it now and return result.
  1232. if (typeof value === 'function') {
  1233. return makeFunction ?
  1234. function (time) {
  1235. return value(point, dataExtremes, time);
  1236. } :
  1237. value(point, dataExtremes);
  1238. }
  1239. // String, this is a data prop.
  1240. if (typeof value === 'string') {
  1241. // Find data extremes if we don't have them
  1242. dataExtremes[value] = dataExtremes[value] ||
  1243. utilities.calculateDataExtremes(point.series.chart, value);
  1244. // Find the value
  1245. return utilities.virtualAxisTranslate(pick(point[value], point.options[value]), dataExtremes[value], allowedExtremes);
  1246. }
  1247. // Fixed number or something else weird, just use that
  1248. return value;
  1249. };
  1250. // Register playing point on chart
  1251. chart.sonification.currentlyPlayingPoint = point;
  1252. // Keep track of instruments playing
  1253. point.sonification = point.sonification || {};
  1254. point.sonification.instrumentsPlaying =
  1255. point.sonification.instrumentsPlaying || {};
  1256. // Register signal handler for the point
  1257. var signalHandler = point.sonification.signalHandler =
  1258. point.sonification.signalHandler ||
  1259. new utilities.SignalHandler(['onEnd']);
  1260. signalHandler.clearSignalCallbacks();
  1261. signalHandler.registerSignalCallbacks({ onEnd: options.onEnd });
  1262. // If we have a null point or invisible point, just return
  1263. if (point.isNull || !point.visible || !point.series.visible) {
  1264. signalHandler.emitSignal('onEnd');
  1265. return;
  1266. }
  1267. // Go through instruments and play them
  1268. options.instruments.forEach(function (instrumentDefinition) {
  1269. var instrument = typeof instrumentDefinition.instrument === 'string' ?
  1270. H.sonification.instruments[instrumentDefinition.instrument] :
  1271. instrumentDefinition.instrument, mapping = instrumentDefinition.instrumentMapping || {}, extremes = H.merge(defaultInstrumentOptions, instrumentDefinition.instrumentOptions), id = instrument.id, onEnd = function (cancelled) {
  1272. // Instrument on end
  1273. if (instrumentDefinition.onEnd) {
  1274. instrumentDefinition.onEnd.apply(this, arguments);
  1275. }
  1276. // Remove currently playing point reference on chart
  1277. if (chart.sonification &&
  1278. chart.sonification.currentlyPlayingPoint) {
  1279. delete chart.sonification.currentlyPlayingPoint;
  1280. }
  1281. // Remove reference from instruments playing
  1282. if (point.sonification && point.sonification.instrumentsPlaying) {
  1283. delete point.sonification.instrumentsPlaying[id];
  1284. // This was the last instrument?
  1285. if (!Object.keys(point.sonification.instrumentsPlaying).length) {
  1286. signalHandler.emitSignal('onEnd', cancelled);
  1287. }
  1288. }
  1289. };
  1290. // Play the note on the instrument
  1291. if (instrument && instrument.play) {
  1292. point.sonification.instrumentsPlaying[instrument.id] =
  1293. instrument;
  1294. instrument.play({
  1295. frequency: getMappingValue(mapping.frequency, true, { min: extremes.minFrequency, max: extremes.maxFrequency }),
  1296. duration: getMappingValue(mapping.duration, false, { min: extremes.minDuration, max: extremes.maxDuration }),
  1297. pan: getMappingValue(mapping.pan, true, { min: extremes.minPan, max: extremes.maxPan }),
  1298. volume: getMappingValue(mapping.volume, true, { min: extremes.minVolume, max: extremes.maxVolume }),
  1299. onEnd: onEnd,
  1300. minFrequency: extremes.minFrequency,
  1301. maxFrequency: extremes.maxFrequency
  1302. });
  1303. }
  1304. else {
  1305. H.error(30);
  1306. }
  1307. });
  1308. }
  1309. /**
  1310. * Cancel sonification of a point. Calls onEnd functions.
  1311. *
  1312. * @requires module:modules/sonification
  1313. *
  1314. * @function Highcharts.Point#cancelSonify
  1315. *
  1316. * @param {boolean} [fadeOut=false]
  1317. * Whether or not to fade out as we stop. If false, the points are
  1318. * cancelled synchronously.
  1319. *
  1320. * @return {void}
  1321. */
  1322. function pointCancelSonify(fadeOut) {
  1323. var playing = this.sonification && this.sonification.instrumentsPlaying, instrIds = playing && Object.keys(playing);
  1324. if (instrIds && instrIds.length) {
  1325. instrIds.forEach(function (instr) {
  1326. playing[instr].stop(!fadeOut, null, 'cancelled');
  1327. });
  1328. this.sonification.instrumentsPlaying = {};
  1329. this.sonification.signalHandler.emitSignal('onEnd', 'cancelled');
  1330. }
  1331. }
  1332. var pointSonifyFunctions = {
  1333. pointSonify: pointSonify,
  1334. pointCancelSonify: pointCancelSonify
  1335. };
  1336. return pointSonifyFunctions;
  1337. });
  1338. _registerModule(_modules, 'modules/sonification/chartSonify.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['modules/sonification/utilities.js']], function (H, U, utilities) {
  1339. /* *
  1340. *
  1341. * (c) 2009-2019 Øystein Moseng
  1342. *
  1343. * Sonification functions for chart/series.
  1344. *
  1345. * License: www.highcharts.com/license
  1346. *
  1347. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1348. *
  1349. * */
  1350. /**
  1351. * An Earcon configuration, specifying an Earcon and when to play it.
  1352. *
  1353. * @requires module:modules/sonification
  1354. *
  1355. * @interface Highcharts.EarconConfiguration
  1356. */ /**
  1357. * An Earcon instance.
  1358. * @name Highcharts.EarconConfiguration#earcon
  1359. * @type {Highcharts.Earcon}
  1360. */ /**
  1361. * The ID of the point to play the Earcon on.
  1362. * @name Highcharts.EarconConfiguration#onPoint
  1363. * @type {string|undefined}
  1364. */ /**
  1365. * A function to determine whether or not to play this earcon on a point. The
  1366. * function is called for every point, receiving that point as parameter. It
  1367. * should return either a boolean indicating whether or not to play the earcon,
  1368. * or a new Earcon instance - in which case the new Earcon will be played.
  1369. * @name Highcharts.EarconConfiguration#condition
  1370. * @type {Function|undefined}
  1371. */
  1372. /**
  1373. * Options for sonifying a series.
  1374. *
  1375. * @requires module:modules/sonification
  1376. *
  1377. * @interface Highcharts.SonifySeriesOptionsObject
  1378. */ /**
  1379. * The duration for playing the points. Note that points might continue to play
  1380. * after the duration has passed, but no new points will start playing.
  1381. * @name Highcharts.SonifySeriesOptionsObject#duration
  1382. * @type {number}
  1383. */ /**
  1384. * The axis to use for when to play the points. Can be a string with a data
  1385. * property (e.g. `x`), or a function. If it is a function, this function
  1386. * receives the point as argument, and should return a numeric value. The points
  1387. * with the lowest numeric values are then played first, and the time between
  1388. * points will be proportional to the distance between the numeric values.
  1389. * @name Highcharts.SonifySeriesOptionsObject#pointPlayTime
  1390. * @type {string|Function}
  1391. */ /**
  1392. * The instrument definitions for the points in this series.
  1393. * @name Highcharts.SonifySeriesOptionsObject#instruments
  1394. * @type {Array<Highcharts.PointInstrumentObject>}
  1395. */ /**
  1396. * Earcons to add to the series.
  1397. * @name Highcharts.SonifySeriesOptionsObject#earcons
  1398. * @type {Array<Highcharts.EarconConfiguration>|undefined}
  1399. */ /**
  1400. * Optionally provide the minimum/maximum data values for the points. If this is
  1401. * not supplied, it is calculated from all points in the chart on demand. This
  1402. * option is supplied in the following format, as a map of point data properties
  1403. * to objects with min/max values:
  1404. * ```js
  1405. * dataExtremes: {
  1406. * y: {
  1407. * min: 0,
  1408. * max: 100
  1409. * },
  1410. * z: {
  1411. * min: -10,
  1412. * max: 10
  1413. * }
  1414. * // Properties used and not provided are calculated on demand
  1415. * }
  1416. * ```
  1417. * @name Highcharts.SonifySeriesOptionsObject#dataExtremes
  1418. * @type {Highcharts.Dictionary<Highcharts.RangeObject>|undefined}
  1419. */ /**
  1420. * Callback before a point is played.
  1421. * @name Highcharts.SonifySeriesOptionsObject#onPointStart
  1422. * @type {Function|undefined}
  1423. */ /**
  1424. * Callback after a point has finished playing.
  1425. * @name Highcharts.SonifySeriesOptionsObject#onPointEnd
  1426. * @type {Function|undefined}
  1427. */ /**
  1428. * Callback after the series has played.
  1429. * @name Highcharts.SonifySeriesOptionsObject#onEnd
  1430. * @type {Function|undefined}
  1431. */
  1432. var isArray = U.isArray, pick = U.pick, splat = U.splat;
  1433. /**
  1434. * Get the relative time value of a point.
  1435. * @private
  1436. * @param {Highcharts.Point} point
  1437. * The point.
  1438. * @param {Function|string} timeProp
  1439. * The time axis data prop or the time function.
  1440. * @return {number}
  1441. * The time value.
  1442. */
  1443. function getPointTimeValue(point, timeProp) {
  1444. return typeof timeProp === 'function' ?
  1445. timeProp(point) :
  1446. pick(point[timeProp], point.options[timeProp]);
  1447. }
  1448. /**
  1449. * Get the time extremes of this series. This is handled outside of the
  1450. * dataExtremes, as we always want to just sonify the visible points, and we
  1451. * always want the extremes to be the extremes of the visible points.
  1452. * @private
  1453. * @param {Highcharts.Series} series
  1454. * The series to compute on.
  1455. * @param {Function|string} timeProp
  1456. * The time axis data prop or the time function.
  1457. * @return {Highcharts.RangeObject}
  1458. * Object with min/max extremes for the time values.
  1459. */
  1460. function getTimeExtremes(series, timeProp) {
  1461. // Compute the extremes from the visible points.
  1462. return series.points.reduce(function (acc, point) {
  1463. var value = getPointTimeValue(point, timeProp);
  1464. acc.min = Math.min(acc.min, value);
  1465. acc.max = Math.max(acc.max, value);
  1466. return acc;
  1467. }, {
  1468. min: Infinity,
  1469. max: -Infinity
  1470. });
  1471. }
  1472. /**
  1473. * Calculate value extremes for used instrument data properties.
  1474. * @private
  1475. * @param {Highcharts.Chart} chart
  1476. * The chart to calculate extremes from.
  1477. * @param {Array<Highcharts.PointInstrumentObject>} instruments
  1478. * The instrument definitions used.
  1479. * @param {Highcharts.Dictionary<Highcharts.RangeObject>} [dataExtremes]
  1480. * Predefined extremes for each data prop.
  1481. * @return {Highcharts.Dictionary<Highcharts.RangeObject>}
  1482. * New extremes with data properties mapped to min/max objects.
  1483. */
  1484. function getExtremesForInstrumentProps(chart, instruments, dataExtremes) {
  1485. return (instruments || []).reduce(function (newExtremes, instrumentDefinition) {
  1486. Object.keys(instrumentDefinition.instrumentMapping || {}).forEach(function (instrumentParameter) {
  1487. var value = instrumentDefinition.instrumentMapping[instrumentParameter];
  1488. if (typeof value === 'string' && !newExtremes[value]) {
  1489. // This instrument parameter is mapped to a data prop.
  1490. // If we don't have predefined data extremes, find them.
  1491. newExtremes[value] = utilities.calculateDataExtremes(chart, value);
  1492. }
  1493. });
  1494. return newExtremes;
  1495. }, H.merge(dataExtremes));
  1496. }
  1497. /**
  1498. * Get earcons for the point if there are any.
  1499. * @private
  1500. * @param {Highcharts.Point} point
  1501. * The point to find earcons for.
  1502. * @param {Array<Highcharts.EarconConfiguration>} earconDefinitions
  1503. * Earcons to check.
  1504. * @return {Array<Highcharts.Earcon>}
  1505. * Array of earcons to be played with this point.
  1506. */
  1507. function getPointEarcons(point, earconDefinitions) {
  1508. return earconDefinitions.reduce(function (earcons, earconDefinition) {
  1509. var cond, earcon = earconDefinition.earcon;
  1510. if (earconDefinition.condition) {
  1511. // We have a condition. This overrides onPoint
  1512. cond = earconDefinition.condition(point);
  1513. if (cond instanceof H.sonification.Earcon) {
  1514. // Condition returned an earcon
  1515. earcons.push(cond);
  1516. }
  1517. else if (cond) {
  1518. // Condition returned true
  1519. earcons.push(earcon);
  1520. }
  1521. }
  1522. else if (earconDefinition.onPoint &&
  1523. point.id === earconDefinition.onPoint) {
  1524. // We have earcon onPoint
  1525. earcons.push(earcon);
  1526. }
  1527. return earcons;
  1528. }, []);
  1529. }
  1530. /**
  1531. * Utility function to get a new list of instrument options where all the
  1532. * instrument references are copies.
  1533. * @private
  1534. * @param {Array<Highcharts.PointInstrumentObject>} instruments
  1535. * The instrument options.
  1536. * @return {Array<Highcharts.PointInstrumentObject>}
  1537. * Array of copied instrument options.
  1538. */
  1539. function makeInstrumentCopies(instruments) {
  1540. return instruments.map(function (instrumentDef) {
  1541. var instrument = instrumentDef.instrument, copy = (typeof instrument === 'string' ?
  1542. H.sonification.instruments[instrument] :
  1543. instrument).copy();
  1544. return H.merge(instrumentDef, { instrument: copy });
  1545. });
  1546. }
  1547. /**
  1548. * Create a TimelinePath from a series. Takes the same options as seriesSonify.
  1549. * To intuitively allow multiple series to play simultaneously we make copies of
  1550. * the instruments for each series.
  1551. * @private
  1552. * @param {Highcharts.Series} series
  1553. * The series to build from.
  1554. * @param {Highcharts.SonifySeriesOptionsObject} options
  1555. * The options for building the TimelinePath.
  1556. * @return {Highcharts.TimelinePath}
  1557. * A timeline path with events.
  1558. */
  1559. function buildTimelinePathFromSeries(series, options) {
  1560. // options.timeExtremes is internal and used so that the calculations from
  1561. // chart.sonify can be reused.
  1562. var timeExtremes = options.timeExtremes || getTimeExtremes(series, options.pointPlayTime, options.dataExtremes),
  1563. // Get time offset for a point, relative to duration
  1564. pointToTime = function (point) {
  1565. return utilities.virtualAxisTranslate(getPointTimeValue(point, options.pointPlayTime), timeExtremes, { min: 0, max: options.duration });
  1566. },
  1567. // Compute any data extremes that aren't defined yet
  1568. dataExtremes = getExtremesForInstrumentProps(series.chart, options.instruments, options.dataExtremes),
  1569. // Make copies of the instruments used for this series, to allow
  1570. // multiple series with the same instrument to play together
  1571. instruments = makeInstrumentCopies(options.instruments),
  1572. // Go through the points, convert to events, optionally add Earcons
  1573. timelineEvents = series.points.reduce(function (events, point) {
  1574. var earcons = getPointEarcons(point, options.earcons || []), time = pointToTime(point);
  1575. return events.concat(
  1576. // Event object for point
  1577. new H.sonification.TimelineEvent({
  1578. eventObject: point,
  1579. time: time,
  1580. id: point.id,
  1581. playOptions: {
  1582. instruments: instruments,
  1583. dataExtremes: dataExtremes
  1584. }
  1585. }),
  1586. // Earcons
  1587. earcons.map(function (earcon) {
  1588. return new H.sonification.TimelineEvent({
  1589. eventObject: earcon,
  1590. time: time
  1591. });
  1592. }));
  1593. }, []);
  1594. // Build the timeline path
  1595. return new H.sonification.TimelinePath({
  1596. events: timelineEvents,
  1597. onStart: function () {
  1598. if (options.onStart) {
  1599. options.onStart(series);
  1600. }
  1601. },
  1602. onEventStart: function (event) {
  1603. var eventObject = event.options && event.options.eventObject;
  1604. if (eventObject instanceof H.Point) {
  1605. // Check for hidden series
  1606. if (!eventObject.series.visible &&
  1607. !eventObject.series.chart.series.some(function (series) {
  1608. return series.visible;
  1609. })) {
  1610. // We have no visible series, stop the path.
  1611. event.timelinePath.timeline.pause();
  1612. event.timelinePath.timeline.resetCursor();
  1613. return false;
  1614. }
  1615. // Emit onPointStart
  1616. if (options.onPointStart) {
  1617. options.onPointStart(event, eventObject);
  1618. }
  1619. }
  1620. },
  1621. onEventEnd: function (eventData) {
  1622. var eventObject = eventData.event && eventData.event.options &&
  1623. eventData.event.options.eventObject;
  1624. if (eventObject instanceof H.Point && options.onPointEnd) {
  1625. options.onPointEnd(eventData.event, eventObject);
  1626. }
  1627. },
  1628. onEnd: function () {
  1629. if (options.onEnd) {
  1630. options.onEnd(series);
  1631. }
  1632. }
  1633. });
  1634. }
  1635. /* eslint-disable no-invalid-this, valid-jsdoc */
  1636. /**
  1637. * Sonify a series.
  1638. *
  1639. * @sample highcharts/sonification/series-basic/
  1640. * Click on series to sonify
  1641. * @sample highcharts/sonification/series-earcon/
  1642. * Series with earcon
  1643. * @sample highcharts/sonification/point-play-time/
  1644. * Play y-axis by time
  1645. * @sample highcharts/sonification/earcon-on-point/
  1646. * Earcon set on point
  1647. *
  1648. * @requires module:modules/sonification
  1649. *
  1650. * @function Highcharts.Series#sonify
  1651. *
  1652. * @param {Highcharts.SonifySeriesOptionsObject} options
  1653. * The options for sonifying this series.
  1654. *
  1655. * @return {void}
  1656. */
  1657. function seriesSonify(options) {
  1658. var timelinePath = buildTimelinePathFromSeries(this, options), chartSonification = this.chart.sonification;
  1659. // Only one timeline can play at a time. If we want multiple series playing
  1660. // at the same time, use chart.sonify.
  1661. if (chartSonification.timeline) {
  1662. chartSonification.timeline.pause();
  1663. }
  1664. // Create new timeline for this series, and play it.
  1665. chartSonification.timeline = new H.sonification.Timeline({
  1666. paths: [timelinePath]
  1667. });
  1668. chartSonification.timeline.play();
  1669. }
  1670. /**
  1671. * Utility function to assemble options for creating a TimelinePath from a
  1672. * series when sonifying an entire chart.
  1673. * @private
  1674. * @param {Highcharts.Series} series
  1675. * The series to return options for.
  1676. * @param {Highcharts.RangeObject} dataExtremes
  1677. * Pre-calculated data extremes for the chart.
  1678. * @param {Highcharts.SonifyChartOptionsObject} chartSonifyOptions
  1679. * Options passed in to chart.sonify.
  1680. * @return {Partial<Highcharts.SonifySeriesOptionsObject>}
  1681. * Options for buildTimelinePathFromSeries.
  1682. */
  1683. function buildSeriesOptions(series, dataExtremes, chartSonifyOptions) {
  1684. var seriesOptions = chartSonifyOptions.seriesOptions || {};
  1685. return H.merge({
  1686. // Calculated dataExtremes for chart
  1687. dataExtremes: dataExtremes,
  1688. // We need to get timeExtremes for each series. We pass this
  1689. // in when building the TimelinePath objects to avoid
  1690. // calculating twice.
  1691. timeExtremes: getTimeExtremes(series, chartSonifyOptions.pointPlayTime),
  1692. // Some options we just pass on
  1693. instruments: chartSonifyOptions.instruments,
  1694. onStart: chartSonifyOptions.onSeriesStart,
  1695. onEnd: chartSonifyOptions.onSeriesEnd,
  1696. earcons: chartSonifyOptions.earcons
  1697. },
  1698. // Merge in the specific series options by ID
  1699. isArray(seriesOptions) ? (H.find(seriesOptions, function (optEntry) {
  1700. return optEntry.id === pick(series.id, series.options.id);
  1701. }) || {}) : seriesOptions, {
  1702. // Forced options
  1703. pointPlayTime: chartSonifyOptions.pointPlayTime
  1704. });
  1705. }
  1706. /**
  1707. * Utility function to normalize the ordering of timeline paths when sonifying
  1708. * a chart.
  1709. * @private
  1710. * @param {string|Array<string|Highcharts.Earcon|Array<string|Highcharts.Earcon>>} orderOptions -
  1711. * Order options for the sonification.
  1712. * @param {Highcharts.Chart} chart - The chart we are sonifying.
  1713. * @param {Function} seriesOptionsCallback
  1714. * A function that takes a series as argument, and returns the series options
  1715. * for that series to be used with buildTimelinePathFromSeries.
  1716. * @return {Array<object|Array<object|Highcharts.TimelinePath>>} If order is
  1717. * sequential, we return an array of objects to create series paths from. If
  1718. * order is simultaneous we return an array of an array with the same. If there
  1719. * is a custom order, we return an array of arrays of either objects (for
  1720. * series) or TimelinePaths (for earcons and delays).
  1721. */
  1722. function buildPathOrder(orderOptions, chart, seriesOptionsCallback) {
  1723. var order;
  1724. if (orderOptions === 'sequential' || orderOptions === 'simultaneous') {
  1725. // Just add the series from the chart
  1726. order = chart.series.reduce(function (seriesList, series) {
  1727. if (series.visible) {
  1728. seriesList.push({
  1729. series: series,
  1730. seriesOptions: seriesOptionsCallback(series)
  1731. });
  1732. }
  1733. return seriesList;
  1734. }, []);
  1735. // If order is simultaneous, group all series together
  1736. if (orderOptions === 'simultaneous') {
  1737. order = [order];
  1738. }
  1739. }
  1740. else {
  1741. // We have a specific order, and potentially custom items - like
  1742. // earcons or silent waits.
  1743. order = orderOptions.reduce(function (orderList, orderDef) {
  1744. // Return set of items to play simultaneously. Could be only one.
  1745. var simulItems = splat(orderDef).reduce(function (items, item) {
  1746. var itemObject;
  1747. // Is this item a series ID?
  1748. if (typeof item === 'string') {
  1749. var series = chart.get(item);
  1750. if (series.visible) {
  1751. itemObject = {
  1752. series: series,
  1753. seriesOptions: seriesOptionsCallback(series)
  1754. };
  1755. }
  1756. // Is it an earcon? If so, just create the path.
  1757. }
  1758. else if (item instanceof H.sonification.Earcon) {
  1759. // Path with a single event
  1760. itemObject = new H.sonification.TimelinePath({
  1761. events: [new H.sonification.TimelineEvent({
  1762. eventObject: item
  1763. })]
  1764. });
  1765. }
  1766. // Is this item a silent wait? If so, just create the path.
  1767. if (item.silentWait) {
  1768. itemObject = new H.sonification.TimelinePath({
  1769. silentWait: item.silentWait
  1770. });
  1771. }
  1772. // Add to items to play simultaneously
  1773. if (itemObject) {
  1774. items.push(itemObject);
  1775. }
  1776. return items;
  1777. }, []);
  1778. // Add to order list
  1779. if (simulItems.length) {
  1780. orderList.push(simulItems);
  1781. }
  1782. return orderList;
  1783. }, []);
  1784. }
  1785. return order;
  1786. }
  1787. /**
  1788. * Utility function to add a silent wait after all series.
  1789. * @private
  1790. * @param {Array<object|Array<object|TimelinePath>>} order
  1791. * The order of items.
  1792. * @param {number} wait
  1793. * The wait in milliseconds to add.
  1794. * @return {Array<object|Array<object|TimelinePath>>}
  1795. * The order with waits inserted.
  1796. */
  1797. function addAfterSeriesWaits(order, wait) {
  1798. if (!wait) {
  1799. return order;
  1800. }
  1801. return order.reduce(function (newOrder, orderDef, i) {
  1802. var simultaneousPaths = splat(orderDef);
  1803. newOrder.push(simultaneousPaths);
  1804. // Go through the simultaneous paths and see if there is a series there
  1805. if (i < order.length - 1 && // Do not add wait after last series
  1806. simultaneousPaths.some(function (item) {
  1807. return item.series;
  1808. })) {
  1809. // We have a series, meaning we should add a wait after these
  1810. // paths have finished.
  1811. newOrder.push(new H.sonification.TimelinePath({
  1812. silentWait: wait
  1813. }));
  1814. }
  1815. return newOrder;
  1816. }, []);
  1817. }
  1818. /**
  1819. * Utility function to find the total amout of wait time in the TimelinePaths.
  1820. * @private
  1821. * @param {Array<object|Array<object|TimelinePath>>} order - The order of
  1822. * TimelinePaths/items.
  1823. * @return {number} The total time in ms spent on wait paths between playing.
  1824. */
  1825. function getWaitTime(order) {
  1826. return order.reduce(function (waitTime, orderDef) {
  1827. var def = splat(orderDef);
  1828. return waitTime + (def.length === 1 &&
  1829. def[0].options &&
  1830. def[0].options.silentWait || 0);
  1831. }, 0);
  1832. }
  1833. /**
  1834. * Utility function to ensure simultaneous paths have start/end events at the
  1835. * same time, to sync them.
  1836. * @private
  1837. * @param {Array<Highcharts.TimelinePath>} paths - The paths to sync.
  1838. */
  1839. function syncSimultaneousPaths(paths) {
  1840. // Find the extremes for these paths
  1841. var extremes = paths.reduce(function (extremes, path) {
  1842. var events = path.events;
  1843. if (events && events.length) {
  1844. extremes.min = Math.min(events[0].time, extremes.min);
  1845. extremes.max = Math.max(events[events.length - 1].time, extremes.max);
  1846. }
  1847. return extremes;
  1848. }, {
  1849. min: Infinity,
  1850. max: -Infinity
  1851. });
  1852. // Go through the paths and add events to make them fit the same timespan
  1853. paths.forEach(function (path) {
  1854. var events = path.events, hasEvents = events && events.length, eventsToAdd = [];
  1855. if (!(hasEvents && events[0].time <= extremes.min)) {
  1856. eventsToAdd.push(new H.sonification.TimelineEvent({
  1857. time: extremes.min
  1858. }));
  1859. }
  1860. if (!(hasEvents && events[events.length - 1].time >= extremes.max)) {
  1861. eventsToAdd.push(new H.sonification.TimelineEvent({
  1862. time: extremes.max
  1863. }));
  1864. }
  1865. if (eventsToAdd.length) {
  1866. path.addTimelineEvents(eventsToAdd);
  1867. }
  1868. });
  1869. }
  1870. /**
  1871. * Utility function to find the total duration span for all simul path sets
  1872. * that include series.
  1873. * @private
  1874. * @param {Array<object|Array<object|Highcharts.TimelinePath>>} order - The
  1875. * order of TimelinePaths/items.
  1876. * @return {number} The total time value span difference for all series.
  1877. */
  1878. function getSimulPathDurationTotal(order) {
  1879. return order.reduce(function (durationTotal, orderDef) {
  1880. return durationTotal + splat(orderDef).reduce(function (maxPathDuration, item) {
  1881. var timeExtremes = (item.series &&
  1882. item.seriesOptions &&
  1883. item.seriesOptions.timeExtremes);
  1884. return timeExtremes ?
  1885. Math.max(maxPathDuration, timeExtremes.max - timeExtremes.min) : maxPathDuration;
  1886. }, 0);
  1887. }, 0);
  1888. }
  1889. /**
  1890. * Function to calculate the duration in ms for a series.
  1891. * @private
  1892. * @param {number} seriesValueDuration - The duration of the series in value
  1893. * difference.
  1894. * @param {number} totalValueDuration - The total duration of all (non
  1895. * simultaneous) series in value difference.
  1896. * @param {number} totalDurationMs - The desired total duration for all series
  1897. * in milliseconds.
  1898. * @return {number} The duration for the series in milliseconds.
  1899. */
  1900. function getSeriesDurationMs(seriesValueDuration, totalValueDuration, totalDurationMs) {
  1901. // A series spanning the whole chart would get the full duration.
  1902. return utilities.virtualAxisTranslate(seriesValueDuration, { min: 0, max: totalValueDuration }, { min: 0, max: totalDurationMs });
  1903. }
  1904. /**
  1905. * Convert series building objects into paths and return a new list of
  1906. * TimelinePaths.
  1907. * @private
  1908. * @param {Array<object|Array<object|Highcharts.TimelinePath>>} order - The
  1909. * order list.
  1910. * @param {number} duration - Total duration to aim for in milliseconds.
  1911. * @return {Array<Array<Highcharts.TimelinePath>>} Array of TimelinePath objects
  1912. * to play.
  1913. */
  1914. function buildPathsFromOrder(order, duration) {
  1915. // Find time used for waits (custom or after series), and subtract it from
  1916. // available duration.
  1917. var totalAvailableDurationMs = Math.max(duration - getWaitTime(order), 0),
  1918. // Add up simultaneous path durations to find total value span duration
  1919. // of everything
  1920. totalUsedDuration = getSimulPathDurationTotal(order);
  1921. // Go through the order list and convert the items
  1922. return order.reduce(function (allPaths, orderDef) {
  1923. var simultaneousPaths = splat(orderDef).reduce(function (simulPaths, item) {
  1924. if (item instanceof H.sonification.TimelinePath) {
  1925. // This item is already a path object
  1926. simulPaths.push(item);
  1927. }
  1928. else if (item.series) {
  1929. // We have a series.
  1930. // We need to set the duration of the series
  1931. item.seriesOptions.duration =
  1932. item.seriesOptions.duration || getSeriesDurationMs(item.seriesOptions.timeExtremes.max -
  1933. item.seriesOptions.timeExtremes.min, totalUsedDuration, totalAvailableDurationMs);
  1934. // Add the path
  1935. simulPaths.push(buildTimelinePathFromSeries(item.series, item.seriesOptions));
  1936. }
  1937. return simulPaths;
  1938. }, []);
  1939. // Add in the simultaneous paths
  1940. allPaths.push(simultaneousPaths);
  1941. return allPaths;
  1942. }, []);
  1943. }
  1944. /**
  1945. * Options for sonifying a chart.
  1946. *
  1947. * @requires module:modules/sonification
  1948. *
  1949. * @interface Highcharts.SonifyChartOptionsObject
  1950. */ /**
  1951. * Duration for sonifying the entire chart. The duration is distributed across
  1952. * the different series intelligently, but does not take earcons into account.
  1953. * It is also possible to set the duration explicitly per series, using
  1954. * `seriesOptions`. Note that points may continue to play after the duration has
  1955. * passed, but no new points will start playing.
  1956. * @name Highcharts.SonifyChartOptionsObject#duration
  1957. * @type {number}
  1958. */ /**
  1959. * Define the order to play the series in. This can be given as a string, or an
  1960. * array specifying a custom ordering. If given as a string, valid values are
  1961. * `sequential` - where each series is played in order - or `simultaneous`,
  1962. * where all series are played at once. For custom ordering, supply an array as
  1963. * the order. Each element in the array can be either a string with a series ID,
  1964. * an Earcon object, or an object with a numeric `silentWait` property
  1965. * designating a number of milliseconds to wait before continuing. Each element
  1966. * of the array will be played in order. To play elements simultaneously, group
  1967. * the elements in an array.
  1968. * @name Highcharts.SonifyChartOptionsObject#order
  1969. * @type {string|Array<string|Highcharts.Earcon|Array<string|Highcharts.Earcon>>}
  1970. */ /**
  1971. * The axis to use for when to play the points. Can be a string with a data
  1972. * property (e.g. `x`), or a function. If it is a function, this function
  1973. * receives the point as argument, and should return a numeric value. The points
  1974. * with the lowest numeric values are then played first, and the time between
  1975. * points will be proportional to the distance between the numeric values. This
  1976. * option can not be overridden per series.
  1977. * @name Highcharts.SonifyChartOptionsObject#pointPlayTime
  1978. * @type {string|Function}
  1979. */ /**
  1980. * Milliseconds of silent waiting to add between series. Note that waiting time
  1981. * is considered part of the sonify duration.
  1982. * @name Highcharts.SonifyChartOptionsObject#afterSeriesWait
  1983. * @type {number|undefined}
  1984. */ /**
  1985. * Options as given to `series.sonify` to override options per series. If the
  1986. * option is supplied as an array of options objects, the `id` property of the
  1987. * object should correspond to the series' id. If the option is supplied as a
  1988. * single object, the options apply to all series.
  1989. * @name Highcharts.SonifyChartOptionsObject#seriesOptions
  1990. * @type {Object|Array<object>|undefined}
  1991. */ /**
  1992. * The instrument definitions for the points in this chart.
  1993. * @name Highcharts.SonifyChartOptionsObject#instruments
  1994. * @type {Array<Highcharts.PointInstrumentObject>|undefined}
  1995. */ /**
  1996. * Earcons to add to the chart. Note that earcons can also be added per series
  1997. * using `seriesOptions`.
  1998. * @name Highcharts.SonifyChartOptionsObject#earcons
  1999. * @type {Array<Highcharts.EarconConfiguration>|undefined}
  2000. */ /**
  2001. * Optionally provide the minimum/maximum data values for the points. If this is
  2002. * not supplied, it is calculated from all points in the chart on demand. This
  2003. * option is supplied in the following format, as a map of point data properties
  2004. * to objects with min/max values:
  2005. * ```js
  2006. * dataExtremes: {
  2007. * y: {
  2008. * min: 0,
  2009. * max: 100
  2010. * },
  2011. * z: {
  2012. * min: -10,
  2013. * max: 10
  2014. * }
  2015. * // Properties used and not provided are calculated on demand
  2016. * }
  2017. * ```
  2018. * @name Highcharts.SonifyChartOptionsObject#dataExtremes
  2019. * @type {Highcharts.Dictionary<Highcharts.RangeObject>|undefined}
  2020. */ /**
  2021. * Callback before a series is played.
  2022. * @name Highcharts.SonifyChartOptionsObject#onSeriesStart
  2023. * @type {Function|undefined}
  2024. */ /**
  2025. * Callback after a series has finished playing.
  2026. * @name Highcharts.SonifyChartOptionsObject#onSeriesEnd
  2027. * @type {Function|undefined}
  2028. */ /**
  2029. * Callback after the chart has played.
  2030. * @name Highcharts.SonifyChartOptionsObject#onEnd
  2031. * @type {Function|undefined}
  2032. */
  2033. /**
  2034. * Sonify a chart.
  2035. *
  2036. * @sample highcharts/sonification/chart-sequential/
  2037. * Sonify a basic chart
  2038. * @sample highcharts/sonification/chart-simultaneous/
  2039. * Sonify series simultaneously
  2040. * @sample highcharts/sonification/chart-custom-order/
  2041. * Custom defined order of series
  2042. * @sample highcharts/sonification/chart-earcon/
  2043. * Earcons on chart
  2044. * @sample highcharts/sonification/chart-events/
  2045. * Sonification events on chart
  2046. *
  2047. * @requires module:modules/sonification
  2048. *
  2049. * @function Highcharts.Chart#sonify
  2050. *
  2051. * @param {Highcharts.SonifyChartOptionsObject} options
  2052. * The options for sonifying this chart.
  2053. *
  2054. * @return {void}
  2055. */
  2056. function chartSonify(options) {
  2057. // Only one timeline can play at a time.
  2058. if (this.sonification.timeline) {
  2059. this.sonification.timeline.pause();
  2060. }
  2061. // Calculate data extremes for the props used
  2062. var dataExtremes = getExtremesForInstrumentProps(this, options.instruments, options.dataExtremes);
  2063. // Figure out ordering of series and custom paths
  2064. var order = buildPathOrder(options.order, this, function (series) {
  2065. return buildSeriesOptions(series, dataExtremes, options);
  2066. });
  2067. // Add waits after simultaneous paths with series in them.
  2068. order = addAfterSeriesWaits(order, options.afterSeriesWait || 0);
  2069. // We now have a list of either TimelinePath objects or series that need to
  2070. // be converted to TimelinePath objects. Convert everything to paths.
  2071. var paths = buildPathsFromOrder(order, options.duration);
  2072. // Sync simultaneous paths
  2073. paths.forEach(function (simultaneousPaths) {
  2074. syncSimultaneousPaths(simultaneousPaths);
  2075. });
  2076. // We have a set of paths. Create the timeline, and play it.
  2077. this.sonification.timeline = new H.sonification.Timeline({
  2078. paths: paths,
  2079. onEnd: options.onEnd
  2080. });
  2081. this.sonification.timeline.play();
  2082. }
  2083. /**
  2084. * Get a list of the points currently under cursor.
  2085. *
  2086. * @requires module:modules/sonification
  2087. *
  2088. * @function Highcharts.Chart#getCurrentSonifyPoints
  2089. *
  2090. * @return {Array<Highcharts.Point>}
  2091. * The points currently under the cursor.
  2092. */
  2093. function getCurrentPoints() {
  2094. var cursorObj;
  2095. if (this.sonification.timeline) {
  2096. cursorObj = this.sonification.timeline.getCursor(); // Cursor per pathID
  2097. return Object.keys(cursorObj).map(function (path) {
  2098. // Get the event objects under cursor for each path
  2099. return cursorObj[path].eventObject;
  2100. }).filter(function (eventObj) {
  2101. // Return the events that are points
  2102. return eventObj instanceof H.Point;
  2103. });
  2104. }
  2105. return [];
  2106. }
  2107. /**
  2108. * Set the cursor to a point or set of points in different series.
  2109. *
  2110. * @requires module:modules/sonification
  2111. *
  2112. * @function Highcharts.Chart#setSonifyCursor
  2113. *
  2114. * @param {Highcharts.Point|Array<Highcharts.Point>} points
  2115. * The point or points to set the cursor to. If setting multiple points
  2116. * under the cursor, the points have to be in different series that are
  2117. * being played simultaneously.
  2118. */
  2119. function setCursor(points) {
  2120. var timeline = this.sonification.timeline;
  2121. if (timeline) {
  2122. splat(points).forEach(function (point) {
  2123. // We created the events with the ID of the points, which makes
  2124. // this easy. Just call setCursor for each ID.
  2125. timeline.setCursor(point.id);
  2126. });
  2127. }
  2128. }
  2129. /**
  2130. * Pause the running sonification.
  2131. *
  2132. * @requires module:modules/sonification
  2133. *
  2134. * @function Highcharts.Chart#pauseSonify
  2135. *
  2136. * @param {boolean} [fadeOut=true]
  2137. * Fade out as we pause to avoid clicks.
  2138. *
  2139. * @return {void}
  2140. */
  2141. function pause(fadeOut) {
  2142. if (this.sonification.timeline) {
  2143. this.sonification.timeline.pause(pick(fadeOut, true));
  2144. }
  2145. else if (this.sonification.currentlyPlayingPoint) {
  2146. this.sonification.currentlyPlayingPoint.cancelSonify(fadeOut);
  2147. }
  2148. }
  2149. /**
  2150. * Resume the currently running sonification. Requires series.sonify or
  2151. * chart.sonify to have been played at some point earlier.
  2152. *
  2153. * @requires module:modules/sonification
  2154. *
  2155. * @function Highcharts.Chart#resumeSonify
  2156. *
  2157. * @param {Function} onEnd
  2158. * Callback to call when play finished.
  2159. *
  2160. * @return {void}
  2161. */
  2162. function resume(onEnd) {
  2163. if (this.sonification.timeline) {
  2164. this.sonification.timeline.play(onEnd);
  2165. }
  2166. }
  2167. /**
  2168. * Play backwards from cursor. Requires series.sonify or chart.sonify to have
  2169. * been played at some point earlier.
  2170. *
  2171. * @requires module:modules/sonification
  2172. *
  2173. * @function Highcharts.Chart#rewindSonify
  2174. *
  2175. * @param {Function} onEnd
  2176. * Callback to call when play finished.
  2177. *
  2178. * @return {void}
  2179. */
  2180. function rewind(onEnd) {
  2181. if (this.sonification.timeline) {
  2182. this.sonification.timeline.rewind(onEnd);
  2183. }
  2184. }
  2185. /**
  2186. * Cancel current sonification and reset cursor.
  2187. *
  2188. * @requires module:modules/sonification
  2189. *
  2190. * @function Highcharts.Chart#cancelSonify
  2191. *
  2192. * @param {boolean} [fadeOut=true]
  2193. * Fade out as we pause to avoid clicks.
  2194. *
  2195. * @return {void}
  2196. */
  2197. function cancel(fadeOut) {
  2198. this.pauseSonify(fadeOut);
  2199. this.resetSonifyCursor();
  2200. }
  2201. /**
  2202. * Reset cursor to start. Requires series.sonify or chart.sonify to have been
  2203. * played at some point earlier.
  2204. *
  2205. * @requires module:modules/sonification
  2206. *
  2207. * @function Highcharts.Chart#resetSonifyCursor
  2208. *
  2209. * @return {void}
  2210. */
  2211. function resetCursor() {
  2212. if (this.sonification.timeline) {
  2213. this.sonification.timeline.resetCursor();
  2214. }
  2215. }
  2216. /**
  2217. * Reset cursor to end. Requires series.sonify or chart.sonify to have been
  2218. * played at some point earlier.
  2219. *
  2220. * @requires module:modules/sonification
  2221. *
  2222. * @function Highcharts.Chart#resetSonifyCursorEnd
  2223. *
  2224. * @return {void}
  2225. */
  2226. function resetCursorEnd() {
  2227. if (this.sonification.timeline) {
  2228. this.sonification.timeline.resetCursorEnd();
  2229. }
  2230. }
  2231. // Export functions
  2232. var chartSonifyFunctions = {
  2233. chartSonify: chartSonify,
  2234. seriesSonify: seriesSonify,
  2235. pause: pause,
  2236. resume: resume,
  2237. rewind: rewind,
  2238. cancel: cancel,
  2239. getCurrentPoints: getCurrentPoints,
  2240. setCursor: setCursor,
  2241. resetCursor: resetCursor,
  2242. resetCursorEnd: resetCursorEnd
  2243. };
  2244. return chartSonifyFunctions;
  2245. });
  2246. _registerModule(_modules, 'modules/sonification/Timeline.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['modules/sonification/utilities.js']], function (H, U, utilities) {
  2247. /* *
  2248. *
  2249. * (c) 2009-2019 Øystein Moseng
  2250. *
  2251. * TimelineEvent class definition.
  2252. *
  2253. * License: www.highcharts.com/license
  2254. *
  2255. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  2256. *
  2257. * */
  2258. var splat = U.splat;
  2259. /**
  2260. * A set of options for the TimelineEvent class.
  2261. *
  2262. * @requires module:modules/sonification
  2263. *
  2264. * @private
  2265. * @interface Highcharts.TimelineEventOptionsObject
  2266. */ /**
  2267. * The object we want to sonify when playing the TimelineEvent. Can be any
  2268. * object that implements the `sonify` and `cancelSonify` functions. If this is
  2269. * not supplied, the TimelineEvent is considered a silent event, and the onEnd
  2270. * event is immediately called.
  2271. * @name Highcharts.TimelineEventOptionsObject#eventObject
  2272. * @type {*}
  2273. */ /**
  2274. * Options to pass on to the eventObject when playing it.
  2275. * @name Highcharts.TimelineEventOptionsObject#playOptions
  2276. * @type {object|undefined}
  2277. */ /**
  2278. * The time at which we want this event to play (in milliseconds offset). This
  2279. * is not used for the TimelineEvent.play function, but rather intended as a
  2280. * property to decide when to call TimelineEvent.play. Defaults to 0.
  2281. * @name Highcharts.TimelineEventOptionsObject#time
  2282. * @type {number|undefined}
  2283. */ /**
  2284. * Unique ID for the event. Generated automatically if not supplied.
  2285. * @name Highcharts.TimelineEventOptionsObject#id
  2286. * @type {string|undefined}
  2287. */ /**
  2288. * Callback called when the play has finished.
  2289. * @name Highcharts.TimelineEventOptionsObject#onEnd
  2290. * @type {Function|undefined}
  2291. */
  2292. /* eslint-disable no-invalid-this, valid-jsdoc */
  2293. /**
  2294. * The TimelineEvent class. Represents a sound event on a timeline.
  2295. *
  2296. * @requires module:modules/sonification
  2297. *
  2298. * @private
  2299. * @class
  2300. * @name Highcharts.TimelineEvent
  2301. *
  2302. * @param {Highcharts.TimelineEventOptionsObject} options
  2303. * Options for the TimelineEvent.
  2304. */
  2305. function TimelineEvent(options) {
  2306. this.init(options || {});
  2307. }
  2308. TimelineEvent.prototype.init = function (options) {
  2309. this.options = options;
  2310. this.time = options.time || 0;
  2311. this.id = this.options.id = options.id || H.uniqueKey();
  2312. };
  2313. /**
  2314. * Play the event. Does not take the TimelineEvent.time option into account,
  2315. * and plays the event immediately.
  2316. *
  2317. * @function Highcharts.TimelineEvent#play
  2318. *
  2319. * @param {Highcharts.TimelineEventOptionsObject} [options]
  2320. * Options to pass in to the eventObject when playing it.
  2321. *
  2322. * @return {void}
  2323. */
  2324. TimelineEvent.prototype.play = function (options) {
  2325. var eventObject = this.options.eventObject, masterOnEnd = this.options.onEnd, playOnEnd = options && options.onEnd, playOptionsOnEnd = this.options.playOptions &&
  2326. this.options.playOptions.onEnd, playOptions = H.merge(this.options.playOptions, options);
  2327. if (eventObject && eventObject.sonify) {
  2328. // If we have multiple onEnds defined, use all
  2329. playOptions.onEnd = masterOnEnd || playOnEnd || playOptionsOnEnd ?
  2330. function () {
  2331. var args = arguments;
  2332. [masterOnEnd, playOnEnd, playOptionsOnEnd].forEach(function (onEnd) {
  2333. if (onEnd) {
  2334. onEnd.apply(this, args);
  2335. }
  2336. });
  2337. } : void 0;
  2338. eventObject.sonify(playOptions);
  2339. }
  2340. else {
  2341. if (playOnEnd) {
  2342. playOnEnd();
  2343. }
  2344. if (masterOnEnd) {
  2345. masterOnEnd();
  2346. }
  2347. }
  2348. };
  2349. /**
  2350. * Cancel the sonification of this event. Does nothing if the event is not
  2351. * currently sonifying.
  2352. *
  2353. * @function Highcharts.TimelineEvent#cancel
  2354. *
  2355. * @param {boolean} [fadeOut=false]
  2356. * Whether or not to fade out as we stop. If false, the event is
  2357. * cancelled synchronously.
  2358. */
  2359. TimelineEvent.prototype.cancel = function (fadeOut) {
  2360. this.options.eventObject.cancelSonify(fadeOut);
  2361. };
  2362. /**
  2363. * A set of options for the TimelinePath class.
  2364. *
  2365. * @requires module:modules/
  2366. *
  2367. * @private
  2368. * @interface Highcharts.TimelinePathOptionsObject
  2369. */ /**
  2370. * List of TimelineEvents to play on this track.
  2371. * @name Highcharts.TimelinePathOptionsObject#events
  2372. * @type {Array<Highcharts.TimelineEvent>}
  2373. */ /**
  2374. * If this option is supplied, this path ignores all events and just waits for
  2375. * the specified number of milliseconds before calling onEnd.
  2376. * @name Highcharts.TimelinePathOptionsObject#silentWait
  2377. * @type {number|undefined}
  2378. */ /**
  2379. * Unique ID for this timeline path. Automatically generated if not supplied.
  2380. * @name Highcharts.TimelinePathOptionsObject#id
  2381. * @type {string|undefined}
  2382. */ /**
  2383. * Callback called before the path starts playing.
  2384. * @name Highcharts.TimelinePathOptionsObject#onStart
  2385. * @type {Function|undefined}
  2386. */ /**
  2387. * Callback function to call before an event plays.
  2388. * @name Highcharts.TimelinePathOptionsObject#onEventStart
  2389. * @type {Function|undefined}
  2390. */ /**
  2391. * Callback function to call after an event has stopped playing.
  2392. * @name Highcharts.TimelinePathOptionsObject#onEventEnd
  2393. * @type {Function|undefined}
  2394. */ /**
  2395. * Callback called when the whole path is finished.
  2396. * @name Highcharts.TimelinePathOptionsObject#onEnd
  2397. * @type {Function|undefined}
  2398. */
  2399. /**
  2400. * The TimelinePath class. Represents a track on a timeline with a list of
  2401. * sound events to play at certain times relative to each other.
  2402. *
  2403. * @requires module:modules/sonification
  2404. *
  2405. * @private
  2406. * @class
  2407. * @name Highcharts.TimelinePath
  2408. *
  2409. * @param {Highcharts.TimelinePathOptionsObject} options
  2410. * Options for the TimelinePath.
  2411. */
  2412. function TimelinePath(options) {
  2413. this.init(options);
  2414. }
  2415. TimelinePath.prototype.init = function (options) {
  2416. this.options = options;
  2417. this.id = this.options.id = options.id || H.uniqueKey();
  2418. this.cursor = 0;
  2419. this.eventsPlaying = {};
  2420. // Handle silent wait, otherwise use events from options
  2421. this.events = options.silentWait ?
  2422. [
  2423. new TimelineEvent({ time: 0 }),
  2424. new TimelineEvent({ time: options.silentWait })
  2425. ] :
  2426. this.options.events;
  2427. // We need to sort our events by time
  2428. this.sortEvents();
  2429. // Get map from event ID to index
  2430. this.updateEventIdMap();
  2431. // Signal events to fire
  2432. this.signalHandler = new utilities.SignalHandler(['playOnEnd', 'masterOnEnd', 'onStart', 'onEventStart', 'onEventEnd']);
  2433. this.signalHandler.registerSignalCallbacks(H.merge(options, { masterOnEnd: options.onEnd }));
  2434. };
  2435. /**
  2436. * Sort the internal event list by time.
  2437. * @private
  2438. */
  2439. TimelinePath.prototype.sortEvents = function () {
  2440. this.events = this.events.sort(function (a, b) {
  2441. return a.time - b.time;
  2442. });
  2443. };
  2444. /**
  2445. * Update the internal eventId to index map.
  2446. * @private
  2447. */
  2448. TimelinePath.prototype.updateEventIdMap = function () {
  2449. this.eventIdMap = this.events.reduce(function (acc, cur, i) {
  2450. acc[cur.id] = i;
  2451. return acc;
  2452. }, {});
  2453. };
  2454. /**
  2455. * Add events to the path. Should not be done while the path is playing.
  2456. * The new events are inserted according to their time property.
  2457. * @private
  2458. * @param {Array<Highcharts.TimelineEvent>} newEvents - The new timeline events
  2459. * to add.
  2460. */
  2461. TimelinePath.prototype.addTimelineEvents = function (newEvents) {
  2462. this.events = this.events.concat(newEvents);
  2463. this.sortEvents(); // Sort events by time
  2464. this.updateEventIdMap(); // Update the event ID to index map
  2465. };
  2466. /**
  2467. * Get the current TimelineEvent under the cursor.
  2468. * @private
  2469. * @return {Highcharts.TimelineEvent} The current timeline event.
  2470. */
  2471. TimelinePath.prototype.getCursor = function () {
  2472. return this.events[this.cursor];
  2473. };
  2474. /**
  2475. * Set the current TimelineEvent under the cursor.
  2476. * @private
  2477. * @param {string} eventId
  2478. * The ID of the timeline event to set as current.
  2479. * @return {boolean}
  2480. * True if there is an event with this ID in the path. False otherwise.
  2481. */
  2482. TimelinePath.prototype.setCursor = function (eventId) {
  2483. var ix = this.eventIdMap[eventId];
  2484. if (typeof ix !== 'undefined') {
  2485. this.cursor = ix;
  2486. return true;
  2487. }
  2488. return false;
  2489. };
  2490. /**
  2491. * Play the timeline from the current cursor.
  2492. * @private
  2493. * @param {Function} onEnd
  2494. * Callback to call when play finished. Does not override other onEnd callbacks.
  2495. * @return {void}
  2496. */
  2497. TimelinePath.prototype.play = function (onEnd) {
  2498. this.pause();
  2499. this.signalHandler.emitSignal('onStart');
  2500. this.signalHandler.clearSignalCallbacks(['playOnEnd']);
  2501. this.signalHandler.registerSignalCallbacks({ playOnEnd: onEnd });
  2502. this.playEvents(1);
  2503. };
  2504. /**
  2505. * Play the timeline backwards from the current cursor.
  2506. * @private
  2507. * @param {Function} onEnd
  2508. * Callback to call when play finished. Does not override other onEnd callbacks.
  2509. * @return {void}
  2510. */
  2511. TimelinePath.prototype.rewind = function (onEnd) {
  2512. this.pause();
  2513. this.signalHandler.emitSignal('onStart');
  2514. this.signalHandler.clearSignalCallbacks(['playOnEnd']);
  2515. this.signalHandler.registerSignalCallbacks({ playOnEnd: onEnd });
  2516. this.playEvents(-1);
  2517. };
  2518. /**
  2519. * Reset the cursor to the beginning.
  2520. * @private
  2521. */
  2522. TimelinePath.prototype.resetCursor = function () {
  2523. this.cursor = 0;
  2524. };
  2525. /**
  2526. * Reset the cursor to the end.
  2527. * @private
  2528. */
  2529. TimelinePath.prototype.resetCursorEnd = function () {
  2530. this.cursor = this.events.length - 1;
  2531. };
  2532. /**
  2533. * Cancel current playing. Leaves the cursor intact.
  2534. * @private
  2535. * @param {boolean} [fadeOut=false] - Whether or not to fade out as we stop. If
  2536. * false, the path is cancelled synchronously.
  2537. */
  2538. TimelinePath.prototype.pause = function (fadeOut) {
  2539. var timelinePath = this;
  2540. // Cancel next scheduled play
  2541. clearTimeout(timelinePath.nextScheduledPlay);
  2542. // Cancel currently playing events
  2543. Object.keys(timelinePath.eventsPlaying).forEach(function (id) {
  2544. if (timelinePath.eventsPlaying[id]) {
  2545. timelinePath.eventsPlaying[id].cancel(fadeOut);
  2546. }
  2547. });
  2548. timelinePath.eventsPlaying = {};
  2549. };
  2550. /**
  2551. * Play the events, starting from current cursor, and going in specified
  2552. * direction.
  2553. * @private
  2554. * @param {number} direction
  2555. * The direction to play, 1 for forwards and -1 for backwards.
  2556. * @return {void}
  2557. */
  2558. TimelinePath.prototype.playEvents = function (direction) {
  2559. var timelinePath = this, curEvent = timelinePath.events[this.cursor], nextEvent = timelinePath.events[this.cursor + direction], timeDiff, onEnd = function (signalData) {
  2560. timelinePath.signalHandler.emitSignal('masterOnEnd', signalData);
  2561. timelinePath.signalHandler.emitSignal('playOnEnd', signalData);
  2562. };
  2563. // Store reference to path on event
  2564. curEvent.timelinePath = timelinePath;
  2565. // Emit event, cancel if returns false
  2566. if (timelinePath.signalHandler.emitSignal('onEventStart', curEvent) === false) {
  2567. onEnd({
  2568. event: curEvent,
  2569. cancelled: true
  2570. });
  2571. return;
  2572. }
  2573. // Play the current event
  2574. timelinePath.eventsPlaying[curEvent.id] = curEvent;
  2575. curEvent.play({
  2576. onEnd: function (cancelled) {
  2577. var signalData = {
  2578. event: curEvent,
  2579. cancelled: !!cancelled
  2580. };
  2581. // Keep track of currently playing events for cancelling
  2582. delete timelinePath.eventsPlaying[curEvent.id];
  2583. // Handle onEventEnd
  2584. timelinePath.signalHandler.emitSignal('onEventEnd', signalData);
  2585. // Reached end of path?
  2586. if (!nextEvent) {
  2587. onEnd(signalData);
  2588. }
  2589. }
  2590. });
  2591. // Schedule next
  2592. if (nextEvent) {
  2593. timeDiff = Math.abs(nextEvent.time - curEvent.time);
  2594. if (timeDiff < 1) {
  2595. // Play immediately
  2596. timelinePath.cursor += direction;
  2597. timelinePath.playEvents(direction);
  2598. }
  2599. else {
  2600. // Schedule after the difference in ms
  2601. this.nextScheduledPlay = setTimeout(function () {
  2602. timelinePath.cursor += direction;
  2603. timelinePath.playEvents(direction);
  2604. }, timeDiff);
  2605. }
  2606. }
  2607. };
  2608. /* ************************************************************************** *
  2609. * TIMELINE *
  2610. * ************************************************************************** */
  2611. /**
  2612. * A set of options for the Timeline class.
  2613. *
  2614. * @requires module:modules/sonification
  2615. *
  2616. * @private
  2617. * @interface Highcharts.TimelineOptionsObject
  2618. */ /**
  2619. * List of TimelinePaths to play. Multiple paths can be grouped together and
  2620. * played simultaneously by supplying an array of paths in place of a single
  2621. * path.
  2622. * @name Highcharts.TimelineOptionsObject#paths
  2623. * @type {Array<(Highcharts.TimelinePath|Array<Highcharts.TimelinePath>)>}
  2624. */ /**
  2625. * Callback function to call before a path plays.
  2626. * @name Highcharts.TimelineOptionsObject#onPathStart
  2627. * @type {Function|undefined}
  2628. */ /**
  2629. * Callback function to call after a path has stopped playing.
  2630. * @name Highcharts.TimelineOptionsObject#onPathEnd
  2631. * @type {Function|undefined}
  2632. */ /**
  2633. * Callback called when the whole path is finished.
  2634. * @name Highcharts.TimelineOptionsObject#onEnd
  2635. * @type {Function|undefined}
  2636. */
  2637. /**
  2638. * The Timeline class. Represents a sonification timeline with a list of
  2639. * timeline paths with events to play at certain times relative to each other.
  2640. *
  2641. * @requires module:modules/sonification
  2642. *
  2643. * @private
  2644. * @class
  2645. * @name Highcharts.Timeline
  2646. *
  2647. * @param {Highcharts.TimelineOptionsObject} options
  2648. * Options for the Timeline.
  2649. */
  2650. function Timeline(options) {
  2651. this.init(options || {});
  2652. }
  2653. Timeline.prototype.init = function (options) {
  2654. this.options = options;
  2655. this.cursor = 0;
  2656. this.paths = options.paths;
  2657. this.pathsPlaying = {};
  2658. this.signalHandler = new utilities.SignalHandler(['playOnEnd', 'masterOnEnd', 'onPathStart', 'onPathEnd']);
  2659. this.signalHandler.registerSignalCallbacks(H.merge(options, { masterOnEnd: options.onEnd }));
  2660. };
  2661. /**
  2662. * Play the timeline forwards from cursor.
  2663. * @private
  2664. * @param {Function} [onEnd]
  2665. * Callback to call when play finished. Does not override other onEnd callbacks.
  2666. * @return {void}
  2667. */
  2668. Timeline.prototype.play = function (onEnd) {
  2669. this.pause();
  2670. this.signalHandler.clearSignalCallbacks(['playOnEnd']);
  2671. this.signalHandler.registerSignalCallbacks({ playOnEnd: onEnd });
  2672. this.playPaths(1);
  2673. };
  2674. /**
  2675. * Play the timeline backwards from cursor.
  2676. * @private
  2677. * @param {Function} onEnd
  2678. * Callback to call when play finished. Does not override other onEnd callbacks.
  2679. * @return {void}
  2680. */
  2681. Timeline.prototype.rewind = function (onEnd) {
  2682. this.pause();
  2683. this.signalHandler.clearSignalCallbacks(['playOnEnd']);
  2684. this.signalHandler.registerSignalCallbacks({ playOnEnd: onEnd });
  2685. this.playPaths(-1);
  2686. };
  2687. /**
  2688. * Play the timeline in the specified direction.
  2689. * @private
  2690. * @param {number} direction
  2691. * Direction to play in. 1 for forwards, -1 for backwards.
  2692. * @return {void}
  2693. */
  2694. Timeline.prototype.playPaths = function (direction) {
  2695. var curPaths = splat(this.paths[this.cursor]), nextPaths = this.paths[this.cursor + direction], timeline = this, signalHandler = this.signalHandler, pathsEnded = 0,
  2696. // Play a path
  2697. playPath = function (path) {
  2698. // Emit signal and set playing state
  2699. signalHandler.emitSignal('onPathStart', path);
  2700. timeline.pathsPlaying[path.id] = path;
  2701. // Do the play
  2702. path[direction > 0 ? 'play' : 'rewind'](function (callbackData) {
  2703. // Play ended callback
  2704. // Data to pass to signal callbacks
  2705. var cancelled = callbackData && callbackData.cancelled, signalData = {
  2706. path: path,
  2707. cancelled: cancelled
  2708. };
  2709. // Clear state and send signal
  2710. delete timeline.pathsPlaying[path.id];
  2711. signalHandler.emitSignal('onPathEnd', signalData);
  2712. // Handle next paths
  2713. pathsEnded++;
  2714. if (pathsEnded >= curPaths.length) {
  2715. // We finished all of the current paths for cursor.
  2716. if (nextPaths && !cancelled) {
  2717. // We have more paths, move cursor along
  2718. timeline.cursor += direction;
  2719. // Reset upcoming path cursors before playing
  2720. splat(nextPaths).forEach(function (nextPath) {
  2721. nextPath[direction > 0 ? 'resetCursor' : 'resetCursorEnd']();
  2722. });
  2723. // Play next
  2724. timeline.playPaths(direction);
  2725. }
  2726. else {
  2727. // If it is the last path in this direction, call onEnd
  2728. signalHandler.emitSignal('playOnEnd', signalData);
  2729. signalHandler.emitSignal('masterOnEnd', signalData);
  2730. }
  2731. }
  2732. });
  2733. };
  2734. // Go through the paths under cursor and play them
  2735. curPaths.forEach(function (path) {
  2736. if (path) {
  2737. // Store reference to timeline
  2738. path.timeline = timeline;
  2739. // Leave a timeout to let notes fade out before next play
  2740. setTimeout(function () {
  2741. playPath(path);
  2742. }, H.sonification.fadeOutDuration);
  2743. }
  2744. });
  2745. };
  2746. /**
  2747. * Stop the playing of the timeline. Cancels all current sounds, but does not
  2748. * affect the cursor.
  2749. * @private
  2750. * @param {boolean} [fadeOut=false]
  2751. * Whether or not to fade out as we stop. If false, the timeline is cancelled
  2752. * synchronously.
  2753. * @return {void}
  2754. */
  2755. Timeline.prototype.pause = function (fadeOut) {
  2756. var timeline = this;
  2757. // Cancel currently playing events
  2758. Object.keys(timeline.pathsPlaying).forEach(function (id) {
  2759. if (timeline.pathsPlaying[id]) {
  2760. timeline.pathsPlaying[id].pause(fadeOut);
  2761. }
  2762. });
  2763. timeline.pathsPlaying = {};
  2764. };
  2765. /**
  2766. * Reset the cursor to the beginning of the timeline.
  2767. * @private
  2768. * @return {void}
  2769. */
  2770. Timeline.prototype.resetCursor = function () {
  2771. this.paths.forEach(function (paths) {
  2772. splat(paths).forEach(function (path) {
  2773. path.resetCursor();
  2774. });
  2775. });
  2776. this.cursor = 0;
  2777. };
  2778. /**
  2779. * Reset the cursor to the end of the timeline.
  2780. * @private
  2781. * @return {void}
  2782. */
  2783. Timeline.prototype.resetCursorEnd = function () {
  2784. this.paths.forEach(function (paths) {
  2785. splat(paths).forEach(function (path) {
  2786. path.resetCursorEnd();
  2787. });
  2788. });
  2789. this.cursor = this.paths.length - 1;
  2790. };
  2791. /**
  2792. * Set the current TimelineEvent under the cursor. If multiple paths are being
  2793. * played at the same time, this function only affects a single path (the one
  2794. * that contains the eventId that is passed in).
  2795. * @private
  2796. * @param {string} eventId
  2797. * The ID of the timeline event to set as current.
  2798. * @return {boolean}
  2799. * True if the cursor was set, false if no TimelineEvent was found for this ID.
  2800. */
  2801. Timeline.prototype.setCursor = function (eventId) {
  2802. return this.paths.some(function (paths) {
  2803. return splat(paths).some(function (path) {
  2804. return path.setCursor(eventId);
  2805. });
  2806. });
  2807. };
  2808. /**
  2809. * Get the current TimelineEvents under the cursors. This function will return
  2810. * the event under the cursor for each currently playing path, as an object
  2811. * where the path ID is mapped to the TimelineEvent under that path's cursor.
  2812. * @private
  2813. * @return {Highcharts.Dictionary<Highcharts.TimelineEvent>}
  2814. * The TimelineEvents under each path's cursors.
  2815. */
  2816. Timeline.prototype.getCursor = function () {
  2817. return this.getCurrentPlayingPaths().reduce(function (acc, cur) {
  2818. acc[cur.id] = cur.getCursor();
  2819. return acc;
  2820. }, {});
  2821. };
  2822. /**
  2823. * Check if timeline is reset or at start.
  2824. * @private
  2825. * @return {boolean}
  2826. * True if timeline is at the beginning.
  2827. */
  2828. Timeline.prototype.atStart = function () {
  2829. return !this.getCurrentPlayingPaths().some(function (path) {
  2830. return path.cursor;
  2831. });
  2832. };
  2833. /**
  2834. * Get the current TimelinePaths being played.
  2835. * @private
  2836. * @return {Array<Highcharts.TimelinePath>}
  2837. * The TimelinePaths currently being played.
  2838. */
  2839. Timeline.prototype.getCurrentPlayingPaths = function () {
  2840. return splat(this.paths[this.cursor]);
  2841. };
  2842. // Export the classes
  2843. var timelineClasses = {
  2844. TimelineEvent: TimelineEvent,
  2845. TimelinePath: TimelinePath,
  2846. Timeline: Timeline
  2847. };
  2848. return timelineClasses;
  2849. });
  2850. _registerModule(_modules, 'modules/sonification/sonification.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['modules/sonification/Instrument.js'], _modules['modules/sonification/instrumentDefinitions.js'], _modules['modules/sonification/Earcon.js'], _modules['modules/sonification/pointSonify.js'], _modules['modules/sonification/chartSonify.js'], _modules['modules/sonification/utilities.js'], _modules['modules/sonification/Timeline.js']], function (H, U, Instrument, instruments, Earcon, pointSonifyFunctions, chartSonifyFunctions, utilities, TimelineClasses) {
  2851. /* *
  2852. *
  2853. * (c) 2009-2019 Øystein Moseng
  2854. *
  2855. * Sonification module for Highcharts
  2856. *
  2857. * License: www.highcharts.com/license
  2858. *
  2859. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  2860. *
  2861. * */
  2862. var addEvent = H.addEvent;
  2863. var extend = U.extend;
  2864. // Expose on the Highcharts object
  2865. /**
  2866. * Global classes and objects related to sonification.
  2867. *
  2868. * @requires module:modules/sonification
  2869. *
  2870. * @name Highcharts.sonification
  2871. * @type {Highcharts.SonificationObject}
  2872. */
  2873. /**
  2874. * Global classes and objects related to sonification.
  2875. *
  2876. * @requires module:modules/sonification
  2877. *
  2878. * @interface Highcharts.SonificationObject
  2879. */ /**
  2880. * Note fade-out-time in milliseconds. Most notes are faded out quickly by
  2881. * default if there is time. This is to avoid abrupt stops which will cause
  2882. * perceived clicks.
  2883. * @name Highcharts.SonificationObject#fadeOutDuration
  2884. * @type {number}
  2885. */ /**
  2886. * Utility functions.
  2887. * @name Highcharts.SonificationObject#utilities
  2888. * @private
  2889. * @type {object}
  2890. */ /**
  2891. * The Instrument class.
  2892. * @name Highcharts.SonificationObject#Instrument
  2893. * @type {Function}
  2894. */ /**
  2895. * Predefined instruments, given as an object with a map between the instrument
  2896. * name and the Highcharts.Instrument object.
  2897. * @name Highcharts.SonificationObject#instruments
  2898. * @type {Object}
  2899. */ /**
  2900. * The Earcon class.
  2901. * @name Highcharts.SonificationObject#Earcon
  2902. * @type {Function}
  2903. */ /**
  2904. * The TimelineEvent class.
  2905. * @private
  2906. * @name Highcharts.SonificationObject#TimelineEvent
  2907. * @type {Function}
  2908. */ /**
  2909. * The TimelinePath class.
  2910. * @private
  2911. * @name Highcharts.SonificationObject#TimelinePath
  2912. * @type {Function}
  2913. */ /**
  2914. * The Timeline class.
  2915. * @private
  2916. * @name Highcharts.SonificationObject#Timeline
  2917. * @type {Function}
  2918. */
  2919. H.sonification = {
  2920. fadeOutDuration: 20,
  2921. // Classes and functions
  2922. utilities: utilities,
  2923. Instrument: Instrument,
  2924. instruments: instruments,
  2925. Earcon: Earcon,
  2926. TimelineEvent: TimelineClasses.TimelineEvent,
  2927. TimelinePath: TimelineClasses.TimelinePath,
  2928. Timeline: TimelineClasses.Timeline
  2929. };
  2930. // Chart specific
  2931. H.Point.prototype.sonify = pointSonifyFunctions.pointSonify;
  2932. H.Point.prototype.cancelSonify = pointSonifyFunctions.pointCancelSonify;
  2933. H.Series.prototype.sonify = chartSonifyFunctions.seriesSonify;
  2934. extend(H.Chart.prototype, {
  2935. sonify: chartSonifyFunctions.chartSonify,
  2936. pauseSonify: chartSonifyFunctions.pause,
  2937. resumeSonify: chartSonifyFunctions.resume,
  2938. rewindSonify: chartSonifyFunctions.rewind,
  2939. cancelSonify: chartSonifyFunctions.cancel,
  2940. getCurrentSonifyPoints: chartSonifyFunctions.getCurrentPoints,
  2941. setSonifyCursor: chartSonifyFunctions.setCursor,
  2942. resetSonifyCursor: chartSonifyFunctions.resetCursor,
  2943. resetSonifyCursorEnd: chartSonifyFunctions.resetCursorEnd
  2944. });
  2945. /* eslint-disable no-invalid-this */
  2946. // Prepare charts for sonification on init
  2947. addEvent(H.Chart, 'init', function () {
  2948. this.sonification = {};
  2949. });
  2950. });
  2951. _registerModule(_modules, 'masters/modules/sonification.src.js', [], function () {
  2952. });
  2953. }));