308 lines
11 KiB
JavaScript
308 lines
11 KiB
JavaScript
(function(context,$) {
|
|
|
|
"use strict";
|
|
|
|
context.JK = context.JK || {};
|
|
context.JK.SessionScreen = function(app) {
|
|
var logger = context.JK.logger;
|
|
var sessionId;
|
|
var session = null;
|
|
var users = {}; // Light cache of user info for session users.
|
|
var tracks = {};
|
|
var mixers = [];
|
|
|
|
// Replicate the Channel Group enum from C++
|
|
var ChannelGroupIds = {
|
|
0: "MasterGroup",
|
|
1: "MonitorGroup",
|
|
2: "AudioInputMusicGroup",
|
|
3: "AudioInputChatGroup",
|
|
4: "MediaTrackGroup",
|
|
5: "StreamOutMusicGroup",
|
|
6: "StreamOutChatGroup",
|
|
7: "UserMusicInputGroup",
|
|
8: "UserChatInputGroup",
|
|
9: "PeerAudioInputMusicGroup"
|
|
};
|
|
|
|
function beforeShow(data) {
|
|
sessionId = data.id;
|
|
}
|
|
|
|
function afterShow(data) {
|
|
context.JK.joinMusicSession(sessionId, app);
|
|
// Subscribe for callbacks on audio events
|
|
context.jamClient.SessionRegisterCallback("JK.HandleBridgeCallback");
|
|
$.ajax({
|
|
type: "GET",
|
|
url: "/api/sessions/" + sessionId
|
|
}).done(updateSession);
|
|
}
|
|
|
|
function beforeHide(data) {
|
|
// Move joinSession function to this file for ease of finding it?
|
|
context.JK.leaveMusicSession(sessionId);
|
|
// 'unregister' for callbacks
|
|
context.jamClient.SessionRegisterCallback("");
|
|
}
|
|
|
|
function updateSession(sessionData) {
|
|
session = sessionData;
|
|
updateParticipants(function() { renderSession(); });
|
|
}
|
|
|
|
/**
|
|
* Make sure that for each participant in the session, we have user info.
|
|
*/
|
|
function updateParticipants(onComplete) {
|
|
var callCount = 0;
|
|
$.each(session.participants, function(index, value) {
|
|
if (!(this.user.id in users)) {
|
|
callCount += 1;
|
|
$.ajax({
|
|
type: "GET",
|
|
url: "/api/users/" + this.user.id
|
|
}).done(function(user) {
|
|
callCount -= 1;
|
|
users[user.id] = user;
|
|
// We'll be using our own photo url instead of gravatar.
|
|
//var hash = context.JK.calcMD5(user.email);
|
|
//users[user.id].photo_url = 'http://www.gravatar.com/avatar/' + hash;
|
|
});
|
|
}
|
|
});
|
|
if (!(onComplete)) {
|
|
return;
|
|
}
|
|
// TODO: generalize this pattern. Likely needed elsewhere.
|
|
function checker() {
|
|
if (callCount === 0) {
|
|
onComplete();
|
|
} else {
|
|
context.setTimeout(checker, 10);
|
|
}
|
|
}
|
|
checker();
|
|
}
|
|
|
|
function renderSession() {
|
|
_updateMixers();
|
|
_renderTracks();
|
|
}
|
|
|
|
// Get the latest list of underlying audio mixer channels
|
|
function _updateMixers() {
|
|
var mixerIds = context.jamClient.SessionGetIDs();
|
|
mixers = context.jamClient.SessionGetControlState(mixerIds);
|
|
logger.debug("Mixers updated:");
|
|
logger.debug(mixers);
|
|
}
|
|
|
|
// Given a clientId, return a dictionary of mixer ids which
|
|
// correspond to that client's tracks
|
|
// TODO - this is hard-coded. Need fix from Nat
|
|
function _inputMixerForClient(clientId) {
|
|
// Iterate over the mixers, but simply return the first
|
|
// mixer in the AudioInputMusicGroup
|
|
var mixer = null;
|
|
for (var i=0; i<mixers.length; i++) {
|
|
mixer = mixers[i];
|
|
if (mixer.group_id === 2) {
|
|
return mixer.id;
|
|
}
|
|
}
|
|
return null; // no audio input music mixers.
|
|
}
|
|
|
|
function _renderTracks() {
|
|
var participantCount = 0;
|
|
var inputMixer = null;
|
|
$.each(session.participants, function(index, value) {
|
|
if (!(this.client_id in tracks)) {
|
|
var user = users[this.user.id];
|
|
// TODO - handle multiple tracks for one user
|
|
inputMixer = _inputMixerForClient(this.client_id);
|
|
var trackData = {
|
|
clientId: this.client_id,
|
|
name: user.first_name + ' ' + user.last_name,
|
|
part: "Keyboard", // TODO - need this
|
|
avatar: user.photo_url,
|
|
latency: "good",
|
|
vu: 0.0,
|
|
gain: 0.5,
|
|
mute: false,
|
|
mixerId: inputMixer
|
|
};
|
|
_addTrack(trackData);
|
|
}
|
|
});
|
|
// Trim out any participants who are no longer in the session.
|
|
for (var clientId in tracks) {
|
|
var hasLeftSession = true;
|
|
for (var i=0; i<session.participants.length; i++) {
|
|
var participantId = session.participants[i].client_id;
|
|
if (participantId === clientId) {
|
|
hasLeftSession = false;
|
|
break;
|
|
}
|
|
}
|
|
if (hasLeftSession) {
|
|
$('[client-id="' + clientId + '"]').remove();
|
|
delete tracks[clientId];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Given a mixerID and a value between 0.0-1.0,
|
|
// light up the proper VU lights.
|
|
function _updateVU(mixerId, value) {
|
|
// Minor tweak to support VU values -- the mixerId here will
|
|
// have a suffix added _vul or _vur to indicate the channel.
|
|
// There are 13 VU lights. Figure out how many to
|
|
// light based on the incoming value.
|
|
var i = 0;
|
|
var state = 'on';
|
|
var lights = Math.round(13 * value);
|
|
var selector = null;
|
|
var $light = null;
|
|
var colorClass = 'vu-green-';
|
|
// Remove all light classes from all lights
|
|
var allLightsSelector = '#tracks table[mixer-id="' + mixerId + '"] td.vulight';
|
|
$(allLightsSelector).removeClass('vu-green-off vu-green-on vu-red-off vu-red-on');
|
|
|
|
// Set the lights
|
|
for (i=0; i<13; i++) {
|
|
colorClass = 'vu-green-';
|
|
state = 'on';
|
|
if (i > 8) {
|
|
colorClass = 'vu-red-';
|
|
}
|
|
if (i >= lights) {
|
|
state = 'off';
|
|
}
|
|
selector = '#tracks table[mixer-id="' + mixerId + '"] td.vu' + i;
|
|
$light = $(selector);
|
|
$light.addClass(colorClass + state);
|
|
}
|
|
}
|
|
|
|
function _addTrack(trackData) {
|
|
trackData["left-vu"] = $('#template-vu').html();
|
|
trackData["right-vu"] = trackData["left-vu"];
|
|
var template = $('#template-session-track').html();
|
|
var newTrack = context.JK.fillTemplate(template, trackData);
|
|
$('#session-mytracks-container').append(newTrack);
|
|
tracks[trackData.clientId] = new context.JK.SessionTrack(trackData.clientId);
|
|
}
|
|
|
|
function _userJoinedSession(header, payload) {
|
|
// Just refetch the session and update.
|
|
$.ajax({
|
|
type: "GET",
|
|
url: "/api/sessions/" + sessionId
|
|
}).done(function(response) {
|
|
updateSession(response);
|
|
});
|
|
}
|
|
|
|
function handleBridgeCallback() {
|
|
var eventName = null;
|
|
var mixerId = null;
|
|
var value = null;
|
|
var tuples = arguments.length / 3;
|
|
for (var i=0; i<tuples; i++) {
|
|
eventName = arguments[3*i];
|
|
mixerId = arguments[(3*i)+1];
|
|
value = arguments[(3*i)+2];
|
|
//logger.debug(tuples + ',' + eventName + ',' + mixerId + ',' + value);
|
|
var vuVal = 0.0;
|
|
if (eventName === 'left_vu' || eventName === 'right_vu') {
|
|
// TODO - no guarantee range will be -80 to 20. Get from the
|
|
// GetControlState for this mixer which returns min/max
|
|
// value is a DB value from -80 to 20. Convert to float from 0.0-1.0
|
|
vuVal = (value + 80) / 100;
|
|
if (eventName === 'left_vu') {
|
|
mixerId = mixerId + "_vul";
|
|
} else {
|
|
mixerId = mixerId + "_vur";
|
|
}
|
|
_updateVU(mixerId, vuVal);
|
|
} else {
|
|
// Examples of other events
|
|
// Add media file track: "add", "The_Abyss_4T", 0
|
|
logger.debug('non-vu event: ' + eventName + ',' + mixerId + ',' + value);
|
|
}
|
|
}
|
|
}
|
|
|
|
function deleteSession(evt) {
|
|
var sessionId = $(evt.currentTarget).attr("action-id");
|
|
if (sessionId) {
|
|
$.ajax({
|
|
type: "DELETE",
|
|
url: "/api/sessions/" + sessionId
|
|
}).done(
|
|
function() { context.location="#/home"; }
|
|
);
|
|
}
|
|
}
|
|
|
|
function _toggleVisualMuteControl($control, muting) {
|
|
if (muting) {
|
|
$control.removeClass('enabled');
|
|
$control.addClass('muted');
|
|
} else {
|
|
$control.removeClass('muted');
|
|
$control.addClass('enabled');
|
|
}
|
|
}
|
|
|
|
function _toggleAudioMute(mixerId, muting) {
|
|
// 'fill' the trackVolume object volumes from what's in the
|
|
// corresponding mixer.
|
|
var mixer = null;
|
|
for (var i=0; i<mixers.length; i++) {
|
|
mixer = mixers[i];
|
|
if (mixer.id === mixerId) {
|
|
context.trackVolumeObject.volL = mixer.volume_left;
|
|
context.trackVolumeObject.volR = mixer.volume_right;
|
|
break;
|
|
}
|
|
}
|
|
context.trackVolumeObject.mute = muting;
|
|
logger.debug("Set Control State:");
|
|
logger.debug(context.trackVolumeObject);
|
|
context.jamClient.SessionSetControlState(mixerId);
|
|
}
|
|
|
|
function toggleMute(evt) {
|
|
var $control = $(evt.currentTarget);
|
|
var mixerId = $control.attr('mixer-id');
|
|
var muting = ($control.hasClass('enabled'));
|
|
_toggleVisualMuteControl($control, muting);
|
|
_toggleAudioMute(mixerId, muting);
|
|
}
|
|
|
|
function events() {
|
|
$('#session-contents').on("click", '[action="delete"]', deleteSession);
|
|
$('#tracks').on('click', 'div[control="mute"]', toggleMute);
|
|
}
|
|
|
|
this.initialize = function() {
|
|
events();
|
|
var screenBindings = {
|
|
'beforeShow': beforeShow,
|
|
'afterShow': afterShow,
|
|
'beforeHide': beforeHide
|
|
};
|
|
app.bindScreen('session', screenBindings);
|
|
app.subscribe(context.JK.MessageType.USER_JOINED_MUSIC_SESSION, _userJoinedSession);
|
|
};
|
|
|
|
this.tracks = tracks;
|
|
|
|
context.JK.HandleBridgeCallback = handleBridgeCallback;
|
|
|
|
};
|
|
|
|
})(window,jQuery); |