Skip to content

Commit 6c041b4

Browse files
committed
refactor: rmsDB -> value, use max power in buffer
1 parent aa585a6 commit 6c041b4

File tree

6 files changed

+16
-39
lines changed

6 files changed

+16
-39
lines changed

android/src/main/java/expo/modules/speechrecognition/ExpoSpeechService.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ class ExpoSpeechService(
466466
val intervalMs = options.volumeChangeEventOptions?.intervalMillis
467467

468468
if (intervalMs == null) {
469-
sendEvent("volumechange", mapOf("rmsdB" to rmsdB))
469+
sendEvent("volumechange", mapOf("value" to rmsdB))
470470
} else {
471471
val currentTime = System.currentTimeMillis()
472472
if (currentTime - lastVolumeChangeEventTime >= intervalMs) {
473-
sendEvent("volumechange", mapOf("rmsdB" to rmsdB))
473+
sendEvent("volumechange", mapOf("value" to rmsdB))
474474
lastVolumeChangeEventTime = currentTime
475475
}
476476
}

example/components/VolumeMeteringAvatar.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ export function VolumeMeteringAvatar() {
4444

4545
useSpeechRecognitionEvent("volumechange", (event) => {
4646
// Don't animate anything if the volume is too low
47-
if (event.rmsdB <= 1) {
47+
if (event.value <= 1) {
4848
return;
4949
}
50+
5051
const newScale = interpolate(
51-
event.rmsdB,
52-
[-2, 10], // The rmsDB range is between -2 and 10
52+
event.value,
53+
[-2, 10], // The value range is between -2 and 10
5354
[minScale, maxScale],
5455
Extrapolation.CLAMP,
5556
);
@@ -61,7 +62,7 @@ export function VolumeMeteringAvatar() {
6162
stiffness: 150,
6263
}),
6364
withTiming(minScale, {
64-
duration: 1000,
65+
duration: 500,
6566
easing: Easing.linear,
6667
}),
6768
);
@@ -74,7 +75,7 @@ export function VolumeMeteringAvatar() {
7475
easing: Easing.out(Easing.quad),
7576
}),
7677
withTiming(minScale, {
77-
duration: 400,
78+
duration: 300,
7879
easing: Easing.linear,
7980
}),
8081
);

example/ios/Podfile.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ PODS:
3939
- ReactCommon/turbomodule/bridging
4040
- ReactCommon/turbomodule/core
4141
- Yoga
42-
- ExpoSpeechRecognition (0.2.21):
42+
- ExpoSpeechRecognition (0.2.22):
4343
- ExpoModulesCore
4444
- EXSplashScreen (0.27.5):
4545
- DoubleConversion
@@ -1466,8 +1466,8 @@ SPEC CHECKSUMS:
14661466
ExpoFileSystem: 80bfe850b1f9922c16905822ecbf97acd711dc51
14671467
ExpoFont: 00756e6c796d8f7ee8d211e29c8b619e75cbf238
14681468
ExpoKeepAwake: 3b8815d9dd1d419ee474df004021c69fdd316d08
1469-
ExpoModulesCore: a113755f96c40590671f01cfcdce8ebdf0cf5f83
1470-
ExpoSpeechRecognition: 80eefd4f4bd7541f5fad24744b19a4b36a365414
1469+
ExpoModulesCore: 260ee156852434da26e782bbb993093186c5aade
1470+
ExpoSpeechRecognition: 6ce34ee84af8950f0e472ba6f6b0b3def2553b2a
14711471
EXSplashScreen: a7e8d13c476f9937e39d654af4235758b567a1be
14721472
FBLazyVector: ac12dc084d1c8ec4cc4d7b3cf1b0ebda6dab85af
14731473
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120

ios/ExpoSpeechRecognitionModule.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ public class ExpoSpeechRecognitionModule: Module {
185185
self?.sendEvent("audioend", ["uri": nil])
186186
}
187187
},
188-
volumeChangeHandler: { [weak self] rmsdB in
189-
self?.sendEvent("volumechange", ["rmsdB": rmsdB])
188+
volumeChangeHandler: { [weak self] value in
189+
self?.sendEvent("volumechange", ["value": value])
190190
}
191191
)
192192
} catch {

ios/ExpoSpeechRecognizer.swift

+1-25
Original file line numberDiff line numberDiff line change
@@ -607,23 +607,6 @@ actor ExpoSpeechRecognizer: ObservableObject {
607607
try audioEngine.start()
608608
}
609609

610-
// private static func calculatePower(buffer: AVAudioPCMBuffer) -> Float? {
611-
// guard let channelData = buffer.floatChannelData?[0] else { return nil }
612-
// let frameLength = Int(buffer.frameLength)
613-
//
614-
// var sumOfSquares: Float = 0.0
615-
// for i in 0..<frameLength {
616-
// let sample = channelData[i]
617-
// sumOfSquares += sample * sample
618-
// }
619-
//
620-
// let meanSquare = sumOfSquares / Float(frameLength)
621-
// let rmsEpsilon: Float = 1e-7 // -160 dB
622-
// let rms = sqrt(meanSquare + rmsEpsilon)
623-
//
624-
// return 20.0 * log10(rms)
625-
// }
626-
627610
private static func calculatePower(buffer: AVAudioPCMBuffer) -> Float? {
628611
// let channelCount = Int(buffer.format.channelCount)
629612
let length = vDSP_Length(buffer.frameLength)
@@ -659,14 +642,7 @@ actor ExpoSpeechRecognizer: ObservableObject {
659642
if max < kMinLevel {
660643
max = kMinLevel
661644
}
662-
663-
var rms: Float = 0.0
664-
vDSP_rmsqv(data, strideFrames, &rms, length)
665-
if rms < kMinLevel {
666-
rms = kMinLevel
667-
}
668-
669-
return 20.0 * log10(rms)
645+
return 20.0 * log10(max)
670646
}
671647

672648
/// Downsamples the audio buffer to a file ref

src/ExpoSpeechRecognitionModule.types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ export type ExpoSpeechRecognitionNativeEventMap = {
129129
soundend: null;
130130
volumechange: {
131131
/**
132-
* A value between -2 and 10 indicating the volume of the input audio
132+
* A float value between -2 and 10 indicating the volume of the input audio
133133
*
134134
* Consider anything below 0 to be inaudible
135135
*/
136-
rmsdB: number;
136+
value: number;
137137
};
138138
};
139139

0 commit comments

Comments
 (0)