File: microphone.py

package info (click to toggle)
psychopy 2020.2.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 44,056 kB
  • sloc: python: 119,649; javascript: 3,022; makefile: 148; sh: 125; xml: 9
file content (1203 lines) | stat: -rw-r--r-- 47,807 bytes parent folder | download
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Part of the PsychoPy library
# Copyright (C) 2002-2018 Jonathan Peirce (C) 2019-2020 Open Science Tools Ltd.
# Distributed under the terms of the GNU General Public License (GPL).

"""Audio capture and analysis using pyo"""

# Author: Jeremy R. Gray, March 2012, March 2013

from __future__ import absolute_import, division, print_function

# from future import standard_library
# standard_library.install_aliases()
from builtins import str
from past.builtins import basestring
from builtins import object
import os
import glob
import threading
from psychopy.constants import PY3
from psychopy.tools.filetools import pathToString

if PY3:
    import urllib.request
    import urllib.error
    import urllib.parse
else:
    import urllib2
    # import urllib.request, urllib.error, urllib.parse

    class FakeURLlib(object):

        def __init__(self, lib):
            self.request = lib
            self.error = lib
            self.parse = lib
    urllib = FakeURLlib(urllib2)

import json
import numpy as np
from scipy.io import wavfile
from psychopy import core, logging, web, prefs
from psychopy.sound import backend_pyo
from psychopy.constants import NOT_STARTED, PLAYING, PSYCHOPY_USERAGENT
# import pyo is done within switchOn to better encapsulate it, can be very
# slow and don't want to delay up to 3 sec when importing microphone
# downside: to make this work requires some trickiness with globals

haveMic = False  # goes True in switchOn, if can import pyo

# flac is used for audio compression; user needs to install it
FLAC_PATH = None  # set on first call to _getFlacPath()


