12
A
回答
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相反)的函數
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處理某些情況下)。
0
嘗試迴音巢不怕API:
from echonest import audio
from util import *
def mixSound(fname1,fname2,f_out_name):
f1 = audio.AudioData(fnem1)
f2 = audio.AudioData(fnem2)
f_out = audio.mix(f1,f2)
f_out.encode(foutnem, True)
如果抱怨的編解碼器,檢查https://superuser.com/questions/196857/how-to-install-libmp3lame-for-ffmpeg。
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')
相關問題
- 1. 使用擴展音頻文件服務將兩個文件混合在一起
- 2. 將兩個視頻混合在一起?
- 3. 如何將兩個m4a音頻文件「混合/疊加」在一起
- 4. 使用混音器混合兩個音頻文件
- 5. 將兩個.caf音頻文件合併爲一個iPhone中的音頻文件
- 6. 合併兩個音頻文件一起,在iPhone
- 7. c#在windows phone 8.1中混合兩個音頻文件silverlight project
- 8. 在Java中混合音頻文件
- 9. 兩個音頻文件的頻譜圖(加在一起)
- 10. Android將兩個音頻文件合併爲一個
- 11. 從MP3文件混合兩個音頻樣本
- 12. 用C#將多個音頻樣本混合成單個文件
- 13. Android-如何將音頻和音頻與ffmpeg混合
- 14. 從文件中混合音頻與產生的聲音
- 15. 合併兩個音頻文件FFMpeg acodec
- 16. 軟件音頻混合
- 17. 合併兩個音頻文件合併爲一個目標C
- 18. 如何在iOS中一起播放兩個音頻文件
- 19. 混合音頻android
- 20. Android:混合音頻
- 21. Xuggle將音頻與生成的音頻結合起來
- 22. 兩個音頻文件合併在一個文件中,並保存在磁盤
- 23. 如何在Android中將兩個音頻文件合併爲一個
- 24. iOS - 將兩個視頻合併爲單個帶有音頻的視頻文件
- 25. 如何使用python混合音頻文件?
- 26. 如何將OpenAL的音頻與iPod音樂混合?
- 27. 在Rails中混合視頻和音頻文件
- 28. 混合兩種音頻緩衝,把一個在另一個背景下,通過使用網絡音頻阿比
- 29. 與Java混合音頻(不帶混音器API)
- 30. 混合兩個文件htaccess
感謝code.I會考慮這一點更多,看看它是如何工作的。 – james 2010-10-28 03:25:04