Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _array_proto_from_format(sampleformat: SampleFormat) -> array.array:
arrays = {
SampleFormat.UNSIGNED8: _create_int_array(1),
SampleFormat.SIGNED16: _create_int_array(2),
SampleFormat.SIGNED32: _create_int_array(4),
SampleFormat.FLOAT32: array.array('f')
}
if sampleformat in arrays:
return arrays[sampleformat]
raise MiniaudioError("the requested sample format can not be used directly: "
+ sampleformat.name + " (convert it first)")
def _format_from_width(sample_width: int, is_float: bool = False) -> SampleFormat:
if is_float:
return SampleFormat.FLOAT32
elif sample_width == 1:
return SampleFormat.UNSIGNED8
elif sample_width == 2:
return SampleFormat.SIGNED16
elif sample_width == 3:
return SampleFormat.SIGNED24
elif sample_width == 4:
return SampleFormat.SIGNED32
else:
raise MiniaudioError("unsupported sample width", sample_width)
def __init__(self, samplerate: int = 0, samplewidth: int = 0, nchannels: int = 0, queue_size: int = 100) -> None:
super().__init__(samplerate, samplewidth, nchannels, queue_size=queue_size)
self.command_queue = queue.Queue(maxsize=queue_size) # type: queue.Queue[Dict[str, Any]]
output_format = {
1: miniaudio.SampleFormat.UNSIGNED8,
2: miniaudio.SampleFormat.SIGNED16,
3: miniaudio.SampleFormat.SIGNED24,
4: miniaudio.SampleFormat.SIGNED32
}[self.samplewidth]
self.device = miniaudio.PlaybackDevice(output_format, self.nchannels, self.samplerate)
stream = self.generator()
next(stream) # start generator
self.device.start(stream)
def wav_read_s32(data: bytes) -> DecodedSoundFile:
"""Reads and decodes the whole wav audio data. Resulting sample format is 32 bits signed integer."""
channels = ffi.new("unsigned int *")
sample_rate = ffi.new("unsigned int *")
num_frames = ffi.new("drwav_uint64 *")
memory = lib.drwav_open_memory_and_read_pcm_frames_s32(data, len(data), channels, sample_rate, num_frames, ffi.NULL)
if not memory:
raise DecodeError("cannot load/decode data")
try:
samples = _create_int_array(4)
buffer = ffi.buffer(memory, num_frames[0] * channels[0] * 4)
samples.frombytes(buffer)
return DecodedSoundFile("", channels[0], sample_rate[0], SampleFormat.SIGNED32, samples)
finally:
lib.drwav_free(memory, ffi.NULL)