class AudioCapture(object):
    """Capture sound sample from the default sound input, and save to a file.

        Untested whether you can have two recordings going on simultaneously.

        **Examples**::

            from psychopy import microphone
            from psychopy import event, visual  # for key events

            microphone.switchOn(sampleRate=16000)  # do once

            # Record for 1.000 seconds, save to mic.savedFile
            mic = microphone.AudioCapture()
            mic.record(1)
            mic.playback()

            # Resample, creates a new file discards orig
            mic.resample(48000, keep=False)

            # Record new file for 60 sec or until key 'q'
            w = visual.Window()  # needed for key-events
            mic.reset()
            mic.record(60, block=False)
            while mic.recorder.running:
                if 'q' in event.getKeys():
                    mic.stop()

        Also see Builder Demo "voiceCapture".

        :Author: Jeremy R. Gray, March 2012
    """

    class _Recorder(object):
        """Class for internal object to make an audio recording using pyo.

        Never needed by end-users; only used internally in __init__:
            self.recorder = _Recorder(None) # instantiate, global
        Then in record(), do:
            self.recorder.run(filename, sec)
        This sets recording parameters, starts recording.
        To stop a recording that is in progress, do
            self.stop()
        This class never handles blocking; AudioCapture has to do that.

        Motivation: Doing pyo Record from within a function worked most of
        the time, but failed catastrophically ~1% of time with a bus error.
        Seemed to be due to a namespace scoping issue, which using globals
        seemed to fix; see pyo mailing list, 7 April 2012. This draws
        heavily on Olivier Belanger's solution.
        """

        def __init__(self):
            self.running = False

        def run(self, filename, sec, sampletype=0, buffering=16,
                chnl=0, chnls=2):
            filename = pathToString(filename)
            self.running = True
            # chnl from psychopy.backend_pyo.get_input_devices()
            inputter = pyo.Input(chnl=chnl, mul=1)
            self.recorder = pyo.Record(inputter, filename, chnls=chnls,
                                       fileformat=0, sampletype=sampletype,
                                       buffering=buffering)
            # handles recording offset
            pyo.Clean_objects(sec, self.recorder).start()
            threading.Timer(sec, self._stop).start()  # set running flag False

        def stop(self):
            self.recorder.stop()
            self._stop()

        def _stop(self):
            self.running = False

    def __init__(self, name='mic', filename='', saveDir='', sampletype=0,
                 buffering=16, chnl=0, stereo=True, autoLog=True):
        """
        :Parameters:
            name :
                Stem for the output file, also used in logging.
            filename :
                optional file name to use; default = 'name-onsetTimeEpoch.wav'
            saveDir :
                Directory to use for output .wav files.
                If a saveDir is given, it will return 'saveDir/file'.
                If no saveDir, then return abspath(file)
            sampletype : bit depth
                pyo recording option:
                0=16 bits int, 1=24 bits int; 2=32 bits int
            buffering : pyo argument
            chnl : which audio input channel to record (default=0)
            stereo : how many channels to record
                (default True, stereo; False = mono)
        """
        if not haveMic:
            raise MicrophoneError('Need to call microphone.switchOn()'
                                  ' before AudioCapture or AdvancedCapture')
        self.name = name
        self.saveDir = saveDir
        filename = pathToString(filename)
        if filename:
            self.wavOutFilename = filename
        else:
            self.wavOutFilename = os.path.join(self.saveDir, name + '.wav')
        if not self.saveDir:
            self.wavOutFilename = os.path.abspath(self.wavOutFilename)
        else:
            if not os.path.isdir(self.saveDir):
                os.makedirs(self.saveDir, 0o770)

        self.onset = None  # becomes onset time, used in filename
        self.savedFile = False  # becomes saved file name
        self.status = NOT_STARTED  # for Builder component

        # pyo server good to go?
        if not pyo.serverCreated():
            raise AttributeError('pyo server not created')
        if not pyo.serverBooted():
            raise AttributeError('pyo server not booted')

        self.autoLog = autoLog
        self.loggingId = self.__class__.__name__
        if self.name:
            self.loggingId += ' ' + self.name

        if type(chnl) != int:
            try:
                chnl = int(chnl)
            except (TypeError, ValueError):
                raise TypeError("AudioCapture argument 'chnl' needs to be an int but received {}".format(repr(chnl)))

        # the recorder object needs to persist, or else get bus errors:
        self.recorder = self._Recorder()
        self.options = {'sampletype': sampletype, 'buffering': buffering,
                        'chnl': chnl, 'chnls': 1 + int(stereo == True)}

    def stop(self, log=True):
        """Interrupt a recording that is in progress; close & keep the file.

        Ends the recording before the duration that was initially specified.
        The same file name is retained, with the same onset time but a
        shorter duration.

        The same recording cannot be resumed after a stop (it is not a pause),
        but you can start a new one.
        """
        if not self.recorder.running:
            if log and self.autoLog:
                msg = '%s: Stop requested, but no record() in progress'
                logging.exp(msg % self.loggingId)
            return
        self.duration = core.getTime() - self.onset  # new shorter duration
        self.recorder.stop()
        if log and self.autoLog:
            msg = '%s: Record stopped early, new duration %.3fs'
            logging.data(msg % (self.loggingId, self.duration))

    def reset(self, log=True):
        """Restores to fresh state, ready to record again
        """
        if log and self.autoLog:
            msg = '%s: resetting at %.3f'
            logging.exp(msg % (self.loggingId, core.getTime()))
        self.__init__(name=self.name, saveDir=self.saveDir)

    def record(self, sec, filename='', block=True):
        """Capture sound input for duration <sec>, save to a file.

        Return the path/name to the new file. Uses onset time (epoch) as
        a meaningful identifier for filename and log.
        """
        return self._record(sec, filename=filename, block=block)

    def _record(self, sec, filename='', block=True, log=True):
        filename = pathToString(filename)
        while self.recorder.running:
            pass
        self.duration = float(sec)
        # for duration estimation, high precision:
        self.onset = core.getTime()
        # use time for unique log and filename, 1 sec precision
        self.fileOnset = core.getAbsTime()
        ms = "%.3f" % (core.getTime() - int(core.getTime()))
        if log and self.autoLog:
            msg = '%s: Record: onset %d, capture %.3fs'
            logging.data(msg % (self.loggingId, self.fileOnset,
                                self.duration))
        if not filename:
            onsettime = '-%d' % self.fileOnset + ms[1:]
            self.savedFile = onsettime.join(
                os.path.splitext(self.wavOutFilename))
        else:
            self.savedFile = os.path.abspath(filename)
            if not self.savedFile.endswith('.wav'):
                self.savedFile += '.wav'

        t0 = core.getTime()
        self.recorder.run(self.savedFile, self.duration, **self.options)

        self.rate = backend_pyo.pyoSndServer.getSamplingRate()
        if block:
            core.wait(self.duration, 0)
            if log and self.autoLog:
                msg = '%s: Record: stop. %.3f, capture %.3fs (est)'
                logging.exp(msg % (self.loggingId, core.getTime(),
                                   core.getTime() - t0))
            while self.recorder.running:
                core.wait(.001, 0)
        else:
            if log and self.autoLog:
                msg = '%s: Record: return immediately, no blocking'
                logging.exp(msg % (self.loggingId))

        return self.savedFile

    def playback(self, block=True, loops=0, stop=False, log=True):
        """Plays the saved .wav file, as just recorded or resampled. Execution
        blocks by default, but can return immediately with `block=False`.

        `loops` : number of extra repetitions; 0 = play once

        `stop` : True = immediately stop ongoing playback (if there is one),
        and return
        """
        if not self.savedFile or not os.path.isfile(self.savedFile):
            msg = '%s: Playback requested but no saved file' % self.loggingId
            logging.error(msg)
            raise ValueError(msg)

        if stop:
            if (hasattr(self, 'current_recording') and
                    self.current_recording.status == PLAYING):
                self.current_recording.stop()
            return

        # play this file:
        name = self.name + '.current_recording'
        self.current_recording = backend_pyo.SoundPyo(
            self.savedFile, name=name, loops=loops)
        self.current_recording.play()
        if block:
            core.wait(self.duration * (loops + 1))  # set during record()

        if log and self.autoLog:
            if loops:
                msg = '%s: Playback: play %.3fs x %d (est) %s'
                vals = (self.loggingId, self.duration, loops + 1,
                        self.savedFile)
                logging.exp(msg % vals)
            else:
                msg = '%s: Playback: play %.3fs (est) %s'
                logging.exp(msg % (self.loggingId, self.duration,
                                   self.savedFile))

    def resample(self, newRate=16000, keep=True, log=True):
        """Re-sample the saved file to a new rate, return the full path.

        Can take several visual frames to resample a 2s recording.

        The default values for resample() are for Google-speech, keeping the
        original (presumably recorded at 48kHz) to archive.
        A warning is generated if the new rate not an integer factor /
        multiple of the old rate.

        To control anti-aliasing, use pyo.downsamp() or upsamp() directly.
        """
        if not self.savedFile or not os.path.isfile(self.savedFile):
            msg = '%s: Re-sample requested but no saved file' % self.loggingId
            logging.error(msg)
            raise ValueError(msg)
        if newRate <= 0 or type(newRate) != int:
            msg = '%s: Re-sample bad new rate = %s' % (self.loggingId,
                                                       repr(newRate))
            logging.error(msg)
            raise ValueError(msg)

        # set-up:
        if self.rate >= newRate:
            ratio = float(self.rate) / newRate
            info = '-ds%i' % ratio
        else:
            ratio = float(newRate) / self.rate
            info = '-us%i' % ratio
        if ratio != int(ratio):
            msg = '%s: old rate is not an integer factor of new rate'
            logging.warn(msg % self.loggingId)
        ratio = int(ratio)
        newFile = info.join(os.path.splitext(self.savedFile))

        # use pyo's downsamp or upsamp based on relative rates:
        if not ratio:
            msg = '%s: Re-sample by %sx is undefined, skipping'
            logging.warn(msg % (self.loggingId, str(ratio)))
        elif self.rate >= newRate:
            t0 = core.getTime()
            # default 128-sample anti-aliasing
            pyo.downsamp(self.savedFile, newFile, ratio)
            if log and self.autoLog:
                msg = '%s: Down-sampled %sx in %.3fs to %s'
                vals = (self.loggingId, str(ratio), core.getTime() - t0,
                        newFile)
                logging.exp(msg % vals)
        else:
            t0 = core.getTime()
            # default 128-sample anti-aliasing
            pyo.upsamp(self.savedFile, newFile, ratio)
            if log and self.autoLog:
                msg = '%s: Up-sampled %sx in %.3fs to %s'
                vals = (self.loggingId, str(ratio), core.getTime() - t0,
                        newFile)
                logging.exp(msg % vals)

        # clean-up:
        if not keep:
            os.unlink(self.savedFile)
            self.savedFile = newFile
            self.rate = newRate

        return os.path.abspath(newFile)


