audiomotion-analyzer 4.1.1 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -137,6 +137,7 @@ options = {<br>
137
137
  &emsp;&emsp;[loRes](#lores-boolean): **false**,<br>
138
138
  &emsp;&emsp;[lumiBars](#lumibars-boolean): **false**,<br>
139
139
  &emsp;&emsp;[maxDecibels](#maxdecibels-number): **-25**,<br>
140
+ &emsp;&emsp;[maxFPS](#maxfps-number): **0**,<br>
140
141
  &emsp;&emsp;[maxFreq](#maxfreq-number): **22000**,<br>
141
142
  &emsp;&emsp;[minDecibels](#mindecibels-number): **-85**,<br>
142
143
  &emsp;&emsp;[minFreq](#minfreq-number): **20**,<br>
@@ -147,6 +148,7 @@ options = {<br>
147
148
  &emsp;&emsp;[onCanvasResize](#oncanvasresize-function): *undefined*,<br>
148
149
  &emsp;&emsp;[outlineBars](#outlinebars-boolean): **false**,<br>
149
150
  &emsp;&emsp;[overlay](#overlay-boolean): **false**,<br>
151
+ &emsp;&emsp;[peakLine](#peakline-boolean): **false**,<br>
150
152
  &emsp;&emsp;[radial](#radial-boolean): **false**,<br>
151
153
  &emsp;&emsp;[reflexAlpha](#reflexalpha-number): **0.15**,<br>
152
154
  &emsp;&emsp;[reflexBright](#reflexbright-number): **1**,<br>
@@ -219,14 +221,14 @@ After instantiation, [`fsElement`](#fselement-htmlelement-object-read-only) is a
219
221
 
220
222
  #### `source` *HTMLMediaElement or AudioNode object*
221
223
 
222
- If `source` is specified, connects an [*HTMLMediaElement*](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement) object (an `<audio>` or `<video>` HTML tag)
223
- or any instance of [*AudioNode*](https://developer.mozilla.org/en-US/docs/Web/API/AudioNode) to the analyzer.
224
+ If `source` is specified, connects an [*HTMLMediaElement*](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement) (`<audio>` or `<video>` HTML element)
225
+ or [*AudioNode*](https://developer.mozilla.org/en-US/docs/Web/API/AudioNode) object to the analyzer.
224
226
 
225
227
  At least one audio source is required for the analyzer to work. You can also connect audio sources after instantiation, using the [`connectInput()`](#connectinput-source-) method.
226
228
 
227
229
  #### `start` *boolean*
228
230
 
229
- If `start: false` is specified, the analyzer will be created stopped. You can then start it with the [`toggleAnalyzer()`](#toggleanalyzer-boolean-) method.
231
+ If `start: false` is specified, the analyzer will be created stopped. You can then start it with the [`start()`](#start) or [`toggleAnalyzer()`](#toggleanalyzer-boolean-) methods.
230
232
 
231
233
  Defaults to **true**, so the analyzer will start running right after initialization.
232
234
 
@@ -331,11 +333,12 @@ Defaults to **0.7**.
331
333
 
332
334
  Defines the number and layout of analyzer channels.
333
335
 
334
- channelLayout | Description
335
- ----------------|------------
336
- 'single' | Single channel analyzer, representing the combined output of both left and right channels.
337
- 'dual-combined' | Dual channel analyzer, with both channel graphs overlaid. Works best with semi-transparent **Graph** [`mode`](#mode-number) or [`outlineBars`](#outlinebars-boolean).
338
- 'dual-vertical' | Left channel shown at the top half of the canvas and right channel at the bottom.
336
+ channelLayout | Description | Note
337
+ ------------------|-------------|------
338
+ 'single' | Single channel analyzer, representing the combined output of both left and right channels.
339
+ 'dual-combined' | Dual channel analyzer, both channels overlaid. Works best with semi-transparent **Graph** [`mode`](#mode-number) or [`outlineBars`](#outlinebars-boolean).
340
+ 'dual-horizontal' | Dual channel, side by side - see [`mirror`](#mirror-number) for additional layout options. | *since v4.3.0*
341
+ 'dual-vertical' | Dual channel, left channel at the top half of the canvas and right channel at the bottom.
339
342
 
340
343
  !> When a *dual* layout is selected, any mono (single channel) audio source connected to the analyzer will output sound only from the left speaker,
341
344
  unless a stereo source is simultaneously connected to the analyzer, which will force the mono input to be upmixed to stereo.
@@ -500,6 +503,12 @@ or one of the frequency bands modes, in which case [`lumiBars`](#lumibars-boolea
500
503
 
501
504
  See also [`isOctaveBands`](#isoctavebands-boolean-read-only).
502
505
 
506
+ ### `isDestroyed` *boolean* *(Read only)*
507
+
508
+ *Available since v4.2.0*
509
+
510
+ ***true*** when the object has been destroyed with [`destroy()`](#destroy).
511
+
503
512
  ### `isFullscreen` *boolean* *(Read only)*
504
513
 
505
514
  ***true*** when the analyzer is being displayed in fullscreen, or ***false*** otherwise.
@@ -508,7 +517,7 @@ See [`toggleFullscreen()`](#togglefullscreen).
508
517
 
509
518
  ### `isLedBars` *boolean* *(Read only)*
510
519
 
511
- *Available since v3.6.0* (formerly `isLedDisplay`)
520
+ *Available since v3.6.0; formerly `isLedDisplay` (since v3.0.0)*
512
521
 
513
522
  ***true*** when LED bars are effectively being displayed, i.e., [`isBandsMode`](#isbandsmode-boolean-read-only) is *true*, [`ledBars`](#ledBars-boolean) is set to *true* and [`radial`](#radial-boolean) is set to *false*.
514
523
 
@@ -528,7 +537,7 @@ See [`toggleFullscreen()`](#togglefullscreen).
528
537
 
529
538
  ***true*** if the analyzer process is running, or *false* if it's stopped.
530
539
 
531
- See [`toggleAnalyzer()`](#toggleanalyzer-boolean-).
540
+ See [`start()`](#start), [`stop()`](#stop) and [`toggleAnalyzer()`](#toggleanalyzer-boolean-).
532
541
 
533
542
  ### `isOutlineBars` *boolean* *(Read only)*
534
543
 
@@ -546,14 +555,16 @@ and [`ledBars`](#ledbars-boolean) and [`lumiBars`](#lumibars-boolean) are both s
546
555
 
547
556
  ### `ledBars` *boolean*
548
557
 
549
- *Available since v3.6.0* (formerly `showLeds`)
558
+ *Available since v3.6.0; formerly `showLeds` (since v1.0.0)*
550
559
 
551
- *true* to activate the vintage LED bars effect for frequency bands modes (see [`mode`](#mode-number)).
560
+ *true* to activate the LED bars effect for frequency bands modes (see [`mode`](#mode-number)).
552
561
 
553
562
  This effect can be customized via [`setLedParams()`](#setledparams-params-) method.
554
563
 
555
564
  For effect priority when combined with other settings, see [`isLedBars`](#isledbars-boolean-read-only).
556
565
 
566
+ See also [`trueLeds`](#trueleds-boolean).
567
+
557
568
  Defaults to **false**.
558
569
 
559
570
  ### `linearAmplitude` *boolean*
@@ -630,6 +641,16 @@ For more info, see [AnalyserNode.minDecibels](https://developer.mozilla.org/en-U
630
641
 
631
642
  *minDecibels* defaults to **-85** and *maxDecibels* defaults to **-25**.
632
643
 
644
+ ### `maxFPS` *number*
645
+
646
+ *Available since v4.2.0*
647
+
648
+ Sets the maximum desired animation frame rate. This can help reducing CPU usage, especially on high refresh rate monitors.
649
+
650
+ It must be a number, indicating frames per second. A value of **0** means the animation will run at the highest frame rate possible.
651
+
652
+ Defaults to **0**.
653
+
633
654
  ### `maxFreq` *number*
634
655
  ### `minFreq` *number*
635
656
 
@@ -648,15 +669,19 @@ It is preferable to use the [`setFreqRange()`](#setfreqrange-minfreq-maxfreq-) m
648
669
 
649
670
  *Available since v3.3.0*
650
671
 
651
- Horizontal mirroring effect. Valid values are:
672
+ When [`channelLayout`](#channellayout-string) is **dual-horizontal**, this property controls the orientation of the X-axis (frequencies) on both channels.
673
+
674
+ For other layouts, it horizontally mirrors the spectrum image to the left or right side.
652
675
 
653
- mirror | Effect
654
- :-----:|--------
655
- -1 | Mirrors the analyzer to the left (low frequencies at the center of the screen)
656
- 0 | Disables mirror effect (default)
657
- 1 | Mirrors the analyzer to the right (high frequencies at the center of the screen)
676
+ Valid values are:
658
677
 
659
- **Note:** when [`radial`](#radial-boolean) is **_true_**, both `1` and `-1` will produce the same effect.
678
+ mirror | Description
679
+ :-----:|-------------
680
+ -1 | Low frequencies meet at the center of the screen (mirror left)
681
+ 0 | No mirror effect or change to axis orientation (default)
682
+ 1 | High frequencies meet at the center of the screen (mirror right)
683
+
684
+ **Note:** On [`radial`](#radial-boolean) spectrum with channel layouts other than *dual-horizontal*, both `1` and `-1` have the same effect.
660
685
 
661
686
  Defaults to **0**.
662
687
 
@@ -676,7 +701,7 @@ mode | description | notes
676
701
  7 | Half octave bands or 20 bands | *use 'log' `frequencyScale` for octave bands*
677
702
  8 | Full octave bands or 10 bands | *use 'log' `frequencyScale` for octave bands*
678
703
  9 | *(not valid)* | *reserved*
679
- 10 | Graph | *added in v1.1.0*
704
+ 10 | Graph | *since v1.1.0*
680
705
 
681
706
  + **Mode 0** provides the highest resolution, allowing you to visualize individual frequencies as provided by the [FFT](https://en.wikipedia.org/wiki/Fast_Fourier_transform) computation;
682
707
  + **Modes 1 - 8** divide the frequency spectrum in bands; when using the default **logarithmic** [`frequencyScale`](#frequencyscale-string), each band represents the *n*th part of an octave; otherwise, a fixed number of bands is used for each mode;
@@ -719,6 +744,14 @@ Defaults to **false**.
719
744
 
720
745
  ?> In order to keep elements other than the canvas visible in fullscreen, you'll need to set the [`fsElement`](#fselement-htmlelement-object) property in the [constructor](#constructor) options.
721
746
 
747
+ ### `peakLine` *boolean*
748
+
749
+ *Available since v4.2.0*
750
+
751
+ When *true* and [`mode`](#mode-number) is *10* (**Graph**) and [`showPeaks`](#showpeaks-boolean) is *true*, peaks are connected into a continuous line. It has no effect in other modes.
752
+
753
+ Defaults to **false**.
754
+
722
755
  ### `pixelRatio` *number* *(Read only)*
723
756
 
724
757
  Current [devicePixelRatio](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio).
@@ -734,9 +767,9 @@ You can refer to this value to adjust any additional drawings done in the canvas
734
767
 
735
768
  When *true*, the spectrum analyzer is rendered in a circular shape, with radial frequency bars spreading from its center.
736
769
 
737
- In radial view, [`ledBars`](#ledbars-boolean) and [`lumiBars`](#lumibars-boolean) effects are disabled, and [`showPeaks`](#showpeaks-boolean) has no effect when in **Graph** [`mode`](#mode-number).
770
+ In radial view, [`ledBars`](#ledbars-boolean) and [`lumiBars`](#lumibars-boolean) effects are disabled.
738
771
 
739
- When [`channelLayout`](#channellayout-string) is set to *'dual-vertical'*, a larger diameter is used and the right channel bars are rendered towards the center of the analyzer.
772
+ When [`channelLayout`](#channellayout-string) is set to *'dual-vertical'*, graphs for the right channel are rendered towards the center of the screen.
740
773
 
741
774
  See also [`spinSpeed`](#spinspeed-number).
742
775
 
@@ -825,11 +858,13 @@ and setting `showBgColor` to ***true*** will make the "unlit" LEDs visible inste
825
858
 
826
859
  ### `showPeaks` *boolean*
827
860
 
828
- *true* to show amplitude peaks for each frequency. Defaults to **true**.
861
+ *true* to show amplitude peaks. Defaults to **true**.
862
+
863
+ See also [`peakLine`](#peakline-boolean).
829
864
 
830
865
  ### `showScaleX` *boolean*
831
866
 
832
- *Available since v3.0.0 - this property was named `showScale` in earlier versions*
867
+ *Available since v3.0.0; formerly `showScale` (since v1.0.0)*
833
868
 
834
869
  *true* to display scale labels on the X axis.
835
870
 
@@ -918,7 +953,7 @@ Setting it to *false* in the [**constructor**](#constructor) options also preven
918
953
  Please note that the analyzer processing runs regardless of the value of `useCanvas` and any callback defined for [`onCanvasDraw`](#oncanvasdraw-function)
919
954
  will still be triggered on every animation frame, so you can use the [`getBars()`](#getbars) method to create your own visualizations.
920
955
 
921
- If you want to completely stop the audio data processing, see [`toggleAnalyzer()`](#toggleanalyzer-boolean-).
956
+ If you want to completely stop the analyzer's data processing, see [`stop()`](#stop).
922
957
 
923
958
  Defaults to **true**.
924
959
 
@@ -1066,7 +1101,7 @@ Connects an [HTMLMediaElement](https://developer.mozilla.org/en-US/docs/Web/API/
1066
1101
  If `source` is an *HTMLMediaElement*, the method returns a [MediaElementAudioSourceNode](https://developer.mozilla.org/en-US/docs/Web/API/MediaElementAudioSourceNode) created
1067
1102
  for that element; if `source` is an *AudioNode* instance, it returns the `source` object itself; if it's neither an [ERR_INVALID_AUDIO_SOURCE](#custom-errors) error is thrown.
1068
1103
 
1069
- See also [`disconnectInput()`](#disconnectinput-node-) and [`connectedSources`](#connectedsources-array-read-only).
1104
+ See also [`disconnectInput()`](#disconnectinput-node-stoptracks-) and [`connectedSources`](#connectedsources-array-read-only).
1070
1105
 
1071
1106
  ### `connectOutput( [node] )`
1072
1107
 
@@ -1082,20 +1117,39 @@ See also [`disconnectOutput()`](#disconnectoutput-node-) and [`connectedTo`](#co
1082
1117
 
1083
1118
  ?> If called with no argument, analyzer output is connected to the speakers (the *AudioContext* `destination` node).
1084
1119
 
1085
- ### `disconnectInput( [node] )`
1120
+ ### `destroy()`
1086
1121
 
1087
- *Available since v3.0.0*
1122
+ *Available since v4.2.0*
1088
1123
 
1089
- Disconnects audio source nodes previously connected to the analyzer.
1124
+ Destroys the **audioMotion-analyzer** instance and release resources. A destroyed analyzer cannot be started again.
1125
+
1126
+ This method:
1127
+
1128
+ + Stops the analyzer data processing and animation;
1129
+ + Disconnects all input and output nodes;
1130
+ + Clears event listeners and callback functions;
1131
+ + Stops the *AudioContext* created by this instance (won't affect context provided to the [constructor](#constructor) via [`audioCtx`](#audioctx-audiocontext-object) property or an *AudioNode* [`source`](#source-htmlmediaelement-or-audionode-object));
1132
+ + Removes the [`canvas`](#canvas-htmlcanvaselement-object-read-only) from the DOM.
1090
1133
 
1091
- `node` may be an *AudioNode* instance or an **array** of such objects.
1134
+ See usage example in the [minimal demo](/demo/minimal.html).
1092
1135
 
1093
- Please note that if you have connected an `<audio>` or `<video>` element, you should disconnect the respective [MediaElementAudioSourceNode](https://developer.mozilla.org/en-US/docs/Web/API/MediaElementAudioSourceNode)
1094
- created for it.
1136
+ See also [`isDestroyed`](#isdestroyed-boolean-read-only).
1095
1137
 
1096
- See also [`connectInput()`](#connectinput-source-).
1138
+ ### `disconnectInput( [node], [stopTracks] )`
1097
1139
 
1098
- ?> If called with no argument, all connected sources are disconnected.
1140
+ *Available since v3.0.0; `stopTracks` parameter since v4.2.0*
1141
+
1142
+ Disconnects audio source nodes previously connected to the analyzer.
1143
+
1144
+ `node` may be an *AudioNode* instance or an **array** of such objects. If it's **undefined** (or any [*falsy*](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) value),
1145
+ **all connected sources are disconnected.**
1146
+
1147
+ `stopTracks` is a boolean value; if **true**, permanently stops all audio tracks from any [*MediaStream*](https://developer.mozilla.org/en-US/docs/Web/API/MediaStream)s being
1148
+ disconnected, e.g. a microphone. Use it to effectively release the stream if it's no longer needed.
1149
+
1150
+ Please note that when you have connected an `<audio>` or `<video>` element, you need to disconnect the respective [MediaElementAudioSourceNode](https://developer.mozilla.org/en-US/docs/Web/API/MediaElementAudioSourceNode)
1151
+ created for it. The node reference is returned by [`connectInput()`](#connectinput-source-), or can be obtained from [`connectedSources`](#connectedsources-array-read-only)
1152
+ if the element was connected via [`source`](#source-htmlmediaelement-or-audionode-object) constructor option.
1099
1153
 
1100
1154
  ### `disconnectOutput( [node] )`
1101
1155
 
@@ -1244,16 +1298,35 @@ See **[Options object](#options-object)** for object structure and default value
1244
1298
 
1245
1299
  Adjust the analyzer's sensitivity. See [`minDecibels`](#mindecibels-number) and [`maxDecibels`](#maxdecibels-number) properties.
1246
1300
 
1247
- ### `toggleAnalyzer( [boolean] )`
1301
+ ### `start()`
1248
1302
 
1249
- Starts (*true*) or stops (*false*) the analyzer process. If no argument provided, inverts the current status.
1303
+ *Available since v4.2.0*
1250
1304
 
1251
- Returns the resulting status.
1305
+ Starts the analyzer data processing and animation.
1252
1306
 
1253
1307
  The analyzer is started by default after initialization, unless you specify [`start: false`](#start-boolean) in the [constructor](#constructor) options.
1308
+
1309
+ See also [`stop()`](#stop), [`toggleAnalyzer()`](#toggleanalyzer-boolean-) and [`isOn`](#ison-boolean-read-only).
1310
+
1311
+ ### `stop()`
1312
+
1313
+ *Available since v4.2.0*
1314
+
1315
+ Stops the analyzer process.
1316
+
1254
1317
  When the analyzer is off, no audio data is processed and no callbacks to [`onCanvasDraw`](#oncanvasdraw-function) will be triggered.
1255
1318
 
1256
- See also [`isOn`](#ison-boolean-read-only) property.
1319
+ The analyzer can be resumed with [`start()`](#start) or [`toggleAnalyzer()`](#toggleanalyzer-boolean-).
1320
+
1321
+ See also [`destroy()`](#destroy) and [`isOn`](#ison-boolean-read-only).
1322
+
1323
+ ### `toggleAnalyzer( [boolean] )`
1324
+
1325
+ Toggles the analyzer data processing and animation. The boolean argument can be used to force the desired state: *true* to start or *false* to stop the analyzer.
1326
+
1327
+ Returns the resulting state.
1328
+
1329
+ See also [`start()`](#start), [`stop()`](#stop) and [`isOn`](#ison-boolean-read-only).
1257
1330
 
1258
1331
  ### `toggleFullscreen()`
1259
1332
 
@@ -1341,33 +1414,23 @@ myAudio.crossOrigin = 'anonymous';
1341
1414
 
1342
1415
  ### Sound only plays after the user clicks somewhere on the page. <!-- {docsify-ignore} -->
1343
1416
 
1344
- Browser autoplay policy dictates that audio output can only be initiated by a user gesture, and this is enforced by WebAudio API
1345
- by creating [*AudioContext*](#audioctx-audiocontext-object-read-only) objects in *suspended* mode.
1417
+ Browser autoplay policy dictates that audio output can only be initiated by a user gesture, and this policy is enforced by Web Audio API
1418
+ by putting [*AudioContext*](#audioctx-audiocontext-object-read-only) objects into *suspended* mode if they're not created on user action.
1346
1419
 
1347
- **audioMotion-analyzer** tries to automatically start its AudioContext on the first click on the page.
1348
- However, if you're using an `audio` or `video` element with the `controls` property, clicks on those native media controls cannot be detected
1349
- by JavaScript, so the audio will only be enabled if/when the user clicks somewhere else.
1420
+ **audioMotion-analyzer** tries to automatically start its *AudioContext* on the first click on the page. However, if you're using an `audio`
1421
+ or `video` element with the `controls` property, clicks on those native media controls cannot be detected by JavaScript, so the audio will
1422
+ only be enabled if/when the user clicks somewhere else.
1350
1423
 
1351
- Two possible solutions are: **1)** ensure your users have to click somewhere else before using the media controls,
1352
- like a "power on" button, or simply clicking to select a song from a list will do; or **2)** don't use the native
1353
- controls at all, and create your own custom play and stop buttons. A very simple example:
1424
+ Possible solutions are:
1354
1425
 
1355
- ```html
1356
- <audio id="myAudio" src="track.mp3" crossorigin="anonymous"></audio> <!-- do not add the 'controls' property! -->
1426
+ 1. Ensure your users have to click somewhere else before using the native media controls, like a "power on" button;
1357
1427
 
1358
- <button id="play"> Play </button>
1359
- <button id="stop"> Stop </button>
1360
- ```
1428
+ 1. Don't use the native controls at all, and create your own custom play and stop buttons;
1361
1429
 
1362
- ```js
1363
- const myAudio = document.getElementById('audio');
1364
-
1365
- document.getElementById('play').addEventListener( 'click', () => myAudio.play() );
1366
- document.getElementById('stop').addEventListener( 'click', () => myAudio.pause() );
1367
- ```
1430
+ 1. Even better, instantiate your **audioMotion-analyzer** object within a function triggered by a user click. This will allow the *AudioContext* to
1431
+ be started right away and will also prevent the *"The AudioContext was not allowed to start"* warning message from appearing in the browser console.
1368
1432
 
1369
- You can also prevent the _"The AudioContext was not allowed to start"_ warning message from appearing in the browser console, by instantiating
1370
- your **audioMotion-analyzer** object within a function triggered by a user click. See the [minimal demo](/demo/minimal.html) code for an example.
1433
+ See the [minimal demo](/demo/minimal.html) code for an example.
1371
1434
 
1372
1435
 
1373
1436
  ## References and acknowledgments
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "audiomotion-analyzer",
3
3
  "description": "High-resolution real-time graphic audio spectrum analyzer JavaScript module with no dependencies.",
4
- "version": "4.1.1",
4
+ "version": "4.3.0",
5
5
  "main": "./src/audioMotion-analyzer.js",
6
6
  "module": "./src/audioMotion-analyzer.js",
7
7
  "types": "./src/index.d.ts",
8
+ "type": "module",
8
9
  "exports": {
9
- ".": "./src/audioMotion-analyzer.js"
10
+ ".": {
11
+ "import": "./src/audioMotion-analyzer.js",
12
+ "types": "./src/index.d.ts"
13
+ }
10
14
  },
11
15
  "files": [
12
16
  "src/**/*.js",