Audio data and buffer abstraction

These classes are designed to provide objects that will handle reading and writing of audio data as numpy arrays.

They are two: Audio, which is a read-only object, and AudioBuffer, that is a subclass of Audio and multiprocessing.shared_memory.SharedMemory, thus providing a cross-processes data read and write functionality.

Created on Fri May 29 16:07:04 2020.

@author: João Vitor Gutkoski Paes

class ossom.Audio(data: numpy.ndarray, samplerate: int, blocksize: int = 512)[source]

Audio data interface.

__init__(data: numpy.ndarray, samplerate: int, blocksize: int = 512) → None[source]

Audio objects are a representation of a waveform and a sample rate.

They hold the basic information to play sound.

Parameters:
  • data (np.ndarray) – An array containing audio data, or a buffer to be filled with audio.
  • samplerate (int) – Audio sample rate, or how many data represent one second of data.
  • blocksize (int, optional) – Amount of samples to read on each call to next. The default is config.blocksize.
Returns:

Return type:

None

__getitem__(key)[source]

Route Audio getitem to numpy.ndarray getitem.

__iter__()[source]

Iterate method.

__next__() → numpy.ndarray[source]

Return data from ridx to ridx + blocksize.

read_next(blocksize: int) → numpy.ndarray[source]

Read data from ridx to ridx + blocksize.

Parameters:blocksize (int) – The amount of data to read.
Raises:StopIteration – Unavailable to read more data than nsamples.
Returns:data – A numpy array with blocksize rows and nchannels columns.
Return type:np.ndarray
ridx

Read data index.

blocksize

Amount of samples read on each call to next().

samplerate

Sample rate of the audio.

data

Audio data as a numpy.ndarray.

nsamples

Total number of data.

nchannels

Total number of channels.

duration

Total time duration.

samplesize

Size of one sample of audio.

dtype

Type of the data.

bytesize

Size, in bytes, of whole array. Same as samplesize * nsamples * nchannels.

class ossom.AudioBuffer(name: str, samplerate: int, buffersize: int, nchannels: int, blocksize: int, dtype: numpy.dtype = dtype('float32'))[source]

Audio data in a shared memory buffer.

__init__(name: str, samplerate: int, buffersize: int, nchannels: int, blocksize: int, dtype: numpy.dtype = dtype('float32')) → None[source]

Buffer object intended to read and write audio samples.

Parameters:
  • name (str) – The SharedMemory name, can be None to automatically generate one.
  • samplerate (int) – Audio sampling rate.
  • buffersize (int) – Total number of samples.
  • nchannels (int) – Total number of channels.
  • blocksize (int) – Amount of samples to read on each call to next
  • dtype (np.dtype) – Sample data type. The default is config.dtype.
Returns:

Return type:

None.

__del__()[source]

Guarantee that SharedMemory calls close and unlink.

widx

Write data index.

ready2read

How many samples are ready to read.

If the write index is smaller than read index returns None

Returns:Amount of samples available to read.
Return type:int
is_full

Check if ringbuffer is full or not.

clear()[source]

Set all data to zero.

get_audio(blocksize: int = None, copy: bool = False) → ossom.audio.Audio[source]

Audio object that points to shared memory buffer, or a copy of it.

Parameters:
  • blocksize (int, optional) – The read block size. The default is None.
  • copy (bool, optional) – If set to True, the Audio is just a copy of the buffer. The default is False.
Returns:

The buffer as a read-only Audio object.

Return type:

Audio

write_next(data: numpy.ndarray) → int[source]

Write data to buffer.

If widx gets equal to nsamples the buffer is set to full. Checking can be made using the is_full property.

Parameters:data (np.ndarray) – Samples to write on buffer.
Returns:Amount of written samples.
Return type:int