Averaging group volume

This commit is contained in:
James Barnsley
2018-05-02 16:14:53 +12:00
parent 781d46168a
commit 0e87515e29
9 changed files with 192 additions and 62 deletions

View File

@ -5065,10 +5065,13 @@ function setSnapcastGroupMute(id, mute) {
}
function setSnapcastGroupVolume(id, percent) {
var old_percent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
return {
type: 'PUSHER_SET_SNAPCAST_GROUP_VOLUME',
id: id,
percent: percent
percent: percent,
old_percent: old_percent
};
}
@ -24125,6 +24128,7 @@ var VolumeControl = function (_React$Component) {
_createClass(VolumeControl, [{
key: 'handleClick',
value: function handleClick(e) {
var old_percent = this.props.volume;
var slider = e.target;
if (slider.className != 'slider') slider = slider.parentElement;
@ -24138,12 +24142,13 @@ var VolumeControl = function (_React$Component) {
percent = 0;
}
this.props.onVolumeChange(percent);
this.props.onVolumeChange(percent, old_percent);
}
}, {
key: 'handleWheel',
value: function handleWheel(e) {
if (this.props.scrollWheel) {
var old_percent = this.props.volume;
// Identify which direction we've scrolled (inverted)
// This is simplified and doesn't consider momentum as it varies wildly
@ -24159,7 +24164,7 @@ var VolumeControl = function (_React$Component) {
percent = 0;
}
this.props.onVolumeChange(percent);
this.props.onVolumeChange(percent, old_percent);
e.preventDefault();
}
}
@ -24205,6 +24210,10 @@ var VolumeControl = function (_React$Component) {
if (this.props.mute) {
className += " muted";
}
if (this.props.className) {
className += " " + this.props.className;
}
return _react2.default.createElement(
'span',
{ className: className, onWheel: function onWheel(e) {
@ -51721,7 +51730,7 @@ var PusherMiddleware = function () {
}
});
}, function (error) {
store.dispatch(coreActions.handleException('Error', error, error.message));
store.dispatch(coreActions.handleException('Could not change stream', error, error.message));
});
break;
@ -51744,10 +51753,46 @@ var PusherMiddleware = function () {
}
});
}, function (error) {
store.dispatch(coreActions.handleException('Error', error, error.message));
store.dispatch(coreActions.handleException('Could not toggle mute', error, error.message));
});
break;
case 'PUSHER_SET_SNAPCAST_GROUP_VOLUME':
var clients_to_update = [];
var group = store.getState().pusher.snapcast_groups[action.id];
var change = action.percent - action.old_percent;
for (var i = 0; i < group.clients_ids.length; i++) {
// Apply the change proportionately to each client
var client = store.getState().pusher.snapcast_clients[group.clients_ids[i]];
var current_percent = client.config.volume.percent;
var new_percent = current_percent + change;
// Only change if the client is within min/max limits
if (change > 0 && current_percent < 100 || change < 0 && current_percent > 0) {
clients_to_update.push({
id: client.id,
percent: new_percent
});
}
}
// Loop our required changes, and post each to Snapcast
for (var i = 0; i < clients_to_update.length; i++) {
var update = clients_to_update[i];
var percent = update.percent + (group.clients_ids.length - clients_to_update.length) * change;
// Make sure we're not creating an impossible percent
if (percent < 0) {
percent = 0;
} else if (percent > 100) {
percent = 100;
}
store.dispatch(pusherActions.setSnapcastClientVolume(update.id, percent));
}
// This action is irrelevant to us, pass it on to the next middleware
default:
return next(action);
@ -70573,6 +70618,7 @@ var Snapcast = function (_React$Component) {
'div',
{ className: 'col volume' },
_react2.default.createElement(_VolumeControl2.default, {
className: 'client-volume-control',
volume: client.config.volume.percent,
mute: client.config.volume.muted,
onVolumeChange: function onVolumeChange(percent) {
@ -70704,6 +70750,15 @@ var Snapcast = function (_React$Component) {
)
),
groups.map(function (group, i) {
// Average our clients' volume for an overall group volume
var group_volume = 0;
for (var i = 0; i < group.clients.length; i++) {
var client = group.clients[i];
group_volume += client.config.volume.percent;
}
group_volume = group_volume / group.clients.length;
return _react2.default.createElement(
'div',
{ className: 'group', key: group.id },
@ -70774,10 +70829,11 @@ var Snapcast = function (_React$Component) {
'div',
{ className: 'input' },
_react2.default.createElement(_VolumeControl2.default, {
volume: group.volume,
className: 'group-volume-control',
volume: group_volume,
mute: group.muted,
onVolumeChange: function onVolumeChange(percent) {
return _this3.props.pusherActions.setSnapcastGroupVolume(group.id, percent);
onVolumeChange: function onVolumeChange(percent, old_percent) {
return _this3.props.pusherActions.setSnapcastGroupVolume(group.id, percent, old_percent);
},
onMuteChange: function onMuteChange(mute) {
return _this3.props.pusherActions.setSnapcastGroupMute(group.id, mute);