sonification.src.js 144 KB

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