Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/analysis/Amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class Amplitude extends p5soundNode {
constructor(smoothing = 0) {
super();
this.node = new ToneMeter({normalRange:true, smoothing:smoothing});
let toneInput = this.node.input;
while (toneInput && toneInput.input) toneInput = toneInput.input;
this.input.connect(toneInput);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/analysis/FFT.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class FFT extends p5soundNode {
this.node = new ToneGain(1);
this.node.connect(this.analyzer);
this.node.connect(this.samples);
let toneInput = this.node.input;
while (toneInput && toneInput.input) toneInput = toneInput.input;
this.input.connect(toneInput);
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/core/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import { getContext as ToneGetContext, setContext as ToneSetContext } from "tone/build/esm/core/Global.js";
import { start as ToneStart } from "tone/build/esm/core/Global.js";

let audioContext = null;

function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
Expand All @@ -18,13 +20,14 @@ function clamp(value, min, max) {
*/
function getAudioContext() {
// Check if the AudioContext is already created
if (ToneGetContext()) {
return ToneGetContext().rawContext;
// if (ToneGetContext()) {
// return ToneGetContext();
// }
if (!audioContext) {
audioContext = new window.AudioContext();
ToneSetContext(audioContext);
}
const audiocontext = new window.AudioContext();
ToneSetContext(audiocontext);
let context = ToneGetContext();
return context._context;
return audioContext;
}

/**
Expand All @@ -33,6 +36,7 @@ function getAudioContext() {
* @param {AudioContext} the desired AudioContext.
*/
function setAudioContext(context) {
audioContext = context;
ToneSetContext(context);
}

Expand All @@ -49,7 +53,7 @@ function userStartAudio() {
* @function userStopAudio
*/
function userStopAudio() {
context = ToneGetContext();
const context = audioContext || ToneGetContext();
context.suspend();
}

Expand Down
1 change: 0 additions & 1 deletion src/core/p5soundMixEffect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { clamp } from './Utils.js';
class p5soundMixEffect extends p5soundNode {
constructor() {
super();
this.node = null;
}
/**
* Set the wet/dry mix of the effect.
Expand Down
36 changes: 31 additions & 5 deletions src/core/p5soundNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* @for p5.sound
*/

import { Context as ToneContext } from "tone/build/esm/core/context/Context.js";
import { getAudioContext } from "./Utils";
import { gainToDb as ToneGainToDb } from "tone/build/esm/core/type/Conversions.js";

/**
* Generic methods for p5.sound.js nodes
Expand All @@ -14,6 +15,10 @@ import { Context as ToneContext } from "tone/build/esm/core/context/Context.js";
class p5soundNode {
constructor() {
this.node = null;
this.ctx = getAudioContext();
this.input = this.ctx.createGain();
this.output = this.ctx.createGain();
this.output.connect(this.ctx.destination);
}

/**
Expand Down Expand Up @@ -68,18 +73,39 @@ class p5soundNode {

connect(destination) {
if(typeof destination.getNode === 'function') {
this.node.connect(destination.getNode());
this.output.connect(destination.getNode());
} else {
this.node.connect(destination);
this.output.connect(destination);
}
}

setInput(source) {
if (typeof source.getNode === 'function') {
source.connect(this.input)
console.log('firstcase')
return;
}

if (typeof source.connect === 'function' && typeof source.output !== 'undefined') {
source.connect(this.input); // route Tone's output into our input
console.log('secondcase')
return;
}

// Case 3: Raw Web Audio AudioNode — connect directly
if (source instanceof AudioNode) {
source.connect(this.input);
console.log('thirdcase')
return;
}
}

disconnect() {
this.node.disconnect(ToneContext.destination);
this.output.disconnect();
}

getNode() {
return this.node;
return this.input;
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/effects/Biquad.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ class Biquad extends p5soundNode {
super();
this.type = t;
this.cutoff = c;
this.node = new ToneBiquadFilter(this.cutoff, this.type).toDestination();
this.node = new ToneBiquadFilter(this.cutoff, this.type)
const toneInput = this.node.input.input ?? this.node.input;
const toneOutput = this.node.output.output ?? this.node.output;
this.input.connect(toneInput);
toneOutput.connect(this.output);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/effects/Delay.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ class Delay extends p5soundMixEffect {
super();
this.d = d;
this.f = f;
this.node = new ToneFeedbackDelay(this.d, this.f).toDestination();
this.node = new ToneFeedbackDelay(this.d, this.f)
const toneInput = this.node.input.input ?? this.node.input;
const toneOutput = this.node.output.output ?? this.node.output;
this.input.connect(toneInput);
toneOutput.connect(this.output);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/effects/Envelope.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class Envelope extends p5soundNode {
decay: this.decay,
sustain: this.sustain,
release: this.release,
}).toDestination();
})
const toneInput = this.node.input.input ?? this.node.input;
const toneOutput = this.node.output.output ?? this.node.output;
this.input.connect(toneInput);
toneOutput.connect(this.output);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/effects/Gain.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ import { p5soundNode } from "../core/p5soundNode.js";
class Gain extends p5soundNode {
constructor(value = 1) {
super();
this.node = new ToneGain(value).toDestination();
this.node = new ToneGain(value)
const toneInput = this.node.input.input ?? this.node.input;
const toneOutput = this.node.output.output ?? this.node.output;
this.input.connect(toneInput);
toneOutput.connect(this.output);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/effects/Panner.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ import { p5soundNode } from "../core/p5soundNode.js";
class Panner extends p5soundNode {
constructor() {
super();
this.node = new TonePanner(0).toDestination();
this.node = new TonePanner(0)
const toneInput = this.node.input.input ?? this.node.input;
const toneOutput = this.node.output.output ?? this.node.output;
this.input.connect(toneInput);
toneOutput.connect(this.output);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/effects/Panner3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ class Panner3D extends p5soundNode {
positionX:0,
positionY:0,
positionZ:0,
}).toDestination();
})
const toneInput = this.node.input.input ?? this.node.input;
const toneOutput = this.node.output.output ?? this.node.output;
this.input.connect(toneInput);
toneOutput.connect(this.output);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/effects/PitchShifter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ import { p5soundNode } from "../core/p5soundNode.js";
class PitchShifter extends p5soundNode {
constructor(shiftValue = 1) {
super();
this.node = new TonePitchShift(shiftValue).toDestination();
this.node = new TonePitchShift(shiftValue)
const toneInput = this.node.input.input ?? this.node.input;
const toneOutput = this.node.output.output ?? this.node.output;
this.input.connect(toneInput);
toneOutput.connect(this.output);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/effects/Reverb.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ import { p5soundMixEffect } from "../core/p5soundMixEffect.js";
class Reverb extends p5soundMixEffect {
constructor(decayTime) {
super();
this.decayTime = decayTime || 1;
this.node = new ToneReverb(this.decayTime).toDestination();
this.decayTime = decayTime || 10;
this.node = new ToneReverb(this.decayTime);
const toneInput = this.node.input.input ?? this.node.input;
const toneOutput = this.node.output.output ?? this.node.output;
this.input.connect(toneInput);
toneOutput.connect(this.output);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/sources/AudioIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { UserMedia as ToneUserMedia} from "tone/build/esm/source/UserMedia.js";
import { start as ToneStart } from "tone/build/esm/core/Global.js";
import { p5soundSource } from "../core/p5soundSource.js";
/**
* Get sound from an input source, typically a computer microphone.
Expand Down Expand Up @@ -52,15 +51,15 @@ import { p5soundSource } from "../core/p5soundSource.js";
class AudioIn extends p5soundSource {
constructor() {
super();
this.node = new ToneUserMedia();
this.node = new ToneUserMedia().connect(this.output);
}
/**
* Start the audio input.
* @method start
* @for AudioIn
*/
start() {
ToneStart();
userStartAudio();
this.node.open().then(() => {
// promise resolves when input is available
console.log("mic open");
Expand Down
2 changes: 1 addition & 1 deletion src/sources/Noise.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Noise extends p5soundSource {
if (typeof type === "undefined") {
type = "white";
}
this.node = new ToneNoise().toDestination();
this.node = new ToneNoise().connect(this.output)
this.node.type = type;
}
/**
Expand Down
2 changes: 1 addition & 1 deletion src/sources/Oscillator.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Oscillator extends p5soundSource {

this.frequency = frequency;
this.type = type;
this.node = new ToneOscillator().toDestination();
this.node = new ToneOscillator().connect(this.output);
this.node.frequency.value = this.frequency;
this.node.type = this.type;
this.node.volume.value = -6;
Expand Down
2 changes: 1 addition & 1 deletion src/sources/SoundFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function loadSound (path) {
class SoundFile extends p5soundSource {
constructor(buffer, successCallback) {
super();
this.node = new TonePlayer(buffer, successCallback).toDestination();
this.node = new TonePlayer(buffer, successCallback).connect(this.output);
this.playing = false;
this.speed = 1;
this.paused = false;
Expand Down