Adding silent frame to wav file using python
Asked Answered
E

1

10

First time posting here, lets see how this goes.

I trying to write a script in python which would add a second of silence in the beginning of the wav file, but so far been unsuccessfully in doing so.

What I was trying to do is read in the wav header and then adding a \0 to the beginning using the wave module but that did not work quite well. Here is the code which is based from here http://andrewslotnick.com/posts/audio-delay-with-python.html

import wave
from audioop import add

def input_wave(filename,frames=10000000): #10000000 is an arbitrary large number of frames
    wave_file = wave.open(filename,'rb')
    params=wave_file.getparams()
    audio=wave_file.readframes(frames)
    wave_file.close()

    return params, audio

#output to file so we can use ipython notebook's Audio widget
def output_wave(audio, params, stem, suffix):
    #dynamically format the filename by passing in data
    filename=stem.replace('.wav','_{}.wav'.format(suffix))
    wave_file = wave.open(filename,'wb')
    wave_file.setparams(params)
    wave_file.writeframes(audio)

# delay the audio
def delay(audio_bytes,params,offset_ms):
    """version 1: delay after 'offset_ms' milliseconds"""
    #calculate the number of bytes which corresponds to the offset in milliseconds
    offset= params[0]*offset_ms*int(params[2]/1000)
    #create some silence
    beginning= b'\0'
    #remove space from the end
    end= audio_bytes        
    return add(audio_bytes, beginning+end, params[0])

audio_params, aduio_bytes = input_wave(<audio_file>)
output_wave(delay(aduio_bytes,audio_params,10000), audio_params, <audio_file>, <audio_file_suffics> )

Using the above code I get an error when I try to add the silence as the audio length is not the same as the input.

I am also quite new to audio processing so now I am just trying anything and see what sticks.

Any advice or ideas how to approach would be great :).

Also I am using python 2.7.5

Many Thanks.

Ensile answered 15/10, 2017 at 17:22 Comment(0)
B
18

There are libraries that can do these kind of audio manipulation easily with least amount of code. One such is pydub.

You can install pydub as below and detail about dependencies are here
pip install pydub

Using pydub, you can read different audio formats (wav in this case), convert them to audio-segment and then perform manipulations or simply play it.

You can also create an silent audio-segment of set period and add two segment with '+' operator.

Source Code

from pydub import AudioSegment
from pydub.playback import play

audio_in_file = "in_sine.wav"
audio_out_file = "out_sine.wav"

# create 1 sec of silence audio segment
one_sec_segment = AudioSegment.silent(duration=1000)  #duration in milliseconds

#read wav file to an audio segment
song = AudioSegment.from_wav(audio_in_file)

#Add above two audio segments    
final_song = one_sec_segment + song

#Either save modified audio
final_song.export(audio_out_file, format="wav")

#Or Play modified audio
play(final_song)
Brundisium answered 17/10, 2017 at 13:52 Comment(2)
Cool thanks that looks way nicer then combining bunch of hex values together and writing out only specific audio value. Gave that a go and works like a charm.Ensile
@Brundisium Amazing. As for the last line which is supposed to play the audio file, I got error AttributeError: 'str' object has no attribute 'export'Milt

© 2022 - 2024 — McMap. All rights reserved.