Skip to content

fix: resample audio when sample rates differ in merge_audio#107

Open
Mr-Neutr0n wants to merge 1 commit into
AIGC-Audio:mainfrom
Mr-Neutr0n:fix/merge-audio-sample-rate
Open

fix: resample audio when sample rates differ in merge_audio#107
Mr-Neutr0n wants to merge 1 commit into
AIGC-Audio:mainfrom
Mr-Neutr0n:fix/merge-audio-sample-rate

Conversation

@Mr-Neutr0n

Copy link
Copy Markdown

Summary

  • The merge_audio() function in audio-chatgpt.py concatenates two audio signals without checking if they have matching sample rates. When sr_1 != sr_2, the first audio segment is written at the wrong sample rate (sr_2), causing it to play back at the wrong speed and pitch, producing corrupted audio output.
  • Added a sample rate check that resamples signal_1 to match sr_2 using numpy linear interpolation before concatenation. This requires no additional dependencies since numpy is already imported.

Details

Before: sr_1 was read from the first audio file but never used or validated. The merged output always used sr_2, so if the two files had different sample rates, the first segment would be distorted.

After: If sr_1 != sr_2, the first signal is resampled to sr_2 using np.interp with linearly spaced indices, preserving the original duration while matching the target sample rate.

Test plan

  • Merge two audio files with the same sample rate and verify output is unchanged
  • Merge two audio files with different sample rates (e.g., 16kHz and 44.1kHz) and verify both segments play at correct speed/pitch
  • Verify the output WAV file has the correct sample rate in its header

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant