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

Replacement of es5 functions to es6 classes feat p5.panner #535

Merged
merged 2 commits into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 2 additions & 13 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ p5.prototype.freqToMidi = freqToMidi;

import './errorHandler';
import './audioWorklet';
import './panner';
import Panner from './panner';
p5.Panner = Panner;
import './soundfile';

import Amplitude from './amplitude';
Expand Down Expand Up @@ -42,22 +43,15 @@ p5.BandPass = BandPass;
import EQ from './eq';
p5.EQ = EQ;



import listener3D from './listener3d';
p5.listener3D = listener3D;



import Panner3D from './panner3d';
p5.Panner3D = Panner3D;



import Delay from './delay';
p5.Delay = Delay;


import './reverb';

import Metro from './metro';
Expand All @@ -71,11 +65,9 @@ p5.Compressor = Compressor;

import './soundRecorder';


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


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

Expand All @@ -88,11 +80,8 @@ p5.AudioVoice = AudioVoice;
import MonoSynth from './monosynth';
p5.MonoSynth = MonoSynth;



import OnsetDetect from './onsetDetect';
p5.OnsetDetect = OnsetDetect;

import PolySynth from './polysynth';
p5.PolySynth = PolySynth;

159 changes: 84 additions & 75 deletions src/panner.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,111 @@
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);
};
class Panner {
constructor(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;
pan(val, tFromNow) {
var time = tFromNow || 0;
var t = ac.currentTime + time;

this.stereoPanner.pan.linearRampToValueAtTime(val, t);
};
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 () {};
//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() {}

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

p5.Panner.prototype.disconnect = function () {
if (this.stereoPanner) {
this.stereoPanner.disconnect();
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);
class Panner {
constructor(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';
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);
// 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.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);
};
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);
};
// -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);
}

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.splitter = ac.createChannelSplitter(2);
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 panner;