[pitivi] previewers: More precise audio waveforms



commit 8def323b25cd6379cee1cff442fac1acae132634
Author: Thejas Kiran P S <thejaskiranps gmail com>
Date:   Sat Jul 2 16:37:17 2022 +0530

    previewers: More precise audio waveforms
    
    Even though SAMPLE_DURATION was set to 1/100th of a second, we were
    only collecting peak level data every 1/10th second and then average
    the rest of the unknown values.
    
    The SAMPLE_DURATION is now passed to the `level` element of the
    pipeline through the `interval` parameter.
    
    Fixes #2532

 pitivi/timeline/previewers.py | 24 ++++++++++++++-------
 tests/test_previewers.py      | 49 +++++++++++++++----------------------------
 2 files changed, 34 insertions(+), 39 deletions(-)
---
diff --git a/pitivi/timeline/previewers.py b/pitivi/timeline/previewers.py
index 530e86eb2..12224f4ef 100644
--- a/pitivi/timeline/previewers.py
+++ b/pitivi/timeline/previewers.py
@@ -56,7 +56,12 @@ except ImportError:
     import renderer
 
 
-SAMPLE_DURATION = Gst.SECOND / 100
+# This decides how much data we are collecting for AudioPreviewer.
+# We divide the clip into multiple samples of length SAMPLE_DURATION
+# and then fetch average peak data for each sample using 'level'
+# element. Lowering the value results in more detailed waveform
+# but also increases the time it takes to collect all data.
+SAMPLE_DURATION = Gst.SECOND // 50
 
 # Horizontal space between thumbs.
 THUMB_MARGIN_PX = 3
@@ -176,7 +181,8 @@ class WaveformPreviewer(PreviewerBin):
     def __init__(self):
         PreviewerBin.__init__(self,
                               "tee name=at ! queue ! audioconvert ! audioresample ! "
-                              "audio/x-raw,channels=1 ! level name=level"
+                              "audio/x-raw,channels=1 ! level name=level "
+                              f"interval={SAMPLE_DURATION}"
                               " ! fakesink at. ! queue")
         self.level = self.internal_bin.get_by_name("level")
         self.debug("Creating waveforms!!")
@@ -1125,9 +1131,12 @@ class ThumbnailCache(Loggable):
 
 def delete_all_files_in_dir(path):
     """Deletes the files in path without descending into subdirectories."""
-    for dir_entry in os.scandir(path):
-        if dir_entry.is_file() or dir_entry.is_symlink():
-            os.unlink(dir_entry.path)
+    try:
+        for dir_entry in os.scandir(path):
+            if dir_entry.is_file() or dir_entry.is_symlink():
+                os.unlink(dir_entry.path)
+    except FileNotFoundError:
+        pass
 
 
 def gen_filename(uri, extension):
@@ -1142,11 +1151,12 @@ def get_wavefile_location_for_uri(uri):
         uri = ProxyManager.get_target_uri(uri)
     filename = gen_filename(Gst.uri_get_location(uri), "wave.npy")
     waves_dir = xdg_cache_home("waves")
-    cache_dir = os.path.join(waves_dir, "v1")
+    cache_dir = os.path.join(waves_dir, "v2")
 
     if not os.path.exists(cache_dir):
         os.makedirs(cache_dir)
-        GLib.idle_add(delete_all_files_in_dir, waves_dir)
+        for old_cache_dir in (waves_dir, os.path.join(waves_dir, "v1")):
+            GLib.idle_add(delete_all_files_in_dir, old_cache_dir)
 
     return os.path.join(cache_dir, filename)
 
