Git Product home page Git Product logo

microm's Introduction

Build Status npm version Bower version DEMO

Microm

Beautiful library to convert browser microphone to mp3 in Javascript

Microm it's just a wrapper of few audio converting libraries which exposes a fully Promise and Event oriented api. Microm goal it's to make trivial to play and convert audio in the browser.

Installation

$ npm install microm

or

$ bower install microm

Usage

Recording and converting the user microphone

var microm = new Microm();
var mp3 = null;

start();
setTimeout(stop, 1500);

function start() {
  microm.record().then(function() {
    console.log('recording...')
  }).catch(function() {
    console.log('error recording');
  });
}

function stop() {
  microm.stop().then(function(result) {
    mp3 = result;
    console.log(mp3.url, mp3.blob, mp3.buffer);

    play();
    download();
  });
}

function play() {
  microm.play();
}

function download() {
  var fileName = 'cat_voice';
  microm.download(fileName);
}

Reacting to events

microm.on('timeupdate', updateCurrentTime);
microm.on('loadedmetadata', onLoaded);
microm.on('play', onPlayEvent);
microm.on('pause', onPauseEvent);
microm.on('ended', onEndEvent);

function onLoaded(time) {
  duration.innerHTML = time;
}

function updateCurrentTime(time) {
  currentTime.innerHTML = time;
}

function onPlayEvent() {
  status.innerHTML = 'Playing';
}

function onPauseEvent(time) {
  status.innerHTML = 'Paused';
}

function onEndEvent() {
  status.innerHTML = 'Ended';
}

Upload mp3 to the server

microm.getBase64().then(function(base64string) {
  $.ajax({
    type: 'POST',
    contentType: 'application/octet-stream',
    mimeType: 'audio/mpeg',
    processData: false,
    url: 'server/upload_audio',
    data: base64string,
    success: onSuccess
  });
});

Under the hood

To achieve that, Microm uses the following libs:

  • lamejs mp3 encoder in javascript
  • webrtc-adapter Shim to insulate apps from spec changes and prefix differences
  • RecordRTC record WebRTC audio/video media streams
  • RSVP Provides tools for organizing asynchronous code

How Microm uses that libraries?

In order to get the user recorded data, we use webrtc-adapter + RecordRTC which provides some shims and tools to make it easy and without worry about crossbrowser issues.

Later we use lamejs to convert the Wav to Mp3. We can say that the hard work happen in that lib <3.

And finally to provide a Promise based Api we use RSVP which support the Promises/A+ and have a great support.

Browser support

The library just work in Chrome and Firefox right now. More info:

Api reference

download

Forces file download.

Parameters

  • fileName String

Returns void

getBase64

Base64 value of the recorded data.

Examples

microm.getBase64().then(function(base64) {
    console.log(base64);
  });

Returns Promise

getBlob

Blob value of the recorded data.

Returns Blob

getBuffer

ArrayBuffer of the recorded data (raw binary data buffer).

Returns ArrayBuffer

getMp3

Returns all mp3 info. Right now we are converting the recorded data everytime this function it's called.

Returns Promise

getUrl

Link to the mp3. It can be used as a audio "src" value

Examples

microm.getUrl();
  // Something like --> "blob:http%3A//localhost%3A8090/8b40fc63-8bb7-42e3-9622-9dcc59e5df8f"

Returns String

getWav

Blob enconded as Wav.

Returns Blob

off

Remove an event handler

Parameters

  • eventName String

Returns void

on

Attach an event handler function for event name

Parameters

  • eventName String
  • handler Function

Returns void

pause

Pauses the player.

Returns void

play

Reproduce the player audio.

Returns void

record

Request browser microphone access and waits for it resolution. If the user grant access, Microm will start recording the audio.

Returns Promise

stop

Stops recording audio if Micron is recording, if not just pauses the player and set's the currentTime to 0.

Examples

microm.stop().then(function(mp3) {
   console.log(mp3.url, mp3.blob);
  });

Returns Promise Will be resolved with the mp3.

microm's People

Contributors

adrianblynch avatar buzinas avatar dmdark avatar khornberg avatar mirzazulfan avatar zzarcon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

microm's Issues

Better audio quality?

Hi,
I'm trying out this module. The recording works fine but the audio quality is not great. There's a lot of echo and pop noise. This is true for the demo app listed in readme as well. Is there a way to record better quality audio using this module?

LICENSE file?

Was the intent to have microm be under a GPL compatible license of some sort? Could you post a license file if so?

Update dist/microm.js

I'm using the dist/microm.js file to integrate Microm in my JSF web application.

It doesn't seem to have the fix made for #24

Could you update dist/microm.js with #25 ?

Thanks

Incompatible with babel-polyfill

Until I removed import 'babel-polyfill', I was encountering an obscure error during microm.stop().

I was able to avoid this problem by using babel's transform-runtime plugin instead of the babel- polyfill (v6.9.1).

Gulp build task (partial)

...
        .transform(babelify, {
            presets: ['react', 'es2015', 'stage-2'],
            plugins: ['transform-runtime']  // use this instead of the babel-polyfill
        })
...

Browser

Chrome 51.0.2704.103 (64-bit) OSX

