Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wav_get_file_info(filename: str) -> SoundFileInfo:
"""Fetch some information about the audio file (wav format)."""
filenamebytes = _get_filename_bytes(filename)
wav = ffi.new("drwav*")
if not lib.drwav_init_file(wav, filenamebytes, ffi.NULL):
raise DecodeError("could not open/decode file")
try:
duration = wav.totalPCMFrameCount / wav.sampleRate
sample_width = wav.bitsPerSample // 8
is_float = wav.translatedFormatTag == lib.DR_WAVE_FORMAT_IEEE_FLOAT
return SoundFileInfo(filename, FileFormat.WAV, wav.channels, wav.sampleRate,
_format_from_width(sample_width, is_float), duration, wav.totalPCMFrameCount)
finally:
lib.drwav_uninit(wav)
self.sample_format = sample_format
self.sample_format_name = ffi.string(lib.ma_get_format_name(sample_format.value)).decode()
self.sample_width = _width_from_format(sample_format)
self.num_frames = num_frames
self.duration = duration
self.file_format = file_format
def __str__(self) -> str:
return "<{clazz}: '{name}' {nchannels} ch, {sample_rate} hz, {sample_format.name}, " \
"{num_frames} frames={duration:.2f} sec.>".format(clazz=self.__class__.__name__, **(vars(self)))
def __repr__(self) -> str:
return str(self)
class DecodedSoundFile(SoundFileInfo):
"""Contains various properties and also the PCM frames of a fully decoded audio file."""
def __init__(self, name: str, nchannels: int, sample_rate: int,
sample_format: SampleFormat, samples: array.array) -> None:
num_frames = len(samples) // nchannels
duration = num_frames / sample_rate
super().__init__(name, FileFormat.UNKNOWN, nchannels, sample_rate, sample_format, duration, num_frames)
self.samples = samples
class MiniaudioError(Exception):
"""When a miniaudio specific error occurs."""
pass
class DecodeError(MiniaudioError):
"""When something went wrong during decoding an audio file."""
def flac_get_info(data: bytes) -> SoundFileInfo:
"""Fetch some information about the audio data (flac format)."""
flac = lib.drflac_open_memory(data, len(data), ffi.NULL)
if not flac:
raise DecodeError("could not open/decode data")
try:
duration = flac.totalPCMFrameCount / flac.sampleRate
sample_width = flac.bitsPerSample // 8
return SoundFileInfo("", FileFormat.FLAC, flac.channels, flac.sampleRate,
_format_from_width(sample_width), duration, flac.totalPCMFrameCount)
finally:
lib.drflac_close(flac)
def vorbis_get_file_info(filename: str) -> SoundFileInfo:
"""Fetch some information about the audio file (vorbis format)."""
filenamebytes = _get_filename_bytes(filename)
error = ffi.new("int *")
vorbis = lib.stb_vorbis_open_filename(filenamebytes, error, ffi.NULL)
if not vorbis:
raise DecodeError("could not open/decode file")
try:
info = lib.stb_vorbis_get_info(vorbis)
duration = lib.stb_vorbis_stream_length_in_seconds(vorbis)
num_frames = lib.stb_vorbis_stream_length_in_samples(vorbis)
return SoundFileInfo(filename, FileFormat.VORBIS, info.channels, info.sample_rate,
SampleFormat.SIGNED16, duration, num_frames)
finally:
lib.stb_vorbis_close(vorbis)