(function (f) {
if (typeof exports === "object" && typeof module !== "undefined") {
module.exports = f()
} else if (typeof define === "function" && define.amd) {
define([], f)
} else {
var g;
if (typeof window !== "undefined") {
g = window
} else if (typeof global !== "undefined") {
g = global
} else if (typeof self !== "undefined") {
g = self
} else {
g = this
}
g.Recorder = f()
}
})(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);
var f = new Error("Cannot find module '" + o + "'");
throw f.code = "MODULE_NOT_FOUND", f
}
var l = n[o] = {exports: {}};
t[o][0].call(l.exports, function (e) {
var n = t[o][1][e];
return s(n ? n : e)
}, l, l.exports, e, t, n, r)
}
return n[o].exports
} var i = typeof require == "function" && require;
for (var o = 0; o < r.length; o++)s(r[o]);
return s
})({
1: [function (require, module, exports) {
"use strict"; module.exports = require("./recorder").Recorder; }, {"./recorder": 2}], 2: [function (require, module, exports) {
'use strict'; var _createClass = (function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
} return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})(); Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Recorder = undefined; var _inlineWorker = require('inline-worker'); var _inlineWorker2 = _interopRequireDefault(_inlineWorker); function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
} function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
} var Recorder = exports.Recorder = (function () {
function Recorder(source, cfg) {
var _this = this; _classCallCheck(this, Recorder); this.config = {
bufferLen: 4096,
numChannels: 2,
mimeType: 'audio/wav'
};
this.recording = false;
this.callbacks = {
getBuffer: [],
exportWAV: []
}; Object.assign(this.config, cfg);
this.context = source.context;
this.node = (this.context.createScriptProcessor || this.context.createJavaScriptNode).call(this.context, this.config.bufferLen, this.config.numChannels, this.config.numChannels); this.node.onaudioprocess = function (e) {
if (!_this.recording) return; var buffer = [];
for (var channel = 0; channel < _this.config.numChannels; channel++) {
buffer.push(e.inputBuffer.getChannelData(channel));
}
_this.worker.postMessage({
command: 'record',
buffer: buffer
});
}; source.connect(this.node);
this.node.connect(this.context.destination); //this should not be necessary var self = {};
this.worker = new _inlineWorker2.default(function () {
var recLength = 0,
recBuffers = [],
sampleRate = undefined,
numChannels = undefined; self.onmessage = function (e) {
switch (e.data.command) {
case 'init':
init(e.data.config);
break;
case 'record':
record(e.data.buffer);
break;
case 'exportWAV':
exportWAV(e.data.type);
break;
case 'getBuffer':
getBuffer();
break;
case 'clear':
clear();
break;
}
}; function init(config) {
sampleRate = config.sampleRate;
numChannels = config.numChannels;
initBuffers();
} function record(inputBuffer) {
for (var channel = 0; channel < numChannels; channel++) {
recBuffers[channel].push(inputBuffer[channel]);
}
recLength += inputBuffer[0].length;
} function exportWAV(type) {
var buffers = [];
for (var channel = 0; channel < numChannels; channel++) {
buffers.push(mergeBuffers(recBuffers[channel], recLength));
}
var interleaved = undefined;
if (numChannels === 2) {
interleaved = interleave(buffers[0], buffers[1]);
} else {
interleaved = buffers[0];
}
var dataview = encodeWAV(interleaved);
var audioBlob = new Blob([dataview], {type: type}); self.postMessage({command: 'exportWAV', data: audioBlob});
} function getBuffer() {
var buffers = [];
for (var channel = 0; channel < numChannels; channel++) {
buffers.push(mergeBuffers(recBuffers[channel], recLength));
}
self.postMessage({command: 'getBuffer', data: buffers});
} function clear() {
recLength = 0;
recBuffers = [];
initBuffers();
} function initBuffers() {
for (var channel = 0; channel < numChannels; channel++) {
recBuffers[channel] = [];
}
} function mergeBuffers(recBuffers, recLength) {
var result = new Float32Array(recLength);
var offset = 0;
for (var i = 0; i < recBuffers.length; i++) {
result.set(recBuffers[i], offset);
offset += recBuffers[i].length;
}
return result;
} function interleave(inputL, inputR) {
var length = inputL.length + inputR.length;
var result = new Float32Array(length); var index = 0,
inputIndex = 0; while (index < length) {
result[index++] = inputL[inputIndex];
result[index++] = inputR[inputIndex];
inputIndex++;
}
return result;
} function floatTo16BitPCM(output, offset, input) {
for (var i = 0; i < input.length; i++, offset += 2) {
var s = Math.max(-1, Math.min(1, input[i]));
output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
}
} function writeString(view, offset, string) {
for (var i = 0; i < string.length; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
} function encodeWAV(samples) {
var buffer = new ArrayBuffer(44 + samples.length * 2);
var view = new DataView(buffer); /* RIFF identifier */
writeString(view, 0, 'RIFF');
/* RIFF chunk length */
view.setUint32(4, 36 + samples.length * 2, true);
/* RIFF type */
writeString(view, 8, 'WAVE');
/* format chunk identifier */
writeString(view, 12, 'fmt ');
/* format chunk length */
view.setUint32(16, 16, true);
/* sample format (raw) */
view.setUint16(20, 1, true);
/* channel count */
view.setUint16(22, numChannels, true);
/* sample rate */
view.setUint32(24, sampleRate, true);
/* byte rate (sample rate * block align) */
view.setUint32(28, sampleRate * 4, true);
/* block align (channel count * bytes per sample) */
view.setUint16(32, numChannels * 2, true);
/* bits per sample */
view.setUint16(34, 16, true);
/* data chunk identifier */
writeString(view, 36, 'data');
/* data chunk length */
view.setUint32(40, samples.length * 2, true); floatTo16BitPCM(view, 44, samples); return view;
}
}, self); this.worker.postMessage({
command: 'init',
config: {
sampleRate: this.context.sampleRate,
numChannels: this.config.numChannels
}
}); this.worker.onmessage = function (e) {
var cb = _this.callbacks[e.data.command].pop();
if (typeof cb == 'function') {
cb(e.data.data);
}
};
} _createClass(Recorder, [{
key: 'record',
value: function record() {
this.recording = true;
}
}, {
key: 'stop',
value: function stop() {
this.recording = false;
}
}, {
key: 'clear',
value: function clear() {
this.worker.postMessage({command: 'clear'});
}
}, {
key: 'getBuffer',
value: function getBuffer(cb) {
cb = cb || this.config.callback;
if (!cb) throw new Error('Callback not set'); this.callbacks.getBuffer.push(cb); this.worker.postMessage({command: 'getBuffer'});
}
}, {
key: 'exportWAV',
value: function exportWAV(cb, mimeType) {
mimeType = mimeType || this.config.mimeType;
cb = cb || this.config.callback;
if (!cb) throw new Error('Callback not set'); this.callbacks.exportWAV.push(cb); this.worker.postMessage({
command: 'exportWAV',
type: mimeType
});
}
}], [{
key: 'forceDownload',
value: function forceDownload() {
var url = (window.URL || window.webkitURL).createObjectURL(blob);
var link = window.document.createElement('a');
link.href = url;
link.download = filename || 'output.wav';
var click = document.createEvent("Event");
click.initEvent("click", true, true);
link.dispatchEvent(click);
}
}]); return Recorder;
})(); exports.default = Recorder; }, {"inline-worker": 3}], 3: [function (require, module, exports) {
"use strict"; module.exports = require("./inline-worker");
}, {"./inline-worker": 4}], 4: [function (require, module, exports) {
(function (global) {
"use strict"; var _createClass = (function () {
function defineProperties(target, props) {
for (var key in props) {
var prop = props[key];
prop.configurable = true;
if (prop.value) prop.writable = true;
}
Object.defineProperties(target, props);
} return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})(); var _classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}; var WORKER_ENABLED = !!(global === global.window && global.URL && global.Blob && global.Worker); var InlineWorker = (function () {
function InlineWorker(func, self) {
var _this = this; _classCallCheck(this, InlineWorker); if (WORKER_ENABLED) {
var functionBody = func.toString().trim().match(/^function\s*\w*\s*\([\w\s,]*\)\s*{([\w\W]*?)}$/)[1];
var url = global.URL.createObjectURL(new global.Blob([functionBody], {type: "text/javascript"})); return new global.Worker(url);
} this.self = self;
this.self.postMessage = function (data) {
setTimeout(function () {
_this.onmessage({data: data});
}, 0);
}; setTimeout(function () {
func.call(self);
}, 0);
} _createClass(InlineWorker, {
postMessage: {
value: function postMessage(data) {
var _this = this; setTimeout(function () {
_this.self.onmessage({data: data});
}, 0);
}
}
}); return InlineWorker;
})(); module.exports = InlineWorker;
}).call(this, typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
}, {}]
}, {}, [1])(1)
});

