Skip to content

Commit

Permalink
Have the server tell the client whether there is a backing track, and…
Browse files Browse the repository at this point in the history
… if so what kind, what's the client can use to decide whether to show the backing track volume slider, name it properly, and choose whether you can join the first packet. Fixes #169
  • Loading branch information
jeffkaufman committed Feb 7, 2021
1 parent a29ab27 commit abd14e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
10 changes: 9 additions & 1 deletion html/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ function update_active_users(
if (userid == myUserid) {
for (var j = 0 ; j < N_BUCKETS; j++) {
window.buckets.children[j].children[0].children[1].disabled =
est_bucket === j;
(j == 0 && !backingTrackOn && !imLeading) || est_bucket === j;
}
}

Expand Down Expand Up @@ -1638,6 +1638,7 @@ function connect_twilio() {
});
}

let backingTrackOn = false;
async function start_singing() {
var final_url = server_api_path();

Expand Down Expand Up @@ -1713,6 +1714,13 @@ async function start_singing() {
if (metadata["backingVolume"] != null) {
window.backingVolumeControl.value = metadata["backingVolume"];
}
if (metadata["backing_track_type"] != null) {
const backingTrackType = metadata["backing_track_type"];
backingTrackOn = !!backingTrackType;
window.backingSliderDiv.style.display =
backingTrackOn ? "block" : "none";
window.backingTrackTypeName.innerText = backingTrackType;
}

first_bucket_s = metadata["first_bucket"] || first_bucket_s;

Expand Down
8 changes: 5 additions & 3 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@
width: 12em;
}
}

#backingSliderDiv {
display: none;
}
#remainderWindow {
flex-grow: 1;
display: flex;
Expand Down Expand Up @@ -1192,8 +1194,8 @@ <h3>Something else weird is going on</h3>
<div id=spectatorMode>As a spectator, your singing is not included</div>
</div>

<div>
<label for=backingVolumeSlider>Backing Volume:</label>
<div id=backingSliderDiv>
<label for=backingVolumeSlider><span id=backingTrackTypeName></span> Volume:</label>
<input type="range" min="0" max="160" value="100" id="backingVolumeSlider">
</div>

Expand Down
16 changes: 12 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def reset(self):

self.backing_track: Any = np.zeros(0)
self.backing_track_index = 0
self.backing_track_type = ""

self.max_position = DELAY_INTERVAL*LAYERING_DEPTH

Expand Down Expand Up @@ -469,6 +470,7 @@ def run_backing_track() -> None:
inf.readframes(-1), np.int16).astype(np.float32) / (2**15)
state.backing_track *= 0.8 # turn it down a bit
state.backing_track_index = 0
state.backing_track_type = "Backing Track"

# Backing track is used only once.
state.requested_track = None
Expand Down Expand Up @@ -766,6 +768,11 @@ def handle_json_post(in_json_raw, in_data):
"x-audio-metadata": x_audio_metadata,
}), out_data

def end_song():
state.leader = None
state.backing_track_type = ""
sendall("backing_track_type", state.backing_track_type)

# Handle special operations that do not require a user (although they may
# optionally support one), but can be done server-to-server as well.
def handle_special(query_params, server_clock, user=None, client_read_clock=None):
Expand Down Expand Up @@ -817,16 +824,18 @@ def handle_special(query_params, server_clock, user=None, client_read_clock=None
state.song_start_clock = server_clock
state.song_end_clock = 0

state.backing_track_type = ""
if state.bpm > 0:
backfill_metronome()
state.backing_track_type = "Metronome"
elif state.requested_track:
run_backing_track()
# These must be separate from song_start/end_clock, because they
# are used for video sync and must be EXACTLY at the moment the
# backing track starts/ends, not merely close.
#insert_event("backingTrackStart", server_clock)
#insert_event("backingTrackEnd", server_clock + len(state.backing_track))

sendall("backing_track_type", state.backing_track_type)

if query_params.get("mark_stop_singing", None):
# stop the backing track from playing, if it's still going
Expand All @@ -842,8 +851,7 @@ def handle_special(query_params, server_clock, user=None, client_read_clock=None
else:
state.song_end_clock = server_clock

state.leader = None

end_song()

if query_params.get("clear_events", None):
events.clear()
Expand Down Expand Up @@ -977,7 +985,7 @@ def handle_post(in_json, in_data) -> Tuple[Any, str]:
if state.backing_track_index == len(state.backing_track):
# the song has ended, mark it so
state.song_end_clock = clear_index
state.leader = None
end_song()

if clear_samples > 0:
if state.bpm > 0:
Expand Down

0 comments on commit abd14e1

Please sign in to comment.