Fixes re: zognia's testing #2

Merged
skeh merged 11 commits from feat/zogpog into main 2025-01-24 08:28:14 +00:00
2 changed files with 17 additions and 21 deletions
Showing only changes of commit 69fc34396f - Show all commits

View file

@ -26,7 +26,7 @@ os.close(old_stderr)
logger = logging.getLogger(__name__)
class Clip:
def __init__(self, path, samplerate=None, speed=1, force_stereo=True):
def __init__(self, path, samplerate=None, speed=1, keep_pitch=True, force_stereo=True):
self.path = path
raw, native_rate = librosa.load(self.path, sr=None, dtype='float32', mono=False)
@ -42,7 +42,7 @@ class Clip:
self.raw = np.ascontiguousarray(self._stereo_transpose(raw), dtype='float32')
if speed != 1:
self.stretch(speed)
self.stretch(speed, keep_pitch=keep_pitch)
@property
def length(self):
@ -51,8 +51,11 @@ class Clip:
def _stereo_transpose(self, ndata):
return ndata if self.channels == 1 else ndata.T
def stretch(self, speed):
stretched = tsm.wsola(self._stereo_transpose(self.raw), speed)
def stretch(self, speed, keep_pitch=True):
if keep_pitch:
stretched = tsm.wsola(self._stereo_transpose(self.raw), speed)
else:
stretched = librosa.resample(self._stereo_transpose(self.raw), self.samplerate * (1 / speed), self.samplerate, fix=False, scale=True)
self.raw = np.ascontiguousarray(self._stereo_transpose(stretched), dtype='float32')
def save(self, filename):

View file

@ -22,22 +22,21 @@ class AudioAlert(PluginBase):
self._cleanup_task = asyncio.create_task(self._cleanup())
def run(self, path, speed=1, immediate=True, poly=1, **kwargs):
def run(self, path, speed=1, keep_pitch=False, immediate=True, poly=1, **kwargs):
poly = int(poly)
key = f'{path}@{speed}x'
key = f'{path}@{speed}{"X" if keep_pitch else "x"}'
clip = self.clips.get(key, [None, None])[0]
if clip is None:
clip = Clip(path, speed=speed, force_stereo=self.force_stereo)
clip = Clip(path, speed=speed, keep_pitch=keep_pitch, force_stereo=self.force_stereo)
self.clips[key] = [clip, maya.now()]
else:
self.clips[key][1] = maya.now()
stream_dq, refs = self.streams.get(path, (None, set()))
stream_dq = self.streams.get(path, None)
if stream_dq is None:
stream_dq = deque(maxlen=poly)
self.streams[path] = (stream_dq, refs)
refs.add(key)
self.streams[key] = stream_dq
if stream_dq.maxlen != poly:
self.logger.warn('Cannot change poly while streams are active!')
@ -61,15 +60,9 @@ class AudioAlert(PluginBase):
now = maya.now()
for key, [clip, last_used] in list(self.clips.items()):
if now >= last_used.add(minutes=self.timeout_min, seconds=clip.length):
del self.clips[key]
self.logger.debug(f'Dropping {key}')
streams, refs = self.streams.get(clip.path, (None, None))
if refs:
refs.remove(key)
self.logger.debug(f'Stream {clip.path} now refs {refs}')
if len(refs) == 0:
self.logger.debug('Closing streams...')
for stream in streams:
stream.close()
del self.streams[clip.path]
streams = self.streams.get(key, [])
for stream in streams:
stream.close()
del self.streams[key]
del self.clips[key]