Skip to content

Commit 5da2441

Browse files
committed
adapted to new drwav api
1 parent 27087cb commit 5da2441

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

build_ffi_module.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,19 @@
253253
...;
254254
} drwav_allocation_callbacks;
255255
256-
257256
drwav_bool32 drwav_init_file(drwav* pWav, const char* filename, const drwav_allocation_callbacks* pAllocationCallbacks);
258257
drwav_bool32 drwav_init_memory(drwav* pWav, const void* data, size_t dataSize, const drwav_allocation_callbacks* pAllocationCallbacks);
258+
drwav_int32 drwav_uninit(drwav* pWav);
259259
void drwav_free(void* p, const drwav_allocation_callbacks* pAllocationCallbacks);
260260
261+
drwav_bool32 drwav_init_file_write(drwav* pWav, const char* filename, const drwav_data_format* pFormat, const drwav_allocation_callbacks* pAllocationCallbacks);
262+
drwav_bool32 drwav_init_file_write_sequential(drwav* pWav, const char* filename, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, const drwav_allocation_callbacks* pAllocationCallbacks);
263+
drwav_bool32 drwav_init_file_write_sequential_pcm_frames(drwav* pWav, const char* filename, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, const drwav_allocation_callbacks* pAllocationCallbacks);
264+
drwav_bool32 drwav_init_memory_write(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, const drwav_allocation_callbacks* pAllocationCallbacks);
265+
drwav_bool32 drwav_init_memory_write_sequential(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, drwav_uint64 totalSampleCount, const drwav_allocation_callbacks* pAllocationCallbacks);
266+
drwav_bool32 drwav_init_memory_write_sequential_pcm_frames(drwav* pWav, void** ppData, size_t* pDataSize, const drwav_data_format* pFormat, drwav_uint64 totalPCMFrameCount, const drwav_allocation_callbacks* pAllocationCallbacks);
267+
268+
261269
drwav_uint64 drwav_read_pcm_frames(drwav* pWav, drwav_uint64 framesToRead, void* pBufferOut);
262270
drwav_uint64 drwav_write_pcm_frames(drwav* pWav, drwav_uint64 framesToWrite, const void* pData);
263271
drwav_bool32 drwav_seek_to_pcm_frame(drwav* pWav, drwav_uint64 targetFrameIndex);

