I tried pygame for playing wav file like this:
JavaScript
x
7
1
import pygame
2
pygame.init()
3
4
pygame.mixer.music.load("mysound.wav")
5
pygame.mixer.music.play()
6
pygame.event.wait()
7
but It change the voice and I don’t know why! I read this link solutions and can’t solve my problem with playing wave file!
for this solution I dont know what should I import?
JavaScript
1
4
1
s = Sound()
2
s.read('sound.wav')
3
s.play()
4
and for this solution /dev/dsp dosen’t exist in new version of linux :
JavaScript
1
18
18
1
from wave import open as waveOpen
2
from ossaudiodev import open as ossOpen
3
s = waveOpen('tada.wav','rb')
4
(nc,sw,fr,nf,comptype, compname) = s.getparams( )
5
dsp = ossOpen('/dev/dsp','w')
6
try:
7
from ossaudiodev import AFMT_S16_NE
8
except ImportError:
9
if byteorder == "little":
10
AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
11
else:
12
AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
13
dsp.setparameters(AFMT_S16_NE, nc, fr)
14
data = s.readframes(nf)
15
s.close()
16
dsp.write(data)
17
dsp.close()
18
and when I tried pyglet It give me this error:
JavaScript
1
27
27
1
import pyglet
2
3
music = pyglet.resource.media('mysound.wav')
4
music.play()
5
6
pyglet.app.run()
7
--------------------------
8
9
nima@ca005 Desktop]$ python play.py
10
Traceback (most recent call last):
11
File "play.py", line 4, in <module>
12
music = pyglet.resource.media('mysound.wav')
13
File "/usr/lib/python2.7/site-packages/pyglet/resource.py", line 587, in media
14
return media.load(path, streaming=streaming)
15
File "/usr/lib/python2.7/site-packages/pyglet/media/__init__.py", line 1386, in load
16
source = _source_class(filename, file)
17
File "/usr/lib/python2.7/site-packages/pyglet/media/riff.py", line 194, in __init__
18
format = wave_form.get_format_chunk()
19
File "/usr/lib/python2.7/site-packages/pyglet/media/riff.py", line 174, in get_format_chunk
20
for chunk in self.get_chunks():
21
File "/usr/lib/python2.7/site-packages/pyglet/media/riff.py", line 110, in get_chunks
22
chunk = cls(self.file, name, length, offset)
23
File "/usr/lib/python2.7/site-packages/pyglet/media/riff.py", line 155, in __init__
24
raise RIFFFormatException('Size of format chunk is incorrect.')
25
pyglet.media.riff.RIFFFormatException: Size of format chunk is incorrect.
26
AL lib: ReleaseALC: 1 device not closed
27
Advertisement
Answer
You can use PyAudio. An example here on my Linux it works:
JavaScript
1
33
33
1
#!usr/bin/env python
2
#coding=utf-8
3
4
import pyaudio
5
import wave
6
7
#define stream chunk
8
chunk = 1024
9
10
#open a wav format music
11
f = wave.open(r"/usr/share/sounds/alsa/Rear_Center.wav","rb")
12
#instantiate PyAudio
13
p = pyaudio.PyAudio()
14
#open stream
15
stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
16
channels = f.getnchannels(),
17
rate = f.getframerate(),
18
output = True)
19
#read data
20
data = f.readframes(chunk)
21
22
#play stream
23
while data:
24
stream.write(data)
25
data = f.readframes(chunk)
26
27
#stop stream
28
stream.stop_stream()
29
stream.close()
30
31
#close PyAudio
32
p.terminate()
33