Waveform.from_encoded_bytes()¶
- static Waveform.from_encoded_bytes(encoded_bytes, start_time_milliseconds=0, end_time_milliseconds=0, frame_rate_hz=0, num_channels=0, convert_to_mono=False, zero_pad_ending=False, repeat_pad_ending=False, resample_mode=0, decoding_backend=0, file_extension='', mime_type='')¶
Decodes audio stored as
bytes.Example
Decode from bytes while auto-detecting the format as MP3.
>>> from babycat import Waveform >>> with open("audio-for-tests/andreas-theme/track.flac", "rb") as fh: ... the_bytes = fh.read() >>> waveform = Waveform.from_encoded_bytes(the_bytes) >>> waveform <babycat.Waveform: 9586415 frames, 2 channels, 44100 hz>
Example
Decode from bytes with a file extension hint.
>>> waveform2 = Waveform.from_encoded_bytes( ... the_bytes, ... file_extension="mp3", ... )
- Parameters
encoded_bytes (bytes) – A
bytesobject containing an encoded audio file, such as MP3 file.start_time_milliseconds (int, optional) – We discard any audio before this millisecond offset. By default, this does nothing and the audio is decoded from the beginning. Negative offsets are invalid.
end_time_milliseconds (int, optional) – We discard any audio after this millisecond offset. By default, this does nothing and the audio is decoded all the way to the end. If
start_time_millisecondsis specified, thenend_time_millisecondsmust be greater. The resultingframe_rate_hz (int, optional) – A destination frame rate to resample the audio to. Do not specify this parameter if you wish Babycat to preserve the audio’s original frame rate. This does nothing if
frame_rate_hzis equal to the audio’s original frame rate.num_channels (int, optional) – Set this to a positive integer
nto select the firstnchannels stored in the audio file. By default, Babycat will return all of the channels in the original audio. This will raise an exception if you specify anum_channelsgreater than the actual number of channels in the audio.convert_to_mono (bool, optional) – Set to
Trueto average all channels into a single monophonic (mono) channel. Ifnum_channels = nis also specified, then only the firstnchannels will be averaged. Note thatconvert_to_monocannot be set toTruewhile also settingnum_channels = 1.zero_pad_ending (bool, optional) – If you set this to
True, Babycat will zero-pad the ending of the decoded waveform to ensure that the output waveform’s duration is exactlyend_time_milliseconds - start_time_milliseconds. By default,zero_pad_ending = False, in which case the output waveform will be shorter thanend_time_milliseconds - start_time_millisecondsif the input audio is shorter thanend_time_milliseconds. Note that settingzero_pad_ending = Trueis mutually exclusive with settingrepeat_pad_ending = True.repeat_pad_ending (bool, optional) – If you set this to
True, Babycat will repeat the audio waveform to ensure that the output waveform’s duration is exactlyend_time_milliseconds - start_time_milliseconds. By default,repeat_pad_ending = False, in which case the output waveform will be shorter thanend_time_milliseconds - start_time_milliseconds. Note that settingrepeat_pad_ending = Trueis mutually exclusive with settingzero_pad_ending = True.resample_mode (int, optional) – If you set
frame_rate_hzto resample the audio when decoding, you can also setresample_modeto pick which resampling backend to use. Thebabycat.resample_modesubmodule contains the various available resampling algorithms compiled into Babycat. By default, Babycat resamples audio using libsamplerate at its highest-quality setting.file_extension (str, optional) – An optional hint of the input audio file’s encoding. An example of a valid value is
"mp3". Babycat will automatically detect the correct encoding ofinput_audio, even iffile_extensionis an incorrect guess.mime_type (str, optional) – An optional hint of the input audio file’s encoding. An example of a valid value is
"audio/mpeg". Babycat will automatically detect the correct encoding ofinput_audio, even ifmime_typeis an incorrect guess.
- Returns
Returns a waveform decoded from
encoded_bytes.- Return type
- Raises
babycat.exceptions.FeatureNotCompiled – Raised when you are trying to use a feature at runtime that as not included in Babycat at compile-time.
babycat.exceptions.WrongTimeOffset – Raised when
start_time_milliseconds``and/or ``end_time_millisecondsis invalid.babycat.exceptions.WrongNumChannels – Raised when you specified a value for
num_channelsthat is greater than the number of channels the audio has.babycat.exceptions.WrongNumChannelsAndMono – Raised when the user sets both
convert_to_mono = Trueandnum_channels = 1.babycat.exceptions.CannotZeroPadWithoutSpecifiedLength – Raised when
zero_pad_endingis set without settingend_time_milliseconds.babycat.exceptions.UnknownInputEncoding – Raised when we failed to detect valid audio in the input data.
babycat.exceptions.UnknownDecodeError – Raised when we failed to decode the input audio stream, but we don’t know why.
babycat.exceptions.ResamplingError – Raised when we failed to encode an audio stream into an output format.
babycat.exceptions.WrongFrameRate – Raised when the user set
frame_rate_hzto a value that we cannot resample to.babycat.exceptions.WrongFrameRateRatio – Raised when
frame_rate_hzwould upsample or downsample by a factor>= 256. Try resampling in smaller increments.