1111import collections
1212import logging
1313import traceback
14+ import typing as T
1415
1516import av
1617import numpy as np
@@ -25,9 +26,19 @@ class NoAudioLoadedError(Exception):
2526 pass
2627
2728
28- LoadedAudio = collections .namedtuple (
29- "LoadedAudio" , ["container" , "stream" , "timestamps" , "pts" ]
30- )
29+ class LoadedAudio (T .NamedTuple ):
30+ container : T .Any
31+ stream : T .Any
32+ timestamps : T .List [float ]
33+ pts : T .List [int ]
34+
35+ def __str__ (self ):
36+ return (
37+ f"{ type (self ).__name__ } (container={ self .container } , stream={ self .stream } , "
38+ f"timestamps=(N={ len (self .timestamps )} , [{ self .timestamps [0 ]} , "
39+ f"{ self .timestamps [- 1 ]} ]), pts=(N={ len (self .pts )} , [{ self .pts [0 ]} , "
40+ f"{ self .pts [- 1 ]} ]))"
41+ )
3142
3243
3344def load_audio (rec_dir ):
@@ -95,7 +106,8 @@ def _load_audio_single(file_path, return_pts_based_timestamps=False):
95106
96107
97108class Audio_Viz_Transform :
98- def __init__ (self , rec_dir , sps_rate = 60 ):
109+ def __init__ (self , rec_dir , log_scaling = False , sps_rate = 60 ):
110+ logger .debug ("Audio_Viz_Transform.__init__: Loading audio" )
99111 self .audio_all = iter (load_audio (rec_dir ))
100112 self ._setup_next_audio_part ()
101113 self ._first_part_start = self .audio .timestamps [0 ]
@@ -106,13 +118,19 @@ def __init__(self, rec_dir, sps_rate=60):
106118 self .a_levels = None
107119 self .a_levels_log = None
108120 self .final_rescale = True
109- self .log_scaling = False
121+ self .log_scaling = log_scaling
110122
111123 def _setup_next_audio_part (self ):
112124 self .audio = next (self .audio_all )
125+ logger .debug (
126+ f"Audio_Viz_Transform._setup_next_audio_part: Part { self .audio .container } { self .audio .stream } "
127+ )
113128 self .audio_resampler = av .audio .resampler .AudioResampler (
114129 format = self .audio .stream .format , layout = self .audio .stream .layout , rate = 60
115130 )
131+ logger .debug (
132+ "Audio_Viz_Transform._setup_next_audio_part: Resampler initialized"
133+ )
116134 self .next_audio_frame = self ._next_audio_frame ()
117135 self .start_ts = self .audio .timestamps [0 ]
118136
@@ -190,7 +208,7 @@ def get_data(self, seconds=30.0, height=210, log_scale=False):
190208 else :
191209 scaled_samples = abs_samples
192210
193- else :
211+ elif self . a_levels is not None and self . all_abs_samples is not None :
194212 new_ts = self .a_levels [::4 ] # reconstruct correct ts
195213
196214 # self.all_abs_samples = np.log10(self.all_abs_samples)
@@ -212,7 +230,17 @@ def get_data(self, seconds=30.0, height=210, log_scale=False):
212230 self ._setup_next_audio_part ()
213231 except StopIteration :
214232 self .finished = True
215- if not self .finished or self .final_rescale :
233+ else :
234+ logger .debug (
235+ f"Audio_Viz_Transform.get_data: No audio found in { self .audio } "
236+ )
237+ new_ts = None
238+ try :
239+ self ._setup_next_audio_part ()
240+ except StopIteration :
241+ self .finished = True
242+
243+ if new_ts is not None and (not self .finished or self .final_rescale ):
216244 a_levels = self .get_verteces (new_ts , scaled_samples , height )
217245
218246 if self .a_levels is not None :
0 commit comments