@coze/uniapp-api
Version:
Official Coze UniApp SDK for seamless AI integration into your applications | 扣子官方 UniApp SDK,助您轻松集成 AI 能力到应用中
214 lines (213 loc) • 6.77 kB
JavaScript
"use strict";
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* PcmRecorder for WeChat Mini Program
* Records audio using the uni.getRecorderManager API and provides PCM data
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PcmRecorder = exports.RecordingStatus = void 0;
/**
* Recording status types
*/
var RecordingStatus;
(function (RecordingStatus) {
RecordingStatus["IDLE"] = "idle";
RecordingStatus["RECORDING"] = "recording";
RecordingStatus["PAUSED"] = "paused";
})(RecordingStatus || (exports.RecordingStatus = RecordingStatus = {}));
/**
* PcmRecorder class for WeChat Mini Program
* Simplified version without AI denoising and other advanced features
*/
class PcmRecorder {
/**
* Creates a new PcmRecorder instance
* @param {PcmRecorderConfig} config - Configuration options
*/
constructor(config = {}) {
/**
* The recorder manager instance from uni API
*/
Object.defineProperty(this, "recorderManager", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Current recording status
*/
Object.defineProperty(this, "status", {
enumerable: true,
configurable: true,
writable: true,
value: RecordingStatus.IDLE
});
/**
* Callback function for PCM audio data
*/
Object.defineProperty(this, "pcmAudioCallback", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Configuration for the recorder
*/
Object.defineProperty(this, "config", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
// Merge provided config with defaults
this.config = Object.assign({}, config);
// Initialize the recorder manager
this.recorderManager = uni.getRecorderManager();
this.setupEventListeners();
this.log('PcmRecorder initialized');
}
/**
* Set up event listeners for the recorder manager
* @private
*/
setupEventListeners() {
// Handle frame buffer events (PCM data chunks)
this.recorderManager.onFrameRecorded(({ frameBuffer, isLastFrame }) => {
// this.log(
// 'Frame recorded',
// isLastFrame ? '(last frame)' : '',
// frameBuffer.byteLength,
// );
var _a;
if (this.status === RecordingStatus.RECORDING) {
// Pass the PCM data to callback
(_a = this.pcmAudioCallback) === null || _a === void 0 ? void 0 : _a.call(this, { raw: frameBuffer });
}
if (isLastFrame) {
this.status = RecordingStatus.IDLE;
}
});
// Handle errors
this.recorderManager.onError(error => {
console.error('Recording error:', error);
this.status = RecordingStatus.IDLE;
});
// Handle recording stop
this.recorderManager.onStop(() => {
this.log('Recording stopped');
this.status = RecordingStatus.IDLE;
});
// Handle recording pause
this.recorderManager.onPause(() => {
this.log('Recording paused');
this.status = RecordingStatus.PAUSED;
});
// Note: RecorderManager does not have an onResume event in WeChat Mini Program
// We'll handle resume status manually in the resume() method
// Handle recording start
this.recorderManager.onStart(() => {
this.log('Recording started');
this.status = RecordingStatus.RECORDING;
});
}
/**
* Start recording audio
*/
start() {
if (this.status !== RecordingStatus.IDLE) {
this.log('Cannot start recording: already in progress');
return;
}
const options = {
duration: 600000, // 10 minutes max
sampleRate: this.config.sampleRate,
numberOfChannels: 1,
format: 'PCM', // Always PCM for our use case
frameSize: 2,
};
try {
this.recorderManager.start(options);
this.log('Recording started with options:', options);
}
catch (error) {
console.error('Failed to start recording:', error);
throw new Error('Failed to start recording');
}
}
/**
* Start recording and register callbacks
* @param {object} params - Parameters containing callbacks
* @param {function} params.pcmAudioCallback - Callback for PCM audio data
*/
record({ pcmAudioCallback, } = {}) {
// Register the callback
this.pcmAudioCallback = pcmAudioCallback;
}
/**
* Pause recording temporarily
*/
pause() {
if (this.status === RecordingStatus.RECORDING) {
this.recorderManager.pause();
this.log('Recording paused');
}
else {
this.log('Cannot pause: not recording');
}
}
/**
* Resume recording after pause
*/
resume() {
if (this.status === RecordingStatus.PAUSED) {
this.recorderManager.resume();
// Manually update status since there's no onResume event
this.status = RecordingStatus.RECORDING;
this.log('Recording resumed');
}
else {
this.log('Cannot resume: not paused');
}
}
/**
* Stop recording and clean up resources
*/
destroy() {
// Stop recording if in progress
if (this.status !== RecordingStatus.IDLE) {
this.recorderManager.stop();
}
// Clear callbacks
this.pcmAudioCallback = undefined;
this.status = RecordingStatus.IDLE;
this.log('Recorder destroyed');
}
/**
* Get current recording status
* @returns {string} - Current status: 'idle', 'recording', or 'paused'
*/
getStatus() {
return this.status;
}
/**
* Get current sample rate
* @returns {number} - Sample rate in Hz
*/
getSampleRate() {
return this.config.sampleRate || 16000;
}
/**
* Log messages when debug is enabled
* @private
*/
log(...args) {
if (this.config.debug) {
console.log('[PcmRecorder]', ...args);
}
}
}
exports.PcmRecorder = PcmRecorder;
// Export for use with import {PcmRecorder} from '@coze/uniapp-api/ws-tools'
exports.default = PcmRecorder;