Readers and Writers¶
TSM objects use Reader
objects as input and Writer objects as output.
The audiotsm.io package provides Readers and Writers allowing to use
numpy arrays or wav files as input or output of a
TSM, to play the output in real-time, as well as base classes to implement your own Readers and Writers.
Numpy arrays¶
The audiotsm.io.array module provides a Reader and Writers allowing to
use a numpy.ndarray as input or output of a
TSM object.
-
class
audiotsm.io.array.ArrayReader(data)¶ Bases:
audiotsm.io.base.ReaderA
Readerallowing to usenumpy.ndarrayas input of aTSMobject.Parameters: data ( numpy.ndarray) – a matrix of shape (m,n), withmthe number of channels andnthe length of the buffer, where the samples will be read.
-
class
audiotsm.io.array.ArrayWriter(channels)¶ Bases:
audiotsm.io.base.WriterA
Writerallowing to get the output of aTSMobject as anumpy.ndarray.Writing to an
ArrayWriterwill add the data at the end of thedataattribute.Parameters: channels (int) – the number of channels of the signal. -
data¶ A
numpy.ndarrayof shape (m,n), withmthe number of channels andnthe length of the data, where the samples have written.
-
-
class
audiotsm.io.array.FixedArrayWriter(data)¶ Bases:
audiotsm.io.base.WriterA
Writerallowing to usenumpy.ndarrayas output of a TSM object.Contrary to an
ArrayWriter, aFixedArrayWritertakes the buffer in which the data will be written as a parameter of its constructor. The buffer is of fixed size, and it will not be possible to write more samples to theFixedArrayWriterthan the buffer can contain.Parameters: data ( numpy.ndarray) – a matrix of shape (m,n), withmthe number of channels andnthe length of the buffer, where the samples will be written.
Wav files¶
The audiotsm.io.wav module provides a Reader
and a Writer allowing to use wav files as input or
output of a TSM object.
-
class
audiotsm.io.wav.WavReader(filename)¶ Bases:
audiotsm.io.base.ReaderA
Readerallowing to use a wav file as input of aTSMobject.You should close the
WavReaderafter using it with theclose()method, or use it in awithstatement as follow:with WavReader(filename) as reader: # use reader...
Parameters: filename (str) – the name of an existing wav file. -
close()¶ Close the wav file.
-
samplerate¶ The samplerate of the wav file.
-
samplewidth¶ The sample width in bytes of the wav file.
-
-
class
audiotsm.io.wav.WavWriter(filename, channels, samplerate)¶ Bases:
audiotsm.io.base.WriterA
Writerallowing to use a wav file as output of aTSMobject.You should close the
WavWriterafter using it with theclose()method, or use it in awithstatement as follow:with WavWriter(filename, 2, 44100) as writer: # use writer...
Parameters: -
close()¶ Close the wav file.
-
Play in real-time¶
The audiotsm.io.stream module provides a
Writer allowing to play the output of a
TSM object in real-time.
-
class
audiotsm.io.stream.StreamWriter(channels, samplerate, **attrs)¶ Bases:
audiotsm.io.base.WriterA
Writerallowing to play the output of aTSMobject directly.You should stop the
StreamWriterafter using it with thestop()method, or use it in awithstatement as follow:with WavWriter(2, 44100) as writer: # use writer...
Parameters: - channels (int) – the number of channels of the signal.
- samplerate (int) – the sampling rate of the signal.
- attrs – additional parameters used to create the
sounddevice.OutputStreamthat is used by theStreamWriter.
-
stop()¶ Stop the stream.
Implementing your own¶
The audiotsm.io.base module provides base classes for the input and
output of TSM objects.
-
class
audiotsm.io.base.Reader¶ An abstract class for the input of a
TSMobject.-
empty¶ True if there is no more data to read.
-
read(buffer)¶ Reads as many samples from the
Readeras possible, write them tobuffer, and returns the number of samples that were read.Parameters: buffer ( numpy.ndarray) – a matrix of shape (m,n), withmthe number of channels andnthe length of the buffer, where the samples will be written.Returns: the number of samples that were read. It should always be equal to the length of the buffer, except when there is no more values to be read. Raises: ValueError – if the Readerand the buffer do not have the same number of channels
-
skip(n)¶ Try to skip
nsamples, an returns the number of samples that were actually skipped.
-
-
class
audiotsm.io.base.Writer¶ An abstract class for the output of a
TSMobject.-
write(buffer)¶ Write as many samples from the
Writeras possible frombuffer, and returns the number of samples that were written.Parameters: buffer ( numpy.ndarray) – a matrix of shape (m,n), withmthe number of channels andnthe length of the buffer, where the samples will be read.Returns: the number of samples that were written. It should always be equal to the length of the buffer, except when there is no more space in the Writer.Raises: ValueError – if the Writerand the buffer do not have the same number of channels
-