class AdvAudioCapture(AudioCapture):
    """Class extends AudioCapture, plays marker sound as a "start" indicator.

    Has method for retrieving the marker onset time from the file, to allow
    calculation of vocal RT (or other sound-based RT).

    See Coder demo > input > latencyFromTone.py
    """

    def __init__(self, name='advMic', filename='', saveDir='', sampletype=0,
                 buffering=16, chnl=0, stereo=True, autoLog=True):
        AudioCapture.__init__(self, name=name, filename=filename,
                              saveDir=saveDir, sampletype=sampletype,
                              buffering=buffering, chnl=chnl, stereo=stereo)

        self.setMarker()
        self.autoLog = autoLog

    def record(self, sec, filename='', block=False):
        """Starts recording and plays an onset marker tone just prior
        to returning. The idea is that the start of the tone in the
        recording indicates when this method returned, to enable you to sync
        a known recording onset with other events.
        """
        # get effectively the same timing if play after starting the record
        self.playMarker()
        self.filename = self._record(sec, filename=filename, block=block)
        return self.filename

    def setFile(self, filename):
        """Sets the name of the file to work with.
        """
        self.filename = filename

    def setMarker(self, tone=19000, secs=0.015, volume=0.03, log=True):
        """Sets the onset marker, where `tone` is either in hz or a custom
        sound.

        The default tone (19000 Hz) is recommended for auto-detection,
        as being easier to isolate from speech sounds (and so reliable
        to detect). The default duration and volume are appropriate for
        a quiet setting such as a lab testing room. A louder volume, longer
        duration, or both may give better results when recording loud sounds
        or in noisy environments, and will be auto-detected just fine
        (even more easily). If the hardware microphone in use is not
        physically near the speaker hardware, a louder volume is likely
        to be required.

        Custom sounds cannot be auto-detected, but are supported anyway for
        presentation purposes. E.g., a recording of someone saying "go" or
        "stop" could be passed as the onset marker.
        """
        if hasattr(tone, 'play'):
            self.marker_hz = 0
            self.marker = tone
            if log and self.autoLog:
                logging.exp('custom sound set as marker; getMarkerOnset()'
                            ' will not be able to auto-detect onset')
        else:
            self.marker_hz = float(tone)
            sampleRate = backend_pyo.pyoSndServer.getSamplingRate()
            if sampleRate < 2 * self.marker_hz:
                # NyquistError
                msg = ("Recording rate (%i Hz) too slow for %i Hz-based"
                       " marker detection.")
                logging.warning(msg % (int(sampleRate), self.marker_hz))
            if log and self.autoLog:
                msg = 'frequency of recording onset marker: %.1f'
                logging.exp(msg % self.marker_hz)
            self.marker = backend_pyo.SoundPyo(self.marker_hz, secs, volume=volume,
                                      name=self.name + '.marker_tone')

    def playMarker(self):
        """Plays the current marker sound. This is automatically called at the
        start of recording, but can be called anytime to insert a marker.
        """
        self.marker.play()

    def getMarkerInfo(self):
        """Returns (hz, duration, volume) of the marker sound.
        Custom markers always return 0 hz (regardless of the sound).
        """
        dur, vol = self.marker.getDuration(), self.marker.getVolume()
        return self.marker_hz, dur, vol

    def getMarkerOnset(self, chunk=128, secs=0.5, filename=''):
        """Return (onset, offset) time of the first marker within the
        first `secs` of the saved recording.

        Has approx ~1.33ms resolution at 48000Hz, chunk=64. Larger chunks
        can speed up processing times, at a sacrifice of some resolution,
        e.g., to pre-process long recordings with multiple markers.

        If given a filename, it will first set that file as the one to
        work with, and then try to detect the onset marker.
        """
        while self.recorder.running:
            core.wait(0.10, 0)
        if filename:
            self.setFile(filename)
        else:
            filename = self.filename

        return getMarkerOnset(chunk=chunk, secs=secs,
                              filename=filename,
                              marker_hz=self.marker_hz,
                              marker_duration=self.marker.getDuration())

    def getLoudness(self):
        """Return the RMS loudness of the saved recording.
        """
        # use cached value unless the file has changed, based on its mod time:
        try:
            mtime = os.path.getmtime(self.savedFile)
        except (OSError, TypeError):
            logging.error('%s no .savedFile, try again' % self.name)
            core.wait(0.01, 0)
            if not self.savedFile or not os.path.exists(self.savedFile):
                raise ValueError('no such file')
            mtime = os.path.getmtime(self.savedFile)
        if not hasattr(self, 'rms') or self.mtime != mtime:
            self.rms = getRMS(self.savedFile)  # ~3ms for 2s file
            self.mtime = mtime
        return self.rms

    def compress(self, keep=False):
        """Compress using FLAC (lossless compression).
        """
        if os.path.isfile(self.savedFile) and self.savedFile.endswith('.wav'):
            self.savedFile = wav2flac(self.savedFile, keep=keep)

    def uncompress(self, keep=False):
        """Uncompress from FLAC to .wav format.
        """
        isFlac = self.savedFile.endswith('.flac')
        if os.path.isfile(self.savedFile) and isFlac:
            self.savedFile = flac2wav(self.savedFile, keep=keep)


