Skip to content

Commit 9578de9

Browse files
authored
Merge pull request #2206 from pupil-labs/fix_short_audio_loading
Fix loading short audio streams
2 parents ffd3549 + 85429fc commit 9578de9

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

pupil_src/shared_modules/audio_playback.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ def __init__(self, g_pool):
8181

8282
try:
8383
self.audio_all = load_audio(self.g_pool.rec_dir)
84+
logger.debug("Audio_Playback.__init__: Audio loaded successfully")
8485
except NoAudioLoadedError:
86+
logger.debug("Audio_Playback.__init__: No audio loaded")
8587
return
8688

8789
self.calculate_audio_bounds()
8890

8991
self.filter_graph = None
9092
self.filter_graph_list = None
93+
logger.debug("Audio_Playback.__init__: Initializing PyAudio")
9194
self.pa = pa.PyAudio()
95+
logger.debug("Audio_Playback.__init__: PyAudio initialized")
9296

9397
self._setup_input_audio_part(0)
9498

pupil_src/shared_modules/audio_utils.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import collections
1212
import logging
1313
import traceback
14+
import typing as T
1415

1516
import av
1617
import 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

3344
def load_audio(rec_dir):
@@ -95,7 +106,8 @@ def _load_audio_single(file_path, return_pts_based_timestamps=False):
95106

96107

97108
class 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

Comments
 (0)