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

              
                <svg></svg>
<div id="loading">Loading audio...</div>
<div id="buttons">
<button id="play">Play</button>
<button id="stop">Stop</button>
</div>
              
            
!

CSS

              
                body {
  background: #222
}

svg {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

#loading {
  color: white;
  font-family: sans-serif;
  text-align: center;
}

#buttons {
    background: yellow;
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 20px;
    background: #444;
    border-radius: 5px;
}

button {
    display: block;
    color: white;
    width: 50px;
    height: 25px;
    border: none;
    margin-bottom: 5px;
    border-radius: 5px;
    margin-top: 5px;
    cursor: pointer;
    box-shadow: 1px 1px 2px 1px #333;
}

button#play {
    background: #4CAF50;
}

button#stop {
  background: #ea5f54
}


              
            
!

JS

              
                const basePath = 'https://alemangui.github.io/audio/'
const paths = [ basePath + 'synths-group.mp3', basePath + 'bass-group.mp3', basePath + 'drums-group.mp3']
const loadingDiv = document.getElementById('loading')
const buttonContainer = document.getElementById('buttons')
const playButton = document.getElementById('play')
const stopButton = document.getElementById('stop')

buttonContainer.style.display = 'none'

let promises = []

paths.forEach(path => {
  promises.push(new Promise((resolve, reject) => {
    let sound = new Pz.Sound({ source: 'file', options: { path: path, loop: true } }, 
      (error) => { error ? reject(error) : resolve(sound) })
  }))
})
                                 
Promise.all(promises).then(results => {
  
  buttonContainer.style.display = 'block'
  loadingDiv.style.display = 'none'
  
  let synths = results[0]
  let bass = results[1]
  let drums = results[2]
  
  let synthsAnalyser
  let bassAnalyser
  let drumsAnalyser
  
  const group = new Pz.Group([synths, bass, drums])
  
  let scale
  let displayData
  let topCurveFunction
  let bottomCurveFunction

  setupAnalyser()
  setupVisualization()
  group.play()
  window.requestAnimationFrame(() => draw())
  
  playButton.addEventListener('click', () => group.play())
  stopButton.addEventListener('click', () => group.stop())
  
  function setupAnalyser() {
    let fftSize = 2048
    
    synthsAnalyser = Pz.context.createAnalyser()
    bassAnalyser = Pz.context.createAnalyser()
    drumsAnalyser = Pz.context.createAnalyser()
    
    let analysers = [synthsAnalyser, bassAnalyser, drumsAnalyser]
    analysers.forEach(x => { x.fftSize = fftSize })
    
    displayData = new Uint8Array(analysers[0].frequencyBinCount / 3)

    synths.connect(synthsAnalyser)
    bass.connect(bassAnalyser)
    drums.connect(drumsAnalyser)
  }
  
  function setupVisualization() {
    let svg = d3.select('svg')
      .attr('width', Math.min(window.innerHeight, window.innerWidth))
      .attr('height', Math.min(window.innerHeight, window.innerWidth))
    const centerX = +svg.attr('width') / 2
    const centerY = +svg.attr('height') / 2
    const radius = +svg.attr('width') / 8
    
    svg.selectAll('rect').data(['#222', '#000'])
      .enter()
        .append('rect')
          .attr('x', 0)
          .attr('y', (d, i) => i == 0 ? 0 : centerY )
          .attr('width', +svg.attr('height'))
          .attr('height', +svg.attr('height'))
          .attr('fill', d => d)
    
    svg.selectAll('g').data(['yellow', 'magenta', 'cyan'])
      .enter()
        .append('g')
        .attr('id', d => d)
        .attr('stroke-linejoin', 'round')
        .selectAll('path').data(['top', 'bottom'])
        .enter()
          .append('path')
          .attr('class', d => d)

    let angleStep = Math.PI / (displayData.length - 1)
    
    topCurveFunction = d3.line()
      .x((d, i) => centerX + (radius + scale(d)) * Math.cos(angleStep * -i))
      .y((d, i) => centerY + (radius + scale(d)) * Math.sin(angleStep * -i))
    
    bottomCurveFunction = d3.line()
      .x((d, i) => centerX + (radius + scale(d)) * Math.cos(angleStep * i))
      .y((d, i) => centerY + (radius + scale(d)) * Math.sin(angleStep * i))
  }
  
  function draw() {    
    
    let analysers = [synthsAnalyser, bassAnalyser, drumsAnalyser]
    let width = +d3.select('svg').attr('width')
    d3.selectAll('g').each((color, i) => {
      let g = d3.select('#' + color)
      scale = d3.scaleSqrt().domain([0, 255]).range([0, width / 10 * 1.3 * (i + 1)])
      
      analysers[i].getByteFrequencyData(displayData)
      
      g.select('.top')
        .attr('d', topCurveFunction(displayData))
        .style('opacity', 0.5)
      
      g.select('.bottom')
        .attr('d', bottomCurveFunction(displayData))
        .style('opacity', 0.24)
        
      g.selectAll('path').attr("stroke", color)
        .attr('stroke', color)
        .attr('fill', color)
        .style('mix-blend-mode', 'screen')
        .attr('stroke-width', 5)
    })
    
    window.requestAnimationFrame(() => draw())
  } 
  
}, function(error) {
   loadingDiv.style.display = 'none'
   console.error('An error ocurred ' + error)
});
              
            
!
999px

Console