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.Reader
A
Reader
allowing to usenumpy.ndarray
as input of aTSM
object.Parameters: data ( numpy.ndarray
) – a matrix of shape (m
,n
), withm
the number of channels andn
the length of the buffer, where the samples will be read.
-
class
audiotsm.io.array.
ArrayWriter
(channels)¶ Bases:
audiotsm.io.base.Writer
A
Writer
allowing to get the output of aTSM
object as anumpy.ndarray
.Writing to an
ArrayWriter
will add the data at the end of thedata
attribute.Parameters: channels (int) – the number of channels of the signal. -
data
¶ A
numpy.ndarray
of shape (m
,n
), withm
the number of channels andn
the length of the data, where the samples have written.
-
-
class
audiotsm.io.array.
FixedArrayWriter
(data)¶ Bases:
audiotsm.io.base.Writer
A
Writer
allowing to usenumpy.ndarray
as output of a TSM object.Contrary to an
ArrayWriter
, aFixedArrayWriter
takes 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 theFixedArrayWriter
than the buffer can contain.Parameters: data ( numpy.ndarray
) – a matrix of shape (m
,n
), withm
the number of channels andn
the 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.Reader
A
Reader
allowing to use a wav file as input of aTSM
object.You should close the
WavReader
after using it with theclose()
method, or use it in awith
statement 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.Writer
A
Writer
allowing to use a wav file as output of aTSM
object.You should close the
WavWriter
after using it with theclose()
method, or use it in awith
statement 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.Writer
A
Writer
allowing to play the output of aTSM
object directly.You should stop the
StreamWriter
after using it with thestop()
method, or use it in awith
statement 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.OutputStream
that 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
TSM
object.-
empty
¶ True if there is no more data to read.
-
read
(buffer)¶ Reads as many samples from the
Reader
as possible, write them tobuffer
, and returns the number of samples that were read.Parameters: buffer ( numpy.ndarray
) – a matrix of shape (m
,n
), withm
the number of channels andn
the 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 Reader
and the buffer do not have the same number of channels
-
skip
(n)¶ Try to skip
n
samples, an returns the number of samples that were actually skipped.
-
-
class
audiotsm.io.base.
Writer
¶ An abstract class for the output of a
TSM
object.-
write
(buffer)¶ Write as many samples from the
Writer
as possible frombuffer
, and returns the number of samples that were written.Parameters: buffer ( numpy.ndarray
) – a matrix of shape (m
,n
), withm
the number of channels andn
the 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 Writer
and the buffer do not have the same number of channels
-