Repository navigation
fix(audio): scale int16 numpy waveforms to [-1, 1) in load_audio_text_image_video - #3890
Merged
Merged
Conversation
…_image_video A np.int16 array (for example from PyAudio or sounddevice) was converted to an int16 tensor without scaling. The frontend multiplies float waveforms by 32768, so the signal was overdriven and VAD segments changed, and with a sample rate other than 16 kHz the resampler raised a TypeError. Scale int16 arrays by 1/32768 like load_bytes does for raw PCM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A
np.int16array (for example fromsoundfile.read(..., dtype="int16"), PyAudio or sounddevice) was turned into an int16 tensor without scaling, whereasload_bytes, the kaldi_ark branch andfunasr/bin/realtime_ws.pyscale PCM16 by 1/32768. On main:extract_fbank+WavFrontend, the path Paraformer/SenseVoiceinferencetake):NotImplementedError: mean(): could not infer output dtype ... Got: Short, becausewaveform * (1 << 15)wraps in int16 (frontend-level run, no ASR weights);torch.catwith the float cache promotes them), 32768 times too loud, and the segments change;fsother than 16 kHz:TypeError: Expected floating point type for waveform tensor, but received torch.int16from the resampler.int16 numpy arrays are now scaled like the float equivalent. Other dtypes, torch tensors and non-sound
data_typeare unchanged.Type of change
Validation
python -m compileall funasr examples testspython -m pytest tests/test_load_audio_int16_array.py -q: 3 of 5 fail on main, 5 pass.example/vad_example.wav(70 s) read withsoundfile.read(path, dtype="int16"): main returns 3 segments[[70, 26160], [26750, 37670], [38210, 70450]]; with the fix it returns the same 4 segments as the float32 array[[70, 26270], [26770, 37600], [38210, 59550], [59830, 70450]].fs=8000):TypeErroron main; with the fix the same segments as the float32 array at 8 kHz.extract_fbank+WavFrontendwith an int16 array at 16 kHz:NotImplementedErroron main, features computed with the fix.User impact
Raw PCM16 buffers (PyAudio, sounddevice,
soundfilewithdtype="int16") can be passed straight togenerate()and give the same result as float input, at anyfs.Notes for reviewers
np.int16is scaled. torch int16 tensors, int32 and uint8 arrays behave as before (int32 has no single obvious scale; the kaldi_ark branch divides it by 32768).funasr_onnx/funasr_torchload_datapass arrays through unchanged as well (code reading); they are separate packages and not changed here.