Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/micahfang/recorder
录制音频,可转成 PCM 或者 WAV 格式
https://github.com/micahfang/recorder
Last synced: 3 days ago
JSON representation
录制音频,可转成 PCM 或者 WAV 格式
- Host: GitHub
- URL: https://github.com/micahfang/recorder
- Owner: MICAHFANG
- Created: 2023-08-25T06:15:23.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-09T06:22:26.000Z (over 1 year ago)
- Last Synced: 2024-12-14T19:39:27.211Z (about 1 month ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @micahfang/recorder
录制音频文件
## 安装
```
npm install -S @micahfang/recorder
```## 使用
```javascript
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);
// base64recorder.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` 初始化录音
- `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 字符串