Giter VIP home page Giter VIP logo

recorder's Introduction

@micahfang/recorder

录制音频文件

安装

npm install -S @micahfang/recorder

使用

import Recorder from "@micahfang/recorder";
// 或 import {Recorder} from '@micahfang/recorder'

const options = {
  sampleBits: 16, // 采样位数 8, 16
  sampleRate: 16000, // 采样率16khz 16000
  bufferSize: 2048, // 录音缓存大小 1024/2048/4096
  inputChannels: 1, // 输入声道数
  outputChannels: 1, // 输出声道数
  inputSampleRate: 48000, // 音频输入的采样率
};

const recorder = new Recorder(options);

recorder.check(); // 检查浏览器是否能够录音

const startRecord = async () => {
  await recorder.initAudio(); // 初始化录音 track
  recorder.start(); // 开始录音
};

const stopRecord = async () => {
  recorder.stop(); // 结束录音

  const bytes = recorder.getAllBytes();
  const base64 = await recorder.toPCMBase64(bytes);
  // base64

  recorder.destroy(); // 销毁 track
};

API

const recorder = new Recorder(config:)

  • ** config:RecorderOptions **

    • sampleBits: number 采样位数 8, 16 默认: 16
    • sampleRate: number 采样率 16khz 16000 8khz 8000, 默认 16000
    • bufferSize: number 录音缓存大小 1024/2048/4096 默认 2048
    • inputChannels: number 输入声道数 默认 1
    • outputChannels: number 输出声道数 默认 1
    • inputSampleRate: number 音频输入的采样率 默认 48000
  • Recorder.check() 检查浏览器是否支持录音,返回 boolean

  • recorder.initAudio(): Promise<undefined> 初始化录音

  • recorder.start(): void 开始录音

  • recorder.stop(): void 结束录音

  • recorder.destroy(): void 销毁 tracks,用于关闭浏览器录音

  • recorder.getConfig(): RecorderOptions 获取 recorder 的配置

  • recorder.clearBuffer(): void 清空缓存

  • recorder.getAllBytes(): Blob 返回缓存中的所有数据

  • recorder.getBufferBytes(index: number): Blob 获取单个分片的数据

  • recorder.encodeWavBlob(bytes: Blob): Blob 将音频流转化成 wav 文件

  • recorder.toPCMDataView(bytes: Blob): DataView 将 Blob 数据 转化成 pcm DataView

  • recorder.toPCMBase64(bytes: Blob): string 将 Blob 数据转化成 Base64 字符串

recorder's People

Contributors

micahfang avatar

Watchers

 avatar

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.