-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.py
792 lines (791 loc) · 32.9 KB
/
sound.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#!/usr/bin/env python3
# coding=UTF-8
# from tkinter import Frame,Tk
# from tkinter import Tk as tkinter.Tk
import pyaudio
import wave
import logsetup
log=logsetup.getlog(__name__)
# logsetup.setlevel('INFO',log) #for this file
logsetup.setlevel('DEBUG',log) #for this file
try:
import asr
log.info("ASR loaded OK")
except Exception as e:
log.error("Exception importing ASR: {}".format(e))
# import soundfile
import file
import sys
import math #?import needed modules
try:
import numpy
except ModuleNotFoundError:
log.error("This isn't going to work, but you can hopefully reboot after"
"installing Numpy.")
raise
try:
_
except:
def _(x):
return x
class AudioInterface(pyaudio.PyAudio):
def stop(self):
self.terminate()
log.info("PyAudio Terminated")
done=close=finished=stop
def __init__(self):
log.debug("PA Version: {}".format(pyaudio.get_portaudio_version()))
log.debug("PA Version: {}".format(pyaudio.get_portaudio_version_text()))
super(AudioInterface,self).__init__()
class SoundSettings(object):
def test(self,pa):
filenameURL="test_{}_{}_{}.wav".format(self.fs,self.sample_format,
self.audio_card_in)
self.print1()
try:
recorder=SoundFileRecorder(filenameURL,pa,self)
recorder.start()
time.sleep(1)
recorder.stop()
log.info("Finished recording!")
except Exception as e:
log.info("Problem recording! %s ({})".format(e))
return 1
player=SoundFilePlayer(filenameURL,pa,self)
# for i in range(5):
try:
player.play()
except BaseException as e:
log.debug("Hey, It didn't work! ({})".format(e))
except:
log.info("Problem playing! %s")
return 1
def print1(self):
values=[]
for setting in ['audio_card_in','sample_format','fs','audio_card_out']:
values+=[setting,getattr(self,setting)]
log.info("{}: {}; {}: {}; {}: {}; {}: {}".format(*values))
def print(self):
for setting in ['audio_card_in','sample_format','fs','audio_card_out']:
log.info("{}: {}".format(setting,getattr(self,setting)))
def default_in(self):
self.audio_card_in=min(self.cards['in'])
def default_out(self):
self.audio_card_out=min(self.cards['out'])
def default_fs(self):
self.fs=max(self.cards['in'][self.audio_card_in])
def default_sf(self):
self.sample_format=min(self.cards['in'][self.audio_card_in][self.fs])
def max_sf(self):
self.sample_format=max(self.cards['in'][self.audio_card_in][self.fs])
def defaults(self):
self.default_in()
self.default_out()
self.default_fs()
self.default_sf()
def next_card_in(self):
ins=sorted(self.cards['in'].keys())
insi=ins.index(self.audio_card_in)
if insi == len(ins)-1:
return 1
else:
self.audio_card_in=ins[insi+1]
log.debug("next_card_in {} (of {})".format(self.audio_card_in,ins))
self.default_fs()
self.default_sf()
def next_card_out(self):
outs=sorted(self.cards['out'].keys())
outsi=outs.index(self.audio_card_out)
if outsi == len(outs)-1:
return 1
else:
self.audio_card_out=outs[outsi+1]
log.debug("next_card_out {} (of {})".format(self.audio_card_out,outs))
# self.default_fs()
# self.default_sf()
def next_fs(self):
log.debug("next_fs")
exit=False
fss=sorted(self.cards['in'][self.audio_card_in].keys(),reverse=True)
fssi=fss.index(self.fs)
if fssi == len(fss)-1:
exit=self.next_card_in()
if exit == False:
self.default_fs()
else:
self.fs=fss[fssi+1]
return exit
def next_sf(self):
log.debug("next_sf")
exit=False
sfs=sorted(self.cards['in'][self.audio_card_in][self.fs],reverse=True)
sfsi=sfs.index(self.sample_format)
if sfsi == len(sfs)-1:
exit=self.next_fs()
if exit == False:
self.default_sf()
else:
self.sample_format=sfs[sfsi+1]
return exit
def next(self):
return self.next_sf()
def getactual(self,test=False):
self.cards={'in':{},'out':{},'dict':{}}
hostinfo = self.pyaudio.get_host_api_info_by_index(0)
numdevices = hostinfo.get('deviceCount')
for i in range(numdevices):
devinfo=self.pyaudio.get_device_info_by_host_api_device_index(0, i)
d={'code':i,'name':devinfo['name']}
if (devinfo.get('maxInputChannels')) > 0: #microphone
log.info("Input Device id {} - {} ({}/{}); channels in: {}; "
"out: {}".format(i,d['name'], devinfo['index'],
numdevices-1, devinfo['maxInputChannels'],
devinfo['maxOutputChannels']))
self.cards['in'][i]={}
if (devinfo.get('maxOutputChannels')) > 0: #speaker
log.info("Output Device id {} - {} ({}/{}); channels in: {}; "
"out: {}".format(i,d['name'], devinfo['index'],
numdevices-1, devinfo['maxInputChannels'],
devinfo['maxOutputChannels']))
self.cards['out'][i]={}
self.cards['dict'][i]=devinfo['name']
for card in self.cards['in'].copy():
self.cards['in'][card]={}
for fs in self.hypothetical['fss']:
self.cards['in'][card][fs]=list()
for sf in self.hypothetical['sample_formats']:
try:
if test:
ifs=self.pyaudio.is_format_supported(rate=fs,
input_device=card,
input_channels=1,
input_format=sf)
self.cards['in'][card][fs].append(sf)
except ValueError as e:
log.info("Config not supported; no worries: rate={}; "
"output_device={}; "
"output_channels=1, "
"output_format={} ({})".format(fs,card,sf,e))
if self.cards['in'][card][fs] == []:
del self.cards['in'][card][fs]
if self.cards['in'][card] == {}:
del self.cards['in'][card]
for card in self.cards['out'].copy():
self.cards['out'][card]={}
for fs in self.hypothetical['fss']:
self.cards['out'][card][fs]=list()
for sf in self.hypothetical['sample_formats']:
try:
if test:
ifs=self.pyaudio.is_format_supported(rate=fs,
output_device=card,
output_channels=1,
output_format=sf)
self.cards['out'][card][fs].append(sf)
except ValueError as e:
log.info("Config not supported; no worries: rate={}; "
"output_device={}; "
"output_channels=1, "
"output_format={} ({})".format(fs,card,sf,e))
if self.cards['out'][card][fs] == []:
del self.cards['out'][card][fs]
if self.cards['out'][card] == {}:
del self.cards['out'][card]
def printactuals(self):
for io in ['in','out']:
for card in self.cards[io]:
log.debug("{} {} ({}):".format(io,self.cards['dict'][card],
card))
for fs in self.cards[io][card]:
for sf in self.cards[io][card][fs]:
log.debug('\t{}_{}'.format(self.hypothetical['fss'][fs],
self.hypothetical['sample_formats'][sf]))
def sethypothetical(self):
self.hypothetical={}
self.hypothetical['fss']={192000:'192khz',
96000:'96khz',
44100:'44.1khz',
28000:'28khz',
8000:'8khz'
}
self.hypothetical['sample_formats']={
# pyaudio.paFloat32:'32 bit float',
pyaudio.paInt32:'32 bit integer',
pyaudio.paInt24:'24 bit integer',
pyaudio.paInt16:'16 bit integer',
# pyaudio.paInt8:'8 bit integer'
}
def makedefaultifnot(self):
if (not hasattr(self,'audio_card_in')
or self.audio_card_in not in self.cards['in']
or self.audio_card_in not in self.cards['dict']
):
self.default_in()
if (not hasattr(self,'fs') or
self.fs not in self.cards['in'][self.audio_card_in]):
self.default_fs()
if (not hasattr(self,'sample_format') or
self.sample_format not in self.cards['in'][self.audio_card_in][
self.fs]):
self.default_sf()
if (not hasattr(self,'audio_card_out')
or self.audio_card_out not in self.cards['out']
or self.audio_card_out not in self.cards['dict']
):
self.default_out()
def check(self):
# log.info(_("Testing speaker settings:"))
# self.max_sf()
try:
ifs=self.pyaudio.is_format_supported(rate=self.fs,
output_device=self.audio_card_out,
output_channels=1,
output_format=self.sample_format)
except ValueError as e:
# log.info("{}; {}".format(e,type(e)))
if 'Device unavailable' in e.args[0]:
self.next_card_out()
self.check()
log.info("Config not supported; no worries: rate={}; "
"output_device={}; "
"output_channels=1, "
"output_format={} ({})".format(self.fs,self.audio_card_out,
self.sample_format,e))
# log.info(_("Testing microphone settings:"))
try:
ifs=self.pyaudio.is_format_supported(rate=self.fs,
input_device=self.audio_card_in,
input_channels=1,
input_format=self.sample_format)
except ValueError as e:
# log.info("{}; {}".format(e,type(e)))
if 'Device unavailable' in e.args[0]:
self.next_card_in()
self.check()
if 'Invalid sample rate' in e.args[0]:
self.next_sf()
# self.next_fs()
self.check()
log.info("Config not supported; no worries: rate={}; "
"output_device={}; "
"output_channels=1, "
"output_format={} ({})".format(self.fs,self.audio_card_in,
self.sample_format,e))
def __init__(self,pyaudio=None):
if not pyaudio:
pyaudio = AudioInterface()
self.pyaudio=pyaudio
self.sethypothetical()
self.getactual()
self.makedefaultifnot()
# self.defaults() #pick best of actuals
self.check()
# self.printactuals()
self.chunk=1024
self.channels=1 #Always record in mono
class SoundFilePlayer(object):
def playcallback(self, in_data, frame_count, time_info, status):
data = self.wf.readframes(frame_count)
return (data, pyaudio.paContinue)
def soundcheck(self,rate,channels):
log.debug("checking for support")
format=self.getformat()
if None in [rate,format,channels]: #This should never happen here.
log.error("Problem playing on {} card on {} channels with {} rate, "
"{} format".format(self.settings.audio_card_out,channels,rate,
format))
return
for setting in [rate,format,channels]:
log.debug(setting)
try:
ok=self.pa.is_format_supported(float(rate),
output_format=format,
output_device=self.settings.audio_card_out,
output_channels=channels)
except TypeError as e:
log.exception("Unsupported config TypeError trying to play sound! %s",
e)
raise
return
except ValueError as e:
log.exception("Unsupported config ValueError trying to play sound! %s",
e)
raise
return
return ok
def paopen(self):
self.paclose()
self.pa = AudioInterface()
def paclose(self):
self.streamclose() #i.e., if there
if hasattr(self,'pa'):
self.pa.close()
def streamopen(self,rate,channels):
if hasattr(self,'stream'):
self.stream.close()
format=self.getformat()
try:
self.stream = self.pa.open(format=format,
output_device_index=self.settings.audio_card_out,
channels=channels,
rate=rate,
output=True)
except ValueError as e:
log.debug("Valuerror: {}".format(e))
raise
return
except OSError as e:
log.debug("OSError: {}".format(e))
if '(no default output device)' in str(e):
self.settings.audio_card_out=None
return
def streamclose(self):
# if hasattr(self,'stream'):
try:
self.stream.stop_stream()
self.stream.close()
except Exception as e:
log.info("Stream didn't stop and close; was it open? {}".format(e))
def getformat(self):
format=self.pa.get_format_from_width(self.wf.getsampwidth())
return max(format,2)
def play(self,event=None):
log.debug("I'm playing the recording now ({})".format(self.filenameURL))
self.streamclose() #just in case
timeout=5 #seconds or None
process=False
# process=True #This takes precedence
thread=False #over this
self.wf = wave.open(self.filenameURL, 'rb')
frames = self.wf.getnframes()
rate=int(self.wf.getframerate())
duration = frames / float(rate)
channels=self.wf.getnchannels()
self.chunk = self.settings.chunk * channels
format = self.getformat()
log.debug("Trying to play with {} card, {} rate, {} format, and "
"{} channels".format(self.settings.audio_card_out,rate,format,
channels))
if self.settings.audio_card_out not in self.settings.cards['out']:
log.debug("{} doesn't seem to be a supported card; giving up."
"".format(self.settings.audio_card_out))
return
elif rate not in self.settings.cards['out'][
self.settings.audio_card_out]:
log.debug("{} doesn't seem to be a supported rate; giving up."
"".format(rate))
return
def block():
self.streamopen(rate,channels)
if not hasattr(self,'stream'):
log.info("Sorry, couldn't make stream!")
return
log.debug("running play block with args "
"output_device_index={}, channels={}, "
"rate={}, duration={}, format={}".format(
self.settings.audio_card_out,
channels,rate,duration,format))
if (format not in self.settings.cards['out'][
self.settings.audio_card_out][rate]
and format != 1): #play this format, for now.
log.debug("{} doesn't seem to be a supported format ({}); "
"giving up.".format(format,format not in self.settings.cards[
'out'][self.settings.audio_card_out][rate]))
return
framesleft = frames
self.data = self.wf.readframes(self.settings.chunk)
while len(self.data) > 0:
try:
self.stream.write(self.data,exception_on_underflow=True)
except:
log.exception("Other exception trying to play "
"sound! %s {}".format(sys.exc_info()[0]))
log.info("The above may indicate a systemic problem!")
# raise
try:
self.data = self.wf.readframes(self.chunk)
except OSError as e:
if "Output underflowed" in e.arg[0]:
self.streamopen(rate,channels)
log.exception("Unexpected exception trying to read "
"frames %s {}".format(sys.exc_info()[0]))
log.info("The above may indicate a systemic problem!")
# raise
log.debug("apparently we're out of data")
self.streamclose()
def callback(): #This just isn't working faithfully, for some reason.
log.info("Playing {} sample rate with card {} on {} channels with "
"{} format".format(rate,self.settings.audio_card_out,channels,
format))
try:
log.debug("trying stream...")
self.stream = self.pa.open(
output_device_index=self.settings.audio_card_out,
format=format,
channels=channels,
rate=rate,
output=True,
stream_callback=self.playcallback)
log.debug("Starting stream...")
self.stream.start_stream()
log.debug("Keeping stream active...")
while (self.stream.is_active()) and (not self.stream.is_stopped()):
try:
log.debug(format/8*1024)
except:
log.error("No stream to write!")
return
time.sleep(0.1)
log.log(3,"Keeping stream active (again)...")
log.log(3,"Stopping stream...")
self.stream.stop_stream()
log.log(3,"Stream stopped.")
except Exception as e:
log.exception("Can't play file! %s",e)
return
if process:
from multiprocessing import Process
log.info("Running as multi-process")
p = Process(target=block)
elif thread:
from threading import Thread
log.info("Running as threaded")
p = Thread(target=block)
else:
log.info("Running in line")
block()
if process or thread:
p.exception = None
try:
p.start()
except BaseException as e:
log.error("Exception!", traceback.format_exc())
p.exception = e
p.join(timeout) #finish this after timeout, in any case
if p.exception:
log.error("Exception2!", traceback.format_exc())
raise p.exception
if process:
p.terminate() #for processes, not threads
self.wf.close()
# soundfile.SoundFile.close()
def __init__(self,filenameURL,pyaudio,settings):
self.pa=pyaudio
self.filenameURL=filenameURL
self.settings=settings
class SoundFileRecorder(object):
def recordcallback(self, in_data, frame_count, time_info, flag):
log.log(3,"Callback Recording!")
if not hasattr(self,'fulldata'):
self.fulldata= in_data
else:
self.fulldata+=in_data
return (self.fulldata, pyaudio.paContinue)
def paopen(self):
log.debug("Initializing PyAudio (but probably should't be...)")
self.paclose()
self.pa = AudioInterface()
def paclose(self):
self.streamclose() #i.e., if there
if hasattr(self,'pa'):
self.pa.close()
def streamclose(self):
if hasattr(self,'stream'):
self.stream.close()
def start(self):
log.log(3,"I'm recording now")
if hasattr(self,'fulldata'):
delattr(self,'fulldata') #let's start each recording afresh.
def callback(self):
log.log(3,"Initializing Callback Recording")
log.log(3,"input card={}({}), rate={}, format={} ({}), channels={}"
"".format(self.settings.cards['dict'][
self.settings.audio_card_in],
self.settings.audio_card_in,
self.settings.hypothetical['fss'][self.settings.fs],
self.settings.hypothetical['sample_formats'][
self.settings.sample_format],
self.settings.sample_format,self.settings.channels))
try:
log.log(3,"opening stream on {}".format(self.pa))
self.stream = self.pa.open(
input_device_index=self.settings.audio_card_in,
format=self.settings.sample_format,
channels=self.settings.channels,
rate=self.settings.fs,
input=True,
stream_callback=self.recordcallback)
log.log(3,"starting stream")
self.stream.start_stream()
log.log(3,"stream started")
except Exception as e:
log.error("Exception in Callback Recording! ({})".format(e))
return
self.fileopen()
"""input=True, p.open() method → stream.read() to read from
microphone. output=True, stream.write() to the speaker."""
def block(self):
self.stream = self.pa.open(format=self.sample_format,
channels=self.settings.channels,
rate=self.settings.fs,
frames_per_buffer=self.settings.chunk,
input=True)
self.frames = []
for i in range(0, int(
self.settings.fs / self.settings.chunk * self.seconds)):
data = self.stream.read(self.settings.chunk)
self.frames.append(data)
if self.callbackrecording==True:
callback(self)
else:
block(self)
def fileopen(self):
#This fn is for recording, not playing
file.remove(self.filenameURL) #don't do this until recording new file.
try:
self.wf = wave.open(self.filenameURL, 'wb')
self.wf.setnchannels(self.settings.channels)
self.wf.setsampwidth(self.pa.get_sample_size(
self.settings.sample_format))
self.wf.setframerate(self.settings.fs)
log.log(3,"File opened to write ({}): {}".format(self.wf,
self.filenameURL))
except:
log.error("Trouble opening file to write ({}): {}".format(self.wf,
self.filenameURL))
def fileclose(self):
while hasattr(self,'stream') and self.stream.is_active():
time.sleep(0.1)
if hasattr(self,'fulldata'):
self.wf.writeframes(self.fulldata)
self.wf.close()
else:
log.error("Nothing recorded!")
def toaudiosample(self):
from pydub import AudioSegment
return AudioSegment(self.fulldata,
frame_rate=self.settings.fs,
sample_width=self.settings.sample_format,
channels=self.settings.channels
)
def stop(self):
log.log(3,"I'm stopping recording now")
if hasattr(self,'stream'):
self.stream.stop_stream()
self.fileclose()
if self.asrOK:
import time
audio = self.toaudiosample()
for asr in [self.asr,
# self.asr1,
self.asr2
]:
start_time = time.time()
# self.filenameURL
start_time = time.time()
# self.filenameURL
# 50% faster with language specified
msg=asr.transcribe(audio, language='en')
end_time=time.time()
log.info("{} ({}s)".format(msg, end_time - start_time))
self.streamclose()
def __init__(self,filenameURL,pyaudio,settings):
log.debug("Initializing Recording to {}".format(filenameURL))
try:
assert 'asr' in sys.modules
self.asr=asr.ASRtoText()
# self.asr1=asr.ASRtoText('base')
self.asr2=asr.ASRtoText('small')
self.asrOK=True
except Exception as e:
log.error("Exception loading ASR: {}".format(e))
self.asrOK=False
self.asrOK=False #for now, to make this public without testing it
self.callbackrecording=True
self.pa=pyaudio
self.settings=settings
self.filenameURL=filenameURL
class BeepGenerator(object):
def play(self):
# self.compile(pitches=str(pitches))
self.stream.write(self.wavdata)
def pause(self):
self.stream.write(chr(0)*self.framespersyl)
def done(self):
self.stream.stop_stream()
self.stream.close()
self.p.stop()
def bitratecheck(self):
if self.hz > self.bitrate:
self.bitrate = self.hz+100
def longer(self):
self.secpersyl*=1.1
self.setparameters()
def shorter(self):
self.secpersyl/=1.1
self.setparameters()
def higher(self):
self.hz*=1.05
self.setparameters()
def lower(self):
self.hz/=1.05
self.setparameters()
def wider(self):
self.deltaHL*=1.3
self.setparameters()
def narrower(self):
self.deltaHL/=1.3
self.setparameters()
def setparameters(self):
self.bitratecheck()
self.framespersyl=int(self.secpersyl*self.bitrate)
self.pitchdict={
'˥':self.hz+self.deltaHL*2,
'˦':self.hz+self.deltaHL,
'˧':self.hz,
'˨':self.hz-self.deltaHL,
'˩':self.hz-self.deltaHL*2,
' ':0,
' ':0
}
def compile(self,pitches='˥˥ ˩˩ ˧˩'):#' ˦˧˨˩'):
self.wavdata=numpy.zeros(int(self.secpersylbreak*self.bitrate))
words=str(pitches).split(' ')
# log.info("words: {}".format(words))
for w in words:
syllables=w.split(' ')
# log.info("syllables: {}".format(syllables))
for syl in syllables:
badchars=set(syl)-set(['˥','˦','˧','˨','˩'])
for c in badchars:
# log.info("replacing {}".format(c))
syl=syl.replace(c,'')
contour=done=0
for n,c in enumerate(syl):
log.info("character: {} ({})".format(c,n))
tohz=fromhz=self.pitchdict[c]
if n+1 < len(syl) and c != syl[n+1]: #not last, not next
tohz=self.pitchdict[syl[n+1]]
contour=1
elif n+1 == len(syl) and c != syl[n-1]: #last of contour
done=1
nframes=self.framespersyl//(len(syl)-contour)
hz=fromhz
if fromhz != tohz:
dhz=tohz-fromhz
hz=numpy.arange(fromhz,tohz-dhz/2,(tohz-fromhz)/nframes/2,
dtype=numpy.float32)
print(hz)
# print(numpy.arange(nframes,dtype=numpy.float32).dtype)
step=1
# log.info("hz: {}-{} (len: {},{}-{})".format(
# fromhz,tohz,len(syl)-contour,nframes,done))
if not done:
self.wavdata=numpy.append(self.wavdata,numpy.sin(
0.8*numpy.pi*(numpy.arange(#0,100,
nframes,dtype=numpy.float32
)*hz)/self.bitrate #/(len(syl)-contour))
)
).astype(numpy.float32)
self.wavdata=numpy.append(self.wavdata,numpy.zeros(int(self.secperwordbreak*self.bitrate),dtype=numpy.float32))
with wave.open('test'+__name__+'.wav','wb') as f:
f.setnchannels(self.settings.channels)
# log.info(self.p.get_sample_size(self.format))
f.setsampwidth(self.p.get_sample_size(self.format))
f.setframerate(self.bitrate)
f.writeframes(
# bytes(
self.wavdata#,'utf-8'
# )
)
f.close()
self.wavdata = self.wavdata.astype(numpy.float32).tobytes()
# self.wavdata = self.wavdata.astype(numpy.float32).tostring()
def __init__(self,pyAudio=None,settings=None):
if not pyAudio:
self.p = AudioInterface() #initialize pyaudio
else:
self.p = pyAudio
if not settings:
self.settings=SoundSettings(self.p)
else:
self.settings=settings
#See https://en.wikipedia.org/wiki/Bit_rate#Audio
self.format=pyaudio.paFloat32
self.bitrate = 64000 #number of frames per second/frameset.
self.hz = 350 #Hz, waves per second, 261.63=C4-note.
self.secpersyl = .4 #seconds to play sound
self.secpersylbreak = .05
self.secperwordbreak = .15
self.deltaHL = 40 #difference between high and low, in hz
self.stream = self.p.open(format = self.format,
channels = 2,
rate = self.bitrate,
output = True)
self.setparameters()
# self.play()
if __name__ == "__main__":
"""Set volume, somehow!!"""
b=BeepGenerator()
b.compile()
b.play()
exit()
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt): #ignore Ctrl-C
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
log.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = handle_exception
# sysdefault=6
# pulse=12
# default=14
# self.sample_format=4
# self.fs=96000
# print(pyaudio.paInt8)
import time
import timeit
pa = AudioInterface()
times=100
# class Test:
# def test1():
# pa.get_device_count()
# def test2():
# pa.get_host_api_count()
# def test3():
# pa.get_default_host_api_info()
# def test4():
# pass #pa.get_host_api_info_by_type(pyaudio.paInDevelopment)
# def test5():
# pa.get_host_api_info_by_index(0)
# def test6():
# pa.get_device_info_by_host_api_device_index(0,0)
# def test7():
# pa.get_device_count()
# def test8():
# pa.get_default_input_device_info()
# def test9():
# pa.get_default_output_device_info()
# def test10():
# pa.get_device_info_by_index(0)
# def test11():
# pa.get_sample_size(1)
# def test12():
# pa.get_format_from_width(1)
# for i in range(1,13):
# cmd=getattr(Test,'test'+str(i))
# # cmd
# out=timeit.timeit(cmd, number=times)
# print(i,out)
# exit()
try:
pa = AudioInterface()
settings=SoundSettings(pa)
time.sleep(1)
done=settings.test(pa)
time.sleep(1)
while done != True:
done=settings.next()
settings.test(pa)
time.sleep(1)
except:
log.error("uncaught exception: %s", traceback.format_exc())