def getMarkerOnset(filename, chunk=128, secs=0.5, marker_hz=19000,
                   marker_duration=0.015):
    """Returns marker sound (onset, offset) in sec, as read from filename.
    """
    def thresh2SD(data, mult=2, thr=None):
        """Return index of first value in abs(data) exceeding 2 * std(data),
        or length of the data + 1 if nothing > threshold

        Return threshold so can re-use the same threshold later
        """
        # this algorithm could use improvement
        data = abs(data)
        if not thr:
            thr = mult * np.std(data)
        val = data[(data > thr)]
        if not len(val):
            return len(data) + 1, thr
        first = val[0]
        for i, v in enumerate(data):
            if v == first:
                return i, thr

    # read data from file:
    data, sampleRate = readWavFile(filename)
    if marker_hz == 0:
        raise ValueError("Custom marker sounds cannot be auto-detected.")
    if sampleRate < 2 * marker_hz:
        # NyquistError
        msg = "Recording rate (%i Hz) too slow for %i Hz-based marker detection."
        raise ValueError(msg % (int(sampleRate), marker_hz))

    # extract onset:
    chunk = max(16, chunk)  # trades-off against size of bandpass filter
    # precision in time-domain (= smaller chunks) requires wider freq
    # {16: 2400, 32: 1200, 64: 600, 128: 300}
    bandSize = 150 * 2 ** (8 - int(np.log2(chunk)))
    dataToUse = data[:int(sampleRate * secs)]  # only look at first secs
    lo = max(0, marker_hz - bandSize)  # for bandpass filter
    hi = marker_hz + bandSize
    dftProfile = getDftBins(dataToUse, sampleRate, lo, hi, chunk)
    # leading edge of startMarker in chunks
    onsetChunks, thr = thresh2SD(dftProfile)
    onsetSecs = onsetChunks * chunk / sampleRate  # in secs

    # extract offset:
    ratio = chunk / sampleRate
    start = onsetChunks - 4
    stop = int(onsetChunks + marker_duration / ratio) + 4
    backwards = dftProfile[max(start, 0):min(stop, len(dftProfile))]
    offChunks, _junk = thresh2SD(backwards[::-1], thr=thr)
    offSecs = (start + len(backwards) - offChunks) * ratio
    # in secs

    return onsetSecs, offSecs


def readWavFile(filename):
    """Return (data, sampleRate) as read from a wav file, expects int16 data.
    """
    filename = pathToString(filename)
    try:
        sampleRate, data = wavfile.read(filename)
    except Exception:
        if os.path.exists(filename) and os.path.isfile(filename):
            core.wait(0.01, 0)
        try:
            sampleRate, data = wavfile.read(filename)
        except Exception:
            msg = 'Failed to open wav sound file "%s"'
            raise SoundFileError(msg % filename)
    if data.dtype != 'int16':
        msg = 'expected `int16` data in .wav file %s'
        raise AttributeError(msg % filename)
    if len(data.shape) == 2 and data.shape[1] == 2:
        data = data.transpose()
        data = data[0]  # left channel only? depends on how the file was made
    return data, sampleRate


