changeset 114:87db8004ca23

Add function to load compressed audio formats with pydub
author Amine Sehili <amine.sehili@gmail.com>
date Tue, 22 Jan 2019 20:22:32 +0100
parents 7771f11179fb
children 0d4a23668858
files auditok/io.py
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/auditok/io.py	Sun Jan 20 17:37:19 2019 +0100
+++ b/auditok/io.py	Tue Jan 22 20:22:32 2019 +0100
@@ -713,6 +713,39 @@
     )
 
 
+def _load_with_pydub(filename, audio_format, use_channel=0):
+    """Open compressed audio file using pydub. If a video file
+    is passed, its audio track(s) are extracted and loaded.
+    This function should not be called directely, use :func:`from_file`
+    instead.
+
+    :Parameters:
+
+    `filename`:
+        path to audio file.
+    `audio_format`:
+        string, audio file format (e.g. raw, webm, wav, ogg)
+    """
+    func_dict = {
+        "mp3": AudioSegment.from_mp3,
+        "ogg": AudioSegment.from_ogg,
+        "flv": AudioSegment.from_flv,
+    }
+    open_function = func_dict.get(audio_format, AudioSegment.from_file)
+    segment = open_function(filename)
+    data = segment._data
+    if segment.channels > 1:
+        data = _extract_selected_channel(
+            data, segment.channels, segment.sample_width, use_channel
+        )
+    return BufferAudioSource(
+        data_buffer=data,
+        sampling_rate=segment.frame_rate,
+        sample_width=segment.sample_width,
+        channels=1,
+    )
+
+
 def from_file(filename):
     """
     Create an `AudioSource` object using the audio file specified by `filename`.