Exemple #1
0
    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')
Exemple #2
0
    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))