diff --git a/tests/test_previewers.py b/tests/test_previewers.py
index 66d16c7a3..b362f3ab4 100644
--- a/tests/test_previewers.py
+++ b/tests/test_previewers.py
@@ -40,37 +40,22 @@ from tests.test_medialibrary import BaseTestMediaLibrary
 
 
 SIMPSON_WAVFORM_VALUES = [
-    0.10277689604421922, 0.5078891671078481, 0.913001438171477, 1.318113709235106,
-    1.7232259802987349, 2.1283382513623637, 2.5334505224259924, 2.9385627934896212,
-    3.34367506455325, 3.748787335616879, 3.7487873356168793, 3.697464549118655,
-    3.646141762620431, 3.5948189761222067, 3.5434961896239825, 3.4921734031257583,
-    3.440850616627534, 3.38952783012931, 3.3382050436310857, 3.2868822571328615,
-    3.2868822571328606, 3.4364524655420996, 3.5860226739513386, 3.7355928823605775,
-    3.8851630907698165, 4.034733299179055, 4.1843035075882939, 4.3338737159975329,
-    4.4834439244067719, 4.6330141328160108, 4.6330141328160108, 4.6166031969548991,
-    4.6001922610937873, 4.5837813252326756, 4.5673703893715638, 4.5509594535104521,
-    4.5345485176493403, 4.5181375817882286, 4.5017266459271168, 4.485315710066005,
-    4.4853157100660033, 4.5547060070202106, 4.6240963039744178, 4.6934866009286251,
-    4.7628768978828324, 4.8322671948370397, 4.901657491791247, 4.9710477887454543,
-    5.0404380856996616, 5.1098283826538689, 5.109828382653868, 5.0866990072166436,
-    5.0635696317794192, 5.0404402563421948, 5.0173108809049705, 4.9941815054677461,
-    4.9710521300305217, 4.9479227545932973, 4.924793379156073, 4.9016640037188486,
-    4.901664003718845, 4.8508680589687616, 4.8000721142186782, 4.7492761694685948,
-    4.6984802247185113, 4.6476842799684279, 4.5968883352183445, 4.5460923904682611,
-    4.4952964457181777, 4.4445005009680942, 4.4445005009680925, 4.5544001248210364,
-    4.6642997486739803, 4.7741993725269243, 4.8840989963798682, 4.9939986202328122,
-    5.1038982440857561, 5.2137978679387, 5.323697491791644, 5.4335971156445879,
-    5.433597115644587, 5.3988578703107724, 5.3641186249769577, 5.329379379643143,
-    5.2946401343093283, 5.2599008889755137, 5.225161643641699, 5.1904223983078843,
-    5.1556831529740696, 5.120943907640255, 5.120943907640255, 5.0895954243424724,
-    5.0582469410446897, 5.0268984577469071, 4.9955499744491245, 4.9642014911513419,
-    4.9328530078535593, 4.9015045245557767, 4.8701560412579941, 4.8388075579602114,
-    4.8388075579602079, 4.7336937056290518, 4.6285798532978957, 4.5234660009667396,
-    4.4183521486355835, 4.3132382963044273, 4.2081244439732712, 4.1030105916421151,
-    3.9978967393109586, 3.892782886979802, 3.8927828869797994, 3.8630968485705619,
-    3.8334108101613245, 3.803724771752087, 3.7740387333428496, 3.7443526949336121,
-    3.7146666565243747, 3.6849806181151372, 3.6552945797058998, 3.6256085412966623,
-    3.6256085412966614, 0.0]
+    1.0000000180025155e-33, 1.0000000180025155e-33, 9.466735608724685e-05,
+    0.0016802854651403348, 0.2298099638424507, 2.3616320439402143, 4.049185439556839,
+    3.284195196383849, 4.05835985413913, 4.586676466667793, 4.940914967594509,
+    3.5891955340346477, 2.2974444692377567, 1.5337796548101932, 3.015340518747337,
+    4.402133429862438, 4.773357722910441, 4.779009324079252, 4.655537541962497,
+    4.543942909514233, 4.9320770351156735, 3.7114167237366957, 2.6811979044348884,
+    5.377787572575852, 5.136230693687676, 5.419229650541714, 5.35647659134891,
+    4.5933491070305665, 5.460428009340401, 4.645073622414212, 4.378695790226624,
+    5.259486582101626, 4.5281589368036546, 5.265094016436164, 5.00708854897167,
+    4.513459848980695, 3.792516292044058, 3.836904642287806, 4.531396212120468,
+    5.362651864191655, 5.408963618250316, 5.120883787773891, 4.934966728544448,
+    6.175705535014139, 5.444842919425695, 4.957031005987477, 5.53745374776641,
+    5.580341542054587, 4.611911374969583, 4.845084505605837, 5.450476172859177,
+    5.7348769111251405, 4.443313266936532, 4.367055669701466, 3.9572188728697317,
+    3.305387555242148, 4.614435114384071, 3.5434378665402257, 4.452167697134032,
+    3.3425208440888636, 3.5801852530575835]
 
 
 class TestPreviewers(BaseTestMediaLibrary):
@@ -233,7 +218,7 @@ class TestAudioPreviewer(TestPreviewers):
         def set_source_surface(surface, offset_x, offset_y):
             offsets.append(offset_x)
 
-        samples = list(range(199))
+        samples = list(range(99))
         for previewer in audio_previewers:
             previewer.samples = samples
             with mock.patch.object(Gdk, "cairo_get_clip_rectangle") as cairo_get_clip_rectangle:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]