Skip to content

Commit

Permalink
Added setPan behavior for android (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelrpinto authored Aug 28, 2021
1 parent b52ba14 commit 87ae91b
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ function isRelativePath(path) {
return !/^(\/|http(s?)|asset)/.test(path);
}

function calculateRelativeVolume(volume, pan) {
// calculates a lower volume relative to the pan value
const relativeVolume = (volume * (1 - Math.abs(pan)));
return Number(relativeVolume.toFixed(1));
}

function setAndroidVolumes(sound) {
// calculates the volumes for left and right channels
if (sound._pan) {
const relativeVolume = calculateRelativeVolume(sound._volume, sound._pan);
if (sound._pan < 0) {
// left is louder
RNSound.setVolume(sound._key, sound._volume, relativeVolume);
} else {
// right is louder
RNSound.setVolume(sound._key, relativeVolume, sound._volume);
}
} else {
// no panning, same volume on both channels
RNSound.setVolume(sound._key, sound._volume, sound._volume);
}
}

function Sound(filename, basePath, onError, options) {
var asset = resolveAssetSource(filename);
if (asset) {
Expand Down Expand Up @@ -155,15 +178,29 @@ Sound.prototype.getPitch = function() {
Sound.prototype.setVolume = function(value) {
this._volume = value;
if (this._loaded) {
if (IsAndroid || IsWindows) {
RNSound.setVolume(this._key, value, value);
if (IsAndroid) {
setAndroidVolumes(this)
} else {
RNSound.setVolume(this._key, value);
}
}
return this;
};

Sound.prototype.setPan = function(value) {
this._pan = value;
if (this._loaded) {
if (IsWindows) {
throw new Error('#setPan not supported on windows');
} else if (IsAndroid) {
setAndroidVolumes(this)
} else {
RNSound.setPan(this._key, value);
}
}
return this;
};

Sound.prototype.getSystemVolume = function(callback) {
if(!IsWindows) {
RNSound.getSystemVolume(callback);
Expand All @@ -182,13 +219,6 @@ Sound.prototype.getPan = function() {
return this._pan;
};

Sound.prototype.setPan = function(value) {
if (this._loaded) {
RNSound.setPan(this._key, this._pan = value);
}
return this;
};

Sound.prototype.getNumberOfLoops = function() {
return this._numberOfLoops;
};
Expand Down

0 comments on commit 87ae91b

Please sign in to comment.