auditok.io

Module for low-level audio input-output operations.

Class summary

AudioSource([sampling_rate, sample_width, …]) Base class for audio source objects.
Rewindable Base class for rewindable audio streams.
BufferAudioSource(data_buffer[, …]) An AudioSource that encapsulates and reads data from a memory buffer.
WaveAudioSource(filename) A class for an AudioSource that reads data from a wave file.
PyAudioSource([sampling_rate, sample_width, …]) A class for an AudioSource that reads data the built-in microphone using PyAudio.
StdinAudioSource([sampling_rate, …]) A class for an AudioSource that reads data from standard input.
PyAudioPlayer([sampling_rate, sample_width, …]) A class for audio playback using Pyaudio

Function summary

from_file(filename) Create an AudioSource object using the audio file specified by filename.
player_for(audio_source) Return a PyAudioPlayer that can play data from audio_source.
class auditok.io.AudioSource(sampling_rate=16000, sample_width=2, channels=1)[source]

Base class for audio source objects.

Subclasses should implement methods to open/close and audio stream and read the desired amount of audio samples.

Parameters:
sampling_rate : int

Number of samples per second of audio stream. Default = 16000.

sample_width : int

Size in bytes of one audio sample. Possible values : 1, 2, 4. Default = 2.

channels : int

Number of channels of audio stream. The current version supports only mono audio streams (i.e. one channel).

close()[source]

Close audio source

get_channels()[source]

Return the number of channels of this audio source

get_sample_width()[source]

Return the number of bytes used to represent one audio sample

get_sampling_rate()[source]

Return the number of samples per second of audio stream

is_open()[source]

Return True if audio source is open, False otherwise

open()[source]

Open audio source

read(size)[source]

Read and return size audio samples at most.

Parameters:
size : int

the number of samples to read.

Returns:

Audio data as a string of length ‘N’ * ‘smaple_width’ * ‘channels’, where ‘N’ is:

  • size if size < ‘left_samples’
  • ‘left_samples’ if size > ‘left_samples’
class auditok.io.Rewindable[source]

Base class for rewindable audio streams. Subclasses should implement methods to return to the beginning of an audio stream as well as method to move to an absolute audio position expressed in time or in number of samples.

get_position()[source]

Return the total number of already read samples

get_time_position()[source]

Return the total duration in seconds of already read data

rewind()[source]

Go back to the beginning of audio stream

set_position(position)[source]

Move to an absolute position

Parameters:
position : int

number of samples to skip from the start of the stream

set_time_position(time_position)[source]

Move to an absolute position expressed in seconds

Parameters:
time_position : float

seconds to skip from the start of the stream

class auditok.io.BufferAudioSource(data_buffer, sampling_rate=16000, sample_width=2, channels=1)[source]

An AudioSource that encapsulates and reads data from a memory buffer. It implements methods from Rewindable and is therefore a navigable AudioSource.

append_data(data_buffer)[source]

Append data to this audio stream

Parameters:
data_buffer : str, basestring, Bytes

a buffer with a length multiple of (sample_width * channels)

close()[source]

Close audio source

get_data_buffer()[source]

Return all audio data as one string buffer.

get_position()[source]

Return the total number of already read samples

get_time_position()[source]

Return the total duration in seconds of already read data

is_open()[source]

Return True if audio source is open, False otherwise

open()[source]

Open audio source

read(size)[source]

Read and return size audio samples at most.

Parameters:
size : int

the number of samples to read.

Returns:

Audio data as a string of length ‘N’ * ‘smaple_width’ * ‘channels’, where ‘N’ is:

  • size if size < ‘left_samples’
  • ‘left_samples’ if size > ‘left_samples’
rewind()[source]

Go back to the beginning of audio stream

set_data(data_buffer)[source]

Set new data for this audio stream.

Parameters:
data_buffer : str, basestring, Bytes

a string buffer with a length multiple of (sample_width * channels)

set_position(position)[source]

Move to an absolute position

Parameters:
position : int

number of samples to skip from the start of the stream

set_time_position(time_position)[source]

Move to an absolute position expressed in seconds

Parameters:
time_position : float

seconds to skip from the start of the stream

class auditok.io.WaveAudioSource(filename)[source]

A class for an AudioSource that reads data from a wave file.

Parameters:
filename :

path to a valid wave file

close()[source]

Close audio source

is_open()[source]

Return True if audio source is open, False otherwise

open()[source]

Open audio source

read(size)[source]

Read and return size audio samples at most.

Parameters:
size : int

the number of samples to read.

Returns:

Audio data as a string of length ‘N’ * ‘smaple_width’ * ‘channels’, where ‘N’ is:

  • size if size < ‘left_samples’
  • ‘left_samples’ if size > ‘left_samples’
class auditok.io.PyAudioSource(sampling_rate=16000, sample_width=2, channels=1, frames_per_buffer=1024)[source]

A class for an AudioSource that reads data the built-in microphone using PyAudio.

close()[source]

Close audio source

is_open()[source]

Return True if audio source is open, False otherwise

open()[source]

Open audio source

read(size)[source]

Read and return size audio samples at most.

Parameters:
size : int

the number of samples to read.

Returns:

Audio data as a string of length ‘N’ * ‘smaple_width’ * ‘channels’, where ‘N’ is:

  • size if size < ‘left_samples’
  • ‘left_samples’ if size > ‘left_samples’
class auditok.io.StdinAudioSource(sampling_rate=16000, sample_width=2, channels=1)[source]

A class for an AudioSource that reads data from standard input.

close()[source]

Close audio source

is_open()[source]

Return True if audio source is open, False otherwise

open()[source]

Open audio source

read(size)[source]

Read and return size audio samples at most.

Parameters:
size : int

the number of samples to read.

Returns:

Audio data as a string of length ‘N’ * ‘smaple_width’ * ‘channels’, where ‘N’ is:

  • size if size < ‘left_samples’
  • ‘left_samples’ if size > ‘left_samples’
class auditok.io.PyAudioPlayer(sampling_rate=16000, sample_width=2, channels=1)[source]

A class for audio playback using Pyaudio

auditok.io.from_file(filename)[source]

Create an AudioSource object using the audio file specified by filename. The appropriate AudioSource class is guessed from file’s extension.

Parameters:
filename :

path to an audio file.

Returns:

an AudioSource object that reads data from the given file.

auditok.io.player_for(audio_source)[source]

Return a PyAudioPlayer that can play data from audio_source.

Parameters:
audio_source :

an AudioSource object.

Returns:

PyAudioPlayer that has the same sampling rate, sample width and number of channels as audio_source.