audio

audio is the module responsible for controlling the audio interface settings and audio stream. Real-time audio processing is done by splitting into two processes: one main process and one process handling the audio stream using a callback function. Information is shared by multiprocessing. The audio interface is controlled by sounddevice.

class backlooper.audio.AudioStream(channels: int = 2, block_size: int = 1000, sample_rate: int | None = 44100, log_level: int = 10)

Bases: object

AudioStream controls the audio interface settings and audio stream. The callback function is the real-time audio processing function, running in a separate process.

block_size: int = 1000

Number of samples that are handled at one time by the callback function.

callback(indata, outdata, frames, callback_time, status)

The callback function is the real-time audio processing function, running in a separate process. This function must be able to return before the following buffer is handled to prevent audio clicks. There are three main parts:

  • Saving the latest recorded audio indata,

  • Mixing in the tracks that are currently playing into outdata,

  • And mixing in the clicktrack into outdata.

channels: int = 2

Number of channels per track (stereo).

property clicktrack_bpm

Wrapper around the multiprocessing.Value object containing the tempo.

property clicktrack_origin

Wrapper around the multiprocessing.Value object containing the start time of the clicktrack.

property clicktrack_volume: float

Wrapper around the multiprocessing.Value object containing the clicktrack volume.

property latency_seconds

Wrapper around the multiprocessing.Value object containing the latency in seconds.

log_level: int = 10

Log level

property origin

Wrapper around the multiprocessing.Value object containing the first execution time of the callback function.

play()

Starts the audio stream in a separate process.

read(start_timestamp: float, end_timestamp: float) array

Reads a np.array with recorded audio between time interval start_timestamp and end_timestamp.

reset_loop(track_id: int)

Stops the loop for track track_id.

run()

Starts the audio stream and waits indefinitely.

sample_rate: int | None = 44100

Sample rate of the audio interface.

set_start_end_loop(start_time_value: float, end_time_value: float, track_id: int, offset: float = 0)

Configures the audio stream to start looping an interval between start_time_value and end_time_value for a track track_id. The offset allows the start of the track to be adjusted: otherwise the track could start in the middle, because the callback function “wraps around” the track automatically (irrespectively of whether the track was playing or not).

write(start_timestamp: float, array_to_store: array, overwrite: bool = True)

Writes a np.array with recorded audio starting at time start_timestamp.