2013-06-25 29 views
2

使用pygame調音臺,我打開一個音頻文件並對其進行處理。我無法找到一種方法將「聲音對象」保存到磁盤上的本地文件。pygame調音臺保存音頻到磁盤?

sound_file = "output.mp3" 
    sound = pygame.mixer.Sound(sound_file) 

有沒有辦法做到這一點?我一直在研究pygame混音器文檔,但是我找不到與此相關的任何內容。

回答

2

你的問題是快兩歲了,但如果人們仍然在尋找一個答案:你可以保存 pygame的聲音情況下使用wave模塊(本機Python)。

# create a sound from NumPy array of file 
snd = pygame.mixer.Sound(my_sound_source) 

# open new wave file 
sfile = wave.open('pure_tone.wav', 'w') 

# set the parameters 
sfile.setframerate(SAMPLINGFREQ) 
sfile.setnchannels(NCHANNELS) 
sfile.setsampwidth(2) 

# write raw PyGame sound buffer to wave file 
sfile.writeframesraw(snd.get_buffer().raw) 

# close file 
sfile.close() 

更多信息和例子在GitHub上:https://github.com/esdalmaijer/Save_PyGame_Sound

0

我從來沒有試過這個,所以我只是猜測它可能會工作。 pygame.mixer.Sound對象有一個名爲get_raw()的函數,它返回Python 3.x中的字節數組和Python 2.x中的字符串。我想你可能會使用這些字節來保存你的聲音。

http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound.get_raw

我希望它會是這個樣子:

sound = pygame.mixer.Sound(sound_file) 
... # your code that manipulates the sound 
sound_raw = sound.get_raw() 
file = open("editedsound.mp3", "w") 
file.write(sound_raw) 
file.close() 
+0

它可能不會爲MP3文件格式轉儲數據。它可能是一個wav文件。 –

+0

這不起作用。這裏是一個ipython測試的摘錄: In [23]:sound = pygame.mixer.Sound('FishPolka.mid') In [24]:sr = sound.get_raw() ------- -------------------------------------------------- ------------------ AttributeError的回溯(最近通話最後一個) E:\ Documents和Settings \我\桌面\ () AttributeError:'聲音'對象沒有屬性'get_raw' In [25]:sound.g sound.get_buffer sound.get_length sound.get_num_channels sound.get_volume –

0

這不是一個asnwer。這是一個評論,因爲我上面寫的評論不清楚,因爲我無法弄清楚如何使回車工作。

我的評論是:上述解決方案不起作用。

以下是ipython測試的摘錄。

In [23]: sound = pygame.mixer.Sound('FishPolka.mid') 

In [24]: sr = sound.get_raw() 
--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 

E:\Documents and Settings\Me\Desktop\<ipython console> in <module>() 

AttributeError: 'Sound' object has no attribute 'get_raw' 

In [25]: sound.g 
sound.get_buffer  sound.get_length  sound.get_num_channels sound.get_volume 
+0

您使用的是什麼版本的Pygame?文檔(我特別提到的)明確指出它在Pygame 1.9.2中是新的。 – Haz

+0

感謝您的澄清,Haz。那是好消息。我正在使用In [1]:import pygame In [2]:pygame.version.ver Out [2]:'1.9.1release' –