Skip to content

Commit

Permalink
Adding license, octave doubler -> pitch shifter
Browse files Browse the repository at this point in the history
  • Loading branch information
cwilso committed Jan 24, 2014
1 parent 65737b6 commit 9416e63
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 19 deletions.
Binary file modified .DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Chris Wilson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<option>Ring mod </option>
<option>Stereo Chorus </option>
<option>Stereo Flange </option>
<option>Octave Doubler </option>
<option>Pitch Shifter </option>
<option>Mod Delay </option>
<option>Ping-pong delay</option>
<option>LFO Filter</option>
Expand Down Expand Up @@ -127,8 +127,8 @@
Flanger depth: <input id="sfldepth" type="range" min="0.0005" max="0.02" step="0.00025" value="0.005" style="height: 20px; width: 200px;" onInput="if (sflldepth) sflldepth.gain.value = event.target.value; if (sflrdepth) sflrdepth.gain.value = -1.0 * event.target.value;"><br>
Flanger feedback: <input id="sflfb" type="range" min="0" max="1" step="0.01" value="0.9" style="height: 20px; width: 200px;" onInput="if (sfllfb) sfllfb.gain.value = event.target.value; if (sflrfb) sflrfb.gain.value = event.target.value;">
</div>
<div id="doublerControls">An octave-doubling effect, using sample-accurate delay ramping.<br>
<!-- TODO: doesn't need to be just octave, should be able to control. -->
<div id="doublerControls">A pitch-shifting effect, using sample-accurate delay ramping. Defaults to one octave down.<br>
Pitch-shift: <input id="octpitch" type="range" min="-1" max="1" step="0.05" value="-1" style="height: 20px; width: 200px;" onInput="if (effect.setPitchOffset) effect.setPitchOffset(event.target.value);"><br>
</div>
<div id="modDelayControls">A delay with a basic chorus effect.<br>
Delay time: <input id="mdtime" type="range" min="0.01" max="3" step="0.01" value="0.15" style="height: 20px; width: 200px" onInput="if (mdtime) mdtime.delayTime.value = event.target.value;"><br>
Expand Down
14 changes: 9 additions & 5 deletions js/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ function changeEffect(effect) {
case 9: // Stereo Flange
currentEffectNode = createStereoFlange();
break;
case 10: // Octave doubling
currentEffectNode = createDoubler();
case 10: // Pitch shifting
currentEffectNode = createPitchShifter();
break;
case 11: // Mod Delay
currentEffectNode = createModDelay();
Expand Down Expand Up @@ -295,7 +295,7 @@ function changeEffect(effect) {
var tempWetGain = wetGain;
wetGain = pingPong.input;
wetGain = createAutowah();
currentEffectNode = createDoubler();
currentEffectNode = createPitchShifter();
wetGain = tempWetGain;
break;
case 18: // Distorted Wah Chorus
Expand Down Expand Up @@ -341,7 +341,11 @@ function createTelephonizer() {
}

function createDelay() {
var delayNode = audioContext.createDelay();
var delayNode = null;
if (window.location.search.substring(1) == "webkit")
delayNode = audioContext.createDelayNode();
else
delayNode = audioContext.createDelay();
delayNode.delayTime.value = parseFloat( document.getElementById("dtime").value );
dtime = delayNode;

Expand Down Expand Up @@ -680,7 +684,7 @@ function createStereoFlange() {
return inputNode;
}

function createDoubler() {
function createPitchShifter() {
effect = new Jungle( audioContext );
effect.output.connect( wetGain );
return effect.input;
Expand Down
70 changes: 59 additions & 11 deletions js/jungle.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function createFadeBuffer(context, activeTime, fadeTime) {
return buffer;
}

function createDelayTimeBuffer(context, activeTime, fadeTime) {
function createDelayTimeBuffer(context, activeTime, fadeTime, shiftUp) {
var length1 = activeTime * context.sampleRate;
var length2 = (activeTime - 2*fadeTime) * context.sampleRate;
var length = length1 + length2;
Expand All @@ -76,10 +76,12 @@ function createDelayTimeBuffer(context, activeTime, fadeTime) {

// 1st part of cycle
for (var i = 0; i < length1; ++i) {
// This line does octave-down transpose
p[i] = i / length1;
// This line does octave-up transpose
// p[i] = (length1-i)/length;
if (shiftUp)
// This line does shift-up transpose
p[i] = (length1-i)/length;
else
// This line does shift-down transpose
p[i] = i / length1;
}

// 2nd part
Expand All @@ -105,20 +107,42 @@ function Jungle(context) {
// Delay modulation.
var mod1 = context.createBufferSource();
var mod2 = context.createBufferSource();
var delayTimeBuffer = createDelayTimeBuffer(context, bufferTime, fadeTime);
mod1.buffer = delayTimeBuffer;
mod2.buffer = delayTimeBuffer;
var mod3 = context.createBufferSource();
var mod4 = context.createBufferSource();
this.shiftDownBuffer = createDelayTimeBuffer(context, bufferTime, fadeTime, false);
this.shiftUpBuffer = createDelayTimeBuffer(context, bufferTime, fadeTime, true);
mod1.buffer = this.shiftDownBuffer;
mod2.buffer = this.shiftDownBuffer;
mod3.buffer = this.shiftUpBuffer;
mod4.buffer = this.shiftUpBuffer;
mod1.loop = true;
mod2.loop = true;

mod3.loop = true;
mod4.loop = true;

// for switching between oct-up and oct-down
var mod1Gain = context.createGainNode();
var mod2Gain = context.createGainNode();
var mod3Gain = context.createGainNode();
mod3Gain.gain.value = 0;
var mod4Gain = context.createGainNode();
mod4Gain.gain.value = 0;

mod1.connect(mod1Gain);
mod2.connect(mod2Gain);
mod3.connect(mod3Gain);
mod4.connect(mod4Gain);

// Delay amount for changing pitch.
var modGain1 = context.createGainNode();
var modGain2 = context.createGainNode();

var delay1 = context.createDelayNode();
var delay2 = context.createDelayNode();
mod1.connect(modGain1);
mod2.connect(modGain2);
mod1Gain.connect(modGain1);
mod2Gain.connect(modGain2);
mod3Gain.connect(modGain1);
mod4Gain.connect(modGain2);
modGain1.connect(delay1.delayTime);
modGain2.connect(delay2.delayTime);

Expand Down Expand Up @@ -152,11 +176,17 @@ function Jungle(context) {
var t2 = t + bufferTime - fadeTime;
mod1.start(t);
mod2.start(t2);
mod3.start(t);
mod4.start(t2);
fade1.start(t);
fade2.start(t2);

this.mod1 = mod1;
this.mod2 = mod2;
this.mod1Gain = mod1Gain;
this.mod2Gain = mod2Gain;
this.mod3Gain = mod3Gain;
this.mod4Gain = mod4Gain;
this.modGain1 = modGain1;
this.modGain2 = modGain2;
this.fade1 = fade1;
Expand All @@ -173,3 +203,21 @@ Jungle.prototype.setDelay = function(delayTime) {
this.modGain1.gain.setTargetValueAtTime(0.5*delayTime, 0, 0.010);
this.modGain2.gain.setTargetValueAtTime(0.5*delayTime, 0, 0.010);
}

var previousPitch = -1;

Jungle.prototype.setPitchOffset = function(mult) {
if (mult>0) { // pitch up
this.mod1Gain.gain.value = 0;
this.mod2Gain.gain.value = 0;
this.mod3Gain.gain.value = 1;
this.mod4Gain.gain.value = 1;
} else { // pitch down
this.mod1Gain.gain.value = 1;
this.mod2Gain.gain.value = 1;
this.mod3Gain.gain.value = 0;
this.mod4Gain.gain.value = 0;
}
this.setDelay(delayTime*Math.abs(mult));
previousPitch = mult;
}

1 comment on commit 9416e63

@s213413
Copy link

@s213413 s213413 commented on 9416e63 Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how i change the pitch shift by 12th roots of 2? 2^(1/12)=1.05946309436 https://en.wikipedia.org/wiki/Twelfth_root_of_two

Please sign in to comment.