how to mask portions of a .wav file in pytorch

13 Views Asked by At

I have a audio file 'sample.wav' and I am trying to mask timestamp 1:00-1:20 of the audio. The output audio would be the original audio with the 1:00-1:20 timespan silent. I am running the following code but no masking is happening.

import soundfile as sf
import torch

# Load the audio file
audio_path = 'sample.wav'
waveform, sample_rate = sf.read(audio_path)

# Define the start and end indices of the segments you want to mask (in seconds)
start_time = 1.0  
end_time = 1.20   

# Convert time to samples
start_sample = int(start_time * sample_rate)
end_sample = int(end_time * sample_rate)

# Apply mask to the segment
waveform[start_sample:end_sample] = 0  # Zeroing out the segment

output_path = "masked_audio.wav"
sf.write(output_path, waveform, sample_rate)
1

There are 1 best solutions below

0
afsara_ben On

ok it worked when i used the pydub package for audio manipulation

from pydub import AudioSegment

# Load the audio file
audio_path = 'sample.mp3'
audio = AudioSegment.from_file(audio_path, format="mp3")

# Define the start and end times of the segment you want to mask (in milliseconds)
start_time = 1000  # 1 second
end_time = 2500    # 2.5 seconds

# Apply mask to the segment
masked_audio = audio[:start_time] + AudioSegment.silent(duration=(end_time - start_time)) + audio[end_time:]

# Save the masked audio
output_path = "masked_audio.wav"
masked_audio.export(output_path, format="wav")