css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div id="synth" tabindex="1">
  Click to start/stop the theremin.

<div id="controls">
        <select id="oscillator-type">
          <option value="sine">sine</option>
          <option value="square">square</option>
          <option value="sawtooth">sawtooth</option>
          <option value="triangle">triangle</option>
        LFO freq:
        <input id="lfo-freq" type="range" min="0" max="10" step="0.25" value="0"/>
        <input id="gain" type="range" />
        <select id="filter">
          <option value="none">none</option>
          <option value="lowpass">lowpass</option>
          <option value="highpass">highpass</option>
          <option value="bandpass">bandpass</option>
          <option value="lowshelf">lowshelf</option>
          <option value="highshelf">highshelf</option>
          <option value="peaking">peaking</option>
          <option value="notch">notch</option>
          <option value="allpass">allpass</option>
        <input id="distortion" type="range" min="0" max="100" step="1" value="0"/>
              @mixin uisection ($background) {
  position: absolute;
  width: 300px;
  height: 300px;
  background-color: $background; 
  color: white;

#synth {
  @include uisection(#222222);

#controls {
  @include uisection(#444444);
  left: 320px;

select, input {
  margin-left: 0.5em;

li {
  list-style-type: none;
  margin-bottom: 1em;
              // Create an audio context
var audio_context = window.AudioContext || window.webkitAudioContext;
var context = new audio_context();

// Create Audio Nodes
var oscillator = context.createOscillator();
var lfo = context.createOscillator();
var lfo_amp = context.createGain();
var filter = context.createBiquadFilter();
var distortion = context.createWaveShaper();
distortion.curve = makeDistortionCurve(0);
distortion.oversample = '4x';
var volume = context.createGain();

// Create Audio Routing Graph

// Get references to UI elements
var synth = document.getElementById("synth");
var oscillatorTypeSelector = document.getElementById("oscillator-type");
var LFOFrequencyControl = document.getElementById("lfo-freq");
var gainControl = document.getElementById("gain");
var filterControl = document.getElementById("filter");
var distortionControl = document.getElementById("distortion");

// Initialise State
var isOn = false;
gainControl.value = 0;

// Set Theremin Event Listeners
synth.addEventListener("click", function(){
  if (isOn) {
    isOn = false;
  else {
    isOn = true;

synth.addEventListener("mousemove", changeVolume);
synth.addEventListener("mousemove", changeOscillatorPitch);

// Set Controls Event Listeners
oscillatorTypeSelector.addEventListener("change", changeWaveform);
LFOFrequencyControl.addEventListener("change", changeLFOFrequency);
gainControl.addEventListener("change", changeLFOGain);
filterControl.addEventListener("change", changeFilter);
distortionControl.addEventListener("change", changeDistortion);

// Theremin Event Handlers
function changeOscillatorPitch(event) {
  oscillator.frequency.value = 350 + 2 * event.clientX;

function changeVolume(event) {
  volume.gain.value = event.offsetY / synth.clientHeight;

// Controls Event Handlers
function changeWaveform() {
  oscillator.type = oscillatorTypeSelector.value;

function changeLFOFrequency() {
   lfo.frequency.value = LFOFrequencyControl.value;

function changeLFOGain() {
  lfo_amp.gain.value = gainControl.value;

function changeFilter() {
  if (filterControl.value === "none") {
  else {
    filter.type = filterControl.value;

function changeDistortion() {
  distortion.curve = makeDistortionCurve(parseInt(distortionControl.value));

function makeDistortionCurve(amount) {
  var k = typeof amount === 'number' ? amount : 0,
    n_samples = 44100,
    curve = new Float32Array(n_samples),
    deg = Math.PI / 180,
    i = 0,
  for ( ; i < n_samples; ++i ) {
    x = i * 2 / n_samples - 1;
    curve[i] = ( 3 + k ) * x * 20 * deg / ( Math.PI + k * Math.abs(x) );
  return curve;

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................