sonification.d.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*!*
  2. *
  3. * Copyright (c) Highsoft AS. All rights reserved.
  4. *
  5. *!*/
  6. import * as globals from "../globals";
  7. import * as _Highcharts from "../highcharts";
  8. /**
  9. * Adds the module to the imported Highcharts namespace.
  10. *
  11. * @param highcharts
  12. * The imported Highcharts namespace to extend.
  13. */
  14. export function factory(highcharts: typeof Highcharts): void;
  15. declare module "../highcharts" {
  16. interface Chart {
  17. /**
  18. * Cancel current sonification and reset cursor.
  19. *
  20. * @param fadeOut
  21. * Fade out as we pause to avoid clicks.
  22. */
  23. cancelSonify(fadeOut?: boolean): void;
  24. /**
  25. * Get a list of the points currently under cursor.
  26. *
  27. * @return The points currently under the cursor.
  28. */
  29. getCurrentSonifyPoints(): Array<Point>;
  30. /**
  31. * Pause the running sonification.
  32. *
  33. * @param fadeOut
  34. * Fade out as we pause to avoid clicks.
  35. */
  36. pauseSonify(fadeOut?: boolean): void;
  37. /**
  38. * Reset cursor to start. Requires series.sonify or chart.sonify to have
  39. * been played at some point earlier.
  40. */
  41. resetSonifyCursor(): void;
  42. /**
  43. * Reset cursor to end. Requires series.sonify or chart.sonify to have
  44. * been played at some point earlier.
  45. */
  46. resetSonifyCursorEnd(): void;
  47. /**
  48. * Resume the currently running sonification. Requires series.sonify or
  49. * chart.sonify to have been played at some point earlier.
  50. *
  51. * @param onEnd
  52. * Callback to call when play finished.
  53. */
  54. resumeSonify(onEnd: Function): void;
  55. /**
  56. * Play backwards from cursor. Requires series.sonify or chart.sonify to
  57. * have been played at some point earlier.
  58. *
  59. * @param onEnd
  60. * Callback to call when play finished.
  61. */
  62. rewindSonify(onEnd: Function): void;
  63. /**
  64. * Set the cursor to a point or set of points in different series.
  65. *
  66. * @param points
  67. * The point or points to set the cursor to. If setting multiple
  68. * points under the cursor, the points have to be in different
  69. * series that are being played simultaneously.
  70. */
  71. setSonifyCursor(points: (Point|Array<Point>)): void;
  72. /**
  73. * Sonify a chart.
  74. *
  75. * @param options
  76. * The options for sonifying this chart. If not provided, uses
  77. * options set on chart and series.
  78. */
  79. sonify(options?: SonificationOptions): void;
  80. }
  81. /**
  82. * An Earcon configuration, specifying an Earcon and when to play it.
  83. */
  84. interface EarconConfiguration {
  85. /**
  86. * A function to determine whether or not to play this earcon on a
  87. * point. The function is called for every point, receiving that point
  88. * as parameter. It should return either a boolean indicating whether or
  89. * not to play the earcon, or a new Earcon instance - in which case the
  90. * new Earcon will be played.
  91. */
  92. condition?: Function;
  93. /**
  94. * An Earcon instance.
  95. */
  96. earcon: Earcon;
  97. /**
  98. * The ID of the point to play the Earcon on.
  99. */
  100. onPoint?: string;
  101. }
  102. /**
  103. * Define an Instrument and the options for playing it.
  104. */
  105. interface EarconInstrument {
  106. /**
  107. * An instrument instance or the name of the instrument in the
  108. * Highcharts.sonification.instruments map.
  109. */
  110. instrument: (string|Instrument);
  111. /**
  112. * The options to pass to Instrument.play.
  113. */
  114. playOptions: InstrumentPlayOptionsObject;
  115. }
  116. /**
  117. * Options for an Earcon.
  118. */
  119. interface EarconOptionsObject {
  120. /**
  121. * The unique ID of the Earcon. Generated if not supplied.
  122. */
  123. id?: string;
  124. /**
  125. * The instruments and their options defining this earcon.
  126. */
  127. instruments: Array<EarconInstrument>;
  128. /**
  129. * Callback function to call when earcon has finished playing.
  130. */
  131. onEnd?: Function;
  132. /**
  133. * Global panning of all instruments. Overrides all panning on
  134. * individual instruments. Can be a number between -1 and 1.
  135. */
  136. pan?: number;
  137. /**
  138. * Master volume for all instruments. Volume settings on individual
  139. * instruments can still be used for relative volume between the
  140. * instruments. This setting does not affect volumes set by functions in
  141. * individual instruments. Can be a number between 0 and 1. Defaults to
  142. * 1.
  143. */
  144. volume?: number;
  145. }
  146. /**
  147. * A set of options for the Instrument class.
  148. */
  149. interface InstrumentOptionsObject {
  150. /**
  151. * A list of allowed frequencies for this instrument. If trying to play
  152. * a frequency not on this list, the closest frequency will be used. Set
  153. * to `null` to allow all frequencies to be used. Defaults to `null`.
  154. */
  155. allowedFrequencies?: Array<number>;
  156. /**
  157. * The unique ID of the instrument. Generated if not supplied.
  158. */
  159. id?: string;
  160. /**
  161. * Options specific to oscillator instruments.
  162. */
  163. oscillator?: OscillatorOptionsObject;
  164. /**
  165. * When using functions to determine frequency or other parameters
  166. * during playback, this options specifies how often to call the
  167. * callback functions. Number given in milliseconds. Defaults to 20.
  168. */
  169. playCallbackInterval?: number;
  170. /**
  171. * The type of instrument. Currently only `oscillator` is supported.
  172. * Defaults to `oscillator`.
  173. */
  174. type?: string;
  175. }
  176. /**
  177. * Options for playing an instrument.
  178. */
  179. interface InstrumentPlayOptionsObject {
  180. /**
  181. * The duration of the note in milliseconds.
  182. */
  183. duration: number;
  184. /**
  185. * The frequency of the note to play. Can be a fixed number, or a
  186. * function. The function receives one argument: the relative time of
  187. * the note playing (0 being the start, and 1 being the end of the
  188. * note). It should return the frequency number for each point in time.
  189. * The poll interval of this function is specified by the
  190. * Instrument.playCallbackInterval option.
  191. */
  192. frequency: (number|Function);
  193. /**
  194. * The master volume multiplier to apply to the instrument, regardless
  195. * of other volume changes. Defaults to 1.
  196. */
  197. masterVolume?: number;
  198. /**
  199. * The maximum frequency to allow. If the instrument has a set of
  200. * allowed frequencies, the closest frequency is used by default. Use
  201. * this option to stop too high frequencies from being used.
  202. */
  203. maxFrequency?: number;
  204. /**
  205. * The minimum frequency to allow. If the instrument has a set of
  206. * allowed frequencies, the closest frequency is used by default. Use
  207. * this option to stop too low frequencies from being used.
  208. */
  209. minFrequency?: number;
  210. /**
  211. * Callback function to be called when the play is completed.
  212. */
  213. onEnd?: Function;
  214. /**
  215. * The panning of the instrument. Can be a fixed number between -1 and
  216. * 1, or a function. The function receives one argument: the relative
  217. * time of the note playing (0 being the start, and 1 being the end of
  218. * the note). It should return the panning value for each point in time.
  219. * The poll interval of this function is specified by the
  220. * Instrument.playCallbackInterval option. Defaults to 0.
  221. */
  222. pan?: (number|Function);
  223. /**
  224. * The volume of the instrument. Can be a fixed number between 0 and 1,
  225. * or a function. The function receives one argument: the relative time
  226. * of the note playing (0 being the start, and 1 being the end of the
  227. * note). It should return the volume for each point in time. The poll
  228. * interval of this function is specified by the
  229. * Instrument.playCallbackInterval option. Defaults to 1.
  230. */
  231. volume?: (number|Function);
  232. }
  233. interface OscillatorOptionsObject {
  234. /**
  235. * The waveform shape to use for oscillator instruments. Defaults to
  236. * `sine`.
  237. */
  238. waveformShape?: string;
  239. }
  240. interface Point {
  241. /**
  242. * Cancel sonification of a point. Calls onEnd functions.
  243. *
  244. * @param fadeOut
  245. * Whether or not to fade out as we stop. If false, the points
  246. * are cancelled synchronously.
  247. */
  248. cancelSonify(fadeOut?: boolean): void;
  249. /**
  250. * Sonify a single point.
  251. *
  252. * @param options
  253. * Options for the sonification of the point.
  254. */
  255. sonify(options: PointSonifyOptionsObject): void;
  256. }
  257. /**
  258. * Define the parameter mapping for an instrument.
  259. */
  260. interface PointInstrumentMappingObject {
  261. /**
  262. * Define the duration of the notes for this instrument. This can be a
  263. * string with a data property name, e.g. `'y'`, in which case this data
  264. * property is used to define the duration relative to the `y`-values of
  265. * the other points. A higher `y` value would then result in a longer
  266. * duration. Alternatively, `'-y'` can be used, in which case the
  267. * polarity is inverted, and a higher `y` value would result in a
  268. * shorter duration. This option can also be a fixed number or a
  269. * function. If it is a function, this function is called once before
  270. * the note starts playing, and should return the duration in
  271. * milliseconds. It receives two arguments: The point, and the
  272. * dataExtremes.
  273. */
  274. duration: (number|string|Function);
  275. /**
  276. * Define the frequency of the instrument. This can be a string with a
  277. * data property name, e.g. `'y'`, in which case this data property is
  278. * used to define the frequency relative to the `y`-values of the other
  279. * points. A higher `y` value would then result in a higher frequency.
  280. * Alternatively, `'-y'` can be used, in which case the polarity is
  281. * inverted, and a higher `y` value would result in a lower frequency.
  282. * This option can also be a fixed number or a function. If it is a
  283. * function, this function is called in regular intervals while the note
  284. * is playing. It receives three arguments: The point, the dataExtremes,
  285. * and the current relative time - where 0 is the beginning of the note
  286. * and 1 is the end. The function should return the frequency of the
  287. * note as a number (in Hz).
  288. */
  289. frequency: (number|string|Function);
  290. /**
  291. * Define the panning of the instrument. This can be a string with a
  292. * data property name, e.g. `'x'`, in which case this data property is
  293. * used to define the panning relative to the `x`-values of the other
  294. * points. A higher `x` value would then result in a higher panning
  295. * value (panned further to the right). Alternatively, `'-x'` can be
  296. * used, in which case the polarity is inverted, and a higher `x` value
  297. * would result in a lower panning value (panned further to the left).
  298. * This option can also be a fixed number or a function. If it is a
  299. * function, this function is called in regular intervals while the note
  300. * is playing. It receives three arguments: The point, the dataExtremes,
  301. * and the current relative time - where 0 is the beginning of the note
  302. * and 1 is the end. The function should return the panning of the note
  303. * as a number between -1 and 1.
  304. */
  305. pan?: (number|string|Function);
  306. /**
  307. * Define the volume of the instrument. This can be a string with a data
  308. * property name, e.g. `'y'`, in which case this data property is used
  309. * to define the volume relative to the `y`-values of the other points.
  310. * A higher `y` value would then result in a higher volume.
  311. * Alternatively, `'-y'` can be used, which inverts the polarity, so
  312. * that a higher `y` value results in a lower volume. This option can
  313. * also be a fixed number or a function. If it is a function, this
  314. * function is called in regular intervals while the note is playing. It
  315. * receives three arguments: The point, the dataExtremes, and the
  316. * current relative time - where 0 is the beginning of the note and 1 is
  317. * the end. The function should return the volume of the note as a
  318. * number between 0 and 1.
  319. */
  320. volume: (number|string|Function);
  321. }
  322. /**
  323. * An instrument definition for a point, specifying the instrument to play
  324. * and how to play it.
  325. */
  326. interface PointInstrumentObject {
  327. /**
  328. * An Instrument instance or the name of the instrument in the
  329. * Highcharts.sonification.instruments map.
  330. */
  331. instrument: (string|Instrument);
  332. /**
  333. * Mapping of instrument parameters for this instrument.
  334. */
  335. instrumentMapping: PointInstrumentMappingObject;
  336. /**
  337. * Options for this instrument.
  338. */
  339. instrumentOptions?: PointInstrumentOptionsObject;
  340. /**
  341. * Callback to call when the instrument has stopped playing.
  342. */
  343. onEnd?: Function;
  344. }
  345. interface PointInstrumentOptionsObject {
  346. /**
  347. * The maximum duration for a note when using a data property for
  348. * duration. Can be overridden by using either a fixed number or a
  349. * function for instrumentMapping.duration. Defaults to 2000.
  350. */
  351. maxDuration?: number;
  352. /**
  353. * The maximum frequency for a note when using a data property for
  354. * frequency. Can be overridden by using either a fixed number or a
  355. * function for instrumentMapping.frequency. Defaults to 2200.
  356. */
  357. maxFrequency?: number;
  358. /**
  359. * The maximum pan value for a note when using a data property for
  360. * panning. Can be overridden by using either a fixed number or a
  361. * function for instrumentMapping.pan. Defaults to 1 (fully right).
  362. */
  363. maxPan?: number;
  364. /**
  365. * The maximum volume for a note when using a data property for volume.
  366. * Can be overridden by using either a fixed number or a function for
  367. * instrumentMapping.volume. Defaults to 1.
  368. */
  369. maxVolume?: number;
  370. /**
  371. * The minimum duration for a note when using a data property for
  372. * duration. Can be overridden by using either a fixed number or a
  373. * function for instrumentMapping.duration. Defaults to 20.
  374. */
  375. minDuration?: number;
  376. /**
  377. * The minimum frequency for a note when using a data property for
  378. * frequency. Can be overridden by using either a fixed number or a
  379. * function for instrumentMapping.frequency. Defaults to 220.
  380. */
  381. minFrequency?: number;
  382. /**
  383. * The minimum pan value for a note when using a data property for
  384. * panning. Can be overridden by using either a fixed number or a
  385. * function for instrumentMapping.pan. Defaults to -1 (fully left).
  386. */
  387. minPan?: number;
  388. /**
  389. * The minimum volume for a note when using a data property for volume.
  390. * Can be overridden by using either a fixed number or a function for
  391. * instrumentMapping.volume. Defaults to 0.1.
  392. */
  393. minVolume?: number;
  394. }
  395. /**
  396. * Options for sonifying a point.
  397. */
  398. interface PointSonifyOptionsObject {
  399. /**
  400. * Optionally provide the minimum/maximum values for the points. If this
  401. * is not supplied, it is calculated from the points in the chart on
  402. * demand. This option is supplied in the following format, as a map of
  403. * point data properties to objects with min/max values: (see online
  404. * documentation for example)
  405. */
  406. dataExtremes?: object;
  407. /**
  408. * The instrument definitions for this point.
  409. */
  410. instruments: Array<PointInstrumentObject>;
  411. /**
  412. * Callback called when the sonification has finished.
  413. */
  414. onEnd?: Function;
  415. }
  416. interface Series {
  417. /**
  418. * Sonify a series.
  419. *
  420. * @param options
  421. * The options for sonifying this series. If not provided, uses
  422. * options set on chart and series.
  423. */
  424. sonify(options?: SonifySeriesOptionsObject): void;
  425. }
  426. /**
  427. * Global classes and objects related to sonification.
  428. */
  429. interface SonificationObject {
  430. /**
  431. * The Earcon class.
  432. */
  433. Earcon: Function;
  434. /**
  435. * Note fade-out-time in milliseconds. Most notes are faded out quickly
  436. * by default if there is time. This is to avoid abrupt stops which will
  437. * cause perceived clicks.
  438. */
  439. fadeOutDuration: number;
  440. /**
  441. * The Instrument class.
  442. */
  443. Instrument: Function;
  444. /**
  445. * Predefined instruments, given as an object with a map between the
  446. * instrument name and the Highcharts.Instrument object.
  447. */
  448. instruments: object;
  449. }
  450. /**
  451. * Options for sonifying a chart.
  452. */
  453. interface SonificationOptions {
  454. /**
  455. * Milliseconds of silent waiting to add between series. Note that
  456. * waiting time is considered part of the sonify duration.
  457. */
  458. afterSeriesWait?: number;
  459. /**
  460. * Optionally provide the minimum/maximum data values for the points. If
  461. * this is not supplied, it is calculated from all points in the chart
  462. * on demand. This option is supplied in the following format, as a map
  463. * of point data properties to objects with min/max values: (see online
  464. * documentation for example)
  465. */
  466. dataExtremes?: Dictionary<RangeObject>;
  467. /**
  468. * Duration for sonifying the entire chart. The duration is distributed
  469. * across the different series intelligently, but does not take earcons
  470. * into account. It is also possible to set the duration explicitly per
  471. * series, using `seriesOptions`. Note that points may continue to play
  472. * after the duration has passed, but no new points will start playing.
  473. */
  474. duration: number;
  475. /**
  476. * Earcons to add to the chart. Note that earcons can also be added per
  477. * series using `seriesOptions`.
  478. */
  479. earcons?: Array<EarconConfiguration>;
  480. /**
  481. * The instrument definitions for the points in this chart.
  482. */
  483. instruments?: Array<PointInstrumentObject>;
  484. /**
  485. * Callback after the chart has played.
  486. */
  487. onEnd?: Function;
  488. /**
  489. * Callback after a series has finished playing.
  490. */
  491. onSeriesEnd?: Function;
  492. /**
  493. * Callback before a series is played.
  494. */
  495. onSeriesStart?: Function;
  496. /**
  497. * Define the order to play the series in. This can be given as a
  498. * string, or an array specifying a custom ordering. If given as a
  499. * string, valid values are `sequential` - where each series is played
  500. * in order - or `simultaneous`, where all series are played at once.
  501. * For custom ordering, supply an array as the order. Each element in
  502. * the array can be either a string with a series ID, an Earcon object,
  503. * or an object with a numeric `silentWait` property designating a
  504. * number of milliseconds to wait before continuing. Each element of the
  505. * array will be played in order. To play elements simultaneously, group
  506. * the elements in an array.
  507. */
  508. order: (string|Array<(string|Earcon|Array<(string|Earcon)>)>);
  509. /**
  510. * The axis to use for when to play the points. Can be a string with a
  511. * data property (e.g. `x`), or a function. If it is a function, this
  512. * function receives the point as argument, and should return a numeric
  513. * value. The points with the lowest numeric values are then played
  514. * first, and the time between points will be proportional to the
  515. * distance between the numeric values. This option can not be
  516. * overridden per series.
  517. */
  518. pointPlayTime: (string|Function);
  519. /**
  520. * Options as given to `series.sonify` to override options per series.
  521. * If the option is supplied as an array of options objects, the `id`
  522. * property of the object should correspond to the series' id. If the
  523. * option is supplied as a single object, the options apply to all
  524. * series.
  525. */
  526. seriesOptions?: (object|Array<object>);
  527. }
  528. /**
  529. * Options for sonifying a series.
  530. */
  531. interface SonifySeriesOptionsObject {
  532. /**
  533. * Optionally provide the minimum/maximum data values for the points. If
  534. * this is not supplied, it is calculated from all points in the chart
  535. * on demand. This option is supplied in the following format, as a map
  536. * of point data properties to objects with min/max values: (see online
  537. * documentation for example)
  538. */
  539. dataExtremes?: Dictionary<RangeObject>;
  540. /**
  541. * The duration for playing the points. Note that points might continue
  542. * to play after the duration has passed, but no new points will start
  543. * playing.
  544. */
  545. duration: number;
  546. /**
  547. * Earcons to add to the series.
  548. */
  549. earcons?: Array<EarconConfiguration>;
  550. /**
  551. * The instrument definitions for the points in this series.
  552. */
  553. instruments: Array<PointInstrumentObject>;
  554. /**
  555. * Callback after the series has played.
  556. */
  557. onEnd?: Function;
  558. /**
  559. * Callback after a point has finished playing.
  560. */
  561. onPointEnd?: Function;
  562. /**
  563. * Callback before a point is played.
  564. */
  565. onPointStart?: Function;
  566. /**
  567. * The axis to use for when to play the points. Can be a string with a
  568. * data property (e.g. `x`), or a function. If it is a function, this
  569. * function receives the point as argument, and should return a numeric
  570. * value. The points with the lowest numeric values are then played
  571. * first, and the time between points will be proportional to the
  572. * distance between the numeric values.
  573. */
  574. pointPlayTime: (string|Function);
  575. }
  576. /**
  577. * The Earcon class. Earcon objects represent a certain sound consisting of
  578. * one or more instruments playing a predefined sound.
  579. */
  580. class Earcon {
  581. /**
  582. * The Earcon class. Earcon objects represent a certain sound consisting
  583. * of one or more instruments playing a predefined sound.
  584. *
  585. * @param options
  586. * Options for the Earcon instance.
  587. */
  588. constructor(options: EarconOptionsObject);
  589. /**
  590. * Cancel any current sonification of the Earcon. Calls onEnd functions.
  591. *
  592. * @param fadeOut
  593. * Whether or not to fade out as we stop. If false, the earcon is
  594. * cancelled synchronously.
  595. */
  596. cancelSonify(fadeOut?: boolean): void;
  597. /**
  598. * Play the earcon, optionally overriding init options.
  599. *
  600. * @param options
  601. * Override existing options.
  602. */
  603. sonify(options: EarconOptionsObject): void;
  604. }
  605. /**
  606. * The Instrument class. Instrument objects represent an instrument capable
  607. * of playing a certain pitch for a specified duration.
  608. */
  609. class Instrument {
  610. /**
  611. * The Instrument class. Instrument objects represent an instrument
  612. * capable of playing a certain pitch for a specified duration.
  613. *
  614. * @param options
  615. * Options for the instrument instance.
  616. */
  617. constructor(options: InstrumentOptionsObject);
  618. /**
  619. * Return a copy of an instrument. Only one instrument instance can play
  620. * at a time, so use this to get a new copy of the instrument that can
  621. * play alongside it. The new instrument copy will receive a new ID
  622. * unless one is supplied in options.
  623. *
  624. * @param options
  625. * Options to merge in for the copy.
  626. *
  627. * @return A new Instrument instance with the same options.
  628. */
  629. copy(options?: InstrumentOptionsObject): Instrument;
  630. /**
  631. * Mute an instrument that is playing. If the instrument is not
  632. * currently playing, this function does nothing.
  633. */
  634. mute(): void;
  635. /**
  636. * Play the instrument according to options.
  637. *
  638. * @param options
  639. * Options for the playback of the instrument.
  640. */
  641. play(options: InstrumentPlayOptionsObject): void;
  642. /**
  643. * Stop the instrument playing.
  644. *
  645. * @param immediately
  646. * Whether to do the stop immediately or fade out.
  647. *
  648. * @param onStopped
  649. * Callback function to be called when the stop is completed.
  650. *
  651. * @param callbackData
  652. * Data to send to the onEnd callback functions.
  653. */
  654. stop(immediately: boolean, onStopped?: Function, callbackData?: any): void;
  655. }
  656. /**
  657. * Global classes and objects related to sonification.
  658. */
  659. let sonification: SonificationObject;
  660. }
  661. export default factory;
  662. export let Highcharts: typeof _Highcharts;