Error details

screenshot

This line in stop() was blowing up:

self.buffer = new ArrayBuffer(view);

The Uncaught RangeError: Wrong length! error was thrown from this code block:
(found somewhere in /node_modules/babel-polyfill/node_modules/core-js/modules/...)

var validateArrayBufferArguments = function(that, length){
  anInstance(that, $ArrayBuffer, ARRAY_BUFFER);
  var numberLength = +length
    , byteLength   = toLength(numberLength);
  if(numberLength != byteLength)throw RangeError(WRONG_LENGTH); // <<< ERROR here
  return byteLength;
};

And the underlying problem began here:

    $ArrayBuffer = function ArrayBuffer(length){
      return new BaseBuffer(validateArrayBufferArguments(this, length));
    };

In the return line above, this had a value of:

{
  byteLength:  [Exception: TypeError: Method ArrayBuffer.prototype.byteLength called on 
    incompatible receiver #<ArrayBuffer> at ArrayBuffer.get byteLength [as byteLength] (native) 
    at ArrayBuffer.remoteFunction (<anonymous>:3:14) at new ArrayBuffer 
    (http://localhost:3000/javascripts/app.min.js:2660:7) at 
    http://localhost:3000/javascripts/app.min.js:33323:28 at Worker.webWorker.onmessage 
    (http://localhost:3000/javascripts/app.min.js:33258:14)]
}

Other relevant files

index.js (partial)

I was able to use the debugger and breakpoints to track the problem down to the babel polyfill. Commenting out this line solved the problem.

import 'babel-polyfill';

package.json (partial)

Most of package.json was omitted for brevity.

"dependencies": {
    "babel": "^6.3.26",
    "babelify": "^7.2.0",
    "browserify": "^11.2.0",
    "es6-promise": "^3.0.2",
    "gulp": "^3.9.0",
    "gulp-asset-manifest": "0.0.6",
    "gulp-rename": "^1.2.2",
    "gulp-rev": "^7.0.0",
    "gulp-sass": "^2.0.4",
    "gulp-uglify": "^1.5.3",
    "isomorphic-fetch": "^2.2.0"
    ...
},
"devDependencies": {
    "babel-eslint": "^4.1.5",
    "babel-plugin-transform-regenerator": "^6.9.0",
    "babel-polyfill": "^6.9.1",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-2": "^6.11.0",
    ...
}

Api

  • startRecording
  • stopRecording
  • pause Pause current recorded audio
  • getMp3 Lazy mp3 encoding
  • getWav
  • dowload Forces browser file download
  • getUrl
  • getBlob
  • getBuffer
  • getBase64 Get the base64 representation of the mp3 --> Use FileReader.readAsDataURL

Propose Logo/Icon

Greeting. @zzarcon . I'am a graphic designer. I wanted to contribute to your project and I've designed a logo for your project. If you like it, I'll send you files and pr. what do you think? If you have an idea please share with me, it would be better for me. Thank you. Best Regard
version 1

Demo doesn't work in Chrome 47

Cool library idea. I gave the demo a shot in OS X Chrome Version 47.0.2526.80 (64-bit) and recording didn't work. I see "error recording" in the console.

Ideas?

Not working method .stop() in Mobile Safari (iPhone 6, iOS 12)

Not working method .stop() in Mobile Safari (iPhone 6, iOS 12):

Example code:

var xAudioRec = new Microm();

....

$('.b-btn-stop', context).on('touchstart', function(){
  xAudioRec.stop().then(function(result){
    console.log('Success');
  }).catch(function(){
    console.log('Fail'); // — I get here.
  });
});

Help me, pleace...

Support Mono & Stereo

Encode buffer depending on the recorded data channels:

ld = left.splice(i, i + maxSamples)
rd = right.splice(i, i + maxSamples)

mp3buf = mp3enc.encodeBuffer(ld, rd)

vs

mono = samples.splice(i, i + maxSamples)

mp3buf = mp3enc.encodeBuffer(mono)

Force dowload

 function forceDownload(){
        console.log('forceDownload');
        //var blob = new Blob(mp3Data, {type:'audio/mpeg'});
        var url = (window.URL || window.webkitURL).createObjectURL(blob);
        var link = window.document.createElement('a');
        link.href = url;
        link.download = 'output.mp3';
        var click = document.createEvent("Event");
        click.initEvent("click", true, true);
        link.dispatchEvent(click);
    }

Misleading usage

function start() {
  microm.startRecording().then(function() {
    console.log('recording...')
  }).catch(function() {
    console.log('error recording');
  });
}

Seems like Microm class doesn't have startRecording method

Event system

Using same JS Audio event names.

var microm = new Microm();

microm.on('timeupdate', onTimeUpdate);
microm.on('ended', onEnded);

microm.startRecording();

Web worker usage

Hi , i have a problem with recordings that are more than 1 min long , it takes a lot of time to compress in MP3 and on top of that , it blocks the thread (window freeze). i think the usage of web workers for this kind of job will be more suitable.
But i'm not really sure what blocks the thread, because before compression there's a "filereader" call with big chunks of data getting in, needs to investigate more, in the meantime if someone has a workaround for this, it will be a good help, Thanks

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.