chaudio.source¶
Audio Source (chaudio.source
)¶
All these are essentially abstractions above a data array of samples.
Has support for variable number of channels, any samplerate, and data type
Operators are overriden, so that you can apply them to a constant, numpy array, or other audio source
When you set a property (like source.hz or source.channels), the internal data is updated automatically.
Classes
Mono (data[, hz]) |
|
Source (data[, hz, dtype]) |
Represents the default audio source, with variable number of channels and samplerate |
Stereo (data[, hz]) |
-
class
chaudio.source.
Source
(data, hz=None, dtype=None)[source]¶ Represents the default audio source, with variable number of channels and samplerate
-
__init__
(data, hz=None, dtype=None)[source]¶ Source creation routine
Creates a Source consisting of
data
.If
data
is a np.ndarray, assume that these are raw sample data, taken athz
samplerate (if none is given,44100
). If nodtype
is given, default todata.dtype
If
data
is a tuple or list, assume that it contains channel data, and set the number of channels to the length of the tuple/list, and each individual channel to the np.ndarray at the corresponding index.If
data
ischaudio.source.Source
, copy it, but apply thehz
anddtype
parameters for the new format. Ifhz
isn’t given, usedata.hz
as the default, and do the same withdtype
anddata.dtype
.If
data
ischaudio.arrangers.Arranger
, calculate its data, and turn into a source.If
data
is a chaudio class, resample the data tohz
. Otherwise, assume it is the input was sampled athz
per second.Parameters: - data (np.ndarray,
chaudio.source.Source
,chaudio.arrangers.Arranger
, tuple, list) – Describes how to gather data. See - beats (int, float) – Number of beats (or pulses) per measure
- division (int, float) – Note division that represents a single pulse
- bpm (int, optional) – The speed, in beats per minute
- data (np.ndarray,
-
copy
()[source]¶ Returns a copy of the item
Returns: An exact copy of the current object Return type: chaudio.source.Source
-
copy_data
()[source]¶ Returns a copy of the raw sample data
Returns: Channels with np.ndarray ‘s describing the sample data Return type: list of np.ndarray
-
resample
(tohz)[source]¶ Internally adjust the sample rate in a smart way (using FFT and IFFT)
This doesn’t return anything, so it changes the object it’s called on. To return a new source, and not change the one being called, use
chaudio.source.Source.resampled()
Parameters: tohz (int, float) – The sample rate, in samples per second
-
resampled
(tohz)[source]¶ Returns a copy of the current object resampled to
tohz
To not make a copy, and instead alter the object in place, use
chaudio.source.Source.resample()
Parameters: tohz (int, float) – The sample rate, in samples per second Returns: A copy of the object resampled to tohz
samplerateReturn type: chaudio.source.Source
-
rechannel
(tochannels)[source]¶ Internally adjust how many channels are stored
This doesn’t return anything, so it changes the object it’s called on. To return a new source, and not change the one being called, use
chaudio.source.Source.rechanneled()
If
tochannels == self.channels
, no change is made. Else, the behaviour is thus:If
tochannels == 1
(which meansself.channels == 2
), the new data array contains 1 item, which is the average of the previous two channels. This will roughly sound the same (as in if you have any sounds that are purely in one channel or the other, they will still be heard).If
tochannels == 2
(which meansself.channels == 1
), the new data array is the old one, but duplicated.To return an altered copy, and not change the object itself, use
chaudio.source.Source.rechanneled()
Parameters: tochannels ({ 1, 2 }) – The number of channels the source should have. Must be 1
or2
.
-
rechanneled
(tochannels)[source]¶ Return a copy of the object, with a specified number of channels
To not make a copy, and instead alter the object in place, use
chaudio.source.Source.rechannel()
Parameters: tochannels ({ 1, 2 }) – The number of channels the source should have. Must be 1
or2
.Returns: A copy of the object with the number of channels changed to tochannels
Return type: chaudio.source.Source
-
redtype
(todtype)[source]¶ Internally adjust what data format is used
This probably shouldn’t be used by your application, as it does not rescale values. It’s main use is in the
chaudio.util
module, for outputting as WAVE data.Changes the internal data format
To make a copy, and not alter the current object, use
chaudio.source.Source.redtyped()
Parameters: todtype ({ np.int8, np.int16, np.int32, np.float32, np.float16 }) – Numpy data format
-
redtyped
(todtype)[source]¶ Return a copy of the object, with a specified internal data format
To not make a copy, and instead alter the object in place, use
chaudio.source.Source.redtype()
Parameters: todtype ({ np.int8, np.int16, np.int32, np.float32, np.float16 }) – Numpy data format Returns: A copy of the object with the data format changed to todtype
Return type: chaudio.source.Source
-
__getitem__
(key)[source]¶ Return a portion of the data in a source
If
key
is an int or slice, return the channels indicated, in list format. So, usesource[:]
to return all channels as a list, orsource[0]
for the 0th channel (which is left on a stereo source).If
key
is a tuple, return all the channels represented bysource[key[0]]
subscripted withkey[1]
. So,source[0, :5]
returns the first 5 samples of the0``th channel. ``source[:, :5]
returns a list of the first 5 values for each channel.Parameters: key (int, slice, tuple) – If int or slice, return the channels represented by key
. If it’s a tuple, return channel[key[1]] for each channel represented bykey[0]
. See examples for more info.Returns: If the key
specified a single channel, return just that channel’s specified data as np.ndarray. If multiple channels are indicated, return a list of channel data.Return type: list of np.ndarray or np.ndarray
-
__setitem__
(key, val)[source]¶ Set a portion of the data in a source
If
key
is an int or slice, set the channels indicated, in list format. So, usesource[:] = y
to set channels to a y, which must be a list of np.ndarray.If
key
is a tuple, set all the channels represented bysource[key[0]]
subscripted withkey[1]
toval
. So,source[0, :5] = y
sets the first five values of the0``th channels to y. Note that ``y
must be either a constant, or have the same shape as the values it is replacing. In our example,y
would have to be a constant, or a np.ndarray with length 5In general, the following should hold for any source
x
, keykey
, and valueval
:>>> x[key] = val >>> print (x[key] == val) True
Parameters: - key (int, slice, tuple) – If int or slice, set the channel data represented by
key
. If it’s a tuple, set channel[key[1]] for each channel represented bykey[0]
. See examples for more info. - val (int, float, list, tuple, np.ndarray) – The value to set the specified samples to. If it is a list, tuple, or np.ndarray, it must be the same shape as the values it is replacing. So, if saying
x[0, :5] = y
,y
must be int, float, orlen(y)
must be 5.
Returns: If the
key
specified a single channel, return just that channel’s specified data as np.ndarray. If multiple channels are indicated, return a list of channel data.Return type: list of np.ndarray or np.ndarray
- key (int, slice, tuple) – If int or slice, set the channel data represented by
-
clear
()[source]¶ Empties all data
To return a copy and not modify the original object, use
chaudio.source.Source.cleared()
.This empties all data out
-
cleared
()[source]¶ Empties all data, and returns a copy
To modify in place,
chaudio.source.Source.clear()
.This empties all data out
-
insert
(offset, _val)[source]¶ Inserts samples at a given offset
To return a copy and not modify the original object, use
chaudio.source.Source.inserted()
.This clears
data[offset:offset+len(_val)]
, and sets it to_val
Parameters: - offset (int) – What sample to insert at
- _val (numpy.ndarray,
chaudio.source.Source
,chaudio.arrangers.Arranger
) – This is converted to a source internally, seechaudio.source.Source.__init__()
for details on how this is done.
-
inserted
(offset, _val)[source]¶ Returns a copy with inserted samples at a given offset
To not make a copy, and rather edit inplace, use
chaudio.source.Source.insert()
.Parameters: - offset (int) – What sample to insert at
- _val (numpy.ndarray,
chaudio.source.Source
,chaudio.arrangers.Arranger
) – This is converted to a source internally, seechaudio.source.Source.__init__()
for details on how this is done.
Returns: A copy of the object with the
data[offset:offset+len(_val)]
assigned to_val
.Return type:
-
prepend
(_val)[source]¶ Prepend values to the data array
To return a copy and not modify the original object, use
chaudio.source.Source.prepended()
.This sets
self.data
to_val
anddata
concatenated. This essentially can be used to add delays, silence, or prepend any other dataParameters: _val (numpy.ndarray, chaudio.source.Source
,chaudio.arrangers.Arranger
) – This is converted to a source internally, seechaudio.source.Source.__init__()
for details on how this is done.
-
prepended
(_val)[source]¶ Returns a copy with prepended values to the data array
To modify the original object and not make a copy, use
chaudio.source.Source.prepend()
.This returns a copy of the object called on, with
_val
prepended before it.Parameters: _val (numpy.ndarray, chaudio.source.Source
,chaudio.arrangers.Arranger
) – This is converted to a source internally, seechaudio.source.Source.__init__()
for details on how this is done.Returns: _val
and the object called with concatenatedReturn type: chaudio.source.Source
-
append
(_val)[source]¶ Append values to the data array
To return a copy and not modify the original object, use
chaudio.source.Source.appended()
.This sets
self.data
todata
and_val
concatenated. This tacks on_val
to the end.Parameters: _val (numpy.ndarray, chaudio.source.Source
,chaudio.arrangers.Arranger
) – This is converted to a source internally, seechaudio.source.Source.__init__()
for details on how this is done.
-
appended
(_val)[source]¶ Returns a copy of the object with values appended to the data array
To not make a copy, and instead modify the object called with, use
chaudio.source.Source.append()
.Parameters: _val (numpy.ndarray, chaudio.source.Source
,chaudio.arrangers.Arranger
) – This is converted to a source internally, seechaudio.source.Source.__init__()
for details on how this is done.Returns: A copy of the current object, but with _val
appended.Return type: chaudio.source.Source
-
ensure
(length=None)[source]¶ Makes sure that the source is a certain length, which will append 0’s to the end if needed
To return a copy and not modify the original object, use
chaudio.source.Source.ensured()
.Parameters: _val (numpy.ndarray, chaudio.source.Source
,chaudio.arrangers.Arranger
) – This is converted to a source internally, seechaudio.source.Source.__init__()
for details on how this is done.
-
ensured
(length=None)[source]¶ Makes a copy that is guaranteed to be a certain length, which will append 0’s to the end if needed
To modify the original object, use
chaudio.source.Source.ensure()
.Parameters: _val (numpy.ndarray, chaudio.source.Source
,chaudio.arrangers.Arranger
) – This is converted to a source internally, seechaudio.source.Source.__init__()
for details on how this is done.Returns: A copy of the object being called, but it is guaranteed to be of a certain length Return type: chaudio.source.Source
-
__weakref__
¶ list of weak references to the object (if defined)
-