├── .gitignore ├── .jshintignore ├── .jshintrc ├── README.md ├── build.js ├── mediastream-gain.bundle.js ├── mediastream-gain.js ├── package.json └── test.html /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.bundle.js 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "asi": false, 3 | "expr": true, 4 | "loopfunc": true, 5 | "curly": false, 6 | "evil": true, 7 | "white": true, 8 | "undef": true, 9 | "browser": true, 10 | "node": true, 11 | "trailing": true, 12 | "indent": 4, 13 | "latedef": true, 14 | "newcap": true 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mediastream-gain 2 | 3 | ## What is this? 4 | 5 | A tiny browser module for creating a gain/volume controller for the audio channels in a [MediaStream](https://developer.mozilla.org/en-US/docs/WebRTC/MediaStream_API). 6 | 7 | It's useful for controlling the volume of your microphone input before it's sent accross a peer connection in a WebRTC call, for example. This module is a small part of [SimpleWebRTC](http://simplewebrtc.com) where it is used for minimizing echos, by using [hark](http://latentflip.com/hark) to determine if you're speaking and turning your mic down a bit if you're not. 8 | 9 | This module is suitable for use with [browserify](http://browserify.org)/CommonJS on the client. 10 | 11 | If you're not using browserify or you want AMD support use `mediastream-volume.bundle.js`. 12 | 13 | 14 | ## Important details 15 | 16 | The way this works by replacing the first audio channel in the stream with one that is run through a gain filter. But beware that this *edits the stream you give it in place* it doesn't produce a new one. 17 | 18 | 19 | ## Installing 20 | 21 | ``` 22 | npm install mediastream-gain 23 | ``` 24 | 25 | ## An example 26 | 27 | Here we use another piece of SimpleWebRTC [getusermedia](https://github.com/HenrikJoreteg/getusermedia) to fetch user media in a cross-browser, easy-to-handle-errors-and-lack-of-support sort of way. 28 | 29 | This assumes a commonJS environment, but that's not a requirement (see above). 30 | 31 | ```js 32 | var MicGainController = require('mediastream-gain'); 33 | var getUserMedia = require('getusermedia'); 34 | var gainController; 35 | 36 | getUserMedia(function (err, stream) { 37 | // this will replace the audio channels in the 38 | // stream 39 | gainController = new MicGainController(stream); 40 | // set gain to 20% 41 | gainControl.setGain(.2); 42 | // set gain to 0, effectively muting it 43 | gainControl.setGain(0); 44 | // there's also: 45 | gainControl.off(); // equivalent to setGain(0) 46 | gainControl.on(); // equivalent to setGain(1) 47 | }); 48 | 49 | ``` 50 | 51 | ## Methods 52 | 53 | It couldn't be simpler, but behavior varies slighly based on availability of WebAudio support that can be wired into WebRTC. 54 | 55 | You can check for support by checking the `support` property of the an instance of `gainController` 56 | 57 | These will simply be noop functions if WebAudio isn't fully supported. 58 | 59 | **.setGain(Float)** - takes a number between 1 and 0 60 | **.getGain()** - returns current setting 61 | **.off()** - shortcut for turning mic off 62 | **.on()** - shortcut for full gain 63 | 64 | 65 | ## License 66 | 67 | MIT 68 | 69 | ## Created By 70 | 71 | If you like this, follow: [@HenrikJoreteg](http://twitter.com/henrikjoreteg) on twitter. 72 | 73 | -------------------------------------------------------------------------------- /build.js: -------------------------------------------------------------------------------- 1 | var bundle = require('browserify')(); 2 | var fs = require('fs'); 3 | 4 | bundle.add('./mediastream-gain'); 5 | bundle.bundle({standalone: 'MediaStreamGainController'}).pipe(fs.createWriteStream('mediastream-gain.bundle.js')); 6 | -------------------------------------------------------------------------------- /mediastream-gain.bundle.js: -------------------------------------------------------------------------------- 1 | !function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.MediaStreamGainController=e():"undefined"!=typeof global?global.MediaStreamGainController=e():"undefined"!=typeof self&&(self.MediaStreamGainController=e())}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o= 26; 69 | var AudioContext = window.webkitAudioContext || window.AudioContext; 70 | 71 | 72 | // export support flags and constructors.prototype && PC 73 | module.exports = { 74 | support: !!PC, 75 | dataChannel: isChrome || isFirefox || (PC && PC.prototype && PC.prototype.createDataChannel), 76 | prefix: prefix, 77 | webAudio: !!(AudioContext && AudioContext.prototype.createMediaStreamSource), 78 | mediaStream: !!(MediaStream && MediaStream.prototype.removeTrack), 79 | screenSharing: !!screenSharing, 80 | AudioContext: AudioContext, 81 | PeerConnection: PC, 82 | SessionDescription: SessionDescription, 83 | IceCandidate: IceCandidate 84 | }; 85 | 86 | },{}]},{},[1]) 87 | (1) 88 | }); 89 | ; -------------------------------------------------------------------------------- /mediastream-gain.js: -------------------------------------------------------------------------------- 1 | var support = require('webrtcsupport'); 2 | 3 | 4 | function GainController(stream) { 5 | this.support = support.webAudio && support.mediaStream; 6 | 7 | // set our starting value 8 | this.gain = 1; 9 | 10 | if (this.support) { 11 | var context = this.context = new support.AudioContext(); 12 | this.microphone = context.createMediaStreamSource(stream); 13 | this.gainFilter = context.createGain(); 14 | this.destination = context.createMediaStreamDestination(); 15 | this.outputStream = this.destination.stream; 16 | this.microphone.connect(this.gainFilter); 17 | this.gainFilter.connect(this.destination); 18 | stream.addTrack(this.outputStream.getAudioTracks()[0]); 19 | stream.removeTrack(stream.getAudioTracks()[0]); 20 | } 21 | this.stream = stream; 22 | } 23 | 24 | // setting 25 | GainController.prototype.setGain = function (val) { 26 | // check for support 27 | if (!this.support) return; 28 | this.gainFilter.gain.value = val; 29 | this.gain = val; 30 | }; 31 | 32 | GainController.prototype.getGain = function () { 33 | return this.gain; 34 | }; 35 | 36 | GainController.prototype.off = function () { 37 | return this.setGain(0); 38 | }; 39 | 40 | GainController.prototype.on = function () { 41 | this.setGain(1); 42 | }; 43 | 44 | 45 | module.exports = GainController; 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mediastream-gain", 3 | "description": "cross-browser getUserMedia shim with node.js style error-first API.", 4 | "version": "1.0.1", 5 | "author": "Henrik Joreteg ", 6 | "dependencies": { 7 | "webrtcsupport": "^1.0.0" 8 | }, 9 | "devDependencies": { 10 | "attachmediastream": "1.0.1", 11 | "browserify": "2.x", 12 | "getusermedia": "0.2.1", 13 | "precommit-hook": "0.3.x" 14 | }, 15 | "keywords": [ 16 | "browser", 17 | "getUserMedia", 18 | "WebRTC", 19 | "microphone", 20 | "webaudio" 21 | ], 22 | "license": "MIT", 23 | "main": "mediastream-gain.js", 24 | "repository": { 25 | "type": "git", 26 | "url": "https://github.com/HenrikJoreteg/mediastream-gain" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | mediastream-gain 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 27 | 28 | 29 | --------------------------------------------------------------------------------