[tests] add parse_file_samplerate to fetch samplerate from path
authorPaul Brossier <piem@piem.org>
Sun, 23 Dec 2018 04:48:12 +0000 (05:48 +0100)
committerPaul Brossier <piem@piem.org>
Sun, 23 Dec 2018 04:48:12 +0000 (05:48 +0100)
python/tests/utils.py

index 5b789ea..4b41488 100644 (file)
@@ -1,6 +1,7 @@
 #! /usr/bin/env python
 
 import os
 #! /usr/bin/env python
 
 import os
+import re
 import glob
 import numpy as np
 from tempfile import mkstemp
 import glob
 import numpy as np
 from tempfile import mkstemp
@@ -77,3 +78,16 @@ def count_files_in_directory(samples_dir):
                 if file_path:
                     total_files += 1
     return total_files
                 if file_path:
                     total_files += 1
     return total_files
+
+def parse_file_samplerate(soundfile):
+    samplerate = None
+    # parse samplerate
+    re_sr = re.compile(r'/([0-9]{4,})Hz_.*')
+    match_samplerate = re_sr.findall(soundfile)
+    if match_samplerate:
+        samplerate = int(match_samplerate[0])
+    else:
+        import warnings
+        warnings.warn(UserWarning("could not parse samplerate for {:s}"
+            .format(soundfile)))
+    return samplerate