Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added base for Balance #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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