audiomotion-analyzer 4.1.1 → 4.2.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
 
@@ -500,6 +502,12 @@ or one of the frequency bands modes, in which case [`lumiBars`](#lumibars-boolea
500
502
 
501
503
  See also [`isOctaveBands`](#isoctavebands-boolean-read-only).
502
504
 
505
+ ### `isDestroyed` *boolean* *(Read only)*
506
+
507
+ *Available since v4.2.0*
508
+
509
+ ***true*** when the object has been destroyed with [`destroy()`](#destroy).
510
+
503
511
  ### `isFullscreen` *boolean* *(Read only)*
504
512
 
505
513
  ***true*** when the analyzer is being displayed in fullscreen, or ***false*** otherwise.
@@ -508,7 +516,7 @@ See [`toggleFullscreen()`](#togglefullscreen).
508
516
 
509
517
  ### `isLedBars` *boolean* *(Read only)*
510
518
 
511
- *Available since v3.6.0* (formerly `isLedDisplay`)
519
+ *Available since v3.6.0; formerly `isLedDisplay` (since v3.0.0)*
512
520
 
513
521
  ***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
522
 
@@ -528,7 +536,7 @@ See [`toggleFullscreen()`](#togglefullscreen).
528
536
 
529
537
  ***true*** if the analyzer process is running, or *false* if it's stopped.
530
538
 
531
- See [`toggleAnalyzer()`](#toggleanalyzer-boolean-).
539
+ See [`start()`](#start), [`stop()`](#stop) and [`toggleAnalyzer()`](#toggleanalyzer-boolean-).
532
540
 
533
541
  ### `isOutlineBars` *boolean* *(Read only)*
534
542
 
@@ -546,14 +554,16 @@ and [`ledBars`](#ledbars-boolean) and [`lumiBars`](#lumibars-boolean) are both s
546
554
 
547
555
  ### `ledBars` *boolean*
548
556
 
549
- *Available since v3.6.0* (formerly `showLeds`)
557
+ *Available since v3.6.0; formerly `showLeds` (since v1.0.0)*
550
558
 
551
- *true* to activate the vintage LED bars effect for frequency bands modes (see [`mode`](#mode-number)).
559
+ *true* to activate the LED bars effect for frequency bands modes (see [`mode`](#mode-number)).
552
560
 
553
561
  This effect can be customized via [`setLedParams()`](#setledparams-params-) method.
554
562
 
555
563
  For effect priority when combined with other settings, see [`isLedBars`](#isledbars-boolean-read-only).
556
564
 
565
+ See also [`trueLeds`](#trueleds-boolean).
566
+
557
567
  Defaults to **false**.
558
568
 
559
569
  ### `linearAmplitude` *boolean*
@@ -630,6 +640,16 @@ For more info, see [AnalyserNode.minDecibels](https://developer.mozilla.org/en-U
630
640
 
631
641
  *minDecibels* defaults to **-85** and *maxDecibels* defaults to **-25**.
632
642
 
643
+ ### `maxFPS` *number*
644
+
645
+ *Available since v4.2.0*
646
+
647
+ Sets the maximum desired animation frame rate. This can help reducing CPU usage, especially on high refresh rate monitors.
648
+
649
+ It must be a number, indicating frames per second. A value of **0** means the animation will run at the highest frame rate possible.
650
+
651
+ Defaults to **0**.
652
+
633
653
  ### `maxFreq` *number*
634
654
  ### `minFreq` *number*
635
655
 
@@ -676,7 +696,7 @@ mode | description | notes
676
696
  7 | Half octave bands or 20 bands | *use 'log' `frequencyScale` for octave bands*
677
697
  8 | Full octave bands or 10 bands | *use 'log' `frequencyScale` for octave bands*
678
698
  9 | *(not valid)* | *reserved*
679
- 10 | Graph | *added in v1.1.0*
699
+ 10 | Graph | *since v1.1.0*
680
700
 
681
701
  + **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
702
  + **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 +739,14 @@ Defaults to **false**.
719
739
 
720
740
  ?> 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
741
 
742
+ ### `peakLine` *boolean*
743
+
744
+ *Available since v4.2.0*
745
+
746
+ 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.
747
+
748
+ Defaults to **false**.
749
+
722
750
  ### `pixelRatio` *number* *(Read only)*
723
751
 
724
752
  Current [devicePixelRatio](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio).
@@ -734,9 +762,9 @@ You can refer to this value to adjust any additional drawings done in the canvas
734
762
 
735
763
  When *true*, the spectrum analyzer is rendered in a circular shape, with radial frequency bars spreading from its center.
736
764
 
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).
765
+ In radial view, [`ledBars`](#ledbars-boolean) and [`lumiBars`](#lumibars-boolean) effects are disabled.
738
766
 
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.
767
+ When [`channelLayout`](#channellayout-string) is set to *'dual-vertical'*, graphs for the right channel are rendered towards the center of the screen.
740
768
 
741
769
  See also [`spinSpeed`](#spinspeed-number).
742
770
 
@@ -825,11 +853,13 @@ and setting `showBgColor` to ***true*** will make the "unlit" LEDs visible inste
825
853
 
826
854
  ### `showPeaks` *boolean*
827
855
 
828
- *true* to show amplitude peaks for each frequency. Defaults to **true**.
856
+ *true* to show amplitude peaks. Defaults to **true**.
857
+
858
+ See also [`peakLine`](#peakline-boolean).
829
859
 
830
860
  ### `showScaleX` *boolean*
831
861
 
832
- *Available since v3.0.0 - this property was named `showScale` in earlier versions*
862
+ *Available since v3.0.0; formerly `showScale` (since v1.0.0)*
833
863
 
834
864
  *true* to display scale labels on the X axis.
835
865
 
@@ -918,7 +948,7 @@ Setting it to *false* in the [**constructor**](#constructor) options also preven
918
948
  Please note that the analyzer processing runs regardless of the value of `useCanvas` and any callback defined for [`onCanvasDraw`](#oncanvasdraw-function)
919
949
  will still be triggered on every animation frame, so you can use the [`getBars()`](#getbars) method to create your own visualizations.
920
950
 
921
- If you want to completely stop the audio data processing, see [`toggleAnalyzer()`](#toggleanalyzer-boolean-).
951
+ If you want to completely stop the analyzer's data processing, see [`stop()`](#stop).
922
952
 
923
953
  Defaults to **true**.
924
954
 
@@ -1066,7 +1096,7 @@ Connects an [HTMLMediaElement](https://developer.mozilla.org/en-US/docs/Web/API/
1066
1096
  If `source` is an *HTMLMediaElement*, the method returns a [MediaElementAudioSourceNode](https://developer.mozilla.org/en-US/docs/Web/API/MediaElementAudioSourceNode) created
1067
1097
  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
1098
 
1069
- See also [`disconnectInput()`](#disconnectinput-node-) and [`connectedSources`](#connectedsources-array-read-only).
1099
+ See also [`disconnectInput()`](#disconnectinput-node-stoptracks-) and [`connectedSources`](#connectedsources-array-read-only).
1070
1100
 
1071
1101
  ### `connectOutput( [node] )`
1072
1102
 
@@ -1082,20 +1112,39 @@ See also [`disconnectOutput()`](#disconnectoutput-node-) and [`connectedTo`](#co
1082
1112
 
1083
1113
  ?> If called with no argument, analyzer output is connected to the speakers (the *AudioContext* `destination` node).
1084
1114
 
1085
- ### `disconnectInput( [node] )`
1115
+ ### `destroy()`
1086
1116
 
1087
- *Available since v3.0.0*
1117
+ *Available since v4.2.0*
1088
1118
 
1089
- Disconnects audio source nodes previously connected to the analyzer.
1119
+ Destroys the **audioMotion-analyzer** instance and release resources. A destroyed analyzer cannot be started again.
1120
+
1121
+ This method:
1122
+
1123
+ + Stops the analyzer data processing and animation;
1124
+ + Disconnects all input and output nodes;
1125
+ + Clears event listeners and callback functions;
1126
+ + 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));
1127
+ + Removes the [`canvas`](#canvas-htmlcanvaselement-object-read-only) from the DOM.
1128
+
1129
+ See usage example in the [minimal demo](/demo/minimal.html).
1090
1130
 
1091
- `node` may be an *AudioNode* instance or an **array** of such objects.
1131
+ See also [`isDestroyed`](#isdestroyed-boolean-read-only).
1092
1132
 
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.
1133
+ ### `disconnectInput( [node], [stopTracks] )`
1095
1134
 
1096
- See also [`connectInput()`](#connectinput-source-).
1135
+ *Available since v3.0.0; `stopTracks` parameter since v4.2.0*
1097
1136
 
1098
- ?> If called with no argument, all connected sources are disconnected.
1137
+ Disconnects audio source nodes previously connected to the analyzer.
1138
+
1139
+ `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),
1140
+ **all connected sources are disconnected.**
1141
+
1142
+ `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
1143
+ disconnected, e.g. a microphone. Use it to effectively release the stream if it's no longer needed.
1144
+
1145
+ 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)
1146
+ created for it. The node reference is returned by [`connectInput()`](#connectinput-source-), or can be obtained from [`connectedSources`](#connectedsources-array-read-only)
1147
+ if the element was connected via [`source`](#source-htmlmediaelement-or-audionode-object) constructor option.
1099
1148
 
1100
1149
  ### `disconnectOutput( [node] )`
1101
1150
 
@@ -1244,16 +1293,35 @@ See **[Options object](#options-object)** for object structure and default value
1244
1293
 
1245
1294
  Adjust the analyzer's sensitivity. See [`minDecibels`](#mindecibels-number) and [`maxDecibels`](#maxdecibels-number) properties.
1246
1295
 
1247
- ### `toggleAnalyzer( [boolean] )`
1296
+ ### `start()`
1248
1297
 
1249
- Starts (*true*) or stops (*false*) the analyzer process. If no argument provided, inverts the current status.
1298
+ *Available since v4.2.0*
1250
1299
 
1251
- Returns the resulting status.
1300
+ Starts the analyzer data processing and animation.
1252
1301
 
1253
1302
  The analyzer is started by default after initialization, unless you specify [`start: false`](#start-boolean) in the [constructor](#constructor) options.
1303
+
1304
+ See also [`stop()`](#stop), [`toggleAnalyzer()`](#toggleanalyzer-boolean-) and [`isOn`](#ison-boolean-read-only).
1305
+
1306
+ ### `stop()`
1307
+
1308
+ *Available since v4.2.0*
1309
+
1310
+ Stops the analyzer process.
1311
+
1254
1312
  When the analyzer is off, no audio data is processed and no callbacks to [`onCanvasDraw`](#oncanvasdraw-function) will be triggered.
1255
1313
 
1256
- See also [`isOn`](#ison-boolean-read-only) property.
1314
+ The analyzer can be resumed with [`start()`](#start) or [`toggleAnalyzer()`](#toggleanalyzer-boolean-).
1315
+
1316
+ See also [`destroy()`](#destroy) and [`isOn`](#ison-boolean-read-only).
1317
+
1318
+ ### `toggleAnalyzer( [boolean] )`
1319
+
1320
+ 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.
1321
+
1322
+ Returns the resulting state.
1323
+
1324
+ See also [`start()`](#start), [`stop()`](#stop) and [`isOn`](#ison-boolean-read-only).
1257
1325
 
1258
1326
  ### `toggleFullscreen()`
1259
1327
 
@@ -1341,33 +1409,23 @@ myAudio.crossOrigin = 'anonymous';
1341
1409
 
1342
1410
  ### Sound only plays after the user clicks somewhere on the page. <!-- {docsify-ignore} -->
1343
1411
 
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.
1412
+ Browser autoplay policy dictates that audio output can only be initiated by a user gesture, and this policy is enforced by Web Audio API
1413
+ by putting [*AudioContext*](#audioctx-audiocontext-object-read-only) objects into *suspended* mode if they're not created on user action.
1346
1414
 
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.
1415
+ **audioMotion-analyzer** tries to automatically start its *AudioContext* on the first click on the page. However, if you're using an `audio`
1416
+ or `video` element with the `controls` property, clicks on those native media controls cannot be detected by JavaScript, so the audio will
1417
+ only be enabled if/when the user clicks somewhere else.
1350
1418
 
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:
1419
+ Possible solutions are:
1354
1420
 
1355
- ```html
1356
- <audio id="myAudio" src="track.mp3" crossorigin="anonymous"></audio> <!-- do not add the 'controls' property! -->
1421
+ 1. Ensure your users have to click somewhere else before using the native media controls, like a "power on" button;
1357
1422
 
1358
- <button id="play"> Play </button>
1359
- <button id="stop"> Stop </button>
1360
- ```
1423
+ 1. Don't use the native controls at all, and create your own custom play and stop buttons;
1361
1424
 
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
- ```
1425
+ 1. Even better, instantiate your **audioMotion-analyzer** object within a function triggered by a user click. This will allow the *AudioContext* to
1426
+ 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
1427
 
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.
1428
+ See the [minimal demo](/demo/minimal.html) code for an example.
1371
1429
 
1372
1430
 
1373
1431
  ## 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.2.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",