From e0f3bf1cbf7272db91b96dad08b37cc24e47e6f8 Mon Sep 17 00:00:00 2001 From: Khushal Agarwal Date: Fri, 11 Apr 2025 13:21:36 +0530 Subject: [PATCH] fix: audio distortion on audio rate change --- package/src/components/Attachment/AudioAttachment.tsx | 2 ++ package/src/hooks/useAudioPlayer.ts | 2 +- package/src/native.ts | 9 ++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/package/src/components/Attachment/AudioAttachment.tsx b/package/src/components/Attachment/AudioAttachment.tsx index f2b5333c38..ad94d4a3c9 100644 --- a/package/src/components/Attachment/AudioAttachment.tsx +++ b/package/src/components/Attachment/AudioAttachment.tsx @@ -184,7 +184,9 @@ export const AudioAttachment = (props: AudioAttachmentProps) => { soundRef.current = await NativeHandlers.Sound.initializeSound( { uri: item.file.uri }, { + pitchCorrectionQuality: 'high', progressUpdateIntervalMillis: 100, + shouldCorrectPitch: true, }, onPlaybackStatusUpdate, ); diff --git a/package/src/hooks/useAudioPlayer.ts b/package/src/hooks/useAudioPlayer.ts index 9ac586132e..a96c5e7500 100644 --- a/package/src/hooks/useAudioPlayer.ts +++ b/package/src/hooks/useAudioPlayer.ts @@ -71,7 +71,7 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => { return; } if (soundRef.current?.setRateAsync) { - await soundRef.current.setRateAsync(speed); + await soundRef.current.setRateAsync(speed, true, 'high'); } }, [isExpoCLI, soundRef], diff --git a/package/src/native.ts b/package/src/native.ts index 04843c2986..a06670dc7c 100644 --- a/package/src/native.ts +++ b/package/src/native.ts @@ -107,9 +107,12 @@ export type PlaybackStatus = { shouldPlay: boolean; }; +export type PitchCorrectionQuality = 'low' | 'medium' | 'high'; + export type AVPlaybackStatusToSet = { isLooping: boolean; isMuted: boolean; + pitchCorrectionQuality: PitchCorrectionQuality; positionMillis: number; progressUpdateIntervalMillis: number; rate: number; @@ -152,7 +155,11 @@ export type SoundReturnType = { seek?: (progress: number, tolerance?: number) => void; setPositionAsync?: (millis: number) => void; setProgressUpdateIntervalAsync?: (progressUpdateIntervalMillis: number) => void; - setRateAsync?: (rate: number) => void; + setRateAsync?: ( + rate: number, + shouldCorrectPitch: boolean, + pitchCorrectionQuality?: PitchCorrectionQuality, + ) => void; soundRef?: React.RefObject; stopAsync?: () => void; style?: StyleProp;