Commit
·
4587340
1
Parent(s):
1f74f2f
oh boy more reverting
Browse files- jam_worker.py +10 -29
jam_worker.py
CHANGED
|
@@ -10,7 +10,6 @@ from utils import (
|
|
| 10 |
apply_micro_fades, make_bar_aligned_context, take_bar_aligned_tail,
|
| 11 |
resample_and_snap, wav_bytes_base64
|
| 12 |
)
|
| 13 |
-
from math import floor, ceil
|
| 14 |
|
| 15 |
@dataclass
|
| 16 |
class JamParams:
|
|
@@ -415,31 +414,15 @@ class JamWorker(threading.Thread):
|
|
| 415 |
chunk_secs = self.params.bars_per_chunk * spb
|
| 416 |
xfade = float(self.mrt.config.crossfade_length) # seconds
|
| 417 |
sr = int(self.mrt.sample_rate)
|
| 418 |
-
|
| 419 |
-
self._emit_phase = getattr(self, "_emit_phase", 0.0)
|
| 420 |
-
|
| 421 |
-
def _need(first_chunk_extra: bool = False) -> int:
|
| 422 |
-
"""
|
| 423 |
-
How many more samples we still need in the stream to emit the next slice.
|
| 424 |
-
Uses the fractional step (chunk_step_f) + current _emit_phase to compute
|
| 425 |
-
the *integer* number of samples required for the next chunk, without
|
| 426 |
-
mutating _emit_phase here.
|
| 427 |
-
"""
|
| 428 |
-
start = getattr(self, "_next_emit_start", 0)
|
| 429 |
-
total = 0 if getattr(self, "_stream", None) is None else self._stream.shape[0]
|
| 430 |
-
have = max(0, total - start)
|
| 431 |
-
|
| 432 |
-
# Compute the integer step we'd use for the next emit, non-mutating.
|
| 433 |
-
emit_phase = float(getattr(self, "_emit_phase", 0.0))
|
| 434 |
-
step_int = int(floor(chunk_step_f + emit_phase)) # matches the logic used when advancing
|
| 435 |
-
|
| 436 |
-
# How much we want available beyond 'start' for this emit.
|
| 437 |
-
want = step_int
|
| 438 |
-
if first_chunk_extra:
|
| 439 |
-
# Reserve two extra bars so the first-chunk onset alignment has material.
|
| 440 |
-
# Use ceil to be conservative so we don't under-request.
|
| 441 |
-
want += int(ceil(2.0 * spb * sr))
|
| 442 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
return max(0, want - have)
|
| 444 |
|
| 445 |
def _mono_env(x: np.ndarray, sr: int, win_ms: float = 10.0) -> np.ndarray:
|
|
@@ -465,6 +448,7 @@ class JamWorker(threading.Thread):
|
|
| 465 |
return 0
|
| 466 |
|
| 467 |
# envelopes + z-score
|
|
|
|
| 468 |
def _z(a):
|
| 469 |
m, s = float(a.mean()), float(a.std() or 1.0); return (a - m) / s
|
| 470 |
e_ref = _z(_mono_env(ref_tail, sr)).astype(np.float32)
|
|
@@ -535,10 +519,7 @@ class JamWorker(threading.Thread):
|
|
| 535 |
|
| 536 |
# 3) Emit exactly bars_per_chunk × spb from the stream
|
| 537 |
start = self._next_emit_start
|
| 538 |
-
|
| 539 |
-
step_int = int(np.floor(step_total))
|
| 540 |
-
self._emit_phase = float(step_total - step_int)
|
| 541 |
-
end = start + step_int
|
| 542 |
if end > self._stream.shape[0]:
|
| 543 |
# shouldn't happen often; generate a bit more and loop
|
| 544 |
continue
|
|
|
|
| 10 |
apply_micro_fades, make_bar_aligned_context, take_bar_aligned_tail,
|
| 11 |
resample_and_snap, wav_bytes_base64
|
| 12 |
)
|
|
|
|
| 13 |
|
| 14 |
@dataclass
|
| 15 |
class JamParams:
|
|
|
|
| 414 |
chunk_secs = self.params.bars_per_chunk * spb
|
| 415 |
xfade = float(self.mrt.config.crossfade_length) # seconds
|
| 416 |
sr = int(self.mrt.sample_rate)
|
| 417 |
+
chunk_samps = int(round(chunk_secs * sr))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
+
def _need(first_chunk_extra=False):
|
| 420 |
+
"""How many more samples we still need in the stream to emit next slice."""
|
| 421 |
+
have = 0 if getattr(self, "_stream", None) is None else self._stream.shape[0] - getattr(self, "_next_emit_start", 0)
|
| 422 |
+
want = chunk_samps
|
| 423 |
+
if first_chunk_extra:
|
| 424 |
+
# reserve two bars extra so first-chunk onset alignment has material
|
| 425 |
+
want += int(round(2 * spb * sr))
|
| 426 |
return max(0, want - have)
|
| 427 |
|
| 428 |
def _mono_env(x: np.ndarray, sr: int, win_ms: float = 10.0) -> np.ndarray:
|
|
|
|
| 448 |
return 0
|
| 449 |
|
| 450 |
# envelopes + z-score
|
| 451 |
+
import numpy as np
|
| 452 |
def _z(a):
|
| 453 |
m, s = float(a.mean()), float(a.std() or 1.0); return (a - m) / s
|
| 454 |
e_ref = _z(_mono_env(ref_tail, sr)).astype(np.float32)
|
|
|
|
| 519 |
|
| 520 |
# 3) Emit exactly bars_per_chunk × spb from the stream
|
| 521 |
start = self._next_emit_start
|
| 522 |
+
end = start + chunk_samps
|
|
|
|
|
|
|
|
|
|
| 523 |
if end > self._stream.shape[0]:
|
| 524 |
# shouldn't happen often; generate a bit more and loop
|
| 525 |
continue
|