chaudio.source package

Module contents

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.

class chaudio.source.Source(data, hz=None, dtype=None)[source]

Bases: object

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 at hz samplerate (if none is given, 44100). If no dtype is given, default to data.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 is chaudio.source.Source, copy it, but apply the hz and dtype parameters for the new format. If hz isn’t given, use data.hz as the default, and do the same with dtype and data.dtype.

If data is chaudio.arrangers.Arranger, calculate its data, and turn into a source.

If data is a chaudio class, resample the data to hz. Otherwise, assume it is the input was sampled at hz 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
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 samplerate
Return 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 means self.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 means self.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 or 2.
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 or 2.
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
get_data()[source]
set_data(v)[source]
data
get_dtype()[source]
set_dtype(v)[source]
dtype
get_channels()[source]
set_channels(v)[source]
channels
get_hz()[source]
set_hz(v)[source]
hz
get_seconds()[source]
set_seconds()[source]
seconds
get_samples()[source]
set_samples(v)[source]
samples
__str__()[source]
__len__()[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, use source[:] to return all channels as a list, or source[0] for the 0th channel (which is left on a stereo source).

If key is a tuple, return all the channels represented by source[key[0]] subscripted with key[1]. So, source[0, :5] returns the first 5 samples of the 0``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 by key[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, use source[:] = 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 by source[key[0]] subscripted with key[1] to val. So, source[0, :5] = y sets the first five values of the 0``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 5

In general, the following should hold for any source x, key key, and value val:

>>> 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 by key[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, or len(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

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:
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:
Returns:

A copy of the object with the data[offset:offset+len(_val)] assigned to _val.

Return type:

chaudio.source.Source

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 and data concatenated. This essentially can be used to add delays, silence, or prepend any other data

Parameters:_val (numpy.ndarray, chaudio.source.Source, chaudio.arrangers.Arranger) – This is converted to a source internally, see chaudio.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, see chaudio.source.Source.__init__() for details on how this is done.
Returns:_val and the object called with concatenated
Return 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 to data 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, see chaudio.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, see chaudio.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, see chaudio.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, see chaudio.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
__opit__(v)[source]
__add__(_v)[source]
__sub__(_v)[source]
__mul__(_v)[source]
__div__(_v)[source]
__truediv__(_v)[source]
__floordiv__(_v)[source]
__mod__(_v)[source]
__pow__(_v)[source]
__radd__(_v)
__rsub__(_v)
__rmul__(_v)
__rdiv__(_v)
__dict__ = mappingproxy({'data': <property object>, 'prepend': <function Source.prepend>, '__getitem__': <function Source.__getitem__>, 'insert': <function Source.insert>, 'hz': <property object>, 'get_samples': <function Source.get_samples>, 'get_hz': <function Source.get_hz>, '__truediv__': <function Source.__truediv__>, 'get_seconds': <function Source.get_seconds>, 'ensure': <function Source.ensure>, 'copy_data': <function Source.copy_data>, '__dict__': <attribute '__dict__' of 'Source' objects>, 'dtype': <property object>, '__rsub__': <function Source.__sub__>, 'set_data': <function Source.set_data>, 'set_samples': <function Source.set_samples>, '__rmul__': <function Source.__mul__>, 'appended': <function Source.appended>, 'resample': <function Source.resample>, 'append': <function Source.append>, '__mod__': <function Source.__mod__>, '__setitem__': <function Source.__setitem__>, 'channels': <property object>, 'seconds': <property object>, '__rfloordiv__': <function Source.__floordiv__>, 'set_dtype': <function Source.set_dtype>, '__rdiv__': <function Source.__div__>, 'prepended': <function Source.prepended>, '__init__': <function Source.__init__>, '__module__': 'chaudio.source', 'resampled': <function Source.resampled>, 'clear': <function Source.clear>, 'get_channels': <function Source.get_channels>, 'copy': <function Source.copy>, '__opit__': <function Source.__opit__>, 'rechannel': <function Source.rechannel>, '__floordiv__': <function Source.__floordiv__>, '__rpow__': <function Source.__pow__>, '__str__': <function Source.__str__>, '__add__': <function Source.__add__>, 'set_seconds': <function Source.set_seconds>, 'samples': <property object>, 'set_hz': <function Source.set_hz>, 'get_data': <function Source.get_data>, 'cleared': <function Source.cleared>, '__mul__': <function Source.__mul__>, '__rmod__': <function Source.__mod__>, '__len__': <function Source.__len__>, 'ensured': <function Source.ensured>, '__radd__': <function Source.__add__>, 'rechanneled': <function Source.rechanneled>, '__sub__': <function Source.__sub__>, 'get_dtype': <function Source.get_dtype>, '__doc__': 'Represents the default audio source, with variable number of channels and samplerate\n\n\n ', 'redtyped': <function Source.redtyped>, '__weakref__': <attribute '__weakref__' of 'Source' objects>, 'set_channels': <function Source.set_channels>, 'redtype': <function Source.redtype>, '__div__': <function Source.__div__>, '__rtruediv__': <function Source.__truediv__>, '__pow__': <function Source.__pow__>, 'inserted': <function Source.inserted>})
__module__ = 'chaudio.source'
__rtruediv__(_v)
__weakref__

list of weak references to the object (if defined)

__rfloordiv__(_v)
__rmod__(_v)
__rpow__(_v)
class chaudio.source.Mono(data, hz=None)[source]

Bases: chaudio.source.Source

__module__ = 'chaudio.source'
__init__(data, hz=None)[source]

A source with only 1 channel

Returns:The same as using chaudio.source.Source.__init__(), but then ensures there is only 1 channel
Return type:chaudio.source.Mono
set_channels(v)[source]
class chaudio.source.Stereo(data, hz=None)[source]

Bases: chaudio.source.Source

__module__ = 'chaudio.source'
__init__(data, hz=None)[source]

A source with only 2 channels

Returns:The same as using chaudio.source.Source.__init__(), but then ensures there is only 2 channel
Return type:chaudio.source.Stereo
set_channels(v)[source]