hypnosound 1.8.0 → 1.9.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/.eslintrc +12 -11
- package/.nvmrc +1 -1
- package/LICENSE +21 -21
- package/index.js +23 -147
- package/package.json +4 -10
- package/scripts/generate-barrel.mjs +26 -0
- package/src/audio/bass.js +34 -34
- package/src/audio/energy.js +19 -19
- package/src/audio/index.js +30 -14
- package/src/audio/mids.js +33 -33
- package/src/audio/pitchClass.js +26 -26
- package/src/audio/spectralCentroid.js +25 -25
- package/src/audio/spectralCrest.js +17 -17
- package/src/audio/spectralEntropy.js +28 -28
- package/src/audio/spectralFlux.js +17 -17
- package/src/audio/spectralKurtosis.js +26 -26
- package/src/audio/spectralRolloff.js +20 -20
- package/src/audio/spectralRoughness.js +16 -16
- package/src/audio/spectralSkew.js +26 -26
- package/src/audio/spectralSpread.js +26 -26
- package/src/audio/treble.js +33 -33
- package/src/utils/applyKaiserWindow.js +31 -31
- package/src/utils/calculateStats.js +206 -200
- package/src/utils/mu.js +12 -12
package/.eslintrc
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
{
|
|
2
|
-
"parserOptions": {
|
|
3
|
-
"ecmaVersion":
|
|
4
|
-
"sourceType": "module"
|
|
5
|
-
},
|
|
6
|
-
"plugins": [
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"parserOptions": {
|
|
3
|
+
"ecmaVersion": 2024,
|
|
4
|
+
"sourceType": "module"
|
|
5
|
+
},
|
|
6
|
+
"plugins": [
|
|
7
|
+
"prettier"
|
|
8
|
+
],
|
|
9
|
+
"rules": {
|
|
10
|
+
"prettier/prettier": "warn"
|
|
11
|
+
}
|
|
12
|
+
}
|
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
v21.6.1
|
|
1
|
+
v21.6.1
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 hypnodroid
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 hypnodroid
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.js
CHANGED
|
@@ -1,153 +1,29 @@
|
|
|
1
1
|
import { StatTypes, makeCalculateStats } from './src/utils/calculateStats.js'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
import spectralFlux from './src/audio/spectralFlux.js'
|
|
8
|
-
import spectralKurtosis from './src/audio/spectralKurtosis.js'
|
|
9
|
-
import spectralRolloff from './src/audio/spectralRolloff.js'
|
|
10
|
-
import spectralRoughness from './src/audio/spectralRoughness.js'
|
|
11
|
-
import spectralSkew from './src/audio/spectralSkew.js'
|
|
12
|
-
import spectralSpread from './src/audio/spectralSpread.js'
|
|
13
|
-
import pitchClass from './src/audio/pitchClass.js'
|
|
14
|
-
import bass from './src/audio/bass.js'
|
|
15
|
-
import treble from './src/audio/treble.js'
|
|
16
|
-
import mids from './src/audio/mids.js'
|
|
2
|
+
export * from './src/audio/index.js'
|
|
3
|
+
export { applyKaiserWindow } from './src/utils/applyKaiserWindow.js'
|
|
4
|
+
export { StatTypes, makeCalculateStats } from './src/utils/calculateStats.js'
|
|
5
|
+
import * as audio from './src/audio/index.js'
|
|
6
|
+
|
|
17
7
|
class AudioProcessor {
|
|
18
8
|
constructor() {
|
|
19
|
-
|
|
20
|
-
this.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.statCalculators.spectralSkew = makeCalculateStats()
|
|
39
|
-
|
|
40
|
-
this.statCalculators.spectralRoughness = makeCalculateStats()
|
|
41
|
-
|
|
42
|
-
this.statCalculators.spectralSpread = makeCalculateStats()
|
|
43
|
-
|
|
44
|
-
this.statCalculators.pitchClass = makeCalculateStats()
|
|
45
|
-
|
|
46
|
-
this.statCalculators.bass = makeCalculateStats()
|
|
47
|
-
|
|
48
|
-
this.statCalculators.treble = makeCalculateStats()
|
|
49
|
-
|
|
50
|
-
this.statCalculators.mids = makeCalculateStats()
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
energy = (fft) => {
|
|
54
|
-
const value = energy(fft)
|
|
55
|
-
const stats = this.statCalculators.energy(value)
|
|
56
|
-
return { value, stats }
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
spectralCentroid = (fft) => {
|
|
60
|
-
const value = spectralCentroid(applyKaiserWindow(fft))
|
|
61
|
-
const stats = this.statCalculators.spectralCentroid(value)
|
|
62
|
-
return { value, stats }
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
spectralCrest = (fft) => {
|
|
66
|
-
const value = spectralCrest(fft)
|
|
67
|
-
const stats = this.statCalculators.spectralCentroid(value)
|
|
68
|
-
return { value, stats }
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
spectralEntropy = (fft) => {
|
|
72
|
-
const value = spectralEntropy(fft)
|
|
73
|
-
const stats = this.statCalculators.spectralEntropy(value)
|
|
74
|
-
return { value, stats }
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
spectralFlux = (fft) => {
|
|
78
|
-
const value = spectralFlux(fft, this.previousValue.spectralFlux)
|
|
79
|
-
this.previousValue.spectralFlux = new Uint8Array(fft)
|
|
80
|
-
const stats = this.statCalculators.spectralFlux(value)
|
|
81
|
-
return { value, stats }
|
|
82
|
-
}
|
|
83
|
-
spectralKurtosis = (fft) => {
|
|
84
|
-
const value = spectralKurtosis(fft)
|
|
85
|
-
const stats = this.statCalculators.spectralKurtosis(value)
|
|
86
|
-
return { value, stats }
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
spectralRolloff = (fft) => {
|
|
90
|
-
const value = spectralRolloff(fft)
|
|
91
|
-
const stats = this.statCalculators.spectralRolloff(value)
|
|
92
|
-
return { value, stats }
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
spectralRoughness = (fft) => {
|
|
96
|
-
const value = spectralRoughness(fft)
|
|
97
|
-
const stats = this.statCalculators.spectralRoughness(value)
|
|
98
|
-
return { value, stats }
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
spectralSkew = (fft) => {
|
|
102
|
-
const value = spectralSkew(fft)
|
|
103
|
-
const stats = this.statCalculators.spectralSkew(value)
|
|
104
|
-
return { value, stats }
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
spectralSpread = (fft) => {
|
|
108
|
-
const value = spectralSpread(fft)
|
|
109
|
-
const stats = this.statCalculators.spectralSpread(value)
|
|
110
|
-
return { value, stats }
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
pitchClass = (fft) => {
|
|
114
|
-
const value = pitchClass(fft)
|
|
115
|
-
const stats = this.statCalculators.pitchClass(value)
|
|
116
|
-
return { value, stats }
|
|
117
|
-
}
|
|
118
|
-
bass = (fft) => {
|
|
119
|
-
const value = bass(fft)
|
|
120
|
-
const stats = this.statCalculators.bass(value)
|
|
121
|
-
return { value, stats }
|
|
122
|
-
}
|
|
123
|
-
treble = (fft) => {
|
|
124
|
-
const value = treble(fft)
|
|
125
|
-
const stats = this.statCalculators.treble(value)
|
|
126
|
-
return { value, stats }
|
|
127
|
-
}
|
|
128
|
-
mids = (fft) => {
|
|
129
|
-
const value = mids(fft)
|
|
130
|
-
const stats = this.statCalculators.mids(value)
|
|
131
|
-
return { value, stats }
|
|
9
|
+
const { AudioFeatures } = audio
|
|
10
|
+
this.state = AudioFeatures.reduce((acc, feature) => {
|
|
11
|
+
acc[feature] = {
|
|
12
|
+
analyzer: audio[feature],
|
|
13
|
+
statCalculator: makeCalculateStats(),
|
|
14
|
+
}
|
|
15
|
+
return acc
|
|
16
|
+
}, {})
|
|
17
|
+
for (const feature of AudioFeatures) {
|
|
18
|
+
this[feature] = (fft) => {
|
|
19
|
+
const { previousValue = 0, statCalculator, analyzer } = this.state[feature]
|
|
20
|
+
const value = analyzer(fft, previousValue)
|
|
21
|
+
this.state[feature].previousValue = value
|
|
22
|
+
this.state[feature].statCalculator = statCalculator
|
|
23
|
+
this.state[feature].stats = statCalculator(value, previousValue)
|
|
24
|
+
return { value, ...this.state[feature] }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
132
27
|
}
|
|
133
28
|
}
|
|
134
29
|
export default AudioProcessor
|
|
135
|
-
export {
|
|
136
|
-
energy,
|
|
137
|
-
spectralCentroid,
|
|
138
|
-
spectralCrest,
|
|
139
|
-
spectralEntropy,
|
|
140
|
-
spectralFlux,
|
|
141
|
-
spectralKurtosis,
|
|
142
|
-
spectralRolloff,
|
|
143
|
-
spectralRoughness,
|
|
144
|
-
spectralSkew,
|
|
145
|
-
spectralSpread,
|
|
146
|
-
pitchClass,
|
|
147
|
-
bass,
|
|
148
|
-
mids,
|
|
149
|
-
treble,
|
|
150
|
-
makeCalculateStats,
|
|
151
|
-
StatTypes,
|
|
152
|
-
applyKaiserWindow,
|
|
153
|
-
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hypnosound",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.9.0",
|
|
5
5
|
"description": "A small library for analyzing audio",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"format": "eslint --fix ."
|
|
8
|
+
"start": "python -m http.server 1337",
|
|
9
|
+
"build": "bun scripts/generate-barrel.mjs"
|
|
11
10
|
},
|
|
12
11
|
"repository": {
|
|
13
12
|
"type": "git",
|
|
@@ -23,10 +22,5 @@
|
|
|
23
22
|
"bugs": {
|
|
24
23
|
"url": "https://github.com/hypnodroid/hypnosound/issues"
|
|
25
24
|
},
|
|
26
|
-
"homepage": "https://github.com/hypnodroid/hypnosound#readme"
|
|
27
|
-
"devDependencies": {
|
|
28
|
-
"eslint": "^8.57.0",
|
|
29
|
-
"eslint-plugin-prettier": "^5.1.3",
|
|
30
|
-
"live-server": "^1.2.2"
|
|
31
|
-
}
|
|
25
|
+
"homepage": "https://github.com/hypnodroid/hypnosound#readme"
|
|
32
26
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { readdir } from 'node:fs/promises'
|
|
3
|
+
import { basename } from 'node:path'
|
|
4
|
+
|
|
5
|
+
const main = async (dir = './src/audio', out = './src/audio') => {
|
|
6
|
+
const functionNamesAndPaths = (await readdir(dir, { recursive: true }))
|
|
7
|
+
.filter((file) => file.endsWith('.js'))
|
|
8
|
+
.filter((file) => file !== 'index.js')
|
|
9
|
+
.map((file) => file.replace(dir + '/', ''))
|
|
10
|
+
.map((file) => file.replace(/\\/g, '/'))
|
|
11
|
+
.map((file) => {
|
|
12
|
+
const name = basename(file, '.js')
|
|
13
|
+
const path = file.replace(/\\/g, '/')
|
|
14
|
+
return { name, path }
|
|
15
|
+
})
|
|
16
|
+
Bun.write(`${out}/index.js`, exportTemplate(functionNamesAndPaths))
|
|
17
|
+
}
|
|
18
|
+
const exportTemplate = (functionNamesAndPaths) => {
|
|
19
|
+
return `${functionNamesAndPaths.map((fn) => `export { default as ${fn.name} } from "./${fn.path}"`).join('\n')}
|
|
20
|
+
export const AudioFeatures = ${JSON.stringify(
|
|
21
|
+
functionNamesAndPaths.map(({ name }) => name),
|
|
22
|
+
null,
|
|
23
|
+
2,
|
|
24
|
+
)}`.trim()
|
|
25
|
+
}
|
|
26
|
+
main()
|
package/src/audio/bass.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
export default function bass(fft) {
|
|
2
|
-
const sampleRate = 44100
|
|
3
|
-
const totalSamples = fft.length
|
|
4
|
-
return calculateBassPower(fft, sampleRate, totalSamples)
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function calculateBassPower(fft, sampleRate, totalSamples) {
|
|
8
|
-
const lowerBound = 0
|
|
9
|
-
const upperBound = 400
|
|
10
|
-
let bassEnergy = 0
|
|
11
|
-
let maxEnergy = 0
|
|
12
|
-
|
|
13
|
-
// Calculate frequency resolution
|
|
14
|
-
const frequencyResolution = sampleRate / totalSamples
|
|
15
|
-
|
|
16
|
-
for (let i = 0; i < fft.length; i++) {
|
|
17
|
-
let frequency = i * frequencyResolution
|
|
18
|
-
// Normalize each FFT value from 0 to 1 (assuming Uint8Array values 0-255)
|
|
19
|
-
let magnitude = fft[i] / 255
|
|
20
|
-
let power = magnitude * magnitude
|
|
21
|
-
|
|
22
|
-
// Accumulate max energy for normalization
|
|
23
|
-
maxEnergy += power
|
|
24
|
-
|
|
25
|
-
// Isolate and accumulate bass frequencies
|
|
26
|
-
if (frequency >= lowerBound && frequency <= upperBound) {
|
|
27
|
-
bassEnergy += power
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Normalize bass energy from 0 to 1
|
|
32
|
-
let normalizedBassPower = maxEnergy > 0 ? bassEnergy / maxEnergy : 0
|
|
33
|
-
return isNaN(normalizedBassPower) ? 0 : normalizedBassPower
|
|
34
|
-
}
|
|
1
|
+
export default function bass(fft) {
|
|
2
|
+
const sampleRate = 44100
|
|
3
|
+
const totalSamples = fft.length
|
|
4
|
+
return calculateBassPower(fft, sampleRate, totalSamples)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function calculateBassPower(fft, sampleRate, totalSamples) {
|
|
8
|
+
const lowerBound = 0
|
|
9
|
+
const upperBound = 400
|
|
10
|
+
let bassEnergy = 0
|
|
11
|
+
let maxEnergy = 0
|
|
12
|
+
|
|
13
|
+
// Calculate frequency resolution
|
|
14
|
+
const frequencyResolution = sampleRate / totalSamples
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < fft.length; i++) {
|
|
17
|
+
let frequency = i * frequencyResolution
|
|
18
|
+
// Normalize each FFT value from 0 to 1 (assuming Uint8Array values 0-255)
|
|
19
|
+
let magnitude = fft[i] / 255
|
|
20
|
+
let power = magnitude * magnitude
|
|
21
|
+
|
|
22
|
+
// Accumulate max energy for normalization
|
|
23
|
+
maxEnergy += power
|
|
24
|
+
|
|
25
|
+
// Isolate and accumulate bass frequencies
|
|
26
|
+
if (frequency >= lowerBound && frequency <= upperBound) {
|
|
27
|
+
bassEnergy += power
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Normalize bass energy from 0 to 1
|
|
32
|
+
let normalizedBassPower = maxEnergy > 0 ? bassEnergy / maxEnergy : 0
|
|
33
|
+
return isNaN(normalizedBassPower) ? 0 : normalizedBassPower
|
|
34
|
+
}
|
package/src/audio/energy.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
export default function energy(fft) {
|
|
2
|
-
return calculateFFTEnergy(fft) / 1000
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
function calculateFFTEnergy(currentSignal) {
|
|
6
|
-
let energy = 0
|
|
7
|
-
const maxPossibleValue = 1 // This should be 1 if your data is normalized between 0 and 1
|
|
8
|
-
const maxPossibleEnergy = currentSignal.length // Total samples if each sample was at maximum value
|
|
9
|
-
|
|
10
|
-
for (let i = 0; i < currentSignal.length; i++) {
|
|
11
|
-
let normalizedValue = currentSignal[i] / maxPossibleValue // Normalize each FFT value
|
|
12
|
-
energy += normalizedValue * normalizedValue // Sum the squares of the normalized values
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Normalize the computed energy by the number of samples since each sample's max energy would be 1 if maxPossibleValue is 1
|
|
16
|
-
energy = energy / currentSignal.length
|
|
17
|
-
|
|
18
|
-
return energy
|
|
19
|
-
}
|
|
1
|
+
export default function energy(fft) {
|
|
2
|
+
return calculateFFTEnergy(fft) / 1000
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function calculateFFTEnergy(currentSignal) {
|
|
6
|
+
let energy = 0
|
|
7
|
+
const maxPossibleValue = 1 // This should be 1 if your data is normalized between 0 and 1
|
|
8
|
+
const maxPossibleEnergy = currentSignal.length // Total samples if each sample was at maximum value
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < currentSignal.length; i++) {
|
|
11
|
+
let normalizedValue = currentSignal[i] / maxPossibleValue // Normalize each FFT value
|
|
12
|
+
energy += normalizedValue * normalizedValue // Sum the squares of the normalized values
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Normalize the computed energy by the number of samples since each sample's max energy would be 1 if maxPossibleValue is 1
|
|
16
|
+
energy = energy / currentSignal.length
|
|
17
|
+
|
|
18
|
+
return energy
|
|
19
|
+
}
|
package/src/audio/index.js
CHANGED
|
@@ -1,14 +1,30 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
1
|
+
export { default as bass } from "./bass.js"
|
|
2
|
+
export { default as energy } from "./energy.js"
|
|
3
|
+
export { default as mids } from "./mids.js"
|
|
4
|
+
export { default as pitchClass } from "./pitchClass.js"
|
|
5
|
+
export { default as spectralCentroid } from "./spectralCentroid.js"
|
|
6
|
+
export { default as spectralCrest } from "./spectralCrest.js"
|
|
7
|
+
export { default as spectralEntropy } from "./spectralEntropy.js"
|
|
8
|
+
export { default as spectralFlux } from "./spectralFlux.js"
|
|
9
|
+
export { default as spectralKurtosis } from "./spectralKurtosis.js"
|
|
10
|
+
export { default as spectralRolloff } from "./spectralRolloff.js"
|
|
11
|
+
export { default as spectralRoughness } from "./spectralRoughness.js"
|
|
12
|
+
export { default as spectralSkew } from "./spectralSkew.js"
|
|
13
|
+
export { default as spectralSpread } from "./spectralSpread.js"
|
|
14
|
+
export { default as treble } from "./treble.js"
|
|
15
|
+
export const AudioFeatures = [
|
|
16
|
+
"bass",
|
|
17
|
+
"energy",
|
|
18
|
+
"mids",
|
|
19
|
+
"pitchClass",
|
|
20
|
+
"spectralCentroid",
|
|
21
|
+
"spectralCrest",
|
|
22
|
+
"spectralEntropy",
|
|
23
|
+
"spectralFlux",
|
|
24
|
+
"spectralKurtosis",
|
|
25
|
+
"spectralRolloff",
|
|
26
|
+
"spectralRoughness",
|
|
27
|
+
"spectralSkew",
|
|
28
|
+
"spectralSpread",
|
|
29
|
+
"treble"
|
|
30
|
+
]
|
package/src/audio/mids.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
export default function mids(fft) {
|
|
2
|
-
const sampleRate = 44100
|
|
3
|
-
const totalSamples = fft.length
|
|
4
|
-
return calculateMidPower(fft, sampleRate, totalSamples)
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function calculateMidPower(fft, sampleRate, totalSamples) {
|
|
8
|
-
const lowerBound = 400 // 400 Hz
|
|
9
|
-
const upperBound = 4000 // 4000 Hz
|
|
10
|
-
let midEnergy = 0
|
|
11
|
-
let maxEnergy = 0
|
|
12
|
-
|
|
13
|
-
// Calculate frequency resolution
|
|
14
|
-
const frequencyResolution = sampleRate / totalSamples
|
|
15
|
-
|
|
16
|
-
for (let i = 0; i < fft.length; i++) {
|
|
17
|
-
let frequency = i * frequencyResolution
|
|
18
|
-
let magnitude = Math.abs(fft[i]) / totalSamples
|
|
19
|
-
let power = magnitude * magnitude
|
|
20
|
-
|
|
21
|
-
// Accumulate max energy for normalization
|
|
22
|
-
maxEnergy += power
|
|
23
|
-
|
|
24
|
-
// Isolate and accumulate mid frequencies
|
|
25
|
-
if (frequency >= lowerBound && frequency <= upperBound) {
|
|
26
|
-
midEnergy += power
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Normalize mid energy from 0 to 1
|
|
31
|
-
let normalizedMidPower = midEnergy / maxEnergy
|
|
32
|
-
return isNaN(normalizedMidPower) ? 0 : normalizedMidPower // Scale by 10 if needed, similar to your original function
|
|
33
|
-
}
|
|
1
|
+
export default function mids(fft) {
|
|
2
|
+
const sampleRate = 44100
|
|
3
|
+
const totalSamples = fft.length
|
|
4
|
+
return calculateMidPower(fft, sampleRate, totalSamples)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function calculateMidPower(fft, sampleRate, totalSamples) {
|
|
8
|
+
const lowerBound = 400 // 400 Hz
|
|
9
|
+
const upperBound = 4000 // 4000 Hz
|
|
10
|
+
let midEnergy = 0
|
|
11
|
+
let maxEnergy = 0
|
|
12
|
+
|
|
13
|
+
// Calculate frequency resolution
|
|
14
|
+
const frequencyResolution = sampleRate / totalSamples
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < fft.length; i++) {
|
|
17
|
+
let frequency = i * frequencyResolution
|
|
18
|
+
let magnitude = Math.abs(fft[i]) / totalSamples
|
|
19
|
+
let power = magnitude * magnitude
|
|
20
|
+
|
|
21
|
+
// Accumulate max energy for normalization
|
|
22
|
+
maxEnergy += power
|
|
23
|
+
|
|
24
|
+
// Isolate and accumulate mid frequencies
|
|
25
|
+
if (frequency >= lowerBound && frequency <= upperBound) {
|
|
26
|
+
midEnergy += power
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Normalize mid energy from 0 to 1
|
|
31
|
+
let normalizedMidPower = midEnergy / maxEnergy
|
|
32
|
+
return isNaN(normalizedMidPower) ? 0 : normalizedMidPower // Scale by 10 if needed, similar to your original function
|
|
33
|
+
}
|
package/src/audio/pitchClass.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
export default function pitchClass(fft, sampleRate = 44100) {
|
|
2
|
-
// Constants for the FFT processing
|
|
3
|
-
const fftSize = fft.length
|
|
4
|
-
const freqResolution = sampleRate / fftSize
|
|
5
|
-
|
|
6
|
-
// Finding the dominant frequency in the FFT data
|
|
7
|
-
let maxIndex = 0
|
|
8
|
-
let maxValue = 0
|
|
9
|
-
for (let i = 1; i < fft.length; i++) {
|
|
10
|
-
// start from 1 to skip DC offset
|
|
11
|
-
if (fft[i] > maxValue) {
|
|
12
|
-
maxValue = fft[i]
|
|
13
|
-
maxIndex = i
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
const dominantFreq = maxIndex * freqResolution
|
|
17
|
-
|
|
18
|
-
// Convert to MIDI note then to pitchClass
|
|
19
|
-
const midiNote = 69 + 12 * Math.log2(dominantFreq / 440)
|
|
20
|
-
const pitchClass = Math.round(midiNote) % 12 // round to reduce minor fluctuation effects
|
|
21
|
-
|
|
22
|
-
// Normalize to a 0-1 range
|
|
23
|
-
const normalizedpitchClass = pitchClass / 12
|
|
24
|
-
|
|
25
|
-
return isNaN(normalizedpitchClass) ? 0 : normalizedpitchClass
|
|
26
|
-
}
|
|
1
|
+
export default function pitchClass(fft, sampleRate = 44100) {
|
|
2
|
+
// Constants for the FFT processing
|
|
3
|
+
const fftSize = fft.length
|
|
4
|
+
const freqResolution = sampleRate / fftSize
|
|
5
|
+
|
|
6
|
+
// Finding the dominant frequency in the FFT data
|
|
7
|
+
let maxIndex = 0
|
|
8
|
+
let maxValue = 0
|
|
9
|
+
for (let i = 1; i < fft.length; i++) {
|
|
10
|
+
// start from 1 to skip DC offset
|
|
11
|
+
if (fft[i] > maxValue) {
|
|
12
|
+
maxValue = fft[i]
|
|
13
|
+
maxIndex = i
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const dominantFreq = maxIndex * freqResolution
|
|
17
|
+
|
|
18
|
+
// Convert to MIDI note then to pitchClass
|
|
19
|
+
const midiNote = 69 + 12 * Math.log2(dominantFreq / 440)
|
|
20
|
+
const pitchClass = Math.round(midiNote) % 12 // round to reduce minor fluctuation effects
|
|
21
|
+
|
|
22
|
+
// Normalize to a 0-1 range
|
|
23
|
+
const normalizedpitchClass = pitchClass / 12
|
|
24
|
+
|
|
25
|
+
return isNaN(normalizedpitchClass) ? 0 : normalizedpitchClass
|
|
26
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
export default function spectralCentroid(fft) {
|
|
2
|
-
const computed = calculateSpectralCentroid(fft) // Process FFT data
|
|
3
|
-
return computed * 1.5
|
|
4
|
-
}
|
|
5
|
-
function calculateSpectralCentroid(ampSpectrum) {
|
|
6
|
-
if (!ampSpectrum.length) return null // Early exit if the spectrum is empty
|
|
7
|
-
|
|
8
|
-
let numerator = 0
|
|
9
|
-
let denominator = 0
|
|
10
|
-
|
|
11
|
-
// Calculate the weighted sum (numerator) and the sum of the amplitudes (denominator)
|
|
12
|
-
ampSpectrum.forEach((amplitude, index) => {
|
|
13
|
-
numerator += index * amplitude
|
|
14
|
-
denominator += amplitude
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
// Avoid dividing by zero
|
|
18
|
-
if (denominator === 0) return null
|
|
19
|
-
|
|
20
|
-
const centroidIndex = numerator / denominator
|
|
21
|
-
// Normalize the centroid index to be between 0 and 1
|
|
22
|
-
const normalizedCentroid = centroidIndex / (ampSpectrum.length - 1)
|
|
23
|
-
|
|
24
|
-
return normalizedCentroid
|
|
25
|
-
}
|
|
1
|
+
export default function spectralCentroid(fft) {
|
|
2
|
+
const computed = calculateSpectralCentroid(fft) // Process FFT data
|
|
3
|
+
return computed * 1.5
|
|
4
|
+
}
|
|
5
|
+
function calculateSpectralCentroid(ampSpectrum) {
|
|
6
|
+
if (!ampSpectrum.length) return null // Early exit if the spectrum is empty
|
|
7
|
+
|
|
8
|
+
let numerator = 0
|
|
9
|
+
let denominator = 0
|
|
10
|
+
|
|
11
|
+
// Calculate the weighted sum (numerator) and the sum of the amplitudes (denominator)
|
|
12
|
+
ampSpectrum.forEach((amplitude, index) => {
|
|
13
|
+
numerator += index * amplitude
|
|
14
|
+
denominator += amplitude
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
// Avoid dividing by zero
|
|
18
|
+
if (denominator === 0) return null
|
|
19
|
+
|
|
20
|
+
const centroidIndex = numerator / denominator
|
|
21
|
+
// Normalize the centroid index to be between 0 and 1
|
|
22
|
+
const normalizedCentroid = centroidIndex / (ampSpectrum.length - 1)
|
|
23
|
+
|
|
24
|
+
return normalizedCentroid
|
|
25
|
+
}
|