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. 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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <svg id="symbols" style="display: none">
  <symbol id="square" viewBox="0 0 128 128">
    <path style="fill:none;stroke-width:8px;stroke-linecap:square;" d="M35,43 l0,40 l30,0 l0,-40 l30,0 l0,40" />
  </symbol>
  <symbol id="sawtooth" viewBox="0 0 128 128">
    <path style="fill:none;stroke-width:8px;stroke-linecap:square;stroke-linejoin:bevel;" d="M30,80 l30,-40 l0,40 l30,-40 l0,40"/>
  </symbol>
  <symbol id="triangle" viewBox="0 0 128 128">
    <path style="fill:none;stroke-width:8px;stroke-linecap:square;stroke-linejoin:bevel;" d="M30,80 l17,-40 l17,40 l17,-40 l17,40"/>
  </symbol>
  <symbol id="sine" viewBox="0 0 128 128">
    <path style="fill:none;stroke-width:8px;stroke-linecap:square;stroke-linejoin:bevel;" d="M30,64 q17,-40 32,0 q17,40 32,0"/>
  </symbol>
  <symbol id="play" viewBox="0 0 128 128">
    <path style="stroke-width:4px;stroke-linecap:square;stroke-linejoin:bevel" d="M33,30 l0,68 l68,-34z"/>
  </symbol>
  <symbol id="pause" viewBox="0 0 128 128">
    <rect style="stroke-width:2px;stroke-linecap:square;stroke-linejoin:bevel" x="30" y="30" width="30" height="68"/>
    <rect style="stroke-width:2px;stroke-linecap:square;stroke-linejoin:bevel;" x="68" y="30" width="30" height="68"/>
  </symbol>
  <symbol id="codepen" viewBox="-25 -25 150 150">
    <path d="M100 34.2c-.4-2.6-3.3-4-5.3-5.3-3.6-2.4-7.1-4.7-10.7-7.1-8.5-5.7-17.1-11.4-25.6-17.1-2-1.3-4-2.7-6-4-1.4-1-3.3-1-4.8 0-5.7 3.8-11.5 7.7-17.2 11.5L5.2 29C3 30.4.1 31.8 0 34.8c-.1 3.3 0 6.7 0 10v16c0 2.9-.6 6.3 2.1 8.1 6.4 4.4 12.9 8.6 19.4 12.9 8 5.3 16 10.7 24 16 2.2 1.5 4.4 3.1 7.1 1.3 2.3-1.5 4.5-3 6.8-4.5 8.9-5.9 17.8-11.9 26.7-17.8l9.9-6.6c.6-.4 1.3-.8 1.9-1.3 1.4-1 2-2.4 2-4.1V37.3c.1-1.1.2-2.1.1-3.1 0-.1 0 .2 0 0zM54.3 12.3L88 34.8 73 44.9 54.3 32.4V12.3zm-8.6 0v20L27.1 44.8 12 34.8l33.7-22.5zM8.6 42.8L19.3 50 8.6 57.2V42.8zm37.1 44.9L12 65.2l15-10.1 18.6 12.5v20.1zM50 60.2L34.8 50 50 39.8 65.2 50 50 60.2zm4.3 27.5v-20l18.6-12.5 15 10.1-33.6 22.4zm37.1-30.5L80.7 50l10.8-7.2-.1 14.4z"></path>
  </symbol>
</svg>  



