-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathplay_wave_macosx_channelmap.py
74 lines (54 loc) · 1.54 KB
/
play_wave_macosx_channelmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
PyAudio Example: Mac OS X-only: Play a wave file with channel maps.
"""
import pyaudio
import wave
import sys
chunk = 1024
PyAudio = pyaudio.PyAudio
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
p = PyAudio()
# standard L-R stereo
# channel_map = (0, 1)
# reverse: R-L stereo
# channel_map = (1, 0)
# no audio
# channel_map = (-1, -1)
# left channel audio --> left speaker; no right channel
# channel_map = (0, -1)
# right channel audio --> right speaker; no left channel
# channel_map = (-1, 1)
# left channel audio --> right speaker
# channel_map = (-1, 0)
# right channel audio --> left speaker
channel_map = (1, -1)
# etc...
try:
stream_info = pyaudio.PaMacCoreStreamInfo(
flags=pyaudio.PaMacCoreStreamInfo.paMacCorePlayNice, # default
channel_map=channel_map)
except AttributeError:
print("Sorry, couldn't find PaMacCoreStreamInfo. Make sure that "
"you're running on Mac OS X.")
sys.exit(-1)
print("Stream Info Flags:", stream_info.get_flags())
print("Stream Info Channel Map:", stream_info.get_channel_map())
# open stream
stream = p.open(
format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True,
output_host_api_specific_stream_info=stream_info)
# read data
data = wf.readframes(chunk)
# play stream
while len(data) > 0:
stream.write(data)
data = wf.readframes(chunk)
stream.stop_stream()
stream.close()
p.terminate()