hypnosound 1.2.1 → 1.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/index.html +14 -13
- package/index.js +10 -1
- package/package.json +1 -1
- package/src/audio/index.js +1 -0
- package/src/audio/pitchClass.js +26 -0
package/index.html
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
const analyser = audioContext.createAnalyser();
|
|
20
20
|
|
|
21
21
|
source.connect(analyser);
|
|
22
|
-
analyser.fftSize = 32768; // Or whatever size you need
|
|
22
|
+
analyser.fftSize = 32768/2; // Or whatever size you need
|
|
23
23
|
analyser.smoothingTimeConstant = 0
|
|
24
24
|
const bufferLength = analyser.frequencyBinCount;
|
|
25
25
|
const dataArray = new Uint8Array(bufferLength);
|
|
@@ -30,18 +30,19 @@
|
|
|
30
30
|
analyser.getByteFrequencyData(dataArray);
|
|
31
31
|
|
|
32
32
|
// This is where the magic happens, but be careful what you log...
|
|
33
|
-
console.log(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
console.log(
|
|
34
|
+
// energy: a.energy(dataArray),
|
|
35
|
+
// spectralCentroid: a.spectralCentroid(dataArray),
|
|
36
|
+
// spectralCrest: a.spectralCrest(dataArray),
|
|
37
|
+
// spectralEntropy: a.spectralEntropy(dataArray),
|
|
38
|
+
// spectralFlux: a.spectralFlux(dataArray),
|
|
39
|
+
// spectralKurtosis: a.spectralKurtosis(dataArray),
|
|
40
|
+
// spectralRolloff: a.spectralRolloff(dataArray),
|
|
41
|
+
// spectralRoughness: a.spectralRoughness(dataArray),
|
|
42
|
+
// spectralSkew: a.spectralSkew(dataArray),
|
|
43
|
+
// spectralSpread: a.spectralSpread(dataArray),
|
|
44
|
+
a.pitchClass(dataArray),
|
|
45
|
+
);
|
|
45
46
|
};
|
|
46
47
|
|
|
47
48
|
draw();
|
package/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import spectralRolloff from './src/audio/spectralRolloff.js'
|
|
|
10
10
|
import spectralRoughness from './src/audio/spectralRoughness.js'
|
|
11
11
|
import spectralSkew from './src/audio/spectralSkew.js'
|
|
12
12
|
import spectralSpread from './src/audio/spectralSpread.js'
|
|
13
|
-
|
|
13
|
+
import pitchClass from './src/audio/pitchClass.js'
|
|
14
14
|
class AudioProcessor {
|
|
15
15
|
constructor() {
|
|
16
16
|
// aah, state management
|
|
@@ -37,6 +37,8 @@ class AudioProcessor {
|
|
|
37
37
|
this.statCalculators.spectralRoughness = makeCalculateStats()
|
|
38
38
|
|
|
39
39
|
this.statCalculators.spectralSpread = makeCalculateStats()
|
|
40
|
+
|
|
41
|
+
this.statCalculators.pitchClass = makeCalculateStats()
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
energy = (fft) => {
|
|
@@ -108,6 +110,13 @@ class AudioProcessor {
|
|
|
108
110
|
const stats = this.statCalculators.spectralSpread(value)
|
|
109
111
|
return { value, stats }
|
|
110
112
|
}
|
|
113
|
+
|
|
114
|
+
pitchClass = (fft) => {
|
|
115
|
+
const windowedFft = applyKaiserWindow(fft)
|
|
116
|
+
const value = pitchClass(windowedFft)
|
|
117
|
+
const stats = this.statCalculators.pitchClass(value)
|
|
118
|
+
return { value, stats }
|
|
119
|
+
}
|
|
111
120
|
}
|
|
112
121
|
export default AudioProcessor
|
|
113
122
|
export {
|
package/package.json
CHANGED
package/src/audio/index.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export default function pitchClass(fft) {
|
|
2
|
+
// Constants for the FFT processing
|
|
3
|
+
const sampleRate = 44100; // This could vary
|
|
4
|
+
const fftSize = fft.length; // This is an example, adjust based on your FFT setup
|
|
5
|
+
const freqResolution = sampleRate / fftSize;
|
|
6
|
+
|
|
7
|
+
// Finding the dominant frequency in the FFT data
|
|
8
|
+
let maxIndex = 0;
|
|
9
|
+
let maxValue = 0;
|
|
10
|
+
for (let i = 0; i < fft.length; i++) {
|
|
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 class
|
|
19
|
+
const midiNote = 69 + 12 * Math.log2(dominantFreq / 440);
|
|
20
|
+
const pitchClass = midiNote % 12;
|
|
21
|
+
|
|
22
|
+
// Normalize to a 0-1 range
|
|
23
|
+
const normalizedpitchClass = pitchClass / 12;
|
|
24
|
+
|
|
25
|
+
return normalizedpitchClass;
|
|
26
|
+
}
|