def getDftBins(data=None, sampleRate=None, low=100, high=8000, chunk=64):
    """Return DFT (discrete Fourier transform) of ``data``, doing so in
    time-domain bins, each of size ``chunk`` samples.

    e.g., for getting FFT magnitudes in a ms-by-ms manner.

    If given a sampleRate, the data are bandpass filtered (low, high).
    """
    # good to reshape & vectorize data rather than use a python loop
    if data is None:
        data = []
    bins = []
    i = chunk
    if sampleRate:
        # just to get freq vector
        _junk, freq = getDft(data[:chunk], sampleRate)
        band = (freq > low) & (freq < high)  # band (frequency range)
    while i <= len(data):
        magn = getDft(data[i - chunk:i])
        if sampleRate:
            bins.append(np.std(magn[band]))  # filtered by frequency
        else:
            bins.append(np.std(magn))  # unfiltered
        i += chunk
    return np.array(bins)


def getDft(data, sampleRate=None, wantPhase=False):
    """Compute and return magnitudes of numpy.fft.fft() of the data.

    If given a sample rate (samples/sec), will return (magn, freq).
    If wantPhase is True, phase in radians is also returned
    (magn, freq, phase). data should have power-of-2 samples,
    or will be truncated.
    """
    # www.vibrationdata.com/Shock_and_Vibration_Signal_Analysis.pdf
    # and .../python/fft.py
    # truncate to power-of-2 slice; zero-padding to round up is ok too
    samples = 2 ** int(np.log2(len(data)))
    samplesHalf = samples // 2
    dataSlice = data[:samples]

    # get magn & phase from the DFT:
    dft = np.fft.fft(dataSlice)
    dftHalf = dft[:samplesHalf] / samples
    magn = abs(dftHalf) * 2
    magn[0] /= 2.
    if wantPhase:
        phase = np.arctan2(dftHalf.real, dftHalf.imag)  # in radians
    if sampleRate:
        deltaf = sampleRate / samplesHalf / 2.
        freq = np.linspace(0, samplesHalf * deltaf,
                           samplesHalf, endpoint=False)
        if wantPhase:
            return magn, freq, phase
        return magn, freq
    else:
        if wantPhase:
            return magn, phase
        return magn


def getRMSBins(data, chunk=64):
    """Return RMS (loudness) in bins of ``chunk`` samples
    """
    # better to vectorize
    bins = []
    i = chunk
    while i <= len(data):
        r = getRMS(data[i - chunk:i])
        bins.append(r)
        i += chunk
    return np.array(bins)


def getRMS(data):
    """Compute and return the audio power ("loudness").

    Uses numpy.std() as RMS. std() is same as RMS if the mean is 0,
    and .wav data should have a mean of 0.
    Returns an array if given stereo data (RMS computed within-channel).

    `data` can be an array (1D, 2D) or filename; .wav format only.
    data from .wav files will be normalized to -1..+1 before RMS is computed.
    """
    def _rms(data):
        """Audio loudness / power, as rms; ~2x faster than std()
        """
        if len(data.shape) > 1:
            return np.std(data, axis=1)  # np.sqrt(np.mean(data ** 2, axis=1))
        return np.std(data)  # np.sqrt(np.mean(data ** 2))
    if isinstance(data, basestring):
        if not os.path.isfile(data):
            raise ValueError('getRMS: could not find file %s' % data)
        _junk, data = wavfile.read(data)
        data_tr = np.transpose(data)
        data = data_tr / 32768.
    elif not isinstance(data, np.ndarray):
        data = np.array(data).astype(np.float)
    return _rms(data)


class SoundFormatNotSupported(Exception):
    """Class to report an unsupported sound format"""


class SoundFileError(Exception):
    """Class to report sound file failed to load"""


class MicrophoneError(Exception):
    """Class to report a microphone error"""


class _GSQueryThread(threading.Thread):
    """Internal thread class to send a sound file to Google, stash the response.
    """

    def __init__(self, request):
        threading.Thread.__init__(self, None, 'GoogleSpeechQuery', None)

        # request is a previously established urllib2.request() obj, namely:
        # request = urllib2.Request(url, audio, header) at end of
        # Speech2Text.__init__
        self.request = request

        # set vars and flags:
        self.t0 = None
        self.response = None
        self.duration = None
        self.stopflag = False
        self.running = False
        self.timedout = False
        self._reset()

    def _reset(self):
        # whether run() has been started, not thread start():
        self.started = False
        # initialize data fields that will be exposed:
        self.confidence = None
        self.json = None
        self.raw = ''
        self.word = ''
        self.detailed = ''
        self.words = []

    def elapsed(self):
        # report duration depending on the state of the thread:
        if self.started is False:
            return None
        elif self.running:
            return core.getTime() - self.t0
        else:  # whether timed-out or not:
            return self.duration

    def _unpackRaw(self):
        # parse raw url response from google, expose via data fields (see
        # _reset):
        if type(self.raw) != str:
            self.json = json.load(self.raw)
        else:
            self._reset()
            self.status = 'FAILED'
            self.stop()
            return
        self.status = self.json['status']
        report = []
        for utter_list in self.json["hypotheses"]:
            for k in utter_list:
                report.append("%-10s : %s" % (k, utter_list[k]))
                if k == 'confidence':
                    self.conf = self.confidence = float(utter_list[k])
        for key in self.json:
            if key != "hypotheses":
                report.append("%-10s : %s" % (key, self.json[key]))
        self.detailed = '\n'.join(report)
        self.words = tuple([line.split(':')[1].lstrip() for line in report
                            if line.startswith('utterance')])
        if len(self.words):
            self.word = self.words[0]
        else:
            self.word = ''

    def run(self):
        self.t0 = core.getTime()  # before .running goes True
        self.running = True
        self.started = True
        self.duration = 0
        try:
            self.raw = urllib.request.urlopen(self.request)
        except Exception:  # pragma: no cover
            # yeah, its the internet, stuff happens
            # maybe temporary HTTPError: HTTP Error 502: Bad Gateway
            try:
                self.raw = urllib.request.urlopen(self.request)
            except Exception as ex:  # or maybe a dropped connection, etc
                logging.error(str(ex))
                self.running = False  # proceeds as if "timedout"
        self.duration = core.getTime() - self.t0
        # if no one called .stop() in the meantime, unpack the data:
        if self.running:
            self._unpackRaw()
            self.running = False
            self.timedout = False
        else:
            self.timedout = True

    def stop(self):
        self.running = False


