chaudio.io package

Module contents

Input/Output functionality (chaudio.io)

Allows any data type to be stored to a file, returned as a string, read from a file or string, and other I/O issues.

At this point, only WAVE integer formats are accepted, so 8 bit, 16 bit, 24 bit, and 32 bit WAVE formats all work.

WAVE 32f format does not work.

In the future, support for .ogg and .mp3 files will be hopefully added.

class chaudio.io.WaveFormat(name, dtype, samplewidth, scale_factor)[source]

Bases: object

__init__(name, dtype, samplewidth, scale_factor)[source]
__dict__ = mappingproxy({'__module__': 'chaudio.io', '__weakref__': <attribute '__weakref__' of 'WaveFormat' objects>, '__dict__': <attribute '__dict__' of 'WaveFormat' objects>, '__init__': <function WaveFormat.__init__>, '__doc__': None})
__module__ = 'chaudio.io'
__weakref__

list of weak references to the object (if defined)

chaudio.io.play(_audio, waveformat='16i')[source]

Plays the audio through the system speaker

Requires simpleaudio pip3 install simpleaudio to work (which is a dependency of chaudio)

Parameters:
Returns:

`simpleaudio.PlayObject <http – You can use this to cancel or change playback

Return type:

//simpleaudio.readthedocs.io/en/latest/simpleaudio.html#simpleaudio.PlayObject>`_

chaudio.io.fromfile(filename, silent=False)[source]

Returns file contents of a WAVE file (either name or file pointer) as a chaudio.source.Source

Note that they are not “normalized” as in using chaudio.util.normalize(), but rather simply converted from the internal WAVE formats (which are integers), and divided by the maximum integer of that size. That way, all WAVE formats will return (within rounding) the same result when called with this function, so the original volume is conserved. This is the behaviour audacity has when reading files, which is to convert to 32f format internally.

This supports all standard WAVE integer formats, 8 bit, 16 bit, 24 bit, and 32 bit.

Note that WAVE 32f format is NOT supported yet

Parameters:
  • filename (str, file) – If a string, that file is opened, or if it is a file object already (which can be an io.StringIO object), that is used instead of opening another.
  • silent (bool) – Print out what file is being used, so that the user knows what’s happening
Returns:

A chaudio Source class, with appropriate channels, samplerate, and a dtype of float32

Return type:

chaudio.source.Source

chaudio.io.fromstring(strdata, *args, **kwargs)[source]

Treat the input as WAVE file contents, and return a chaudio.source.Source.

Parameters:strdata (str) – Treat strdata as the WAVE file contents
Returns:A chaudio Source class, with appropriate channels, samplerate, and a dtype of float32
Return type:chaudio.source.Source
chaudio.io.tofile(filename, _audio, waveformat='16i', normalize=True, silent=False)[source]

Output some sort of audio to a file (which can be a name or file pointer).

Always to WAVE format, and specify waveformat in order to change what kind. Default, it is 16 bit integer (CD quality).

Parameters:
  • filename (str, file) – If a string, that file is opened, or if it is a file object already (which can be an io.StringIO object), that is used instead of opening another.
  • _audio (np.ndarray, chaudio.source.Source, chaudio.source.Arranger) – Casts _audio to a chaudio.source.Source, which will work on any chaudio type. You shouldn’t have to worry about this, it stays truthful to the input.
  • waveformat ({ '8i', '16i', '24i', '32i' }) – Describes the number of bits per sample, and what type of data to write (‘i’ is integer).
  • normalize (bool) – Whether or not to normalize before writing. This should be the default, to avoid any clipping.
  • silent (bool) – Print out what file is being used, so that the user knows what’s happening
chaudio.io.tostring(_audio, *args, **kwargs)[source]

Returns the WAVE file contents. Essentially returns what chaudio.io.tofile() would have written to a file.

Parameters:
  • _audio (np.ndarray, chaudio.source.Source, chaudio.source.Arranger) – Casts _audio to a chaudio.source.Source, which will work on any chaudio type. You shouldn’t have to worry about this, it stays truthful to the input.
  • waveformat ({ '8i', '16i', '24i', '32i' }) – Describes the number of bits per sample, and what type of data to write (‘i’ is integer).
  • normalize (bool) – Whether or not to normalize before writing. This should be the default, to avoid any clipping.
Returns:

An str representing the WAVE file contents.

Return type:

str