<div id="app" class="app">
  <header class="header">
    <div class="header__logo"></div>
    <h1 class="header__title">Oscillate the oscillator ☺</h1>  
  </header>
  <nav class="menu">
    <button class="menu__btn" @click="play" title="play">
      <svg><use xlink:href="#play" /></svg>
    </button>
    <button class="menu__btn" @click="stop" title="pause">
      <svg><use xlink:href="#pause" /></svg>
    </button>
    <button class="menu__btn" @click="addOscillator('sine')" title="add sine oscillator">
      <svg><use xlink:href="#sine" /></svg>
    </button>
    <button class="menu__btn" @click="addOscillator('square')" title="add square oscillator">
      <svg><use xlink:href="#square" /></svg>
    </button>
    <button class="menu__btn" @click="addOscillator('triangle')" title="add triangle oscillator">
      <svg><use xlink:href="#triangle" /></svg>
    </button>
    <button class="menu__btn" @click="addOscillator('sawtooth')" title="add sawtooth oscillator">
      <svg><use xlink:href="#sawtooth" /></svg>
    </button>
    <button class="menu__btn" @click="createPen()" title="export to new pen">
      <svg><use xlink:href="#codepen" /></svg>
    </button>
  </nav>
  <fieldset class="form">
    <legend class="form__title">Graph</legend>
    <canvas class="form__canvas"></canvas>
  </fieldset>
  <fieldset class="form">
    <legend class="form__title">Carrier</legend>
    <div class="form__controls">
      <div class="form__group">
        <label class="form__label">type</label>
        <select class="form__field" v-model="carrier.type">
          <option value="sine">sine</option>
          <option value="square">square</option>
          <option value="triangle">triangle</option>
          <option value="sawtooth">sawtooth</option>
        </select>
      </div>
      <div class="form__group">
        <label for="carrfreq" class="form__label">frequency: {{carrier.freq}} Hz</label>
        <input id="carrfreq" class="form__field" type="range" v-model="carrier.freq" min="1" max="1000">  
      </div>
      <div class="form__group">
        <label for="carrfreq" class="form__label">gain: {{carrier.gain.toFixed(2)}}</label>
        <input id="carrgain" class="form__field" type="range" v-model="carrier.gain" min="0.0" max="1.0" step="0.01">  
      </div>
    </div>
  </fieldset>
  <fieldset class="form" v-for="(mod, idx) in mods">
    <legend class="form__title">Modulator {{idx+1}}</legend>
    <button class="form__close" @click="removeOscillator(idx)" aria-label="remove"> x </button>
    <div class="form__controls">
      <div class="form__group">
        <label class="form__label">type</label>
        <select class="form__field" v-model="mod.type">
          <option value="sine">sine</option>
          <option value="square">square</option>
          <option value="triangle">triangle</option>
          <option value="sawtooth">sawtooth</option>
        </select>
      </div>
      <div class="form__group">
        <label :for="'modfreq'+(idx+1)" class="form__label">frequency: {{mod.freq}} Hz</label>
        <input id="'modfreq'+(idx+1)" class="form__field" v-model="mod.freq" type="range" min="1" max="1000">
      </div>
      <div class="form__group">
        <label :for="'modgain'+(idx+1)" class="form__label">gain: {{mod.gain}}</label>
        <input :id="'modgain'+(idx+1)" class="form__field" v-model="mod.gain" type="range" min="1" max="1000">
      </div>
    </div>
  </fieldset>
</div>
<template class="pen__html" type="text/plain">
  <button id="play"> play </button>
  <button id="stop"> stop </button>
</template>
<template class="pen__js" type="text/plain">
const playButton = document.getElementById('play')
const stopButton = document.getElementById('stop')

const AC = new AudioContext()
const masterVolume = AC.createGain()  
masterVolume.gain.value = 0.0
masterVolume.connect(AC.destination)

const createAudioNode = ({type = 'sine', freq = 440, gain = 1.0} = {}) => {
  const osc = AC.createOscillator()
  osc.frequency.value = freq
  osc.type = type
  const _gain = AC.createGain()
  _gain.gain.value = gain
  osc.connect(_gain)
  return { osc, gain: _gain }
}
const carrier = createAudioNode(%%CARRIER%%)
const modulators = [%%MODULATORS%%].map(m => createAudioNode(m))
const startSound = () => {
  if (modulators.length > 0) {
    modulators[0].gain.connect(carrier.osc.frequency)
    modulators.reduce((a, b) => {
      b.gain.connect(a.gain.gain)
      return b
    })
    modulators.map(m => m.osc.start())  
  }
  carrier.osc.start()
  carrier.gain.connect(masterVolume)
}
startSound()

const discardSound = () => {
  modulators.map(m => {
    m.gain.disconnect()
    m.osc.stop()
  })
  carrier.osc.disconnect()
  carrier.osc.stop()
}

playButton.addEventListener('click', () => {
  if (AC.state === "suspended") {
    AC.resume()
  }
  if (masterVolume.gain.value === 0.0) {
    masterVolume.gain.setValueAtTime(0.01, AC.currentTime)
  }
  masterVolume.gain.exponentialRampToValueAtTime(1.0, AC.currentTime + 1)
})

stopButton.addEventListener('click', () => {
  masterVolume.gain.exponentialRampToValueAtTime(0.001, AC.currentTime + 1)
})
</template>



              
            
!

CSS

              
                /* =========================================
   Colors
   =========================================
 */

:root {
  --primary-color: #345;
}

/* =============================================
 * Layout
 * ============================================= */

