-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsound4python.py
More file actions
173 lines (139 loc) · 6 KB
/
sound4python.py
File metadata and controls
173 lines (139 loc) · 6 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
try: import tempfile, wave, subprocess, os, signal, struct
except:
print("E: sound4python is unable to import a combination of %s"%
("tempfile, wave, subprocess, os, signal, struct"))
import threading
import scipy.io.wavfile
import numpy as np
import datetime as dt
class Sound4Python(object):
def __init__(self):
self.seekSec = 0
self.FNULL = open(os.devnull,'w')
self.playing = False
self.sec = 0
def launchWithoutConsole(self, args,output=False):
"""Launches args windowless and waits until finished"""
startupinfo = None
if 'STARTUPINFO' in dir(subprocess):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
if output:
return subprocess.Popen(args, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,startupinfo=startupinfo, )
else:
return subprocess.Popen(args, stdin=subprocess.PIPE,
stdout=self.FNULL ,stderr=self.FNULL, startupinfo=startupinfo)
def createMemfile(self, itr,samprate=16000,autoscale=True,output=False):
try: import numpy as np; foundNumpy=True;
except:
foundNumpy=False;
self.itr = itr
self.samprate = samprate
#for now, assume 1-D iterable
mult = 1
if autoscale:
mult = 32768.0 / max(itr)
#mult = 128.0 / max(itr)
#create file in memory
#with tempfile.SpooledTemporaryFile() as memFile:
self.memFile = tempfile.SpooledTemporaryFile()
#create wave write objection pointing to memFile
self.waveWrite = wave.open(self.memFile,'wb')
self.waveWrite.setsampwidth(2) # int16 default
self.waveWrite.setnchannels(1) # mono default
self.waveWrite.setframerate(samprate) # 8kHz default
wroteFrames=False
#Let's try to create sound from NumPy vector
if foundNumpy :
if type(itr)==np.array:
if( itr.ndim == 1 or itr.shape.count(1) == itr.ndim - 1 ):
self.waveWrite.writeframes( (mult*itr.flatten()).astype(np.int16).tostring() )
wroteFrames=True
else: #we have np, but the iterable isn't a vector
self.waveWrite.writeframes( (mult*np.array(itr)).astype(np.int16).tostring() )
wroteFrames=True
if not wroteFrames and not foundNumpy:
#python w/o np doesn't have "short"/"int16", "@h" is "native,aligned short"
self.waveWrite.writeframes( struct.pack(len(itr)*"@h",[int(mult*itm) for itm in itr]) )
wroteFrames=True
if not wroteFrames:
print("E: Unable to create sound. Only 1D numpy arrays and numerical lists are supported.")
self.waveWrite.close()
return None
def _play(self):
#configure the file object, memFile, as if it has just been opened for reading
self.memFile.seek(0)
try:
# getting here means wroteFrames == True
print("\nAttempting to play a mono audio stream of length")
print(" %.2f seconds (%.3f thousand samples at sample rate of %.3f kHz)"%
( 1.0*len(self.itr)/self.samprate , len(self.itr)/1000. , int(self.samprate)/1000.) )
self.p=self.launchWithoutConsole(['sox','-','-d'])
except:
print("E: Unable to launch sox.")
print("E: Please ensure that sox is installed and on the path.")
print("E: Try 'sox -h' to test sox installation.")
self.waveWrite.close()
return None
self.startTime = dt.datetime.now()
try:
self.p.communicate(self.memFile.read())
self.p.wait()
except:
print("E: Unable to send in-memory wave file to stdin of sox subprocess.")
self.waveWrite.close()
return None
def seek(self, sec):
sr = self.wav[0]
self.sec = sec
idx = np.floor(sec * sr)
if idx > self.wav[1].shape[0]:
raise ValueError("tried to seek outside of wav length")
self.seekSec = sec
if len(self.wav[1].shape) == 1:
self.createMemfile(self.wav[1][idx:], self.wav[0])
else:
self.createMemfile(self.wav[1][idx:,0], self.wav[0])
def loadWav(self, wavPath=None):
if wavPath is not None:
self.wavPath = wavPath
self.wav = scipy.io.wavfile.read(self.wavPath)
self.seekSec = 0
if len(self.wav[1].shape) == 1:
self.createMemfile(self.wav[1], self.wav[0])
else:
self.createMemfile(self.wav[1][:,0], self.wav[0])
self.sec = 0
def play(self):
if self.playing:
return
self.playing = True
self.wt = Sound4PythonThread(self)
self.wt.Run()
def terminateProcess(self):
try:
self.p.terminate()
except OSError, e:
if str(e) == "[Errno 3] No such process":
pass
else:
raise e
def stop(self):
self.terminateProcess()
self.seek(0)
self.playing = False
def pause(self):
self.stopTime = dt.datetime.now()
self.terminateProcess()
secDiff = (self.stopTime - self.startTime).total_seconds()
self.seek(self.sec + secDiff)
self.playing = False
class Sound4PythonThread(threading.Thread):
def __init__(self, parent):
threading.Thread.__init__(self)
self.parent = parent
def run(self):
self.parent._play()
def Run(self):
self.start()