0

I have an audio file audio.wav and I have an array of time frames looking like this one:

X = [(12.31, 14.), (15.4, 18.9), ...]

These are the time frames that I would like to be completely silent in a .wav audio file that I have. How can I achieve this?

Petar
  • 195
  • 2
  • 14
  • This post may be useful: https://stackoverflow.com/questions/46757852/adding-silent-frame-to-wav-file-using-python It shows how to create a audio frame of duration n that is silent. But how can I plug them in the dataframes that I have? – Petar Feb 19 '21 at 10:35
  • 1
    you should add your comment to question. More people will see it. What `DataFrame` do you mean? If you mean list `X` then maybe you should use it to cut wav to create parts with sound and join with parts with silence. `song[0:12310] + silence_long_14000_minus_12310 + song[14000:15400] + silence_long_18900_minus_15400 + ...` - See `slice` in documentation [pydub](https://github.com/jiaaro/pydub) – furas Feb 19 '21 at 21:36
  • 1
    eventually you can try to replace `song[12310:14000] = silence_long_14000_minus_12310` and `song[15400:18900] = silence_long_18900_minus_15400` – furas Feb 19 '21 at 21:36

1 Answers1

0

Based on your link I see it as

from pydub import AudioSegment

a = AudioSegment.from_wav("audio.wav")

# X = [(12.31, 14.), (15.4, 18.9), ...]

duration = (14.0 - 12.31) * 1000
s1 = AudioSegment.silent(duration)

duration = (18.9 - 15.4) * 1000
s2 = AudioSegment.silent(duration)

b = a[:12310] + s1 + a[14000:15400] + s2 + a[18900:]

b.export('new_audio.wav', format='wav')

now problem is to use for-loop to automate it

I can't test it but I see it as

from pydub import AudioSegment

a = AudioSegment.from_wav("audio.wav")

X = [(12.31, 14.), (15.4, 18.9)]

parts = []

# start for audio
begin = 0

for start, end in X:
    # keep sound before silence
    s = a[begin*1000:start*1000]
    parts.append(s)  
    
    # create silence
    duration = (end - start) * 1000
    s = AudioSegment.silent(duration)
    parts.append(s)

    # value for next loop
    begin = end

# keep part after last silence
parts.append(a[begin*1000:])

# join all parts using standard `sum()` but it need `parts[0]` as start value   
b = sum(parts[1:], parts[0])

# save it
b.export('new_audio.wav', format='wav')
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you. I have a bit of a problem, some of the original audio is lost using this method. – Petar Feb 22 '21 at 08:19
  • Indeed, this method is losing and changing the audio for some reason. Not sure why... – Petar Feb 22 '21 at 08:42
  • I don't have your file and full `X`. Maybe you use wrong values in `X`. You would have to create new question on new page and add example `audio.wav` which you use, `new_audio.wav` which you get and manually created file which you expect. – furas Feb 22 '21 at 18:00