2013-07-15 302 views
16

tôi đã cố gắng pygame để chơi file wav như thế này:cách phát tệp wav trong python?

import pygame 
pygame.init() 

pygame.mixer.music.load("mysound.wav") 
pygame.mixer.music.play() 
pygame.event.wait() 

nhưng Nó thay đổi giọng nói và tôi không biết tại sao! Tôi đọc các giải pháp this link và không thể giải quyết vấn đề của mình khi phát tệp sóng!

cho giải pháp này Tôi không biết tôi nên nhập gì?

s = Sound() 
s.read('sound.wav') 
s.play() 

và cho điều này giải pháp/dev/dsp doesnt tồn tại trong phiên bản mới của Linux:

from wave import open as waveOpen 
from ossaudiodev import open as ossOpen 
s = waveOpen('tada.wav','rb') 
(nc,sw,fr,nf,comptype, compname) = s.getparams() 
dsp = ossOpen('/dev/dsp','w') 
try: 
    from ossaudiodev import AFMT_S16_NE 
except ImportError: 
    if byteorder == "little": 
    AFMT_S16_NE = ossaudiodev.AFMT_S16_LE 
    else: 
    AFMT_S16_NE = ossaudiodev.AFMT_S16_BE 
dsp.setparameters(AFMT_S16_NE, nc, fr) 
data = s.readframes(nf) 
s.close() 
dsp.write(data) 
dsp.close() 

và khi tôi cố gắng pyglet Nó cho tôi lỗi này:

import pyglet 

music = pyglet.resource.media('mysound.wav') 
music.play() 

pyglet.app.run() 
-------------------------- 

[email protected] Desktop]$ python play.py 
Traceback (most recent call last): 
    File "play.py", line 4, in <module> 
    music = pyglet.resource.media('mysound.wav') 
    File "/usr/lib/python2.7/site-packages/pyglet/resource.py", line 587, in media 
    return media.load(path, streaming=streaming) 
    File "/usr/lib/python2.7/site-packages/pyglet/media/__init__.py", line 1386, in load 
    source = _source_class(filename, file) 
    File "/usr/lib/python2.7/site-packages/pyglet/media/riff.py", line 194, in __init__ 
    format = wave_form.get_format_chunk() 
    File "/usr/lib/python2.7/site-packages/pyglet/media/riff.py", line 174, in get_format_chunk 
    for chunk in self.get_chunks(): 
    File "/usr/lib/python2.7/site-packages/pyglet/media/riff.py", line 110, in get_chunks 
    chunk = cls(self.file, name, length, offset) 
    File "/usr/lib/python2.7/site-packages/pyglet/media/riff.py", line 155, in __init__ 
    raise RIFFFormatException('Size of format chunk is incorrect.') 
pyglet.media.riff.RIFFFormatException: Size of format chunk is incorrect. 
AL lib: ReleaseALC: 1 device not closed 
+0

ý bạn là gì bởi "nó thay đổi giọng nói"? – astrognocci

+0

Nó thay đổi âm thanh của con người thành âm thanh con (xin lỗi tiếng anh của tôi không tốt lắm và tôi không thể tìm được từ thích hợp cho vấn đề của mình) – nim4n

+0

Ý bạn là, âm thanh có độ cao cao hơn nó phải không? – astrognocci

Trả lời

24

Bạn có thể sử dụng PyAudio. Một ví dụ ở đây trên Linux của tôi nó hoạt động:

#!usr/bin/env python 
#coding=utf-8 

import pyaudio 
import wave 

#define stream chunk 
chunk = 1024 

#open a wav format music 
f = wave.open(r"/usr/share/sounds/alsa/Rear_Center.wav","rb") 
#instantiate PyAudio 
p = pyaudio.PyAudio() 
#open stream 
stream = p.open(format = p.get_format_from_width(f.getsampwidth()), 
       channels = f.getnchannels(), 
       rate = f.getframerate(), 
       output = True) 
#read data 
data = f.readframes(chunk) 

#play stream 
while data: 
    stream.write(data) 
    data = f.readframes(chunk) 

#stop stream 
stream.stop_stream() 
stream.close() 

#close PyAudio 
p.terminate() 
+0

Tôi gặp lỗi ALSA http://stackoverflow.com/questions/17137701/pyaudio-alsa-error-messages –

+1

Chỉ một phần bổ sung: thay đổi 'while data! = '':' To ' trong khi len (dữ liệu)> 0: 'giải quyết một vấn đề mà điều này trong khi đi vào một vòng lặp vô hạn. Vấn đề này chỉ xảy ra sau khi cập nhật từ python 3.4 đến 3.5 với pyaudio 0.2.9 Nguồn: [PyAudio Documentation] (https://people.csail.mit.edu/hubert/pyaudio/docs/) –

6

Lý do pygame thay đổi âm thanh của bạn là giá trị mặc định máy trộn với một tỷ lệ 22k mẫu:

initialize the mixer module 
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096): return None 

wav của bạn có lẽ là 8k. Vì vậy, khi pygame chơi nó, nó chơi nhanh gấp hai lần. Vì vậy, chỉ định tần số wav của bạn trong init.

Pyglet có một số vấn đề chính xác khi đọc các tiêu đề RIFF. Nếu bạn có một tệp wav rất cơ bản (với chính xác khối 16 byte fmt) không có thông tin khác trong đoạn fmt (như dữ liệu 'thực tế'), nó hoạt động. Nhưng nó không cung cấp dữ liệu bổ sung trong các khối, vì vậy nó thực sự không tôn trọng đặc tả giao diện RIFF.