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

              
                <header class="max-header" role="banner" aria-label="CODAIT MAX">
  <a class="max-skip" href="#max-main-content" tabindex="0">Skip to main content</a>
  <span class="max-header-logo">
    <svg focusable="false" preserveAspectRatio="xMidYMid meet" style="will-change: transform;" xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 243 225" aria-hidden="true">
      <style type="text/css">
        .st0{clip-path:url(#SVGID_2_);}
        .st1{fill:#706E6F;stroke:#706E6F;stroke-width:1.9754;stroke-miterlimit:10;}
        .st2{fill:#706E6F;}
        .st3{fill:#08cdc7;}
      </style>
      <defs><path id="SVGID_1_" d="M234,116.8c0,50.3-38.4,95.2-96,95.2l-0.3-95.2L203,48.1C221.7,65.4,234,89.8,234,116.8z"/></defs>
      <clipPath id="SVGID_2_"><use xlink:href="#SVGID_1_"  style="overflow:visible;"/></clipPath>
      <g class="st0">
        <rect x="137.8" y="222.6" class="st1" width="95.4" height="7.2"/><rect x="137.8" y="242" class="st1" width="95.4" height="7.2"/>
      </g>
      <g class="st0">
        <rect x="136.8" y="163.7" class="st2" width="98.2" height="9.2"/><rect x="136.8" y="183" class="st2" width="98.2" height="9.2"/><rect x="136.8" y="202.3" class="st2" width="98.2" height="9.2"/>
        <rect x="136.6" y="67" class="st2" width="98.4" height="9.2"/><rect x="136.6" y="47.7" class="st2" width="98.4" height="9.2"/><rect x="136.6" y="86.3" class="st2" width="98.4" height="9.2"/>
        <rect x="136.2" y="144.3" class="st2" width="98.5" height="9.2"/><rect x="136.8" y="125" class="st2" width="98.1" height="9.2"/><rect x="136.8" y="105.7" class="st2" width="98" height="9.2"/>
      </g>
      <path class="st3" d="M107.1,13l-0.5-0.4c-1.8-1.5-3.8-2.3-5.7-2.5l-0.2,0C90.2,7,79.6,10.6,71,19.9c-2.6,2.8-4.7,6.1-6.2,9.9
        l-0.4,1l-1-0.3c-11.1-3.4-22.3-0.1-29.9,8c-8.6,9.1-11.5,23.3-7.5,36.1l0.3,0.9l-0.8,0.5C14.7,82.1,9.5,94.5,9.1,104.2
        c-0.9,19.6,7.6,28.5,16.1,34.6l0.7,0.5l-0.2,0.8c-3,9.9-2.1,20.5,2.5,29.1c7.7,14.2,23.3,20.4,38.1,15.2l1-0.4l0.4,1
        c0.9,2,2,3.8,3.3,5.3c8.2,9.7,19.9,13.3,31.4,9.7l0.2,0c1.5-0.2,2.9-0.7,4.2-1.7l0.2-0.1c3-2.3,5-6.5,5-10.5V22.4
        C111.9,19.2,110.1,15.6,107.1,13z M105.3,49.2L104,49c-2.6-0.4-6.5-1.5-10.1-4.7c-3.3-2.9-5.2-6.3-6.2-9l-6.3,2.1
        c1.3,3.5,3.8,8,8.1,11.8c5.3,4.6,10.9,6.1,14.7,6.5l1,0.1v41.4l-1.5-0.6c-4.5-1.6-9.1-2.4-13.6-2.4c-6.3,0-12.3,1.6-17.8,4.7
        c-3.4,1.9-6.4,4.3-9,7l-0.8,0.8l-0.8-0.8c-2.6-2.5-7.1-5.6-13.2-6.1c-0.6-0.1-1.2-0.1-1.9-0.1c-6.3,0-11.1,2.9-13.4,4.8l4.4,5
        c1.9-1.5,5.6-3.5,10.4-3.1c5,0.5,8.4,3.4,10,5.1l0.6,0.6l-0.4,0.7c-3.8,6.4-5.2,12.8-5.8,16.5l6.5,0.9
        c0.9-5.9,4.2-18.5,16.7-25.6c13-7.4,25.5-2.3,29.1-0.6l0.6,0.3v49.1l-1.3,0c-3.2,0-9.5,0.7-15.3,5.7c-5.3,4.6-7.5,10.4-8.4,14
        l6.5,1.3c0.7-2.5,2.3-6.9,6.2-10.3c4.2-3.7,8.7-4.1,11.1-4.1l1.1,0v27.4c0,1.9-1,3.7-1.8,4.7l-0.2,0.2l-0.2,0.1
        c-3.3,1.5-6.4,2.1-9.4,2.1c-7.5,0-13.8-4-17.7-8.7c-5.2-6.2-5.4-18-3.5-24.1c1.4-4.7,2.9-5.8,8.5-9.9l0.5-0.4
        c6.1-4.5,13.4-15.1,9.3-26.1l-6.1,2.7c2.6,7.8-3.6,15.5-7.1,18.2l-0.5,0.4c-2.1,1.6-4,2.9-5.5,4.3l-1.2,1.1l-0.7-1.4
        c-0.4-0.8-0.8-1.5-1.2-2.1c-2.2-3.5-6.1-6.1-8-7.2l-3.1,5.8c1.6,1,4.2,2.9,5.5,4.9c1,1.6,1.6,3.2,2.1,5c0.3,1,0.6,1.9,0.9,2.7
        L66,160l-0.1,0.4c-1.3,4.7-1.6,10.7-0.7,16.2l0.1,0.9l-0.9,0.3c-11.6,4.4-24.5-0.7-30.6-12c-6.9-12.7-3.4-29.9,7.7-38.9l-3.9-5.3
        c-3.4,2.7-6.3,6.1-8.6,10.1l-0.7,1.2l-1-0.8c-6.5-5.1-12.4-11.9-11.7-27.7c0.5-10.6,7.5-23,19.6-25.2l0.1,0l0.1,0
        c1.5,0.1,3,0,4.4-0.2l0.7-0.1l0.4,0.6c1.6,2.4,3.5,4.5,5.6,6.1c3.6,2.7,8,4.1,12.5,4.2L59,83.2c-2.3-0.1-5.3-0.7-8.3-2.9
        c-1.1-0.8-2.1-1.8-3-3l-0.7-0.9l0.9-0.7c2.9-2.4,4.7-5.9,5.4-10.3l-6.6-0.7c-0.6,3.9-2.7,8.3-10.9,7.9c-0.4,0-0.7,0-1.1,0
        c-0.4,0.1-0.9,0.1-1.3,0.2l-1,0.2l-0.3-1C29,61.6,31.4,50.2,38.2,43c6-6.4,15-8.8,23.3-6.2l1,0.3l-0.2,1
        c-1,5.6-0.7,11.4,0.7,16.7c3,11.3,10.3,17.8,14.2,20.5l3.6-5.5c-3.2-2.3-9-7.6-11.4-16.7c-2.7-10.3-0.2-21.6,6.4-28.8
        c4.8-5.2,14.6-12.9,26.9-6.4l0.3,0.2c0.5,0.5,2.3,2.3,2.3,4.1V49.2z"/>
      <path class="st3" d="M174.8,30.7c-14-13.4-32.4-20.7-51.8-20.7c-1.5,0-2.8,1.2-2.8,2.7L120,84.6c0,1.1,0.7,2.1,1.8,2.6
        c0.3,0.1,0.7,0.2,1,0.2c0.8,0,1.5-0.3,2-0.9l50-51.9C176,33.5,175.9,31.7,174.8,30.7z"/>
      <path class="st3" d="M123.8,200.3c-1.9,0-3.5-1.5-3.5-3.5l-0.3-90.7c0-0.9,0.3-1.8,1-2.4l62.2-65.5c0.6-0.7,1.5-1.1,2.4-1.1
        c1,0,1.8,0.3,2.5,1c18.5,17.9,28.7,42.1,28.7,68C216.8,158.1,175.1,200.3,123.8,200.3z M127,107.6l0.3,85.7
        c45.9-1.8,82.6-40.2,82.6-87.1c0-22.8-8.5-44.2-24.1-60.5L127,107.6z"/>
    </svg>
  </span>
  <a class="max-header-name" href="//ibm.biz/max-models" target="_blank" title="MAX">
    <span class="max-header-name-prefix">MAX &nbsp;</span>
    Demo
  </a>
  <div class="max-header-sub">
    <span class="max-header-sub-text">
      Sending a wav file to a MAX audio model
    </span>
  </div>
  <div class="max-header-filler">
  </div>
</header>

<section class="max-subheader">
  <div class="max-field">
    <label for="modelEndpoint">Model Endpoint</label>
    <input readonly id="modelEndpoint" type="text" value="https://max-audio-classifier.codait-prod-41208c73af8fca213512856c7a09db52-0000.us-east.containers.appdomain.cloud/model/predict">
  </div>
  <div class="max-field">
    <label for="labelFilter">Filter</label>
    <input id="labelFilter" type="text" value="" placeholder="Enter comma separated list  of labels to filter  (i.e., Engine,Car,Alarm)">
  </div>
</section>

<div id="max-main-content" class="max-main-content">
  <div class="left-side">
    <p>
      See how to <code>POST</code> a WAV audio file to the MAX <a href="https://developer.ibm.com/exchanges/models/all/max-audio-classifier/" target="_blank">Audio Classifier</a>.
    </p>
    <p>
      The <strong>Filter</strong> input accepts a list of comma separated labels to filter the response (e.g., Bird,Thunderstorm,Wind).
      View the <strong><a href="https://research.google.com/audioset/ontology/index.html" target="_blank">AudioSet ontology</a></strong> for list of some of the available labels.
      <br><strong>Note</strong>: The filter labels are case sensitive and subject to change depending on the model version.
    </p>
    
    <div class="max-field">
      <input id="audioFile" type="file" accept="audio/wave, audio/wav, audio/x-wav" onchange="fileSelected();">
      <label for="audioFile" class="max-btn max-btn-primary" role="button" tabindex="0">Select an audio file</label>
    </div>

    <button id="predictionButton" class="max-btn-primary" onclick="getPrediction();">Get prediction</button>

    <audio id="audio" controls="controls" title="Select audio file">
      <source id="audioSource"  src=""></source>
    </audio>
  </div>

  <div class="right-side">
    <div class="max-log"></div>
    <div id="prediction">
<!-- prediction goes here -->
    </div>
  </div>
</div>

              
            
!

CSS

              
                .max-subheader .max-field {
  min-width: 315px;
  width: calc(50% - 5rem);
  margin-right: 2rem;
}
.max-subheader fieldset.max-field {
  min-width: initial;
  width: initial;
}
.left-side .max-field {
  margin-top: 1rem;
}
.left-side {
  display: inline-block;
  min-width: 315px;
  vertical-align: top;
  width: 30%;
}
.left-side img {
  margin-top: 1rem;
  width: 100%;
}
.left-side audio {
  display: none;
  margin-top: 2rem;
  width: 100%;
}
.right-side {
  display: inline-block;
  margin-left: 3rem;
  vertical-align: top;
  width: 60%;
}
.right-side pre {
  padding: 1rem;
  background-color: #f3f3f3;
}

.max-log {
  margin-bottom: 0.5rem;
}
.max-log > span {
  display: block;
  line-height: 1.1rem;
}
.max-log > span.max-error {
  color: red;
  font-family:  'IBM Plex Mono', monospace;
}

              
            
!

JS

              
                // triggered when when a file is selected
function fileSelected () {
  cleanup()
  const input = document.getElementById('audioFile')
  // make sure we have files
  if (input.files && input.files[0]) {
    // use FileReader to read the audio file
    const reader = new FileReader()
    reader.onload = function (e) {
      // we have audio data now load onto the page
      const audioElt = document.getElementById('audio')
      const audioSource = document.getElementById('audioSource')
      // this will be in the form of a Data URI (i.e., data:audio/wav;base64,/9j/4AAQSkZJR...)
      audioSource.src = reader.result
      audioElt.load()
      audioElt.style.display = 'initial'
    }

    reader.readAsDataURL(input.files[0])
  } else {
    maxLog('please upload a file', true)
  }
}

// triggered by 'Get prediction' button
function getPrediction () {
  cleanup()
  // get model endpoint
  const endpoint = document.getElementById('modelEndpoint').value
  // get the audio file
  const audioElt = document.getElementById('audioSource')

  // make sure we have an audio file and the endpoint
  if (endpoint && audioElt && audioElt.src) {
    // disable inputs/buttons while we get prediction
    disableElements(true)
    maxLog('fetching prediction...')

    // convert the audio to a Blob
    const audioBlob = dataURItoBlob(audioElt.src)

    // create a new form data to send the audio
    const formData = new FormData()
    formData.append('audio', audioBlob, 'audio.wav')

    // get filter
    let filter = document.getElementById('labelFilter').value
    let query = ''
    if (filter) {
      filter = filter.split(',').map(label => {
        return label.trim()
      }).join(',')
      query = endpoint.lastIndexOf('?') === (endpoint.length - 1) ? `&filter=${filter}` : `?filter=${filter}`
    }

    // make the POST call to the model endpoint and send the form data containing the audio
    fetch((endpoint + query), {
      method: 'POST',
      body: formData,
      headers: {
        'Accept': 'application/json'
      }
    }).then(function (response) {
      // yay! we got a response. convert it to JSON
      return response.json()
    }).then(function (prediction) {
      // display the prediction response
      displayPrediction(prediction)
      maxLog('')
    }).catch(function (err) {
      maxLog(err, true)
    }).then(function () {
      // enable inputs/buttons
      disableElements(false)
    })
  } else {
    maxLog('missing endpoint and/or audio file', true)
  }
}

// convert Data URI (i.e., data:audio/wav;base64,/9j/4AAQSkZJR...) to a Blob
function dataURItoBlob (dataURI) {
  const byteString = atob(dataURI.split(',')[1])
  const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

  const ab = new ArrayBuffer(byteString.length)

  const ia = new Uint8Array(ab)

  for (let i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i)
  }

  return new Blob([ab], { type: mimeString })
}

function displayPrediction (prediction) {
  const c = document.createElement('code')
  c.innerText = JSON.stringify(prediction, null, 4)
  const p = document.createElement('pre')
  p.appendChild(c)
  document.getElementById('prediction').appendChild(p)
}

function cleanup () {
  maxLog('')
  document.getElementById('prediction').innerHTML = ''
}

// disable.enable buttons/inputs
function disableElements (disable) {
  const input = document.getElementById('audioFile')
  const button = document.getElementById('predictionButton')
  if (disable) {
    input.setAttribute('disabled', 'disabled')
    button.setAttribute('disabled', 'disabled')
  } else {
    input.removeAttribute('disabled')
    button.removeAttribute('disabled')
  }
}

const maxLog = function (txt, isError) {
  const messageWrapper = document.querySelector('.max-log')
  if (messageWrapper) {
    if (txt) {
      const message = document.createElement('span')
      message.innerText = txt.message || txt
      if (isError) {
        message.className = 'max-error'
        console.error(txt)
      }
      messageWrapper.appendChild(message)
    } else {
      messageWrapper.innerHTML = ''
    }
  }
}

              
            
!
999px

Console