2017-10-15 19 views
2

ここに初めて投稿すると、これがどのようになるかを見てみましょう。pythonを使用してwavファイルにサイレントフレームを追加

私は、wavファイルの先頭に2番目の無音を追加するスクリプトをPythonで作成しようとしましたが、これまでにうまくいかなかったのです。私がやろうとした何

は、wavファイルのヘッダを読み込み、その後、波モジュールを使用して先頭に\ 0を付加するが、それは非常によく動作しませんでした。ここで私はオーディオな長さは、入力と同じではないとして沈黙を追加しようと、私はエラーを取得する上記のコードを使用して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>) 

ここから基づいていたコードです。

私はオーディオ処理にも非常に新しいので、今は何かを試していて何がスティックであるかを見極めています。

どのアドバイスやアイデアにアプローチするかはすばらしいでしょう:)。

また、私はpythonを使用しています。2.7.5

多くのありがとうございます。

答えて

1

コードの最低額で簡単にオーディオ操作のこれらの種類を行うことができますライブラリがあります。 1つはpydubです。

あなたは以下のようにpydubをインストールすることができますし、依存関係についての詳細はhere
pip install pydub

は、あなたが、(この場合はwav)を別のオーディオ形式を読み込むオーディオセグメントに変換してから操作を実行したりすることができpydubを使用しています単にそれを再生します。

また、設定された期間のサイレント・オーディオ・セグメントを作成し、「+」演算子を持つ2つのセグメントを追加することができます。

ソースコードを一緒進値の束を組み合わせて、特定のオーディオ値を書き出し、その後道に良く見えます

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) 
+0

クール感謝。それを与えて、魅力のように働く。 – Madmax

関連する問題