recorder.js的更多相关文章

  1. 【Recorder.js+百度语音识别】全栈方案技术细节

    项目中需要利用百度语音接口在Web端实现语音识别功能,采用了这样的技术方案,但实现时遇到了很多问题,发现网上大部分文章都只是在详解官方提供的example示例,对实际开发没有提供什么有价值的建议,而r ...

  2. recorder.js 基于H5录音功能

    recorder.js 基于HTML5的录音功能,输出格式为mp3文件. 前言 完全依赖H5原生API所涉及的API:WebRTC.AudioContext.Worker.Video/Audio AP ...

  3. 你需要了解的JS框架

    excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js       用途:构建数据统计图表,兼容多浏览器 ...

  4. 前端开发需要了解的JS插件

    excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 jquer ...

  5. electron项目中使用js web worker时,new worker(path)路径问题

    如题,在new worker时需要传入js文件路径,可是在electron环境中使用出现问.同目录下,recorder.jsworker.js recorder.js中调用 var path = '. ...

  6. 动手做第一个Chrome插件

    Chrome插件是令人惊讶的简单,一旦你弄懂它的工作和实现原理.它是由一部分HTML,一部分Js,然后混合了一个叫做manifest.json的Json文件组合而成的整体.这意味着你可以使用你最擅长的 ...

  7. HTML5录音控件

    最近的项目又需要用到录音,年前有过调研,再次翻出来使用,这里做一个记录. HTML5提供了录音支持,因此可以方便使用HTML5来录音,来实现录音.语音识别等功能,语音开发必备.但是ES标准提供的API ...

  8. 人工智能初识(百度ai)

    目前的人工智能做了什么? 语音识别:小米的小爱同学,苹果的siri,微软的Cortana语音合成:小米的小爱同学,苹果的siri,微软的Cortana图像识别:交通摄像头拍违章,刷脸解锁手机等视频识别 ...

  9. python全栈开发day115、116-websocket、websocket原理、websocket加解密、简单问答机器人实现

    1.websocket 1.websocket 与轮询 轮询: 不断向服务器发起询问,服务器还不断的回复 浪费带宽,浪费前后端资源 保证数据的实时性 长轮询: 1.客户端向服务器发起消息,服务端轮询, ...

