aiy.voice.audio

APIs to record and play audio files.

Note

These APIs are designed for the Voice Kit, but have no dependency on the Voice HAT/Bonnet specifically. However, many of the APIs require some type of sound card attached to the Raspberry Pi that can be detected by the ALSA subsystem.

Recording

aiy.voice.audio.arecord(fmt, filetype='raw', filename=None, device='default')

Returns an arecord command-line command.

Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • filetype – The type of file. Must be either ‘wav’, ‘raw’, ‘voc’, or ‘au’.
  • filename – The audio file to play.
  • device – The PCM device name. Leave as default to use the default ALSA soundcard.
aiy.voice.audio.record_file(fmt, filename, filetype, wait, device='default')

Records an audio file (blocking). The length of the recording is determined by a blocking wait function that you provide. When your wait function finishes, so does this function and the recording.

For an example, see src/examples/voice/voice_recorder.py.

Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • filename – The file where the recording should be saved.
  • filetype – The type of file. Must be either ‘wav’, ‘raw’, ‘voc’, or ‘au’.
  • wait – A blocking function that determines the length of the recording (and thus the length of time that this function is blocking).
  • device – The PCM device name. Leave as default to use the default ALSA soundcard.
aiy.voice.audio.record_file_async(fmt, filename, filetype, device='default')

Records an audio file, asynchronously. To stop the recording, terminate the returned Popen object.

Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • filename – The file where the recording should be saved.
  • filetype – The type of file. Must be either ‘wav’, ‘raw’, ‘voc’, or ‘au’.
  • device – The PCM device name. Leave as default to use the default ALSA soundcard.
Returns:

The Popen object for the subprocess in which audio is recording.

class aiy.voice.audio.Recorder

Bases: object

done()

Stops the recording that started via record().

join()
record(fmt, chunk_duration_sec, device='default', num_chunks=None, on_start=None, on_stop=None, filename=None)

Records audio with the ALSA soundcard driver, via arecord.

Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • chunk_duration_sec – The duration of each audio chunk, in seconds (may be float).
  • device – The PCM device name. Leave as default to use the default ALSA soundcard.
  • num_chunks – The number of chunks to record. Leave as None to instead record indefinitely, until you call done().
  • on_start – A function callback to call when recording starts.
  • on_stop – A function callback to call when recording stops.
  • filename – A filename to use if you want to save the recording as a WAV file.
Yields:

A chunk of audio data. Each chunk size = chunk_duraction_sec * fmt.bytes_per_second

Playback

aiy.voice.audio.aplay(fmt, filetype='raw', filename=None, device='default')

Returns an aplay command-line command.

Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • filetype – The type of file. Must be either ‘wav’, ‘raw’, ‘voc’, or ‘au’.
  • filename – The audio file to play.
  • device – The PCM device name. Leave as default to use the default ALSA soundcard.
aiy.voice.audio.play_raw(fmt, filename_or_data)

Plays raw audio data (blocking).

Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • filename_or_data – The file or bytes to play.
aiy.voice.audio.play_raw_async(fmt, filename_or_data)

Plays raw audio data asynchronously.

Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • filename_or_data – The file or bytes to play.
Returns:

The Popen object for the subprocess in which audio is playing.

aiy.voice.audio.play_wav(filename_or_data)

Plays a WAV file or data (blocking).

Parameters:filename_or_data – The WAV file or bytes to play.
aiy.voice.audio.play_wav_async(filename_or_data)

Plays a WAV file or data asynchronously.

Parameters:filename_or_data – The WAV file or bytes to play.
Returns:The Popen object for the subprocess in which audio is playing.
class aiy.voice.audio.BytesPlayer

Bases: aiy.voice.audio.Player

Plays audio from a given byte data source.

join()
play(fmt, device='default')
Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • device – The PCM device name. Leave as default to use the default ALSA soundcard.
Returns:

A closure with an inner function push() that accepts the byte data.

class aiy.voice.audio.FilePlayer

Bases: aiy.voice.audio.Player

Plays audio from a file.

join()
play_raw(fmt, filename, device='default')

Plays a raw audio file.

Parameters:
  • fmt – The audio format; an instance of AudioFormat.
  • filename – The audio file to play.
  • device – The PCM device name. Leave as default to use the default ALSA soundcard.
play_wav(filename, device='default')

Plays a WAV file.

Parameters:
  • filename – The WAV file to play.
  • device – The PCM device name. Leave as default to use the default ALSA soundcard.

Audio format

aiy.voice.audio.wave_set_format(wav_file, fmt)

Sets the format for the given WAV file, using the given AudioFormat.

Parameters:
  • wav_file – A wave.Wave_write object.
  • fmt – A AudioFormat object.
aiy.voice.audio.wave_get_format(wav_file)

Returns the AudioFormat corresponding to the WAV file provided.

Parameters:wav_file – A wave.Wave_read object.
class aiy.voice.audio.AudioFormat

Bases: aiy.voice.audio.AudioFormat

CD = AudioFormat(sample_rate_hz=44100, num_channels=2, bytes_per_sample=2)
bytes_per_second