Code Monkey home page Code Monkey logo

audio-recorder's Introduction

Audio Recorder

A audio-record demo, with recording audio wave and playing audio wave.

Experience it online

click me

How to run

Install Parcel:

npm install -g parcel-bundler

Go to the demo root folder and install dependencies:

npm install

Run:

npm start

Visit http://localhost:1234/.

Build:

npm run build

Detail

Record

Using Recorder lib to record audio.

Initiate:

const rec = Recorder({
  type: "mp3",
  sampleRate: 16000,
  bitRate: 16
});

Start:

rec.open(function () {
  rec.start();
});

Stop and process audio data:

rec.stop(function (blob) {
  rec.close();
  // Show audio wave.
  loadWave(blob);
  // Enable audio download. 
  enableDownload(blob);
}, function () {
  console.log('Record failed');
});

Wave

The audio wave is generated with Wavesurfer.js.

HTML code:

<!-- Wave container -->
<div id="waveform"></div>

<script src="https://unpkg.com/wavesurfer.js"></script>
<!-- Microphone plugin for recording wave -->
<script src="https://unpkg.com/wavesurfer.js/dist/plugin/wavesurfer.microphone.min.js"></script>

Initiate:

const wavesurfer = WaveSurfer.create({
  container: '#waveform',
  waveColor: 'violet',
  progressColor: 'purple',
  cursorColor: 'white',
  plugins: [
    // Using microphone plugin to create wave when recording.
    WaveSurfer.microphone.create()
  ]
});

Generate playing wave with load method and ObjectURL:

function loadWave(blob) {
  objectURL = URL.createObjectURL(blob);
  wavesurfer.load(objectURL);
}

Recording wave is created by microphone plugin of WaveSurfer.js, when the start method below is success, the wave is generated.

wavesurfer.microphone.start();

Download

HTML:

<button class="btn btn-download" id="downloadButton" disabled>
  Download
  <a href="#" class="download-link" id="download-link" download></a>
</button>

Style (Hide the a tag when download button was disabled):

.btn-download {
  position: relative;

  &:disabled {
    a {
      display: none;
    }
  }

  a {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
  }
}

JavaScript:

function enableDownload(blob) {
  const downloadLinkEl = document.getElementById('download-link');
  const fileName = new Date().toLocaleString() + 'mp3';
  downloadLinkEl.href = URL.createObjectURL(blob);
  downloadLinkEl.download = fileName;

  downloadButton.disabled = false;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.