Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
  <body>
    <canvas id="oscilloscope"></canvas>
    <canvas id="spectrum"></canvas>
    <div id="controls">
      <div id="controls-top">
        <label>
          <span>Frequency</span>
          <input type="range" id="f" min="10" max="400" value="100" />
        </label>
        <button id="t">Turn on</button>
      </div>
      <div id="xy"></div>
    </div>
  </body>
</html>

              
            
!

CSS

              
                #oscilloscope {
  position: absolute;
  left: 0;
  top: 0%;
  width: 100%;
  height: 10%;
}
#spectrum {
  position: absolute;
  left: 0;
  top: 10%;
  width: 100%; 
  height: 90%;
}

#controls {
  position: absolute;
  z-index: 1;
  color: white;
  font-family: sans-serif;
  font-size: 16px;
  left: calc(50% - 250px);
  width: 500px;
  bottom: 240px;
}
#controls-top {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding: 5px 0;
  color: white;
  mix-blend-mode: difference;
}
#xy {
  position: absolute;
  width: 500px;
  height: 200px;
}
button {
  font-size: 18px;
}
              
            
!

JS

              
                import { Oscilloscope } from "https://unpkg.com/@teropa/oscilloscope@1.0.0/dist/index.esm.js";
import { SpectrumAnalyser } from "https://unpkg.com/@teropa/spectrum-analyser@1.0.0/dist/index.esm.js";

const dsfProcessorModule = `
  const TWO_PI = Math.PI * 2;

  function cMulRe(re1, im1, re2, im2) {
    return re1 * re2 - im1 * im2;
  }

  function cMulIm(re1, im1, re2, im2) {
    return re1 * im2 + im1 * re2;
  }

  function cDivRe(re1, im1, re2, im2) {
    return (re1 * re2 + im1 * im2) / (re2 * re2 + im2 * im2);
  }

  function cDivIm(re1, im1, re2, im2) {
    return (im1 * re2 - re1 * im2) / (re2 * re2 + im2 * im2);
  }

  class DSFProcessor extends AudioWorkletProcessor {
      static get parameterDescriptors() {
          return [
            {
              name: "frequency",
              defaultValue: 440,
              minValue: 1,
              maxValue: 800
            },
            {
              name: "distance",
              defaultValue: 0.5,
              minValue: 0.001,
              maxValue: 1
            },
            {
              name: "index",
              defaultValue: 0.5,
              minValue: 0,
              maxValue: 0.999
            }
          ];
      }
  
      constructor() {
          super();
          this.freqPhase = 0;
          this.freqDistPhase = 0;
      }
  
      process(inputs, outputs, params) {
          const frequency = params.frequency;
          const distance = params.distance;
          const index = params.index;
          const quantumSize = outputs[0][0].length;
  
          for (let s=0 ; s<quantumSize ; s++) {
              // Get parameter inputs for sample
              const freq = frequency.length > 1 ? frequency[s] : frequency[0];
              const dist = distance.length > 1 ? distance[s] : distance[0];
              const idx  = index.length > 1 ? index[s] : index[0];
            
              // How many partials fit between the base frequency and the nyquist
              const n = Math.floor(((sampleRate / 2) - freq) / (freq * dist));
              
              // What is the maximum value of the signal with this amount of partials
              const max = (1 - Math.pow(idx, (n + 1))) / (1 - idx);

              // Calculate sample

              const aRe = Math.cos(this.freqPhase);
              const aIm = Math.sin(this.freqPhase);

              const bRe = 1 - Math.pow(idx, n + 1) * Math.cos((n + 1) * this.freqDistPhase);
              const bIm = -Math.pow(idx, n + 1) * Math.sin((n + 1) * this.freqDistPhase);

              const cRe = 1 - idx * Math.cos(this.freqDistPhase);
              const cIm = -idx * Math.sin(this.freqDistPhase);

              const bcRe = cDivRe(bRe, bIm, cRe, cIm);
              const bcIm = cDivIm(bRe, bIm, cRe, cIm);

              const abcRe = cMulRe(aRe, aIm, bcRe, bcIm);
              const abcIm = cMulIm(aRe, aIm, bcRe, bcIm);
              
              // Normalize 
              const left = abcRe / max;
              const right = abcIm / max;

              // Output sample to all channels of all outputs   
              for (let o=0 ; o<outputs.length ; o++) {
                  for (let ch=0 ; ch<outputs[o].length ; ch++) {
                      outputs[o][ch][s] = ch % 2 === 0 ? left : right;
                  }
              }

              // Increment phasors
              const freqPhaseIncrement = freq / sampleRate * TWO_PI;
              const freqDistPhaseIncrement = (freq * dist) / sampleRate * TWO_PI;
              this.freqPhase = (this.freqPhase + freqPhaseIncrement) % TWO_PI;
              this.freqDistPhase = (this.freqDistPhase + freqDistPhaseIncrement) % TWO_PI;
          }
          
          return true;
      }
  }
  
  registerProcessor('dsf-processor', DSFProcessor);
  `;

let on = false,
  fundamental = 100,
  distance = 0.5,
  index = 0.5,
  audioCtx = new AudioContext(),
  dsf,
  masterGain,
  oscilloscope = new Oscilloscope(
    document.getElementById("oscilloscope"),
    audioCtx,
    { edgeThreshold: 10 }
  ),
  spectrumAnalyser = new SpectrumAnalyser(
    document.getElementById("spectrum"),
    audioCtx
  );

async function init() {
  await audioCtx.audioWorklet.addModule(
    `data:text/javascript,${encodeURIComponent(dsfProcessorModule)}`
  );

  masterGain = audioCtx.createGain();
  masterGain.gain.value = 0;
  masterGain.connect(audioCtx.destination);

  dsf = new AudioWorkletNode(audioCtx, "dsf-processor", {
    outputChannelCount: [2],
  });
  dsf.parameters.get("frequency").value = fundamental;
  dsf.parameters.get("distance").value = distance;
  dsf.parameters.get("index").value = index;
  dsf.connect(masterGain);

  oscilloscope.connect(dsf);
  oscilloscope.start();

  spectrumAnalyser.connect(dsf);
  spectrumAnalyser.start();
}

document.getElementById("t").addEventListener("click", async () => {
  on = !on;
  if (on) {
    audioCtx.resume();
    masterGain.gain.setValueAtTime(0, audioCtx.currentTime);
    masterGain.gain.linearRampToValueAtTime(1, audioCtx.currentTime + 0.03);
    document.getElementById("t").textContent = "Turn off";
  } else {
    masterGain.gain.setValueAtTime(1, audioCtx.currentTime);
    masterGain.gain.linearRampToValueAtTime(0, audioCtx.currentTime + 0.03);
    document.getElementById("t").textContent = "Turn on";
  }
});
document.getElementById("f").addEventListener("input", (evt) => {
  fundamental = +evt.target.value;
  dsf.parameters.get("frequency").value = fundamental;
});

let xy = new Nexus.Position("#xy", {
  x: 0.5,
  minX: 0,
  maxX: 1,
  y: 0.5,
  minY: 0,
  maxY: 0.99,
});
xy.on("change", (v) => {
  distance = v.x;
  index = v.y;
  dsf.parameters.get("distance").value = distance;
  dsf.parameters.get("index").value = index;
});
xy.colorize("fill", "rgba(200, 200, 200, 0.2)");
xy.colorize("accent", "white");

init();

              
            
!
999px

Console