@siteed/expo-audio-studio 2.18.6 → 3.0.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 +13 -297
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +6 -135
- package/CHANGELOG.md +0 -501
- package/LICENSE +0 -21
- package/android/build.gradle +0 -129
- package/android/src/androidTest/assets/chorus.wav +0 -0
- package/android/src/androidTest/assets/jfk.wav +0 -0
- package/android/src/androidTest/assets/osr_us_000_0010_8k.wav +0 -0
- package/android/src/androidTest/assets/recorder_hello_world.wav +0 -0
- package/android/src/androidTest/java/net/siteed/audiostream/AudioProcessorInstrumentedTest.kt +0 -197
- package/android/src/androidTest/java/net/siteed/audiostream/AudioRecorderInstrumentedTest.kt +0 -541
- package/android/src/androidTest/java/net/siteed/audiostream/AudioRecorderPerformanceInstrumentedTest.kt +0 -234
- package/android/src/androidTest/java/net/siteed/audiostream/integration/AudioFocusStrategyIntegrationTest.kt +0 -332
- package/android/src/androidTest/java/net/siteed/audiostream/integration/BufferDurationIntegrationTest.kt +0 -324
- package/android/src/androidTest/java/net/siteed/audiostream/integration/CompressedOnlyOutputTest.kt +0 -253
- package/android/src/androidTest/java/net/siteed/audiostream/integration/DeviceDisconnectionFallbackTest.kt +0 -218
- package/android/src/androidTest/java/net/siteed/audiostream/integration/EventEmissionIntervalTest.kt +0 -120
- package/android/src/androidTest/java/net/siteed/audiostream/integration/M4aFormatTest.kt +0 -345
- package/android/src/androidTest/java/net/siteed/audiostream/integration/OutputControlIntegrationTest.kt +0 -340
- package/android/src/androidTest/java/net/siteed/audiostream/integration/PcmStreamingDurationTest.kt +0 -252
- package/android/src/androidTest/java/net/siteed/audiostream/integration/README.md +0 -95
- package/android/src/androidTest/java/net/siteed/audiostream/integration/run_integration_tests.sh +0 -43
- package/android/src/main/AndroidManifest.xml +0 -30
- package/android/src/main/java/net/siteed/audiostream/AudioAnalysisData.kt +0 -188
- package/android/src/main/java/net/siteed/audiostream/AudioDataEncoder.kt +0 -9
- package/android/src/main/java/net/siteed/audiostream/AudioDeviceManager.kt +0 -1741
- package/android/src/main/java/net/siteed/audiostream/AudioFileHandler.kt +0 -136
- package/android/src/main/java/net/siteed/audiostream/AudioFormatUtils.kt +0 -354
- package/android/src/main/java/net/siteed/audiostream/AudioNotificationsManager.kt +0 -439
- package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +0 -2237
- package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +0 -2141
- package/android/src/main/java/net/siteed/audiostream/AudioRecordingService.kt +0 -167
- package/android/src/main/java/net/siteed/audiostream/AudioTrimmer.kt +0 -1099
- package/android/src/main/java/net/siteed/audiostream/Constants.kt +0 -37
- package/android/src/main/java/net/siteed/audiostream/EventSender.kt +0 -7
- package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +0 -1113
- package/android/src/main/java/net/siteed/audiostream/FFT.kt +0 -99
- package/android/src/main/java/net/siteed/audiostream/Features.kt +0 -98
- package/android/src/main/java/net/siteed/audiostream/LogUtils.kt +0 -93
- package/android/src/main/java/net/siteed/audiostream/NotificationConfig.kt +0 -72
- package/android/src/main/java/net/siteed/audiostream/PermissionUtils.kt +0 -68
- package/android/src/main/java/net/siteed/audiostream/RecordingActionReceiver.kt +0 -59
- package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +0 -257
- package/android/src/main/java/net/siteed/audiostream/WaveformConfig.kt +0 -19
- package/android/src/main/java/net/siteed/audiostream/WaveformRenderer.kt +0 -159
- package/android/src/main/res/drawable/ic_default_action_icon.xml +0 -16
- package/android/src/main/res/drawable/ic_microphone.xml +0 -13
- package/android/src/main/res/drawable/ic_pause.xml +0 -10
- package/android/src/main/res/drawable/ic_play.xml +0 -10
- package/android/src/main/res/drawable/ic_stop.xml +0 -10
- package/android/src/main/res/layout/notification_recording.xml +0 -37
- package/android/src/test/java/net/siteed/audiostream/AudioFileHandlerTest.kt +0 -279
- package/android/src/test/java/net/siteed/audiostream/AudioFocusStrategyTest.kt +0 -249
- package/android/src/test/java/net/siteed/audiostream/AudioFormatTest.kt +0 -151
- package/android/src/test/java/net/siteed/audiostream/AudioFormatUtilsTest.kt +0 -273
- package/android/src/test/java/net/siteed/audiostream/DeviceDisconnectionFallbackUnitTest.kt +0 -140
- package/android/src/test/resources/chorus.wav +0 -0
- package/android/src/test/resources/generate_test_audio.py +0 -94
- package/android/src/test/resources/jfk.wav +0 -0
- package/android/src/test/resources/osr_us_000_0010_8k.wav +0 -0
- package/android/src/test/resources/recorder_hello_world.wav +0 -0
- package/app.plugin.js +0 -3
- package/build/cjs/AudioAnalysis/AudioAnalysis.types.js +0 -4
- package/build/cjs/AudioAnalysis/AudioAnalysis.types.js.map +0 -1
- package/build/cjs/AudioAnalysis/extractAudioAnalysis.js +0 -210
- package/build/cjs/AudioAnalysis/extractAudioAnalysis.js.map +0 -1
- package/build/cjs/AudioAnalysis/extractAudioData.js +0 -21
- package/build/cjs/AudioAnalysis/extractAudioData.js.map +0 -1
- package/build/cjs/AudioAnalysis/extractMelSpectrogram.js +0 -92
- package/build/cjs/AudioAnalysis/extractMelSpectrogram.js.map +0 -1
- package/build/cjs/AudioAnalysis/extractPreview.js +0 -28
- package/build/cjs/AudioAnalysis/extractPreview.js.map +0 -1
- package/build/cjs/AudioAnalysis/extractWaveform.js +0 -18
- package/build/cjs/AudioAnalysis/extractWaveform.js.map +0 -1
- package/build/cjs/AudioDeviceManager.js +0 -689
- package/build/cjs/AudioDeviceManager.js.map +0 -1
- package/build/cjs/AudioRecorder.provider.js +0 -78
- package/build/cjs/AudioRecorder.provider.js.map +0 -1
- package/build/cjs/ExpoAudioStream.native.js +0 -8
- package/build/cjs/ExpoAudioStream.native.js.map +0 -1
- package/build/cjs/ExpoAudioStream.types.js +0 -11
- package/build/cjs/ExpoAudioStream.types.js.map +0 -1
- package/build/cjs/ExpoAudioStream.web.js +0 -708
- package/build/cjs/ExpoAudioStream.web.js.map +0 -1
- package/build/cjs/ExpoAudioStreamModule.js +0 -718
- package/build/cjs/ExpoAudioStreamModule.js.map +0 -1
- package/build/cjs/WebRecorder.web.js +0 -777
- package/build/cjs/WebRecorder.web.js.map +0 -1
- package/build/cjs/constants/platformLimitations.js +0 -99
- package/build/cjs/constants/platformLimitations.js.map +0 -1
- package/build/cjs/constants.js +0 -17
- package/build/cjs/constants.js.map +0 -1
- package/build/cjs/events.js +0 -29
- package/build/cjs/events.js.map +0 -1
- package/build/cjs/hooks/useAudioDevices.js +0 -179
- package/build/cjs/hooks/useAudioDevices.js.map +0 -1
- package/build/cjs/index.js +0 -58
- package/build/cjs/index.js.map +0 -1
- package/build/cjs/trimAudio.js +0 -76
- package/build/cjs/trimAudio.js.map +0 -1
- package/build/cjs/useAudioRecorder.js +0 -518
- package/build/cjs/useAudioRecorder.js.map +0 -1
- package/build/cjs/utils/BlobFix.js +0 -502
- package/build/cjs/utils/BlobFix.js.map +0 -1
- package/build/cjs/utils/audioProcessing.js +0 -136
- package/build/cjs/utils/audioProcessing.js.map +0 -1
- package/build/cjs/utils/cleanNativeOptions.js +0 -22
- package/build/cjs/utils/cleanNativeOptions.js.map +0 -1
- package/build/cjs/utils/concatenateBuffers.js +0 -25
- package/build/cjs/utils/concatenateBuffers.js.map +0 -1
- package/build/cjs/utils/convertPCMToFloat32.js +0 -124
- package/build/cjs/utils/convertPCMToFloat32.js.map +0 -1
- package/build/cjs/utils/crc32.js +0 -52
- package/build/cjs/utils/crc32.js.map +0 -1
- package/build/cjs/utils/encodingToBitDepth.js +0 -17
- package/build/cjs/utils/encodingToBitDepth.js.map +0 -1
- package/build/cjs/utils/getWavFileInfo.js +0 -96
- package/build/cjs/utils/getWavFileInfo.js.map +0 -1
- package/build/cjs/utils/writeWavHeader.js +0 -88
- package/build/cjs/utils/writeWavHeader.js.map +0 -1
- package/build/cjs/workers/InlineFeaturesExtractor.web.js +0 -859
- package/build/cjs/workers/InlineFeaturesExtractor.web.js.map +0 -1
- package/build/cjs/workers/inlineAudioWebWorker.web.js +0 -184
- package/build/cjs/workers/inlineAudioWebWorker.web.js.map +0 -1
- package/build/esm/AudioAnalysis/AudioAnalysis.types.js +0 -3
- package/build/esm/AudioAnalysis/AudioAnalysis.types.js.map +0 -1
- package/build/esm/AudioAnalysis/extractAudioAnalysis.js +0 -202
- package/build/esm/AudioAnalysis/extractAudioAnalysis.js.map +0 -1
- package/build/esm/AudioAnalysis/extractAudioData.js +0 -14
- package/build/esm/AudioAnalysis/extractAudioData.js.map +0 -1
- package/build/esm/AudioAnalysis/extractMelSpectrogram.js +0 -89
- package/build/esm/AudioAnalysis/extractMelSpectrogram.js.map +0 -1
- package/build/esm/AudioAnalysis/extractPreview.js +0 -25
- package/build/esm/AudioAnalysis/extractPreview.js.map +0 -1
- package/build/esm/AudioAnalysis/extractWaveform.js +0 -11
- package/build/esm/AudioAnalysis/extractWaveform.js.map +0 -1
- package/build/esm/AudioDeviceManager.js +0 -682
- package/build/esm/AudioDeviceManager.js.map +0 -1
- package/build/esm/AudioRecorder.provider.js +0 -40
- package/build/esm/AudioRecorder.provider.js.map +0 -1
- package/build/esm/ExpoAudioStream.native.js +0 -6
- package/build/esm/ExpoAudioStream.native.js.map +0 -1
- package/build/esm/ExpoAudioStream.types.js +0 -8
- package/build/esm/ExpoAudioStream.types.js.map +0 -1
- package/build/esm/ExpoAudioStream.web.js +0 -704
- package/build/esm/ExpoAudioStream.web.js.map +0 -1
- package/build/esm/ExpoAudioStreamModule.js +0 -713
- package/build/esm/ExpoAudioStreamModule.js.map +0 -1
- package/build/esm/WebRecorder.web.js +0 -773
- package/build/esm/WebRecorder.web.js.map +0 -1
- package/build/esm/constants/platformLimitations.js +0 -90
- package/build/esm/constants/platformLimitations.js.map +0 -1
- package/build/esm/constants.js +0 -14
- package/build/esm/constants.js.map +0 -1
- package/build/esm/events.js +0 -21
- package/build/esm/events.js.map +0 -1
- package/build/esm/hooks/useAudioDevices.js +0 -176
- package/build/esm/hooks/useAudioDevices.js.map +0 -1
- package/build/esm/index.js +0 -20
- package/build/esm/index.js.map +0 -1
- package/build/esm/trimAudio.js +0 -69
- package/build/esm/trimAudio.js.map +0 -1
- package/build/esm/useAudioRecorder.js +0 -512
- package/build/esm/useAudioRecorder.js.map +0 -1
- package/build/esm/utils/BlobFix.js +0 -498
- package/build/esm/utils/BlobFix.js.map +0 -1
- package/build/esm/utils/audioProcessing.js +0 -133
- package/build/esm/utils/audioProcessing.js.map +0 -1
- package/build/esm/utils/cleanNativeOptions.js +0 -19
- package/build/esm/utils/cleanNativeOptions.js.map +0 -1
- package/build/esm/utils/concatenateBuffers.js +0 -21
- package/build/esm/utils/concatenateBuffers.js.map +0 -1
- package/build/esm/utils/convertPCMToFloat32.js +0 -120
- package/build/esm/utils/convertPCMToFloat32.js.map +0 -1
- package/build/esm/utils/crc32.js +0 -50
- package/build/esm/utils/crc32.js.map +0 -1
- package/build/esm/utils/encodingToBitDepth.js +0 -13
- package/build/esm/utils/encodingToBitDepth.js.map +0 -1
- package/build/esm/utils/getWavFileInfo.js +0 -92
- package/build/esm/utils/getWavFileInfo.js.map +0 -1
- package/build/esm/utils/writeWavHeader.js +0 -84
- package/build/esm/utils/writeWavHeader.js.map +0 -1
- package/build/esm/workers/InlineFeaturesExtractor.web.js +0 -856
- package/build/esm/workers/InlineFeaturesExtractor.web.js.map +0 -1
- package/build/esm/workers/inlineAudioWebWorker.web.js +0 -181
- package/build/esm/workers/inlineAudioWebWorker.web.js.map +0 -1
- package/build/types/AudioAnalysis/AudioAnalysis.types.d.ts +0 -196
- package/build/types/AudioAnalysis/AudioAnalysis.types.d.ts.map +0 -1
- package/build/types/AudioAnalysis/extractAudioAnalysis.d.ts +0 -74
- package/build/types/AudioAnalysis/extractAudioAnalysis.d.ts.map +0 -1
- package/build/types/AudioAnalysis/extractAudioData.d.ts +0 -3
- package/build/types/AudioAnalysis/extractAudioData.d.ts.map +0 -1
- package/build/types/AudioAnalysis/extractMelSpectrogram.d.ts +0 -14
- package/build/types/AudioAnalysis/extractMelSpectrogram.d.ts.map +0 -1
- package/build/types/AudioAnalysis/extractPreview.d.ts +0 -11
- package/build/types/AudioAnalysis/extractPreview.d.ts.map +0 -1
- package/build/types/AudioAnalysis/extractWaveform.d.ts +0 -8
- package/build/types/AudioAnalysis/extractWaveform.d.ts.map +0 -1
- package/build/types/AudioDeviceManager.d.ts +0 -187
- package/build/types/AudioDeviceManager.d.ts.map +0 -1
- package/build/types/AudioRecorder.provider.d.ts +0 -11
- package/build/types/AudioRecorder.provider.d.ts.map +0 -1
- package/build/types/ExpoAudioStream.native.d.ts +0 -3
- package/build/types/ExpoAudioStream.native.d.ts.map +0 -1
- package/build/types/ExpoAudioStream.types.d.ts +0 -738
- package/build/types/ExpoAudioStream.types.d.ts.map +0 -1
- package/build/types/ExpoAudioStream.web.d.ts +0 -96
- package/build/types/ExpoAudioStream.web.d.ts.map +0 -1
- package/build/types/ExpoAudioStreamModule.d.ts +0 -3
- package/build/types/ExpoAudioStreamModule.d.ts.map +0 -1
- package/build/types/WebRecorder.web.d.ts +0 -198
- package/build/types/WebRecorder.web.d.ts.map +0 -1
- package/build/types/constants/platformLimitations.d.ts +0 -40
- package/build/types/constants/platformLimitations.d.ts.map +0 -1
- package/build/types/constants.d.ts +0 -11
- package/build/types/constants.d.ts.map +0 -1
- package/build/types/events.d.ts +0 -26
- package/build/types/events.d.ts.map +0 -1
- package/build/types/hooks/useAudioDevices.d.ts +0 -15
- package/build/types/hooks/useAudioDevices.d.ts.map +0 -1
- package/build/types/index.d.ts +0 -18
- package/build/types/index.d.ts.map +0 -1
- package/build/types/trimAudio.d.ts +0 -25
- package/build/types/trimAudio.d.ts.map +0 -1
- package/build/types/useAudioRecorder.d.ts +0 -22
- package/build/types/useAudioRecorder.d.ts.map +0 -1
- package/build/types/utils/BlobFix.d.ts +0 -9
- package/build/types/utils/BlobFix.d.ts.map +0 -1
- package/build/types/utils/audioProcessing.d.ts +0 -24
- package/build/types/utils/audioProcessing.d.ts.map +0 -1
- package/build/types/utils/cleanNativeOptions.d.ts +0 -15
- package/build/types/utils/cleanNativeOptions.d.ts.map +0 -1
- package/build/types/utils/concatenateBuffers.d.ts +0 -8
- package/build/types/utils/concatenateBuffers.d.ts.map +0 -1
- package/build/types/utils/convertPCMToFloat32.d.ts +0 -13
- package/build/types/utils/convertPCMToFloat32.d.ts.map +0 -1
- package/build/types/utils/crc32.d.ts +0 -7
- package/build/types/utils/crc32.d.ts.map +0 -1
- package/build/types/utils/encodingToBitDepth.d.ts +0 -5
- package/build/types/utils/encodingToBitDepth.d.ts.map +0 -1
- package/build/types/utils/getWavFileInfo.d.ts +0 -26
- package/build/types/utils/getWavFileInfo.d.ts.map +0 -1
- package/build/types/utils/writeWavHeader.d.ts +0 -34
- package/build/types/utils/writeWavHeader.d.ts.map +0 -1
- package/build/types/workers/InlineFeaturesExtractor.web.d.ts +0 -2
- package/build/types/workers/InlineFeaturesExtractor.web.d.ts.map +0 -1
- package/build/types/workers/inlineAudioWebWorker.web.d.ts +0 -2
- package/build/types/workers/inlineAudioWebWorker.web.d.ts.map +0 -1
- package/expo-module.config.json +0 -10
- package/ios/AudioAnalysisData.swift +0 -74
- package/ios/AudioDeviceManager.swift +0 -670
- package/ios/AudioNotificationManager.swift +0 -154
- package/ios/AudioProcessingHelpers.swift +0 -743
- package/ios/AudioProcessor.swift +0 -1151
- package/ios/AudioStreamError.swift +0 -7
- package/ios/AudioStreamManager.swift +0 -2369
- package/ios/AudioStreamManagerDelegate.swift +0 -16
- package/ios/DataPoint.swift +0 -54
- package/ios/DecodingConfig.swift +0 -59
- package/ios/ExpoAudioStream.podspec +0 -33
- package/ios/ExpoAudioStreamModule.swift +0 -1019
- package/ios/ExpoAudioStudioTests/AudioFileHandlerTests.swift +0 -338
- package/ios/ExpoAudioStudioTests/AudioFormatUtilsTests.swift +0 -331
- package/ios/ExpoAudioStudioTests/AudioTestHelpers.swift +0 -130
- package/ios/ExpoAudioStudioTests/CompressedOnlyOutputTests.swift +0 -294
- package/ios/ExpoAudioStudioTests/EventEmissionIntervalTests.swift +0 -105
- package/ios/ExpoAudioStudioTests/Info.plist +0 -22
- package/ios/ExpoAudioStudioTests/README.md +0 -39
- package/ios/ExpoAudioStudioTests/SimpleAudioTest.swift +0 -98
- package/ios/ExpoAudioStudioTests/TestAudioGenerator.swift +0 -75
- package/ios/FFT.swift +0 -62
- package/ios/Features.swift +0 -95
- package/ios/ISSUE_IOS.md +0 -68
- package/ios/Logger.swift +0 -39
- package/ios/NotificationExtension.swift +0 -15
- package/ios/RecordingResult.swift +0 -22
- package/ios/RecordingSettings.swift +0 -308
- package/ios/WaveformExtractor.swift +0 -105
- package/ios/tests/README.md +0 -41
- package/ios/tests/integration/buffer_and_fallback_test.swift +0 -178
- package/ios/tests/integration/buffer_duration_test.swift +0 -185
- package/ios/tests/integration/compressed_only_output_test.swift +0 -271
- package/ios/tests/integration/output_control_test.swift +0 -322
- package/ios/tests/integration/run_integration_tests.sh +0 -37
- package/ios/tests/opus_support_test_macos.swift +0 -154
- package/ios/tests/standalone/audio_processing_test.swift +0 -144
- package/ios/tests/standalone/audio_recording_test.swift +0 -277
- package/ios/tests/standalone/audio_streaming_test.swift +0 -249
- package/ios/tests/standalone/standalone_test.swift +0 -144
- package/plugin/build/index.cjs +0 -194
- package/plugin/build/index.d.cts +0 -22
- package/plugin/build/index.js +0 -194
- package/plugin/src/index.ts +0 -285
- package/plugin/tsconfig.json +0 -10
- package/plugin/tsconfig.tsbuildinfo +0 -1
- package/src/AudioAnalysis/AudioAnalysis.types.ts +0 -224
- package/src/AudioAnalysis/extractAudioAnalysis.ts +0 -344
- package/src/AudioAnalysis/extractAudioData.ts +0 -17
- package/src/AudioAnalysis/extractMelSpectrogram.ts +0 -154
- package/src/AudioAnalysis/extractPreview.ts +0 -34
- package/src/AudioAnalysis/extractWaveform.ts +0 -22
- package/src/AudioDeviceManager.ts +0 -803
- package/src/AudioRecorder.provider.tsx +0 -57
- package/src/ExpoAudioStream.native.ts +0 -6
- package/src/ExpoAudioStream.types.ts +0 -874
- package/src/ExpoAudioStream.web.ts +0 -905
- package/src/ExpoAudioStreamModule.ts +0 -990
- package/src/WebRecorder.web.ts +0 -1005
- package/src/constants/platformLimitations.ts +0 -118
- package/src/constants.ts +0 -18
- package/src/events.ts +0 -60
- package/src/hooks/useAudioDevices.ts +0 -213
- package/src/index.ts +0 -54
- package/src/trimAudio.ts +0 -94
- package/src/types/crc-32.d.ts +0 -9
- package/src/useAudioRecorder.tsx +0 -766
- package/src/utils/BlobFix.ts +0 -561
- package/src/utils/audioProcessing.ts +0 -205
- package/src/utils/cleanNativeOptions.ts +0 -18
- package/src/utils/concatenateBuffers.ts +0 -24
- package/src/utils/convertPCMToFloat32.ts +0 -170
- package/src/utils/crc32.ts +0 -59
- package/src/utils/encodingToBitDepth.ts +0 -18
- package/src/utils/getWavFileInfo.ts +0 -132
- package/src/utils/writeWavHeader.ts +0 -115
- package/src/workers/InlineFeaturesExtractor.web.tsx +0 -855
- package/src/workers/inlineAudioWebWorker.web.tsx +0 -180
|
@@ -1,859 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InlineFeaturesExtractor = void 0;
|
|
4
|
-
// packages/expo-audio-studio/src/workers/InlineFeaturesExtractor.web.tsx
|
|
5
|
-
exports.InlineFeaturesExtractor = `
|
|
6
|
-
// Constants
|
|
7
|
-
const N_FFT = 1024; // Default FFT size
|
|
8
|
-
const MAX_FFT_SIZE = 8192; // Maximum FFT size to prevent memory issues
|
|
9
|
-
const N_CHROMA = 12;
|
|
10
|
-
|
|
11
|
-
// FFT Implementation with normalized Hann window
|
|
12
|
-
function FFT(n) {
|
|
13
|
-
this.n = n;
|
|
14
|
-
this.cosTable = new Float32Array(n / 2);
|
|
15
|
-
this.sinTable = new Float32Array(n / 2);
|
|
16
|
-
this.hannWindow = new Float32Array(n);
|
|
17
|
-
|
|
18
|
-
// Match Android implementation with precomputed tables
|
|
19
|
-
const normalizationFactor = Math.sqrt(2.0 / n);
|
|
20
|
-
for (var i = 0; i < n / 2; i++) {
|
|
21
|
-
this.cosTable[i] = Math.cos(2.0 * Math.PI * i / n);
|
|
22
|
-
this.sinTable[i] = Math.sin(2.0 * Math.PI * i / n);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Precompute normalized Hann window to match Android
|
|
26
|
-
for (var i = 0; i < n; i++) {
|
|
27
|
-
this.hannWindow[i] = normalizationFactor * 0.5 * (1 - Math.cos(2.0 * Math.PI * i / (n - 1)));
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
FFT.prototype.transform = function(data) {
|
|
32
|
-
const n = data.length;
|
|
33
|
-
|
|
34
|
-
// Validate input length is power of 2
|
|
35
|
-
if ((n & (n - 1)) !== 0) {
|
|
36
|
-
throw new Error('FFT length must be power of 2');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Use iterative bit reversal instead of recursive
|
|
40
|
-
const bitReversedIndices = new Uint32Array(n);
|
|
41
|
-
for (let i = 0; i < n; i++) {
|
|
42
|
-
let reversed = 0;
|
|
43
|
-
let j = i;
|
|
44
|
-
let bits = Math.log2(n);
|
|
45
|
-
while (bits--) {
|
|
46
|
-
reversed = (reversed << 1) | (j & 1);
|
|
47
|
-
j >>= 1;
|
|
48
|
-
}
|
|
49
|
-
bitReversedIndices[i] = reversed;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Apply bit reversal
|
|
53
|
-
for (let i = 0; i < n; i++) {
|
|
54
|
-
const j = bitReversedIndices[i];
|
|
55
|
-
if (i < j) {
|
|
56
|
-
const temp = data[i];
|
|
57
|
-
data[i] = data[j];
|
|
58
|
-
data[j] = temp;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Iterative FFT computation with optimized memory usage
|
|
63
|
-
for (let step = 1; step < n; step <<= 1) {
|
|
64
|
-
const jump = step << 1;
|
|
65
|
-
const angleStep = Math.PI / step;
|
|
66
|
-
|
|
67
|
-
for (let group = 0; group < n; group += jump) {
|
|
68
|
-
for (let pair = group; pair < group + step; pair++) {
|
|
69
|
-
const match = pair + step;
|
|
70
|
-
const angle = angleStep * (pair - group);
|
|
71
|
-
|
|
72
|
-
const currentCos = Math.cos(angle);
|
|
73
|
-
const currentSin = Math.sin(angle);
|
|
74
|
-
|
|
75
|
-
const real = currentCos * data[match] - currentSin * data[match + 1];
|
|
76
|
-
const imag = currentCos * data[match + 1] + currentSin * data[match];
|
|
77
|
-
|
|
78
|
-
data[match] = data[pair] - real;
|
|
79
|
-
data[match + 1] = data[pair + 1] - imag;
|
|
80
|
-
data[pair] += real;
|
|
81
|
-
data[pair + 1] += imag;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
// Add realInverse method
|
|
88
|
-
FFT.prototype.realInverse = function(powerSpectrum, output) {
|
|
89
|
-
const n = powerSpectrum.length;
|
|
90
|
-
const complexData = new Float32Array(n * 2);
|
|
91
|
-
|
|
92
|
-
// Copy power spectrum to complex format
|
|
93
|
-
for (let i = 0; i < n/2 + 1; i++) {
|
|
94
|
-
complexData[2 * i] = powerSpectrum[i];
|
|
95
|
-
if (2 * i + 1 < complexData.length) {
|
|
96
|
-
complexData[2 * i + 1] = 0;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Conjugate for inverse FFT
|
|
101
|
-
for (let i = 0; i < n; i++) {
|
|
102
|
-
if (2 * i + 1 < complexData.length) {
|
|
103
|
-
complexData[2 * i + 1] = -complexData[2 * i + 1];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
this.transform(complexData);
|
|
108
|
-
|
|
109
|
-
// Copy real part to output and scale
|
|
110
|
-
for (let i = 0; i < n; i++) {
|
|
111
|
-
output[i] = complexData[2 * i] / n;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
// Add helper functions to match Android
|
|
116
|
-
function nextPowerOfTwo(n) {
|
|
117
|
-
let value = 1;
|
|
118
|
-
while (value < n) {
|
|
119
|
-
value *= 2;
|
|
120
|
-
}
|
|
121
|
-
return value;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function applyHannWindow(samples) {
|
|
125
|
-
const output = new Float32Array(samples.length);
|
|
126
|
-
for (let i = 0; i < samples.length; i++) {
|
|
127
|
-
const multiplier = 0.5 * (1 - Math.cos(2 * Math.PI * i / (samples.length - 1)));
|
|
128
|
-
output[i] = samples[i] * multiplier;
|
|
129
|
-
}
|
|
130
|
-
return output;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Update spectral feature computation to match Android
|
|
134
|
-
function computeSpectralFeatures(segment, sampleRate, featureOptions = {}) {
|
|
135
|
-
try {
|
|
136
|
-
// Early return if no spectral features are requested
|
|
137
|
-
if (!featureOptions.spectralCentroid &&
|
|
138
|
-
!featureOptions.spectralFlatness &&
|
|
139
|
-
!featureOptions.spectralRollOff &&
|
|
140
|
-
!featureOptions.spectralBandwidth &&
|
|
141
|
-
!featureOptions.magnitudeSpectrum) {
|
|
142
|
-
return {
|
|
143
|
-
centroid: 0,
|
|
144
|
-
flatness: 0,
|
|
145
|
-
rollOff: 0,
|
|
146
|
-
bandwidth: 0,
|
|
147
|
-
magnitudeSpectrum: []
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Ensure we have valid data
|
|
152
|
-
if (!segment || segment.length === 0) {
|
|
153
|
-
throw new Error('Invalid segment data');
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Process in fixed-size chunks
|
|
157
|
-
const chunkSize = N_FFT;
|
|
158
|
-
const numChunks = Math.ceil(segment.length / chunkSize);
|
|
159
|
-
|
|
160
|
-
let results = {
|
|
161
|
-
centroid: 0,
|
|
162
|
-
flatness: 0,
|
|
163
|
-
rollOff: 0,
|
|
164
|
-
bandwidth: 0,
|
|
165
|
-
magnitudeSpectrum: new Float32Array(N_FFT / 2 + 1).fill(0)
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
let validChunks = 0;
|
|
169
|
-
|
|
170
|
-
// Iterate through chunks
|
|
171
|
-
for (let i = 0; i < numChunks; i++) {
|
|
172
|
-
const start = i * chunkSize;
|
|
173
|
-
const end = Math.min(start + chunkSize, segment.length);
|
|
174
|
-
const chunk = segment.slice(start, end);
|
|
175
|
-
|
|
176
|
-
if (chunk.length < N_FFT / 4) continue; // Skip very small chunks
|
|
177
|
-
|
|
178
|
-
// Process the chunk
|
|
179
|
-
const paddedChunk = new Float32Array(N_FFT);
|
|
180
|
-
paddedChunk.set(applyHannWindow(chunk));
|
|
181
|
-
|
|
182
|
-
const fft = new FFT(N_FFT);
|
|
183
|
-
fft.transform(paddedChunk);
|
|
184
|
-
|
|
185
|
-
// Calculate magnitude spectrum
|
|
186
|
-
const chunkMagnitudeSpectrum = new Float32Array(N_FFT / 2 + 1);
|
|
187
|
-
let hasSignal = false;
|
|
188
|
-
|
|
189
|
-
for (let j = 0; j < N_FFT / 2; j++) {
|
|
190
|
-
const re = paddedChunk[2 * j];
|
|
191
|
-
const im = paddedChunk[2 * j + 1];
|
|
192
|
-
const magnitude = Math.sqrt(re * re + im * im);
|
|
193
|
-
chunkMagnitudeSpectrum[j] = magnitude;
|
|
194
|
-
if (magnitude > Number.EPSILON) hasSignal = true;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (!hasSignal) continue;
|
|
198
|
-
validChunks++;
|
|
199
|
-
|
|
200
|
-
// Accumulate results
|
|
201
|
-
if (featureOptions.spectralCentroid) {
|
|
202
|
-
const centroid = computeSpectralCentroid(chunkMagnitudeSpectrum, sampleRate);
|
|
203
|
-
if (!isNaN(centroid)) results.centroid += centroid;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (featureOptions.spectralFlatness) {
|
|
207
|
-
const flatness = computeSpectralFlatness(chunkMagnitudeSpectrum);
|
|
208
|
-
if (!isNaN(flatness)) results.flatness += flatness;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (featureOptions.spectralRollOff) {
|
|
212
|
-
const rolloff = computeSpectralRollOff(chunkMagnitudeSpectrum, sampleRate);
|
|
213
|
-
if (!isNaN(rolloff)) results.rollOff += rolloff;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (featureOptions.spectralBandwidth && !isNaN(results.centroid)) {
|
|
217
|
-
const bandwidth = computeSpectralBandwidth(chunkMagnitudeSpectrum, sampleRate, results.centroid);
|
|
218
|
-
if (!isNaN(bandwidth)) results.bandwidth += bandwidth;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (featureOptions.magnitudeSpectrum) {
|
|
222
|
-
for (let j = 0; j < results.magnitudeSpectrum.length; j++) {
|
|
223
|
-
results.magnitudeSpectrum[j] += chunkMagnitudeSpectrum[j];
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Average the accumulated results
|
|
229
|
-
if (validChunks > 0) {
|
|
230
|
-
results.centroid /= validChunks;
|
|
231
|
-
results.flatness /= validChunks;
|
|
232
|
-
results.rollOff /= validChunks;
|
|
233
|
-
results.bandwidth /= validChunks;
|
|
234
|
-
|
|
235
|
-
if (featureOptions.magnitudeSpectrum) {
|
|
236
|
-
for (let i = 0; i < results.magnitudeSpectrum.length; i++) {
|
|
237
|
-
results.magnitudeSpectrum[i] /= validChunks;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return results;
|
|
243
|
-
} catch (error) {
|
|
244
|
-
console.error('[Worker] Spectral feature computation error:', error);
|
|
245
|
-
return {
|
|
246
|
-
centroid: 0,
|
|
247
|
-
flatness: 0,
|
|
248
|
-
rollOff: 0,
|
|
249
|
-
bandwidth: 0,
|
|
250
|
-
magnitudeSpectrum: []
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function computeSpectralCentroid(magnitudeSpectrum, sampleRate) {
|
|
256
|
-
const sum = magnitudeSpectrum.reduce((a, b) => a + (b || 0), 0);
|
|
257
|
-
if (sum <= Number.EPSILON) return 0;
|
|
258
|
-
|
|
259
|
-
const weightedSum = magnitudeSpectrum.reduce((acc, value, index) =>
|
|
260
|
-
acc + (index * (sampleRate / N_FFT) * (value || 0)), 0);
|
|
261
|
-
|
|
262
|
-
return weightedSum / sum;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function computeSpectralFlatness(powerSpectrum) {
|
|
266
|
-
// Add small epsilon to avoid log(0)
|
|
267
|
-
const epsilon = Number.EPSILON;
|
|
268
|
-
const validSpectrum = powerSpectrum.map(v => Math.max(v, epsilon));
|
|
269
|
-
|
|
270
|
-
const geometricMean = Math.exp(
|
|
271
|
-
validSpectrum
|
|
272
|
-
.map(v => Math.log(v))
|
|
273
|
-
.reduce((a, b) => a + b) / validSpectrum.length
|
|
274
|
-
);
|
|
275
|
-
|
|
276
|
-
const arithmeticMean =
|
|
277
|
-
validSpectrum.reduce((a, b) => a + b) / validSpectrum.length;
|
|
278
|
-
|
|
279
|
-
return geometricMean / arithmeticMean;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function computeSpectralRollOff(magnitudeSpectrum, sampleRate) {
|
|
283
|
-
const totalEnergy = magnitudeSpectrum.reduce((a, b) => a + b, 0);
|
|
284
|
-
const rollOffThreshold = totalEnergy * 0.85;
|
|
285
|
-
let cumulativeEnergy = 0;
|
|
286
|
-
|
|
287
|
-
for (let i = 0; i < magnitudeSpectrum.length; i++) {
|
|
288
|
-
cumulativeEnergy += magnitudeSpectrum[i];
|
|
289
|
-
if (cumulativeEnergy >= rollOffThreshold) {
|
|
290
|
-
return (i / magnitudeSpectrum.length) * (sampleRate / 2);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
return 0;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
function computeSpectralBandwidth(magnitudeSpectrum, sampleRate, centroid) {
|
|
298
|
-
const sum = magnitudeSpectrum.reduce((a, b) => a + (b || 0), 0);
|
|
299
|
-
if (sum <= Number.EPSILON) return 0;
|
|
300
|
-
|
|
301
|
-
const weightedSum = magnitudeSpectrum.reduce(
|
|
302
|
-
(acc, value, index) => {
|
|
303
|
-
const freq = index * sampleRate / (2 * magnitudeSpectrum.length);
|
|
304
|
-
return acc + (value || 0) * Math.pow(freq - centroid, 2);
|
|
305
|
-
}, 0
|
|
306
|
-
);
|
|
307
|
-
|
|
308
|
-
return Math.sqrt(weightedSum / sum);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
function computeChroma(segmentData, sampleRate) {
|
|
312
|
-
// Ensure we have valid input data
|
|
313
|
-
if (!segmentData || segmentData.length === 0) {
|
|
314
|
-
return new Array(N_CHROMA).fill(0);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
const fftLength = nextPowerOfTwo(Math.max(segmentData.length, N_FFT));
|
|
318
|
-
const windowed = applyHannWindow(segmentData);
|
|
319
|
-
const padded = new Float32Array(fftLength);
|
|
320
|
-
padded.set(windowed.slice(0, Math.min(windowed.length, fftLength)));
|
|
321
|
-
|
|
322
|
-
const fft = new FFT(fftLength);
|
|
323
|
-
try {
|
|
324
|
-
fft.transform(padded);
|
|
325
|
-
} catch (e) {
|
|
326
|
-
console.error('[Worker] FFT transform failed in chromagram:', e);
|
|
327
|
-
return new Array(N_CHROMA).fill(0);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const chroma = new Float32Array(N_CHROMA).fill(0);
|
|
331
|
-
const freqsPerBin = sampleRate / fftLength;
|
|
332
|
-
let totalEnergy = 0;
|
|
333
|
-
|
|
334
|
-
// First pass: compute magnitudes and total energy
|
|
335
|
-
for (let i = 0; i < fftLength / 2; i++) {
|
|
336
|
-
const freq = i * freqsPerBin;
|
|
337
|
-
if (freq > 20) { // Only consider frequencies above 20 Hz
|
|
338
|
-
const re = padded[2 * i];
|
|
339
|
-
const im = padded[2 * i + 1] || 0;
|
|
340
|
-
const magnitude = Math.sqrt(re * re + im * im);
|
|
341
|
-
|
|
342
|
-
if (magnitude > Number.EPSILON) {
|
|
343
|
-
// Use a more stable pitch class calculation
|
|
344
|
-
const midiNote = 69 + 12 * Math.log2(freq / 440.0);
|
|
345
|
-
const pitchClass = Math.round(midiNote) % 12;
|
|
346
|
-
|
|
347
|
-
if (pitchClass >= 0 && pitchClass < 12) {
|
|
348
|
-
chroma[pitchClass] += magnitude;
|
|
349
|
-
totalEnergy += magnitude;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
// Normalize chroma values only if we have energy
|
|
356
|
-
if (totalEnergy > Number.EPSILON) {
|
|
357
|
-
for (let i = 0; i < N_CHROMA; i++) {
|
|
358
|
-
chroma[i] = chroma[i] / totalEnergy;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// Convert to regular array and ensure no NaN values
|
|
363
|
-
return Array.from(chroma, v => isNaN(v) ? 0 : v);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function extractHNR(segmentData) {
|
|
367
|
-
const frameSize = segmentData.length;
|
|
368
|
-
const autocorrelation = new Float32Array(frameSize);
|
|
369
|
-
|
|
370
|
-
// Compute the autocorrelation iteratively
|
|
371
|
-
for (let i = 0; i < frameSize; i++) {
|
|
372
|
-
let sum = 0;
|
|
373
|
-
for (let j = 0; j < frameSize - i; j++) {
|
|
374
|
-
sum += segmentData[j] * segmentData[j + i];
|
|
375
|
-
}
|
|
376
|
-
autocorrelation[i] = sum;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
// Find the maximum autocorrelation value iteratively
|
|
380
|
-
let maxAutocorrelation = -Infinity;
|
|
381
|
-
for (let i = 1; i < autocorrelation.length; i++) {
|
|
382
|
-
if (autocorrelation[i] > maxAutocorrelation) {
|
|
383
|
-
maxAutocorrelation = autocorrelation[i];
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// Compute the HNR
|
|
388
|
-
return autocorrelation[0] !== 0
|
|
389
|
-
? 10 * Math.log10(maxAutocorrelation / (autocorrelation[0] - maxAutocorrelation))
|
|
390
|
-
: 0;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
function estimatePitch(segment, sampleRate) {
|
|
394
|
-
// Early validation
|
|
395
|
-
if (!segment || segment.length < 2 || !sampleRate) return 0;
|
|
396
|
-
|
|
397
|
-
try {
|
|
398
|
-
// Apply Hann window
|
|
399
|
-
const windowed = applyHannWindow(segment);
|
|
400
|
-
|
|
401
|
-
// Pad for FFT
|
|
402
|
-
const fftLength = nextPowerOfTwo(segment.length * 2);
|
|
403
|
-
const padded = new Float32Array(fftLength);
|
|
404
|
-
padded.set(windowed);
|
|
405
|
-
|
|
406
|
-
// Perform FFT
|
|
407
|
-
const fft = new FFT(fftLength);
|
|
408
|
-
fft.transform(padded);
|
|
409
|
-
|
|
410
|
-
// Compute power spectrum
|
|
411
|
-
const powerSpectrum = new Float32Array(fftLength / 2 + 1);
|
|
412
|
-
for (let i = 0; i <= fftLength / 2; i++) {
|
|
413
|
-
const re = padded[2 * i];
|
|
414
|
-
const im = padded[2 * i + 1] || 0;
|
|
415
|
-
powerSpectrum[i] = re * re + im * im;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// Find peak frequency
|
|
419
|
-
let maxPower = 0;
|
|
420
|
-
let peakIndex = 0;
|
|
421
|
-
const minFreq = 50; // Minimum frequency to consider (Hz)
|
|
422
|
-
const maxFreq = 1000; // Maximum frequency to consider (Hz)
|
|
423
|
-
const minBin = Math.floor(minFreq * fftLength / sampleRate);
|
|
424
|
-
const maxBin = Math.ceil(maxFreq * fftLength / sampleRate);
|
|
425
|
-
|
|
426
|
-
for (let i = minBin; i <= maxBin; i++) {
|
|
427
|
-
if (powerSpectrum[i] > maxPower) {
|
|
428
|
-
maxPower = powerSpectrum[i];
|
|
429
|
-
peakIndex = i;
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
// Convert peak index to frequency
|
|
434
|
-
const fundamentalFreq = peakIndex * sampleRate / fftLength;
|
|
435
|
-
|
|
436
|
-
// Return 0 if the detected frequency is outside reasonable bounds
|
|
437
|
-
return (fundamentalFreq >= minFreq && fundamentalFreq <= maxFreq) ?
|
|
438
|
-
fundamentalFreq : 0;
|
|
439
|
-
|
|
440
|
-
} catch (error) {
|
|
441
|
-
console.error('[Worker] Pitch estimation error:', error);
|
|
442
|
-
return 0;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// Unique ID counter - the only state we need to maintain
|
|
447
|
-
let uniqueIdCounter = 0
|
|
448
|
-
let lastEmitTime = Date.now()
|
|
449
|
-
|
|
450
|
-
self.onmessage = function (event) {
|
|
451
|
-
// Extract enableLogging early so we can use it consistently
|
|
452
|
-
const enableLogging = event.data.enableLogging || false;
|
|
453
|
-
|
|
454
|
-
// Create consistent logger that only logs when enabled
|
|
455
|
-
const logger = enableLogging ? {
|
|
456
|
-
debug: (...args) => console.debug('[Worker]', ...args),
|
|
457
|
-
log: (...args) => console.log('[Worker]', ...args),
|
|
458
|
-
warn: (...args) => console.warn('[Worker]', ...args),
|
|
459
|
-
error: (...args) => console.error('[Worker]', ...args)
|
|
460
|
-
} : {
|
|
461
|
-
debug: () => {},
|
|
462
|
-
log: () => {},
|
|
463
|
-
warn: () => {},
|
|
464
|
-
error: () => {}
|
|
465
|
-
};
|
|
466
|
-
|
|
467
|
-
// Check if this is a reset command
|
|
468
|
-
if (event.data.command === 'resetCounter') {
|
|
469
|
-
const newValue = event.data.value;
|
|
470
|
-
logger.log('Reset counter request received with value:', newValue);
|
|
471
|
-
|
|
472
|
-
// Always respect explicit resets through the resetCounter command
|
|
473
|
-
uniqueIdCounter = typeof newValue === 'number' ? newValue : 0;
|
|
474
|
-
logger.log('Counter explicitly set to:', uniqueIdCounter);
|
|
475
|
-
|
|
476
|
-
return; // Exit early, don't process audio
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// Regular audio processing
|
|
480
|
-
const {
|
|
481
|
-
channelData,
|
|
482
|
-
sampleRate,
|
|
483
|
-
segmentDurationMs,
|
|
484
|
-
algorithm,
|
|
485
|
-
bitDepth,
|
|
486
|
-
fullAudioDurationMs,
|
|
487
|
-
numberOfChannels,
|
|
488
|
-
features: _features,
|
|
489
|
-
intervalAnalysis = 500,
|
|
490
|
-
} = event.data
|
|
491
|
-
|
|
492
|
-
// Calculate subChunkStartTime safely, defaulting to 0 if fullAudioDurationMs is not a valid number
|
|
493
|
-
const subChunkStartTime = (typeof fullAudioDurationMs === 'number' && !isNaN(fullAudioDurationMs) && fullAudioDurationMs >= 0)
|
|
494
|
-
? fullAudioDurationMs / 1000
|
|
495
|
-
: 0;
|
|
496
|
-
|
|
497
|
-
const features = _features || {}
|
|
498
|
-
const bytesPerSample = bitDepth / 8; // Calculate bytes per sample
|
|
499
|
-
|
|
500
|
-
const SILENCE_THRESHOLD = 0.01
|
|
501
|
-
const MIN_SILENCE_DURATION = 1.5 * sampleRate // 1.5 seconds of silence
|
|
502
|
-
const SPEECH_INERTIA_DURATION = 0.1 * sampleRate // Speech inertia duration in samples
|
|
503
|
-
const RMS_THRESHOLD = 0.01
|
|
504
|
-
const ZCR_THRESHOLD = 0.1
|
|
505
|
-
|
|
506
|
-
// Placeholder functions for feature extraction
|
|
507
|
-
const extractMFCC = (segmentData, sampleRate) => {
|
|
508
|
-
// Implement MFCC extraction logic here
|
|
509
|
-
return []
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const extractSpectralCentroid = (segmentData, sampleRate) => {
|
|
513
|
-
const magnitudeSpectrum = segmentData.map((v) => v * v)
|
|
514
|
-
const sum = magnitudeSpectrum.reduce((a, b) => a + b, 0)
|
|
515
|
-
if (sum === 0) return 0
|
|
516
|
-
|
|
517
|
-
const weightedSum = magnitudeSpectrum.reduce(
|
|
518
|
-
(acc, value, index) => acc + index * value,
|
|
519
|
-
0
|
|
520
|
-
)
|
|
521
|
-
return (
|
|
522
|
-
((weightedSum / sum) * (sampleRate / 2)) / magnitudeSpectrum.length
|
|
523
|
-
)
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
const extractSpectralFlatness = (segmentData) => {
|
|
527
|
-
const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))
|
|
528
|
-
const geometricMean = Math.exp(
|
|
529
|
-
magnitudeSpectrum
|
|
530
|
-
.map((v) => Math.log(v + Number.MIN_VALUE))
|
|
531
|
-
.reduce((a, b) => a + b) / magnitudeSpectrum.length
|
|
532
|
-
)
|
|
533
|
-
const arithmeticMean =
|
|
534
|
-
magnitudeSpectrum.reduce((a, b) => a + b) / magnitudeSpectrum.length
|
|
535
|
-
return arithmeticMean === 0 ? 0 : geometricMean / arithmeticMean
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
const extractSpectralRollOff = (segmentData, sampleRate) => {
|
|
539
|
-
const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))
|
|
540
|
-
const totalEnergy = magnitudeSpectrum.reduce((a, b) => a + b, 0)
|
|
541
|
-
const rollOffThreshold = totalEnergy * 0.85
|
|
542
|
-
let cumulativeEnergy = 0
|
|
543
|
-
|
|
544
|
-
for (let i = 0; i < magnitudeSpectrum.length; i++) {
|
|
545
|
-
cumulativeEnergy += magnitudeSpectrum[i]
|
|
546
|
-
if (cumulativeEnergy >= rollOffThreshold) {
|
|
547
|
-
return (i / magnitudeSpectrum.length) * (sampleRate / 2)
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
return 0
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
const extractSpectralBandwidth = (segmentData, sampleRate) => {
|
|
555
|
-
const centroid = extractSpectralCentroid(segmentData, sampleRate)
|
|
556
|
-
const magnitudeSpectrum = segmentData.map((v) => Math.abs(v))
|
|
557
|
-
const sum = magnitudeSpectrum.reduce((a, b) => a + b, 0)
|
|
558
|
-
if (sum === 0) return 0
|
|
559
|
-
|
|
560
|
-
const weightedSum = magnitudeSpectrum.reduce(
|
|
561
|
-
(acc, value, index) => acc + value * Math.pow(index - centroid, 2),
|
|
562
|
-
0
|
|
563
|
-
)
|
|
564
|
-
return Math.sqrt(weightedSum / sum)
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
const extractChromagram = (segmentData, sampleRate) => {
|
|
568
|
-
return [] // TODO implement
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
/**
|
|
572
|
-
* Creates a features object based on requested features
|
|
573
|
-
*/
|
|
574
|
-
function createFeaturesObject(
|
|
575
|
-
features,
|
|
576
|
-
maxAmp,
|
|
577
|
-
rms,
|
|
578
|
-
sumSquares,
|
|
579
|
-
zeroCrossings,
|
|
580
|
-
remainingSamples,
|
|
581
|
-
spectralFeatures,
|
|
582
|
-
channelData,
|
|
583
|
-
startIdx,
|
|
584
|
-
endIdx,
|
|
585
|
-
sampleRate,
|
|
586
|
-
numberOfChannels,
|
|
587
|
-
bytesPerSample
|
|
588
|
-
) {
|
|
589
|
-
// If no features are requested, return undefined
|
|
590
|
-
if (!Object.values(features).some(function(v) { return v; })) {
|
|
591
|
-
return undefined;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
const result = {};
|
|
595
|
-
|
|
596
|
-
if (features.energy) {
|
|
597
|
-
result.energy = sumSquares;
|
|
598
|
-
}
|
|
599
|
-
if (features.rms) {
|
|
600
|
-
result.rms = rms;
|
|
601
|
-
}
|
|
602
|
-
// Always include min/max amplitude if any features are requested
|
|
603
|
-
result.minAmplitude = -maxAmp;
|
|
604
|
-
result.maxAmplitude = maxAmp;
|
|
605
|
-
|
|
606
|
-
if (features.zcr) {
|
|
607
|
-
result.zcr = zeroCrossings / remainingSamples;
|
|
608
|
-
}
|
|
609
|
-
if (features.spectralCentroid) {
|
|
610
|
-
result.spectralCentroid = spectralFeatures.centroid;
|
|
611
|
-
}
|
|
612
|
-
if (features.spectralFlatness) {
|
|
613
|
-
result.spectralFlatness = spectralFeatures.flatness;
|
|
614
|
-
}
|
|
615
|
-
if (features.spectralRolloff) {
|
|
616
|
-
result.spectralRolloff = spectralFeatures.rollOff;
|
|
617
|
-
}
|
|
618
|
-
if (features.spectralBandwidth) {
|
|
619
|
-
result.spectralBandwidth = spectralFeatures.bandwidth;
|
|
620
|
-
}
|
|
621
|
-
if (features.chromagram) {
|
|
622
|
-
result.chromagram = computeChroma(channelData.slice(startIdx, endIdx), sampleRate);
|
|
623
|
-
}
|
|
624
|
-
if (features.hnr) {
|
|
625
|
-
result.hnr = extractHNR(channelData.slice(startIdx, endIdx));
|
|
626
|
-
}
|
|
627
|
-
if (features.pitch) {
|
|
628
|
-
result.pitch = estimatePitch(channelData.slice(startIdx, endIdx), sampleRate);
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
return result;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
function extractWaveform(
|
|
635
|
-
channelData,
|
|
636
|
-
sampleRate,
|
|
637
|
-
segmentDurationMs,
|
|
638
|
-
numberOfChannels,
|
|
639
|
-
bytesPerSample
|
|
640
|
-
) {
|
|
641
|
-
const logger = enableLogging ? {
|
|
642
|
-
debug: (...args) => console.debug('[Worker]', ...args),
|
|
643
|
-
log: (...args) => console.log('[Worker]', ...args),
|
|
644
|
-
error: (...args) => console.error('[Worker]', ...args)
|
|
645
|
-
} : {
|
|
646
|
-
debug: () => {},
|
|
647
|
-
log: () => {},
|
|
648
|
-
error: () => {}
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
// Calculate amplitude range
|
|
652
|
-
let min = Infinity
|
|
653
|
-
let max = -Infinity
|
|
654
|
-
for (let i = 0; i < channelData.length; i++) {
|
|
655
|
-
min = Math.min(min, channelData[i])
|
|
656
|
-
max = Math.max(max, channelData[i])
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
const totalSamples = channelData.length
|
|
660
|
-
const durationMs = (totalSamples / sampleRate) * 1000
|
|
661
|
-
|
|
662
|
-
// Calculate fixed segment sizes
|
|
663
|
-
const samplesPerSegment = Math.floor(sampleRate * (segmentDurationMs / 1000));
|
|
664
|
-
const numPoints = Math.floor(totalSamples / samplesPerSegment);
|
|
665
|
-
const remainingSamples = totalSamples % samplesPerSegment;
|
|
666
|
-
|
|
667
|
-
const dataPoints = []
|
|
668
|
-
|
|
669
|
-
// Process full segments
|
|
670
|
-
for (let i = 0; i < numPoints; i++) {
|
|
671
|
-
const startIdx = i * samplesPerSegment
|
|
672
|
-
const endIdx = startIdx + samplesPerSegment
|
|
673
|
-
|
|
674
|
-
let sumSquares = 0
|
|
675
|
-
let maxAmp = 0
|
|
676
|
-
let zeroCrossings = 0
|
|
677
|
-
|
|
678
|
-
// Calculate segment features
|
|
679
|
-
for (let j = startIdx; j < endIdx; j++) {
|
|
680
|
-
const value = channelData[j]
|
|
681
|
-
sumSquares += value * value
|
|
682
|
-
maxAmp = Math.max(maxAmp, Math.abs(value))
|
|
683
|
-
if (j > 0 && value * channelData[j - 1] < 0) {
|
|
684
|
-
zeroCrossings++
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
const rms = Math.sqrt(sumSquares / samplesPerSegment)
|
|
689
|
-
const startTime = subChunkStartTime + (startIdx / sampleRate)
|
|
690
|
-
const endTime = subChunkStartTime + (endIdx / sampleRate)
|
|
691
|
-
// Calculate byte positions correctly based on numberOfChannels and bytesPerSample
|
|
692
|
-
const startPosition = startIdx * numberOfChannels * bytesPerSample
|
|
693
|
-
const endPosition = endIdx * numberOfChannels * bytesPerSample
|
|
694
|
-
|
|
695
|
-
var spectralFeatures = computeSpectralFeatures(channelData.slice(startIdx, endIdx), sampleRate, features);
|
|
696
|
-
|
|
697
|
-
// Simply use the counter, increment after assigning
|
|
698
|
-
const dataPoint = {
|
|
699
|
-
id: uniqueIdCounter++,
|
|
700
|
-
amplitude: maxAmp,
|
|
701
|
-
rms,
|
|
702
|
-
startTime,
|
|
703
|
-
endTime,
|
|
704
|
-
dB: 20 * Math.log10(rms + 1e-6),
|
|
705
|
-
silent: rms < 0.01,
|
|
706
|
-
startPosition,
|
|
707
|
-
endPosition,
|
|
708
|
-
samples: samplesPerSegment,
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
// Extract features if any are requested
|
|
712
|
-
const extractedFeatures = createFeaturesObject(
|
|
713
|
-
features,
|
|
714
|
-
maxAmp,
|
|
715
|
-
rms,
|
|
716
|
-
sumSquares,
|
|
717
|
-
zeroCrossings,
|
|
718
|
-
samplesPerSegment,
|
|
719
|
-
spectralFeatures,
|
|
720
|
-
channelData,
|
|
721
|
-
startIdx,
|
|
722
|
-
endIdx,
|
|
723
|
-
sampleRate,
|
|
724
|
-
numberOfChannels,
|
|
725
|
-
bytesPerSample
|
|
726
|
-
);
|
|
727
|
-
|
|
728
|
-
if (extractedFeatures) {
|
|
729
|
-
dataPoint.features = extractedFeatures;
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
dataPoints.push(dataPoint)
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
// Handle remaining samples if they exist and are enough to process
|
|
736
|
-
if (remainingSamples > samplesPerSegment / 4) { // Only process if we have at least 1/4 of a segment
|
|
737
|
-
const startIdx = numPoints * samplesPerSegment
|
|
738
|
-
const endIdx = totalSamples
|
|
739
|
-
|
|
740
|
-
let sumSquares = 0
|
|
741
|
-
let maxAmp = 0
|
|
742
|
-
let zeroCrossings = 0
|
|
743
|
-
|
|
744
|
-
for (let j = startIdx; j < endIdx; j++) {
|
|
745
|
-
const value = channelData[j]
|
|
746
|
-
sumSquares += value * value
|
|
747
|
-
maxAmp = Math.max(maxAmp, Math.abs(value))
|
|
748
|
-
if (j > 0 && value * channelData[j - 1] < 0) {
|
|
749
|
-
zeroCrossings++
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
const rms = Math.sqrt(sumSquares / remainingSamples)
|
|
754
|
-
const startTime = subChunkStartTime + (startIdx / sampleRate);
|
|
755
|
-
const endTime = subChunkStartTime + (endIdx / sampleRate);
|
|
756
|
-
// Calculate byte positions correctly based on numberOfChannels and bytesPerSample
|
|
757
|
-
const startPosition = startIdx * numberOfChannels * bytesPerSample
|
|
758
|
-
const endPosition = endIdx * numberOfChannels * bytesPerSample
|
|
759
|
-
|
|
760
|
-
var spectralFeatures = computeSpectralFeatures(channelData.slice(startIdx, endIdx), sampleRate, features);
|
|
761
|
-
|
|
762
|
-
// Simply use the counter, increment after assigning
|
|
763
|
-
const dataPoint = {
|
|
764
|
-
id: uniqueIdCounter++,
|
|
765
|
-
amplitude: maxAmp,
|
|
766
|
-
rms,
|
|
767
|
-
startTime,
|
|
768
|
-
endTime,
|
|
769
|
-
dB: 20 * Math.log10(rms + 1e-6),
|
|
770
|
-
silent: rms < 0.01,
|
|
771
|
-
startPosition,
|
|
772
|
-
endPosition,
|
|
773
|
-
samples: remainingSamples,
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
logger.debug('extractWaveform - dataPoint', dataPoint);
|
|
777
|
-
// Extract features if any are requested
|
|
778
|
-
const extractedFeatures = createFeaturesObject(
|
|
779
|
-
features,
|
|
780
|
-
maxAmp,
|
|
781
|
-
rms,
|
|
782
|
-
sumSquares,
|
|
783
|
-
zeroCrossings,
|
|
784
|
-
remainingSamples,
|
|
785
|
-
spectralFeatures,
|
|
786
|
-
channelData,
|
|
787
|
-
startIdx,
|
|
788
|
-
endIdx,
|
|
789
|
-
sampleRate,
|
|
790
|
-
numberOfChannels,
|
|
791
|
-
bytesPerSample
|
|
792
|
-
);
|
|
793
|
-
|
|
794
|
-
if (extractedFeatures) {
|
|
795
|
-
dataPoint.features = extractedFeatures;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
dataPoints.push(dataPoint)
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
return {
|
|
802
|
-
durationMs,
|
|
803
|
-
dataPoints,
|
|
804
|
-
amplitudeRange: { min, max },
|
|
805
|
-
rmsRange: {
|
|
806
|
-
min: 0,
|
|
807
|
-
max: Math.max(Math.abs(min), Math.abs(max))
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
try {
|
|
813
|
-
// Measure actual processing time using performance.now() for higher precision
|
|
814
|
-
const processingStartTime = performance.now()
|
|
815
|
-
|
|
816
|
-
const result = extractWaveform(
|
|
817
|
-
channelData,
|
|
818
|
-
sampleRate,
|
|
819
|
-
segmentDurationMs,
|
|
820
|
-
numberOfChannels || 1, // Default to 1 channel if not provided
|
|
821
|
-
bytesPerSample
|
|
822
|
-
)
|
|
823
|
-
|
|
824
|
-
const processingEndTime = performance.now()
|
|
825
|
-
const actualExtractionTimeMs = processingEndTime - processingStartTime
|
|
826
|
-
|
|
827
|
-
// Send complete result immediately
|
|
828
|
-
self.postMessage({
|
|
829
|
-
command: 'features',
|
|
830
|
-
result: {
|
|
831
|
-
bitDepth,
|
|
832
|
-
samples: channelData.length,
|
|
833
|
-
numberOfChannels,
|
|
834
|
-
sampleRate,
|
|
835
|
-
segmentDurationMs,
|
|
836
|
-
durationMs: result.durationMs,
|
|
837
|
-
dataPoints: result.dataPoints,
|
|
838
|
-
amplitudeRange: result.amplitudeRange,
|
|
839
|
-
rmsRange: result.rmsRange,
|
|
840
|
-
extractionTimeMs: actualExtractionTimeMs,
|
|
841
|
-
}
|
|
842
|
-
})
|
|
843
|
-
} catch (error) {
|
|
844
|
-
console.error('[Worker] Error', {
|
|
845
|
-
message: error.message,
|
|
846
|
-
stack: error.stack
|
|
847
|
-
});
|
|
848
|
-
|
|
849
|
-
self.postMessage({
|
|
850
|
-
error: {
|
|
851
|
-
message: error.message,
|
|
852
|
-
stack: error.stack,
|
|
853
|
-
name: error.name
|
|
854
|
-
}
|
|
855
|
-
});
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
`;
|
|
859
|
-
//# sourceMappingURL=InlineFeaturesExtractor.web.js.map
|