audiomotion-analyzer 4.1.0 → 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 +107 -49
- package/package.json +6 -2
- package/src/audioMotion-analyzer.js +481 -319
- package/src/index.d.ts +16 -2
package/README.md
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
|
|
4
4
|
**audioMotion-analyzer** is a high-resolution real-time audio spectrum analyzer built upon **Web Audio** and **Canvas** JavaScript APIs.
|
|
5
5
|
|
|
6
|
-
It was originally conceived as part of
|
|
7
|
-
|
|
6
|
+
It was originally conceived as part of my full-featured music player called [**audioMotion**](https://audiomotion.me), but I later decided
|
|
7
|
+
to make the spectrum analyzer available as a self-contained module, so other developers could use it in their own JS projects.
|
|
8
8
|
|
|
9
9
|
My goal is to make this the best looking, most accurate and customizable spectrum analyzer around, in a small-footprint and high-performance package.
|
|
10
10
|
|
|
@@ -137,6 +137,7 @@ options = {<br>
|
|
|
137
137
|
  [loRes](#lores-boolean): **false**,<br>
|
|
138
138
|
  [lumiBars](#lumibars-boolean): **false**,<br>
|
|
139
139
|
  [maxDecibels](#maxdecibels-number): **-25**,<br>
|
|
140
|
+
  [maxFPS](#maxfps-number): **0**,<br>
|
|
140
141
|
  [maxFreq](#maxfreq-number): **22000**,<br>
|
|
141
142
|
  [minDecibels](#mindecibels-number): **-85**,<br>
|
|
142
143
|
  [minFreq](#minfreq-number): **20**,<br>
|
|
@@ -147,6 +148,7 @@ options = {<br>
|
|
|
147
148
|
  [onCanvasResize](#oncanvasresize-function): *undefined*,<br>
|
|
148
149
|
  [outlineBars](#outlinebars-boolean): **false**,<br>
|
|
149
150
|
  [overlay](#overlay-boolean): **false**,<br>
|
|
151
|
+
  [peakLine](#peakline-boolean): **false**,<br>
|
|
150
152
|
  [radial](#radial-boolean): **false**,<br>
|
|
151
153
|
  [reflexAlpha](#reflexalpha-number): **0.15**,<br>
|
|
152
154
|
  [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)
|
|
223
|
-
or
|
|
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-)
|
|
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
|
|
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
|
|
557
|
+
*Available since v3.6.0; formerly `showLeds` (since v1.0.0)*
|
|
550
558
|
|
|
551
|
-
*true* to activate the
|
|
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 | *
|
|
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
|
|
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'*,
|
|
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
|
|
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
|
|
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
|
|
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
|
-
### `
|
|
1115
|
+
### `destroy()`
|
|
1086
1116
|
|
|
1087
|
-
*Available since
|
|
1117
|
+
*Available since v4.2.0*
|
|
1088
1118
|
|
|
1089
|
-
|
|
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
|
-
`
|
|
1131
|
+
See also [`isDestroyed`](#isdestroyed-boolean-read-only).
|
|
1092
1132
|
|
|
1093
|
-
|
|
1094
|
-
created for it.
|
|
1133
|
+
### `disconnectInput( [node], [stopTracks] )`
|
|
1095
1134
|
|
|
1096
|
-
|
|
1135
|
+
*Available since v3.0.0; `stopTracks` parameter since v4.2.0*
|
|
1097
1136
|
|
|
1098
|
-
|
|
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
|
-
### `
|
|
1296
|
+
### `start()`
|
|
1248
1297
|
|
|
1249
|
-
|
|
1298
|
+
*Available since v4.2.0*
|
|
1250
1299
|
|
|
1251
|
-
|
|
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
|
-
|
|
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
|
|
1345
|
-
by
|
|
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
|
-
|
|
1349
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1363
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
-
".":
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./src/audioMotion-analyzer.js",
|
|
12
|
+
"types": "./src/index.d.ts"
|
|
13
|
+
}
|
|
10
14
|
},
|
|
11
15
|
"files": [
|
|
12
16
|
"src/**/*.js",
|