changeset 113:7771f11179fb

Add function to read wave audio data
author Amine Sehili <amine.sehili@gmail.com>
date Sun, 20 Jan 2019 17:37:19 +0100
parents e5813b709634
children 87db8004ca23
files auditok/io.py
diffstat 1 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/auditok/io.py	Sun Jan 20 16:46:35 2019 +0100
+++ b/auditok/io.py	Sun Jan 20 17:37:19 2019 +0100
@@ -691,6 +691,28 @@
         )
 
 
+def _load_wave(filename, large_file=False, use_channel=0):
+    """
+    Load a wave audio file with standard Python.
+    If `large_file` is True, audio data will be lazily
+    loaded to memory.
+
+    See also :func:`to_file`.
+    """
+    if large_file:
+        return WaveAudioSource(filename, use_channel)
+    with wave.open(filename) as fp:
+        channels = fp.getnchannels()
+        srate = fp.getframerate()
+        swidth = fp.getsampwidth()
+        data = fp.readframes(-1)
+    if channels > 1:
+        data = _extract_selected_channel(data, channels, swidth, use_channel)
+    return BufferAudioSource(
+        data, sampling_rate=srate, sample_width=swidth, channels=1
+    )
+
+
 def from_file(filename):
     """
     Create an `AudioSource` object using the audio file specified by `filename`.