class Speech2Text(object):
    """Class for speech-recognition (voice to text), using Google's public API.

        Google's speech API is currently free to use, and seems to work well.
        Intended for within-experiment processing (near real-time, 1-2s delayed), in which
        it's often important to skip a slow or failed response, and not wait a long time;
        `BatchSpeech2Text()` reverses these priorities.

        It is possible (and
        perhaps even likely) that Google will start charging for usage. In addition, they
        can change the interface at any time, including in the middle of an experiment.
        (If so, please post to the user list and we'll try to develop a fix, but
        there could still be some downtime.) Presumably, confidential
        or otherwise sensitive voice data should not be sent to google.

        :Note:

            Requires that flac is installed (free download from  https://xiph.org/flac/download.html).
            If you download and install flac, but get an error that flac is missing,
            try setting the full path to flac in preferences -> general -> flac.

        :Usage:

        a) Always import and make an object; no data are available yet::

            from microphone import Speech2Text
            gs = Speech2Text('speech_clip.wav') # set-up only

        b) Then, either: Initiate a query and wait for response from google (or until the time-out limit is reached). This is "blocking" mode, and is the easiest to do::

            resp = gs.getResponse() # execution blocks here
            print(resp.word, resp.confidence)

        c) Or instead (advanced usage): Initiate a query, but do not wait for a response ("thread" mode: no blocking, no timeout, more control). `running` will change to False when a response is received (or hang indefinitely if something goes wrong--so you might want to implement a time-out as well)::

            resp = gs.getThread() # returns immediately
            while resp.running:
                print('.',) # displays dots while waiting
                sys.stdout.flush()
                core.wait(0.1)
            print(resp.words)

        d) Options: Set-up with a different language for the same speech clip; you'll get a different response (possibly having UTF-8 characters)::

            gs = Speech2Text('speech_clip.wav', lang='ja-JP')
            resp = gs.getResponse()

        :Example:

            See Coder demos / input / speech_recognition.py

        :Known limitations:

            Availability is subject to the whims of google. Any changes google
            makes along the way could either cause complete failure (disruptive),
            or could cause slightly different results to be obtained (without it being
            readily obvious that something had changed). For this reason,
            it's probably a good idea to re-run speech samples through `Speech2Text` at the end of
            a study; see `BatchSpeech2Text()`.

        :Author: Jeremy R. Gray, with thanks to Lefteris Zafiris for his help
            and excellent command-line perl script at https://github.com/zaf/asterisk-speech-recog (GPLv2)
    """

    def __init__(self, filename,
                 lang='en-US',
                 timeout=10,
                 samplingrate=16000,
                 pro_filter=2,
                 level=0):
        """
            :Parameters:

                `filename` : <required>
                    name of the speech file (.flac, .wav, or .spx) to process. wav files will be
                    converted to flac, and for this to work you need to have flac (as an
                    executable). spx format is speex-with-headerbyte (for Google).
                `lang` :
                    the presumed language of the speaker, as a locale code; default 'en-US'
                `timeout` :
                    seconds to wait before giving up, default 10
                `samplingrate` :
                    the sampling rate of the speech clip in Hz, either 16000 or 8000. You can
                    record at a higher rate, and then down-sample to 16000 for speech
                    recognition. `file` is the down-sampled file, not the original.
                    the sampling rate is auto-detected for .wav files.
                `pro_filter` :
                    profanity filter level; default 2 (e.g., f***)
                `level` :
                    flac compression level (0 less compression but fastest)
        """
        # set up some key parameters:
        results = 5  # how many words wanted
        self.timeout = timeout
        useragent = PSYCHOPY_USERAGENT
        host = "www.google.com/speech-api/v1/recognize"

        # determine file type, convert wav to flac if needed:
        if not os.path.isfile(filename):
            raise IOError("Cannot find file: %s" % filename)
        ext = os.path.splitext(filename)[1]
        if ext not in ['.flac', '.wav']:
            raise SoundFormatNotSupported("Unsupported filetype: %s\n" % ext)
        if ext == '.wav':
            _junk, samplingrate = readWavFile(filename)
        if samplingrate not in [16000, 8000]:
            raise SoundFormatNotSupported(
                'Speech2Text sample rate must be 16000 or 8000 Hz')
        self.filename = filename
        if ext == ".flac":
            filetype = "x-flac"
        elif ext == ".wav":  # convert to .flac
            filetype = "x-flac"
            filename = wav2flac(filename, level=level)  # opt for speed
        logging.info("Loading: %s as %s, audio/%s" %
                     (self.filename, lang, filetype))
        # occasional error; core.wait(.1) is not always enough; better slow
        # than fail
        c = 0
        while not os.path.isfile(filename) and c < 10:
            core.wait(.1, 0)
            c += 1
        audio = open(filename, 'rb').read()
        if ext == '.wav' and filename.endswith('.flac'):
            try:
                os.remove(filename)
            except Exception:
                pass

        # urllib2 makes no attempt to validate the server certificate. here's an idea:
        # http://thejosephturner.com/blog/2011/03/19/https-certificate-verification-in-python-with-urllib2/
        # set up the https request:
        url = 'https://' + host + '?xjerr=1&' +\
              'client=psychopy3&' +\
              'lang=' + lang + '&'\
              'pfilter=%d' % pro_filter + '&'\
              'maxresults=%d' % results
        header = {'Content-Type': 'audio/%s; rate=%d' % (filetype, samplingrate),
                  'User-Agent': useragent}
        web.requireInternetAccess()  # needed to access google's speech API
        try:
            self.request = urllib.request.Request(url, audio, header)
        except Exception:  # pragma: no cover
            # try again before accepting defeat
            logging.info("https request failed. %s, %s. trying again..." %
                         (filename, self.filename))
            core.wait(0.2, 0)
            self.request = urllib.request.Request(url, audio, header)

    def getThread(self):
        """Send a query to Google using a new thread, no blocking or timeout.

        Returns a thread which will **eventually** (not immediately) have the speech
        data in its namespace; see getResponse. In theory, you could have several
        threads going simultaneously (almost all the time is spent waiting for a
        response), rather than doing them sequentially (not tested).
        """
        gsqthread = _GSQueryThread(self.request)
        gsqthread.start()
        logging.info("Sending speech-recognition https request to google")
        gsqthread.file = self.filename
        while not gsqthread.running:
            # can return too quickly if thread is slow to start
            core.wait(0.001, 0)
        return gsqthread  # word and time data will eventually be in the namespace

    def getResponse(self):
        """Calls `getThread()`, and then polls the thread until there's a response.

        Will time-out if no response comes within `timeout` seconds. Returns an
        object having the speech data in its namespace. If there's no match,
        generally the values will be equivalent to `None` (e.g., an empty string).

        If you do `resp = getResponse()`, you'll be able to access the data
        in several ways:

            `resp.word` :
                the best match, i.e., the most probably word, or `None`
            `resp.confidence` :
                Google's confidence about `.word`, ranging 0 to 1
            `resp.words` :
                tuple of up to 5 guesses; so `.word` == `.words[0]`
            `resp.raw` :
                the raw response from Google (just a string)
            `resp.json` :
                a parsed version of raw, from `json.load(raw)`
        """
        gsqthread = self.getThread()
        while gsqthread.elapsed() < self.timeout:
            # don't need precise timing to poll an http connection
            core.wait(0.05, 0)
            if not gsqthread.running:
                break
        if gsqthread.running:  # timed out
            gsqthread.status = 408  # same as http code
        return gsqthread  # word and time data are already in the namespace


