chaudio.io¶
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.
Functions
fromfile (filename[, silent]) |
Returns file contents of a WAVE file (either name or file pointer) as a chaudio.source.Source |
fromstring (strdata, *args, **kwargs) |
Treat the input as WAVE file contents, and return a chaudio.source.Source . |
play (_audio[, waveformat]) |
Plays the audio through the system speaker |
tofile (filename, _audio[, waveformat, …]) |
Output some sort of audio to a file (which can be a name or file pointer). |
tostring (_audio, *args, **kwargs) |
Returns the WAVE file contents. |
Classes
WaveFormat (name, dtype, samplewidth, …) |
-
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: - _audio (
chaudio.source.Source
,chaudio.arrangers.Arranger
, np.ndarray) – This is converted tochaudio.source.Source
, and then output. - waveformat (str,
chaudio.io.WaveFormat
) – This describes how to convert the data. Should probably be an integer format, and the default is good enough for anyone. Seechaudio.io.WaveFormat
for how to use it.
Returns: `simpleaudio.PlayObject <http – You can use this to cancel or change playback
Return type: //simpleaudio.readthedocs.io/en/latest/simpleaudio.html#simpleaudio.PlayObject>`_
- _audio (
-
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.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 contentsReturns: 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 achaudio.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 achaudio.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
- _audio (np.ndarray,