Skip to content

Commit

Permalink
Added base for Balance
Browse files Browse the repository at this point in the history
Added base for Balance while keeping backwardcompabillity
  • Loading branch information
Lockzi committed Jun 20, 2017
1 parent 96e6c94 commit cdb05d1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/soap.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const TEMPLATES = Object.freeze({
[TYPE.Previous]: '<u:Previous xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:Previous>',
[TYPE.Mute]: '<u:SetMute xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel><DesiredMute>{mute}</DesiredMute></u:SetMute>',
[TYPE.GroupMute]: '<u:SetGroupMute xmlns:u="urn:schemas-upnp-org:service:GroupRenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel><DesiredMute>{mute}</DesiredMute></u:SetGroupMute>',
[TYPE.Volume]: '<u:SetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel><DesiredVolume>{volume}</DesiredVolume></u:SetVolume>',
[TYPE.Volume]: '<u:SetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>{channel}</Channel><DesiredVolume>{volume}</DesiredVolume></u:SetVolume>',
[TYPE.Seek]: '<u:Seek xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Unit>{unit}</Unit><Target>{value}</Target></u:Seek>',
[TYPE.RemoveAllTracksFromQueue]: '<u:RemoveAllTracksFromQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:RemoveAllTracksFromQueue>',
[TYPE.RemoveTrackFromQueue]: '<u:RemoveTrackFromQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><ObjectID>Q:0/{track}</ObjectID></u:RemoveTrackFromQueue>',
Expand Down
76 changes: 66 additions & 10 deletions lib/models/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ function getState(playerInternal, coordinatorInternal, sub) {

const state = {
volume: playerInternal.volume,
volumeLeft: playerInternal.volumeLeft,
volumeRight: playerInternal.volumeRight,
balance: playerInternal.balance,
mute: playerInternal.mute,
equalizer: playerInternal.equalizer,
currentTrack: coordinatorInternal.currentTrack,
Expand Down Expand Up @@ -191,10 +194,25 @@ function Player(data, listener, system) {
get: () => state
});
_this.ownVolumeEvents = [];
_this._setVolume = function _setVolume(level) {
state.volume = level;
_this._setVolume = function _setVolume(level, channel) {
switch(channel) {
case 'LF':
state.volumeLeft = level;
break;
case 'RF':
state.volumeRight = level;
break;
default:
//Master or null (which by legacy is Master)
state.volume = level;
}
};

_this._setBalance = function _setBalance(level) {
console.log("_setBalance:" + level);
state.balance = level;
}

let uri = url.parse(data.location);
_this.baseUrl = `${uri.protocol}//${uri.host}`;

Expand Down Expand Up @@ -498,28 +516,66 @@ Player.prototype.muteGroup = function muteGroup() {
{ mute: 1 });
};

Player.prototype.setVolume = function setVolume(level) {
Player.prototype.setVolume = function setVolume(level, channel) {
if (this.outputFixed) {
return Promise.resolve();
}

// If prefixed with + or -
if (/^[+\-]/.test(level)) {
level = this.state.volume + parseInt(level);
switch(channel) {
case 'LF':
// If prefixed with + or -
if (/^[+\-]/.test(level)) {
level = this.state.volumeLeft + parseInt(level);
}
break;
case 'RF':
// If prefixed with + or -
if (/^[+\-]/.test(level)) {
level = this.state.volumeRight + parseInt(level);
}
break;
default:
// If prefixed with + or -
if (/^[+\-]/.test(level)) {
level = this.state.volume + parseInt(level);
}
channel = 'Master';
}

if (level < 0) level = 0;
this._setVolume(level);
this._setVolume(level, channel);

// stash this update to ignore the event when it comes back.
this.ownVolumeEvents.push(level);
this.ownVolumeEvents.push(level, channel);

return soap.invoke(
`${this.baseUrl}/MediaRenderer/RenderingControl/Control`,
TYPE.Volume,
{ volume: level });
{ volume: level, channel: channel });
};

Player.prototype.setBalance = function setBalance(level) {

console.log("setBalance: "+level);
let volumeLeft = 100;
let volumeRight = 100;

console.log("Balance: "+level);

const adjustment = (level-50)*2;
if (adjustment < 0) {
volumeRight = 100+adjustment;
} else if(level > 50) {
volumeLeft = 100-adjustment;
}

this.setVolume(volumeLeft, 'LF');
this.setVolume(volumeRight, 'RF');

this._setBalance(level);

return Promise.resolve("success");
}

Player.prototype.timeSeek = function timeSeek(seconds) {
let formattedTime = formatTime(seconds);
return soap.invoke(
Expand Down

0 comments on commit cdb05d1

Please sign in to comment.