miniaudio.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,8 @@ def mp3_stream_file(filename: str, frames_to_read: int = 1024,
638638
def wav_get_file_info(filename: str) -> SoundFileInfo:
639639
"""Fetch some information about the audio file (wav format)."""
640640
filenamebytes = _get_filename_bytes(filename)
641-
wav = lib.drwav_open_file(filenamebytes)
642-
if not wav:
641+
wav = ffi.new("drwav*")
642+
if not lib.drwav_init_file(wav, filenamebytes, ffi.NULL):
643643
raise DecodeError("could not open/decode file")
644644
try:
645645
duration = wav.totalPCMFrameCount / wav.sampleRate
@@ -648,13 +648,13 @@ def wav_get_file_info(filename: str) -> SoundFileInfo:
648648
return SoundFileInfo(filename, FileFormat.WAV, wav.channels, wav.sampleRate,
649649
_format_from_width(sample_width, is_float), duration, wav.totalPCMFrameCount)
650650
finally:
651-
lib.drwav_close(wav)
651+
lib.drwav_uninit(wav)
652652

653653

654654
def wav_get_info(data: bytes) -> SoundFileInfo:
655655
"""Fetch some information about the audio data (wav format)."""
656-
wav = lib.drwav_open_memory(data, len(data))
657-
if not wav:
656+
wav = ffi.new("drwav*")
657+
if not lib.drwav_init_memory(wav, data, len(data), ffi.NULL):
658658
raise DecodeError("could not open/decode data")
659659
try:
660660
duration = wav.totalPCMFrameCount / wav.sampleRate
@@ -663,7 +663,7 @@ def wav_get_info(data: bytes) -> SoundFileInfo:
663663
return SoundFileInfo("<memory>", FileFormat.WAV, wav.channels, wav.sampleRate,
664664
_format_from_width(sample_width, is_float), duration, wav.totalPCMFrameCount)
665665
finally:
666-
lib.drwav_close(wav)
666+
lib.drwav_uninit(wav)
667667

668668

669669
def wav_read_file_s32(filename: str) -> DecodedSoundFile:
@@ -777,8 +777,8 @@ def wav_stream_file(filename: str, frames_to_read: int = 1024,
777777
This uses a fixed chunk size and cannot be used as a generic miniaudio decoder input stream.
778778
Consider using stream_file() instead."""
779779
filenamebytes = _get_filename_bytes(filename)
780-
wav = lib.drwav_open_file(filenamebytes)
781-
if not wav:
780+
wav = ffi.new("drwav*")
781+
if not lib.drwav_init_file(wav, filenamebytes, ffi.NULL):
782782
raise DecodeError("could not open/decode file")
783783
if seek_frame > 0:
784784
result = lib.drwav_seek_to_pcm_frame(wav, seek_frame)
@@ -796,7 +796,7 @@ def wav_stream_file(filename: str, frames_to_read: int = 1024,
796796
samples.frombytes(buffer)
797797
yield samples
798798
finally:
799-
lib.drwav_close(wav)
799+
lib.drwav_uninit(wav)
800800

801801

802802
def wav_write_file(filename: str, sound: DecodedSoundFile) -> None:
@@ -809,13 +809,13 @@ def wav_write_file(filename: str, sound: DecodedSoundFile) -> None:
809809
fmt.bitsPerSample = sound.sample_width * 8
810810
# what about floating point format?
811811
filename_bytes = filename.encode(sys.getfilesystemencoding())
812-
pwav = lib.drwav_open_file_write_sequential(filename_bytes, fmt, sound.num_frames * sound.nchannels)
813-
if pwav == ffi.NULL:
812+
pwav = ffi.new("drwav*")
813+
if not lib.drwav_init_file_write_sequential(pwav, filename_bytes, fmt, sound.num_frames * sound.nchannels, ffi.NULL):
814814
raise IOError("can't open file for writing")
815815
try:
816816
lib.drwav_write_pcm_frames(pwav, sound.num_frames, sound.samples.tobytes())
817817
finally:
818-
lib.drwav_close(pwav)
818+
lib.drwav_uninit(pwav)
819819

820820

821821
def _create_int_array(itemsize: int) -> array.array:
@@ -1483,13 +1483,14 @@ def __init__(self, pcm_sample_gen: PlaybackCallbackGeneratorType, sample_rate: i
14831483
fmt.bitsPerSample = self.sample_width * 8
14841484
data = ffi.new("void**")
14851485
datasize = ffi.new("size_t *")
1486+
pwav = ffi.new("drwav*")
14861487
if max_frames > 0:
1487-
pwav = lib.drwav_open_memory_write_sequential(data, datasize, fmt, max_frames * nchannels)
1488+
lib.drwav_init_memory_write_sequential(pwav, data, datasize, fmt, max_frames * nchannels, ffi.NULL)
14881489
else:
1489-
pwav = lib.drwav_open_memory_write(data, datasize, fmt)
1490-
lib.drwav_close(pwav)
1490+
lib.drwav_init_memory_write(pwav, data, datasize, fmt, ffi.NULL)
1491+
lib.drwav_uninit(pwav)
14911492
self.buffered = bytes(ffi.buffer(data[0], datasize[0]))
1492-
lib.drflac_free(data[0], ffi.NULL)
1493+
lib.drwav_free(data[0], ffi.NULL)
14931494

14941495
def read(self, amount: int = sys.maxsize) -> Optional[bytes]:
14951496
"""Read up to the given amount of bytes from the file."""

0 commit comments

Comments
 (0)