2010-10-28 146 views

回答

3

這是非常依賴的格式,這些都是在這裏是如何做到這一點假設2字節寬,小尾數樣本爲例:

import wave 

w1 = wave.open("/path/to/wav/1") 
w2 = wave.open("/path/to/wav/2") 

#get samples formatted as a string. 
samples1 = w1.readframes(w1.getnframes()) 
samples2 = w2.readframes(w2.getnframes()) 

#takes every 2 bytes and groups them together as 1 sample. ("123456" -> ["12", "34", "56"]) 
samples1 = [samples1[i:i+2] for i in xrange(0, len(samples1), 2)] 
samples2 = [samples2[i:i+2] for i in xrange(0, len(samples2), 2)] 

#convert samples from strings to ints 
def bin_to_int(bin): 
    as_int = 0 
    for char in bin[::-1]: #iterate over each char in reverse (because little-endian) 
     #get the integer value of char and assign to the lowest byte of as_int, shifting the rest up 
     as_int <<= 8 
     as_int += ord(char) 
    return as_int 

samples1 = [bin_to_int(s) for s in samples1] #['\x04\x08'] -> [0x0804] 
samples2 = [bin_to_int(s) for s in samples2] 

#average the samples: 
samples_avg = [(s1+s2)/2 for (s1, s2) in zip(samples1, samples2)] 

現在所有剩下要做的就是轉換samples_avg回到二進制字符串並使用wave.writeframes將其寫入文件。這只是我們剛剛做的事情的反面,因此不應該太難弄明白。對於你的int_to_bin函數,你可能會使用函數chr(code),它返回字符代碼爲code(與ord相反)的函數

+0

感謝code.I會考慮這一點更多,看看它是如何工作的。 – james 2010-10-28 03:25:04

4

python解決方案需要numpy和audiolab,但速度很快而簡單:

import numpy as np 
from scikits.audiolab import wavread 

data1, fs1, enc1 = wavread("file1.wav") 
data2, fs2, enc2 = wavread("file2.wav") 

assert fs1 == fs2 
assert enc1 == enc2 
result = 0.5 * data1 + 0.5 * data2 

如果採樣率(FS *)或編碼(ENC *)是不同的,你可能需要一些音頻處理(斷言嚴格來說是太強大了,作爲wavread可以transparantly處理某些情況下)。

14

可以使用pydub庫(光包裝我的標準庫的蟒蛇波模塊周圍寫)做到這一點倒也乾脆:

from pydub import AudioSegment 

sound1 = AudioSegment.from_file("/path/to/my_sound.wav") 
sound2 = AudioSegment.from_file("/path/to/another_sound.wav") 

combined = sound1.overlay(sound2) 

combined.export("/path/to/combined.wav", format='wav') 
相關問題