class BatchSpeech2Text(list):

    def __init__(self, files, threads=3, verbose=False):
        """Like `Speech2Text()`, but takes a list of sound files or a directory name to search
        for matching sound files, and returns a list of `(filename, response)` tuples.
        `response`'s are described in `Speech2Text.getResponse()`.

        Can use up to 5 concurrent threads. Intended for
        post-experiment processing of multiple files, in which waiting for a slow response
        is not a problem (better to get the data).

        If `files` is a string, it will be used as a directory name for glob
        (matching all `*.wav`, `*.flac`, and `*.spx` files).
        There's currently no re-try on http error."""
        list.__init__(self)  # [ (file1, resp1), (file2, resp2), ...]
        maxThreads = min(threads, 5)  # I get http errors with 6
        self.timeout = 30
        if type(files) == str and os.path.isdir(files):
            f = glob.glob(os.path.join(files, '*.wav'))
            f += glob.glob(os.path.join(files, '*.flac'))
            f += glob.glob(os.path.join(files, '*.spx'))
            fileList = f
        else:
            fileList = list(files)
        web.requireInternetAccess()  # needed to access google's speech API
        for i, filename in enumerate(fileList):
            gs = Speech2Text(filename, level=5)
            self.append((filename, gs.getThread()))  # tuple
            if verbose:
                logging.info("%i %s" % (i, filename))
            while self._activeCount() >= maxThreads:
                core.wait(.1, 0)  # idle at max count

    def _activeCount(self):
        # self is a list of (name, thread) tuples; count active threads
        count = len(
            [f for f, t in self if t.running and t.elapsed() <= self.timeout])
        return count


def _getFlacPath(path=None):
    """Return a path to flac binary. Log flac version (if flac was found).
    """
    global FLAC_PATH
    if FLAC_PATH is None:
        if path:
            FLAC_PATH = path
        elif prefs.general['flac']:
            FLAC_PATH = prefs.general['flac']
        else:
            FLAC_PATH = 'flac'
        try:
            version, se = core.shellCall([FLAC_PATH, '-v'], stderr=True)
            if se:
                raise MicrophoneError
        except Exception:
            msg = ("flac not installed (or wrong path in prefs); "
                   "download from https://xiph.org/flac/download.html")
            logging.error(msg)
            raise MicrophoneError(msg)
        logging.info('Using ' + version)
    return FLAC_PATH


