sonification.src.js 145 KB

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