Microphone - for recording sound

The Microphone class provides an interface to audio recording devices connected to the computer. As of now, Psychtoolbox is required to use this feature and must be installed.

Overview

Microphone([device, sampleRateHz, channels, ...])

Class for managing audio capture devices.

Details

class psychopy.sound.microphone.Microphone(device=None, sampleRateHz=None, channels=None, streamBufferSecs=2.0, maxRecordingSize=24000, policyWhenFull='warn', exclusive=False, audioRunMode=0, name='mic', recordingFolder=WindowsPath('C:/Users/runneradmin'), recordingExt='wav', audioLatencyMode=None)[source]

Class for managing audio capture devices.

This class provides a high-level interface for recording audio from a microphone, storing clips, and saving them to files. The actual audio capture is handled by a backend-specific device class.

property audioLatencyMode
bank(tag=None, transcribe=False, **kwargs)[source]

Store current buffer as a clip within the microphone object.

This method is used internally by the Microphone component in Builder, don’t use it for other applications. Either stop() or pause() must be called before calling this method.

Parameters:
  • tag (str or None) – Label for the clip.

  • transcribe (bool or str) – Set to the name of a transcription engine (e.g. “GOOGLE”) to transcribe using that engine, or set as False to not transcribe.

  • kwargs (dict) – Additional keyword arguments to pass to transcribe().

clear()[source]

Wipe all clips. Deletes previously banked audio clips.

close()[source]

Close the microphone device and release any resources. Should be called when finished with the device.

flush()[source]

Get a copy of all banked clips, then clear the clips from storage.

Returns:

A dictionary containing all banked clips.

Return type:

dict

static getAvailableDevices()[source]

Get a list of available microphone devices.

Return type:

list of dict

getClipFilename(tag, i=0)[source]

Get the filename for a particular clip.

Parameters:
  • tag (str) – Tag assigned to the clip when bank was called

  • i (int) – Index of clip within this tag (default is -1, i.e. the last clip)

Returns:

Constructed filename for this clip

Return type:

str

getCurrentVolume()[source]

Get the microphone volume.

Returns:

Current microphone volume (0.0 to 1.0).

Return type:

float

getRecording()[source]

Get the current recording buffer as an AudioClip object.

Returns:

The current recording buffer as an AudioClip object.

Return type:

AudioClip

getTime()[source]

Current time in the timebase used by the microphone device. This is used for timestamping recordings and clips.

Returns:

Current time in seconds.

Return type:

float

property isRecBufferFull
property isRecording
property isStarted
property latencyBias
property maxRecordingSize
open()[source]

Open the microphone device for recording. Must be called before recording can begin.

pause(blockUntilStopped=True, stopTime=None)[source]
property policyWhenFull

Until a file is saved, the audio data from a Microphone needs to be stored in RAM. To avoid a memory leak, we limit the amount which can be stored by a single Microphone object. The policyWhenFull parameter tells the Microphone what to do when it’s reached that limit.

Parameters:

value (str) – One of: - “ignore”: When full, just don’t record any new samples - “warn”: Same as ignore, but will log a warning - “error”: When full, will raise an error - “rolling”: When full, clears the start of the buffer to make room for new samples

poll()[source]

Poll the microphone device for new audio data. This method can be called periodically while recording to check for new audio data.

property recBufferSecs
record(when=None, waitForStart=0, stopTime=None)[source]

Start recording audio from the microphone. The recording will continue until stop() is called, or until the optional stopTime is reached.

Parameters:
  • when (float or None) – Time at which to start recording, in the timebase used by the microphone device. If None (the default), recording will start immediately.

  • waitForStart (float) – If > 0, record() will block until the recording has actually started, and will return the time at which recording started.

  • stopTime (float or None) – Time at which to stop recording from the start of the recording in seconds. If None (the default), recording will continue until stop() is called.

Returns:

The time at which recording started, in the timebase used by the microphone device.

Return type:

float

property recording
reopen()[source]
property sampleRateHz
saveClips(clear=True)[source]

Save all stored clips to audio files.

Parameters:

clear (bool) – If True, clips will be removed from this object once saved to files.

setMaxRecordingSize(value)[source]
setMaxSize(value)
setPolicyWhenFull(value)[source]

Until a file is saved, the audio data from a Microphone needs to be stored in RAM. To avoid a memory leak, we limit the amount which can be stored by a single Microphone object. The policyWhenFull parameter tells the Microphone what to do when it’s reached that limit.

Parameters:

value (str) – One of: - “ignore”: When full, just don’t record any new samples - “warn”: Same as ignore, but will log a warning - “error”: When full, will raise an error - “rolling”: When full, clears the start of the buffer to make room for new samples

start(when=None, waitForStart=0, stopTime=None)[source]

Start recording audio from the microphone. The recording will continue until stop() is called, or until the optional stopTime is reached.

Alias of the record() method.

stop(blockUntilStopped=True, stopTime=None)[source]

Stop recording audio from the microphone.

property streamBufferSecs
property streamStatus

Back to top