sonification.src.js 145 KB

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