Skip to content

Commit

Permalink
Replacement of es5 functions to es6 classes feat p5.panner (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
endurance21 authored Aug 30, 2020
1 parent 7b24c41 commit b6b1562
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 87 deletions.
10 changes: 8 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ p5.prototype.saveSound = saveSound;

import './errorHandler';
import './audioWorklet';
import './panner';

import Panner from './panner';
p5.Panner = Panner;

import SoundFile, { loadSound } from './soundfile';
p5.SoundFile = SoundFile;
p5.prototype.loadSound = loadSound;
// register preload handling of loadSound
p5.prototype.registerPreloadMethod('loadSound', p5.prototype);


import Amplitude from './amplitude';
p5.Amplitude = Amplitude;

Expand Down Expand Up @@ -72,13 +75,15 @@ import Delay from './delay';
p5.Delay = Delay;



import { Reverb, Convolver, createConvolver } from './reverb';
p5.Reverb = Reverb;
p5.Convolver = Convolver;
p5.prototype.createConvolver = createConvolver;
p5.prototype.registerPreloadMethod('createConvolver', p5.prototype);



import Metro from './metro';
p5.Metro = Metro;

Expand All @@ -97,14 +102,15 @@ import Compressor from './compressor';
p5.Compressor = Compressor;



import peakDetect from './peakDetect';
p5.peakDetect = peakDetect;



import SoundRecorder from './soundRecorder';
p5.SoundRecorder = SoundRecorder;


import Distortion from './distortion';
p5.Distortion = Distortion;

Expand Down
179 changes: 94 additions & 85 deletions src/panner.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,113 @@
import p5sound from './master';
var ac = p5sound.audiocontext;

var panner;
// Stereo panner
// if there is a stereo panner node use it
if (typeof ac.createStereoPanner !== 'undefined') {
p5.Panner = function (input, output) {
this.stereoPanner = this.input = ac.createStereoPanner();
input.connect(this.stereoPanner);
this.stereoPanner.connect(output);
};

p5.Panner.prototype.pan = function (val, tFromNow) {
var time = tFromNow || 0;
var t = ac.currentTime + time;

this.stereoPanner.pan.linearRampToValueAtTime(val, t);
};

//not implemented because stereopanner
//node does not require this and will automatically
//convert single channel or multichannel to stereo.
//tested with single and stereo, not with (>2) multichannel
p5.Panner.prototype.inputChannels = function () {};

p5.Panner.prototype.connect = function (obj) {
this.stereoPanner.connect(obj);
};

p5.Panner.prototype.disconnect = function () {
if (this.stereoPanner) {
this.stereoPanner.disconnect();
class Panner {
constructor(input, output) {
this.stereoPanner = this.input = ac.createStereoPanner();
input.connect(this.stereoPanner);
this.stereoPanner.connect(output);
}

pan(val, tFromNow) {
var time = tFromNow || 0;
var t = ac.currentTime + time;

this.stereoPanner.pan.linearRampToValueAtTime(val, t);
}

//not implemented because stereopanner
//node does not require this and will automatically
//convert single channel or multichannel to stereo.
//tested with single and stereo, not with (>2) multichannel
inputChannels() {}

connect(obj) {
this.stereoPanner.connect(obj);
}

disconnect() {
if (this.stereoPanner) {
this.stereoPanner.disconnect();
}
}
};
}

panner = Panner;
} else {
// if there is no createStereoPanner object
// such as in safari 7.1.7 at the time of writing this
// use this method to create the effect
p5.Panner = function (input, output, numInputChannels) {
this.input = ac.createGain();
input.connect(this.input);

this.left = ac.createGain();
this.right = ac.createGain();
this.left.channelInterpretation = 'discrete';
this.right.channelInterpretation = 'discrete';

// if input is stereo
if (numInputChannels > 1) {
this.splitter = ac.createChannelSplitter(2);
this.input.connect(this.splitter);

this.splitter.connect(this.left, 1);
this.splitter.connect(this.right, 0);
} else {
this.input.connect(this.left);
this.input.connect(this.right);
}
class Panner {
constructor(input, output, numInputChannels) {
this.input = ac.createGain();
input.connect(this.input);

this.output = ac.createChannelMerger(2);
this.left.connect(this.output, 0, 1);
this.right.connect(this.output, 0, 0);
this.output.connect(output);
};

// -1 is left, +1 is right
p5.Panner.prototype.pan = function (val, tFromNow) {
var time = tFromNow || 0;
var t = ac.currentTime + time;
var v = (val + 1) / 2;
var rightVal = Math.cos((v * Math.PI) / 2);
var leftVal = Math.sin((v * Math.PI) / 2);
this.left.gain.linearRampToValueAtTime(leftVal, t);
this.right.gain.linearRampToValueAtTime(rightVal, t);
};

p5.Panner.prototype.inputChannels = function (numChannels) {
if (numChannels === 1) {
this.input.disconnect();
this.input.connect(this.left);
this.input.connect(this.right);
} else if (numChannels === 2) {
if (typeof this.splitter === 'undefined') {
this.left = ac.createGain();
this.right = ac.createGain();
this.left.channelInterpretation = 'discrete';
this.right.channelInterpretation = 'discrete';

// if input is stereo
if (numInputChannels > 1) {
this.splitter = ac.createChannelSplitter(2);
this.input.connect(this.splitter);

this.splitter.connect(this.left, 1);
this.splitter.connect(this.right, 0);
} else {
this.input.connect(this.left);
this.input.connect(this.right);
}

this.output = ac.createChannelMerger(2);
this.left.connect(this.output, 0, 1);
this.right.connect(this.output, 0, 0);
this.output.connect(output);
}

// -1 is left, +1 is right
pan(val, tFromNow) {
var time = tFromNow || 0;
var t = ac.currentTime + time;
var v = (val + 1) / 2;
var rightVal = Math.cos((v * Math.PI) / 2);
var leftVal = Math.sin((v * Math.PI) / 2);
this.left.gain.linearRampToValueAtTime(leftVal, t);
this.right.gain.linearRampToValueAtTime(rightVal, t);
}

inputChannels(numChannels) {
if (numChannels === 1) {
this.input.disconnect();
this.input.connect(this.left);
this.input.connect(this.right);
} else if (numChannels === 2) {
if (typeof this.splitter === 'undefined') {
this.splitter = ac.createChannelSplitter(2);
}
this.input.disconnect();
this.input.connect(this.splitter);
this.splitter.connect(this.left, 1);
this.splitter.connect(this.right, 0);
}
this.input.disconnect();
this.input.connect(this.splitter);
this.splitter.connect(this.left, 1);
this.splitter.connect(this.right, 0);
}
};

p5.Panner.prototype.connect = function (obj) {
this.output.connect(obj);
};
connect(obj) {
this.output.connect(obj);
}

p5.Panner.prototype.disconnect = function () {
if (this.output) {
this.output.disconnect();
disconnect() {
if (this.output) {
this.output.disconnect();
}
}
};
}
panner = Panner;
}

export default p5.Panner;

export default panner;

0 comments on commit b6b1562

Please sign in to comment.