Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

audio - Adding silent frame to wav file using python

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 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''
    #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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...