def flac2wav(path, keep=True):
    """Uncompress: convert .flac file (on disk) to .wav format (new file).

    If `path` is a directory name, convert all .flac files in the directory.

    `keep` to retain the original .flac file(s), default `True`.
    """
    flac_path = _getFlacPath()
    flac_files = []
    path = pathToString(path)
    if path.endswith('.flac'):
        flac_files = [path]
    elif type(path) == str and os.path.isdir(path):
        flac_files = glob.glob(os.path.join(path, '*.flac'))
    if len(flac_files) == 0:
        logging.warn('failed to find .flac file(s) from %s' % path)
        return None
    wav_files = []
    for flacfile in flac_files:
        wavname = flacfile.strip('.flac') + '.wav'
        flac_cmd = [flac_path, "-d", "--totally-silent",
                    "-f", "-o", wavname, flacfile]
        _junk, se = core.shellCall(flac_cmd, stderr=True)
        if se:
            logging.error(se)
        if not keep:
            os.unlink(flacfile)
        wav_files.append(wavname)
    if len(wav_files) == 1:
        return wav_files[0]
    else:
        return wav_files


def wav2flac(path, keep=True, level=5):
    """Lossless compression: convert .wav file (on disk) to .flac format.

    If `path` is a directory name, convert all .wav files in the directory.

    `keep` to retain the original .wav file(s), default `True`.

    `level` is compression level: 0 is fastest but larger,
        8 is slightly smaller but much slower.
    """
    flac_path = _getFlacPath()
    wav_files = []
    path = pathToString(path)
    if path.endswith('.wav'):
        wav_files = [path]
    elif type(path) == str and os.path.isdir(path):
        wav_files = glob.glob(os.path.join(path, '*.wav'))
    if len(wav_files) == 0:
        logging.warn('failed to find .wav file(s) from %s' % path)
        return None
    flac_files = []
    for wavname in wav_files:
        flacfile = wavname.replace('.wav', '.flac')
        flac_cmd = [flac_path, "-%d" % level, "-f",
                    "--totally-silent", "-o", flacfile, wavname]
        _junk, se = core.shellCall(flac_cmd, stderr=True)
        if se or not os.path.isfile(flacfile):  # just try again
            # ~2% incidence when recording for 1s, 650+ trials
            # never got two in a row; core.wait() does not help
            logging.warn('Failed to convert to .flac; trying again')
            _junk, se = core.shellCall(flac_cmd, stderr=True)
            if se:
                logging.error(se)
        if not keep:
            os.unlink(wavname)
        flac_files.append(flacfile)
    if len(wav_files) == 1:
        return flac_files[0]
    else:
        return flac_files


def switchOn(sampleRate=48000, outputDevice=None, bufferSize=None):
    """You need to switch on the microphone before use, which can take
    several seconds. The only time you can specify the sample rate (in Hz)
    is during switchOn().

    Considerations on the default sample rate 48kHz::

        DVD or video = 48,000
        CD-quality   = 44,100 / 24 bit
        human hearing: ~15,000 (adult); children & young adult higher
        human speech: 100-8,000 (useful for telephone: 100-3,300)
        Google speech API: 16,000 or 8,000 only
        Nyquist frequency: twice the highest rate, good to oversample a bit

    pyo's downsamp() function can reduce 48,000 to 16,000 in about 0.02s
    (uses integer steps sizes). So recording at 48kHz will generate
    high-quality archival data, and permit easy downsampling.

    outputDevice, bufferSize: set these parameters on the pyoSndServer
        before booting; None means use pyo's default values
    """
    # imports pyo, creates sound.pyoSndServer using sound.initPyo() if not yet
    # created
    t0 = core.getTime()
    if prefs.hardware['audioLib'][0] != 'pyo':
        logging.warning("Starting Microphone but sound lib preference is set to be {}. "
                        "Clashes might occur since 'pyo' is not "
                        "preferred lib but is needed for Microphone"
                        .format(prefs.hardware['audioLib']))
    try:
        global pyo
        import pyo
        global haveMic
        haveMic = True
    except ImportError:  # pragma: no cover
        msg = ('Microphone class not available, needs pyo; '
               'see http://ajaxsoundstudio.com/software/pyo/')
        logging.error(msg)
        raise ImportError(msg)
    if pyo.serverCreated():
        backend_pyo.pyoSndServer.setSamplingRate(sampleRate)
    else:
        # backend_pyo.init() will create pyoSndServer. We want there only
        # ever to be one server
        # will automatically use duplex=1 and stereo if poss
        backend_pyo.init(rate=sampleRate)
    if outputDevice:
        backend_pyo.pyoSndServer.setOutputDevice(outputDevice)
    if bufferSize:
        backend_pyo.pyoSndServer.setBufferSize(bufferSize)
    logging.exp('%s: switch on (%dhz) took %.3fs' %
                (__file__.strip('.py'), sampleRate, core.getTime() - t0))


def switchOff():
    """(Not needed as of v1.76.00; kept for backwards compatibility only.)
    """
    logging.info("deprecated:  microphone.switchOff() is no longer needed.")