body {
  font-family: sans-serif;
  color: var(--primary-color);
  margin: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  background: center;
  background: linear-gradient(45deg, #f7f, #ff7);
  justify-content: center;
}

.app {
  width: 75%;
  min-width: 200px;
  max-width: 500px;
  overflow: auto;
  background: #fff;
  box-shadow: .5em .5em 1em 0 #000;
  padding: 1.5em;
}

/* ====================================================
 * Header
 * ==================================================== */

.header__title {
  font-family: 'Lobster', cursive;
}

/* ====================================================
 * Menu
 * ==================================================== */

.menu {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 2px 1em 2px;
}

.menu__btn {
  background: #345;
  border-radius: 3px;
  color: #345;
  border: 1px solid #123;
  padding: .25em;
  margin: .5em;
  cursor: pointer;
}

.menu__btn:hover {
  background: #567;
}

.menu__btn svg * {
  fill: #fff;
  stroke:#fff;
}

.menu__btn:active {
  background: #fff;
  fill: #345;
  stroke:#345;
}

.menu__btn:active svg * {
  fill: #345!important;
  stroke: #345!important;
}

.menu__btn svg {
  width: 40px;
  height: 40px;
}

/* ====================================================
 * Form controls
 * ==================================================== */


fieldset.form {
  position: relative;
  border: 1px solid #777;
  border-radius: 3px;
}

legend.form__title {
  font-size: 12px;
}

.form__controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.form__label {
  font-size: 12px;
  display: block;
  padding: 1em 0;
}

.form__field {
  border-radius: 3px;
}

.form__field svg {
  width: 16px;
  height: 16px;
}

.form canvas {
  width: 100%;
  background: #111;
}

.form__close {
  position: absolute;
  right: 1em;
  color: #fff;
  font-weight: 500;
  font-size: 12px;
  border: 1px solid #345;
  background: #345;
  cursor: pointer;
  border-radius: 3px;
}

.form__close:active {
  background: #fff;
  color: #345;
}

template {
  display: none;
}
              
            
!

JS

              
                const $ = document.querySelector.bind(document)
const AC = new AudioContext()

// Creates a wrapper object around a Web Audio Oscillator plus Gain
// The wrapper object is needed because we cannot v-bind directly to
// AudioParam values.
const createAudioNode = ({type = "sine", freq = 440, gain = 1.0} = {}) => {
  const _osc = AC.createOscillator()
  _osc.frequency.value = freq
  _osc.type = type
  _osc.start()
  const _gain = AC.createGain()
  _gain.gain.value = gain
  _osc.connect(_gain)
  return {
    get type() {
      return _osc.type
    },
    set type(val) {
      _osc.type = val
    },
    get freq() {
      return _osc.frequency.value
    },
    set freq(val) {
      _osc.frequency.value = val
    },
    get gain() {
      return _gain.gain.value
    },
    set gain(val) {
      _gain.gain.value = val
    },
    toJSON() {
      return {
        type: _osc.type,
        freq: _osc.frequency.value,
        gain: _gain.gain.value
      }
    },
    _gain, _osc    
  }
}

const app = new Vue({
  el: '#app',
  data: {
    carrier: createAudioNode({
      type: "sine",
      freq: 440,
      gain: 0.3
    }),
    mods: []
  },
  mounted() {
    this.analyser = AC.createAnalyser()
    this.analyser.fftSize = 2048
    this.analyser.connect(AC.destination)
    this.buffer = new Uint8Array(this.analyser.frequencyBinCount)
    this.canvas = document.querySelector('canvas')
    this.ctx = this.canvas.getContext('2d')
    this.anim()
  },
  methods: { 
    addOscillator(type = "sine", freq = 1, gain = 100.0) {
      this.disconnectNodes()
      this.mods.push(createAudioNode({type, freq, gain}))
      this.connectNodes()      
    },
    removeOscillator(idx) {
      this.disconnectNodes()
      this.mods.splice(idx, 1)
      this.connectNodes()
    },
    connectNodes() {
      if (this.mods.length > 0) {
        this.mods[0]._gain.connect(this.carrier._osc.frequency)
        for (i = 1;i < this.mods.length; i++) {
          this.mods[i]._gain.connect(this.mods[i-1]._gain.gain)
        }
      }
    },
    disconnectNodes() {
      this.mods.map(mod => mod._gain.disconnect())
    },
    play() {
      if (AC.state === "suspended") {
        AC.resume()
      }
      this.disconnectNodes()
      this.connectNodes()
      this.carrier._gain.connect(this.analyser)
    },
    stop() {
      this.carrier._gain.disconnect()
    },
    anim() {
      requestAnimationFrame(() => this.anim())
      const { ctx, canvas, analyser, buffer } = this
      const width = canvas.width || 400
      const height = canvas.height || 300
      ctx.strokeStyle="rgb(128,255,128)"
      ctx.clearRect(0,0, width, height)
      analyser.getByteTimeDomainData(buffer)
      const dx = width / buffer.length
      const my = height / 2
      ctx.beginPath()
      ctx.moveTo(0, my + ((127-buffer[0]) / 128.0) * my)
      for (let i = 1; i < buffer.length; i++) {
        ctx.lineTo(i * dx, my + ((127-buffer[i]) / 128.0) * my)
      }
      ctx.stroke()
    },
    createPen() {
      const form = document.createElement('form')
      form.action = 'https://codepen.io/pen/define'
      form.target = '_blank'
      form.method = 'POST'
      const input = document.createElement('input')
      input.type = 'hidden'
      input.name = 'data'
      const carrierStr = JSON.stringify(this.carrier)
      const modulatorStr = this.mods.map(m => JSON.stringify(m)).join(',')
      input.value = JSON.stringify({
        title: 'test',
        html: $`.pen__html`.innerHTML,
        js: $`.pen__js`.innerHTML
          .replace(/%%CARRIER%%/, carrierStr)
          .replace(/%%MODULATORS%%/, modulatorStr)
          .replace(/&gt;/g, '>')
      })
      form.appendChild(input)
      document.body.appendChild(form)
      form.submit()
      document.body.removeChild(form)
    }
  }
})
              
            
!
999px

Console