随机推荐

  1. 修改 hostname

    1.修改hostname hostname是一个kernel变量,可以通过hostname命令来查看本机的hostname.也可以直接cat /proc/sys/kernel/hostname查看. ...

  2. ibatis-java.lang.RuntimeException: Error setting property 'setFileSize'

    ibatis查询问题:      ibatis-java.lang.RuntimeException: Error setting property 'setFileSize'

  3. wamp通过phpMyAdmin修改登录密码

    初始安装wamp后,默认mysql是没有密码的,这个时候如果想要修改密码,可以按照以下步骤进行: 第一.打开phpMyAdmin,看到界面如图所示: 第二.通过导航找到“用户”,再找到“编辑权限”进行 ...

  4. python模块学习之__future__

    每个程序在更新版本之后,都会相较于上一个版本有所改动.这些改动会给程序的使用者造成不小的困扰! 比如python2.xx和3.xx的改动,足够让新手们头大了! 在3中, u'xxx'和'xxx'都是字 ...

  5. javascript执行环境以及作用域链的理解

    在javascript脚步语言中执行环境有两种: 全局环境: 局部环境: 我们可以拿一个田径跑道来打比方,全局环境就可以理解为是最外面跑道,它包含着内部所有的东西,有人在跑步,有人在跳远,这些用着不同 ...

  6. Netbeans取消CTRL+\才能代码提示的方法

    更好的方法: ------------------------------------------------------------ 边打字边按CTRL+\进行代码提示的方法我TM也是醉了 简而言之 ...

  7. 使用Charles对Https请求进行抓包

    昨天对某个APP做分析的时候发现其请求是Https的,抓包工具不能正常的显示请求的内容及返回的情况.通过搜索发现Charles是支持针对Https抓包的.具体的操作如下: 1.电脑端安装SSL证书 2 ...

  8. 如何实现uboot和linux之间的参数传递

    参考http://cgxcn.blog.163.com/blog/static/132312422009101133251202/ 参考:http://blog.chinaunix.net/uid-1 ...

  9. mac下安装phpstorm主题

    在<推荐一个phpstorm主题和字体>中介绍了window下如何安装phpstorm主题,这里我们在介绍一下如何在MAC下安装phpstorm主题. 安装方法和window类似,只是需要 ...

  10. 如何添加、删除、合并PDF文件里的页面?

    使用Adobe Acrobat. Adobe中文官网http://www.adobe.com/cn/products/acrobat.html 能够自己下载破解版. watermark/2/text/ ...