@lightsoft/js-sdk 1.2.3 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +47 -2
- package/dist/index.js +269 -1
- package/dist/index.umd.js +269 -0
- package/dist/types/utils/auido.d.ts +28 -6
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/recorder.d.ts +45 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -306,6 +306,51 @@ declare class StreamRequest {
|
|
|
306
306
|
private onError;
|
|
307
307
|
}
|
|
308
308
|
|
|
309
|
+
interface AudioRecorderOptions {
|
|
310
|
+
media: MediaStreamConstraints;
|
|
311
|
+
mediaRecorder?: MediaRecorderOptions;
|
|
312
|
+
maxTime?: number;
|
|
313
|
+
}
|
|
314
|
+
interface TimerOptions {
|
|
315
|
+
next?: () => void;
|
|
316
|
+
end?: () => void;
|
|
317
|
+
}
|
|
318
|
+
interface AudioInfo {
|
|
319
|
+
sampleRate: number;
|
|
320
|
+
channels: number;
|
|
321
|
+
format: string | null;
|
|
322
|
+
bitDepth: number;
|
|
323
|
+
}
|
|
324
|
+
declare class AudioRecorder {
|
|
325
|
+
private options;
|
|
326
|
+
private stream;
|
|
327
|
+
private mediaRecorder;
|
|
328
|
+
private audioChunks;
|
|
329
|
+
private audioInfo;
|
|
330
|
+
private timer;
|
|
331
|
+
private timerCount;
|
|
332
|
+
private stopCallback;
|
|
333
|
+
private timerCallback;
|
|
334
|
+
private timerEndCallback;
|
|
335
|
+
constructor(options: AudioRecorderOptions);
|
|
336
|
+
start(): Promise<void>;
|
|
337
|
+
stop(): void;
|
|
338
|
+
reset(): void;
|
|
339
|
+
onStop(callback: (audioChunks: Blob[]) => void): void;
|
|
340
|
+
startTimer(options?: TimerOptions): void;
|
|
341
|
+
clearTimer(): void;
|
|
342
|
+
onTimer(callback: (count: number) => void): void;
|
|
343
|
+
onTimerEnd(callback: () => void): void;
|
|
344
|
+
getAudioBlob(): Blob | null;
|
|
345
|
+
getAudioUrl(): string;
|
|
346
|
+
getAudioBase64(blob: Blob): Promise<string>;
|
|
347
|
+
getAudioInfo(blob: Blob): Promise<unknown>;
|
|
348
|
+
getBitDepth(audioContext: AudioContext, blob: Blob): Promise<number>;
|
|
349
|
+
download(blob: Blob): null | undefined;
|
|
350
|
+
getExtension(mimeType: string): string;
|
|
351
|
+
upload(url: string, options?: RequestInit): Promise<any>;
|
|
352
|
+
}
|
|
353
|
+
|
|
309
354
|
/**
|
|
310
355
|
* 匹配中文字母数字
|
|
311
356
|
*/
|
|
@@ -331,5 +376,5 @@ declare const email: RegExp;
|
|
|
331
376
|
*/
|
|
332
377
|
declare const url: RegExp;
|
|
333
378
|
|
|
334
|
-
export { HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, createWebSocket, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getState, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useClose, useCountDown, useDownload, useOnMessage, useSendMessage, zhLetterNumber };
|
|
335
|
-
export type { ISectoralTable, Tag, typeIsNull };
|
|
379
|
+
export { AudioRecorder, HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, createWebSocket, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getState, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useClose, useCountDown, useDownload, useOnMessage, useSendMessage, zhLetterNumber };
|
|
380
|
+
export type { AudioInfo, AudioRecorderOptions, ISectoralTable, Tag, typeIsNull };
|
package/dist/index.js
CHANGED
|
@@ -2977,6 +2977,274 @@ var StreamRequest = /** @class */ (function () {
|
|
|
2977
2977
|
return StreamRequest;
|
|
2978
2978
|
}());
|
|
2979
2979
|
|
|
2980
|
+
var AudioRecorder = /** @class */ (function () {
|
|
2981
|
+
function AudioRecorder(options) {
|
|
2982
|
+
this.options = {
|
|
2983
|
+
media: {
|
|
2984
|
+
audio: {
|
|
2985
|
+
channelCount: { ideal: 2 },
|
|
2986
|
+
sampleRate: { ideal: 44100 },
|
|
2987
|
+
echoCancellation: true,
|
|
2988
|
+
}
|
|
2989
|
+
},
|
|
2990
|
+
mediaRecorder: {
|
|
2991
|
+
mimeType: 'audio/mp4;codecs=mp4a.40.2',
|
|
2992
|
+
},
|
|
2993
|
+
maxTime: 0,
|
|
2994
|
+
};
|
|
2995
|
+
this.stream = null;
|
|
2996
|
+
this.mediaRecorder = null;
|
|
2997
|
+
this.audioChunks = [];
|
|
2998
|
+
this.audioInfo = {
|
|
2999
|
+
sampleRate: 0,
|
|
3000
|
+
channels: 0,
|
|
3001
|
+
format: null,
|
|
3002
|
+
bitDepth: 16,
|
|
3003
|
+
};
|
|
3004
|
+
this.timer = null;
|
|
3005
|
+
this.timerCount = 0;
|
|
3006
|
+
this.stopCallback = null;
|
|
3007
|
+
this.timerCallback = null;
|
|
3008
|
+
this.timerEndCallback = null;
|
|
3009
|
+
this.audioChunks = [];
|
|
3010
|
+
this.options = options;
|
|
3011
|
+
}
|
|
3012
|
+
// 开始录音
|
|
3013
|
+
AudioRecorder.prototype.start = function () {
|
|
3014
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3015
|
+
var _a, media, mediaRecorder, _b, maxTime, _c, error_1;
|
|
3016
|
+
var _this = this;
|
|
3017
|
+
return __generator(this, function (_d) {
|
|
3018
|
+
switch (_d.label) {
|
|
3019
|
+
case 0:
|
|
3020
|
+
_d.trys.push([0, 2, , 3]);
|
|
3021
|
+
_a = this.options, media = _a.media, mediaRecorder = _a.mediaRecorder, _b = _a.maxTime, maxTime = _b === void 0 ? 0 : _b;
|
|
3022
|
+
_c = this;
|
|
3023
|
+
return [4 /*yield*/, navigator.mediaDevices.getUserMedia({ audio: media.audio })];
|
|
3024
|
+
case 1:
|
|
3025
|
+
_c.stream = _d.sent();
|
|
3026
|
+
this.mediaRecorder = new MediaRecorder(this.stream, mediaRecorder);
|
|
3027
|
+
this.mediaRecorder.ondataavailable = function (event) {
|
|
3028
|
+
_this.audioInfo.format = event.data.type;
|
|
3029
|
+
_this.audioChunks.push(event.data);
|
|
3030
|
+
};
|
|
3031
|
+
this.mediaRecorder.onstop = function () {
|
|
3032
|
+
var _a;
|
|
3033
|
+
(_a = _this.stopCallback) === null || _a === void 0 ? void 0 : _a.call(_this, _this.audioChunks);
|
|
3034
|
+
_this.reset();
|
|
3035
|
+
};
|
|
3036
|
+
this.mediaRecorder.start();
|
|
3037
|
+
if (maxTime > 0) {
|
|
3038
|
+
this.startTimer({
|
|
3039
|
+
end: function () {
|
|
3040
|
+
_this.stop();
|
|
3041
|
+
}
|
|
3042
|
+
});
|
|
3043
|
+
}
|
|
3044
|
+
return [3 /*break*/, 3];
|
|
3045
|
+
case 2:
|
|
3046
|
+
error_1 = _d.sent();
|
|
3047
|
+
throw new Error(error_1);
|
|
3048
|
+
case 3: return [2 /*return*/];
|
|
3049
|
+
}
|
|
3050
|
+
});
|
|
3051
|
+
});
|
|
3052
|
+
};
|
|
3053
|
+
// 停止录音
|
|
3054
|
+
AudioRecorder.prototype.stop = function () {
|
|
3055
|
+
var _a;
|
|
3056
|
+
if (this.mediaRecorder) {
|
|
3057
|
+
this.reset();
|
|
3058
|
+
(_a = this.timerEndCallback) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
3059
|
+
this.mediaRecorder.stop();
|
|
3060
|
+
}
|
|
3061
|
+
};
|
|
3062
|
+
AudioRecorder.prototype.reset = function () {
|
|
3063
|
+
var _a, _b;
|
|
3064
|
+
this.clearTimer();
|
|
3065
|
+
this.audioChunks = [];
|
|
3066
|
+
(_b = (_a = this.stream) === null || _a === void 0 ? void 0 : _a.getTracks()) === null || _b === void 0 ? void 0 : _b.forEach(function (track) { return track.stop(); });
|
|
3067
|
+
};
|
|
3068
|
+
AudioRecorder.prototype.onStop = function (callback) {
|
|
3069
|
+
this.stopCallback = callback;
|
|
3070
|
+
};
|
|
3071
|
+
AudioRecorder.prototype.startTimer = function (options) {
|
|
3072
|
+
var _this = this;
|
|
3073
|
+
var _a = options || {}, next = _a.next, end = _a.end;
|
|
3074
|
+
this.timer = setInterval(function () {
|
|
3075
|
+
var _a, _b;
|
|
3076
|
+
next === null || next === void 0 ? void 0 : next();
|
|
3077
|
+
(_a = _this.timerCallback) === null || _a === void 0 ? void 0 : _a.call(_this, _this.timerCount);
|
|
3078
|
+
if (_this.timerCount === _this.options.maxTime) {
|
|
3079
|
+
_this.clearTimer();
|
|
3080
|
+
end === null || end === void 0 ? void 0 : end();
|
|
3081
|
+
(_b = _this.timerEndCallback) === null || _b === void 0 ? void 0 : _b.call(_this);
|
|
3082
|
+
}
|
|
3083
|
+
_this.timerCount++;
|
|
3084
|
+
}, 1000);
|
|
3085
|
+
};
|
|
3086
|
+
// 清除定时器
|
|
3087
|
+
AudioRecorder.prototype.clearTimer = function () {
|
|
3088
|
+
if (this.timer)
|
|
3089
|
+
clearInterval(this.timer);
|
|
3090
|
+
this.timer = null;
|
|
3091
|
+
this.timerCount = 0;
|
|
3092
|
+
};
|
|
3093
|
+
AudioRecorder.prototype.onTimer = function (callback) {
|
|
3094
|
+
this.timerCallback = callback;
|
|
3095
|
+
};
|
|
3096
|
+
AudioRecorder.prototype.onTimerEnd = function (callback) {
|
|
3097
|
+
this.timerEndCallback = callback;
|
|
3098
|
+
};
|
|
3099
|
+
AudioRecorder.prototype.getAudioBlob = function () {
|
|
3100
|
+
if (!this.mediaRecorder)
|
|
3101
|
+
return null;
|
|
3102
|
+
var type = this.getExtension(this.mediaRecorder.mimeType);
|
|
3103
|
+
return new Blob(this.audioChunks, { type: "audio/".concat(type) });
|
|
3104
|
+
};
|
|
3105
|
+
AudioRecorder.prototype.getAudioUrl = function () {
|
|
3106
|
+
var blob = this.getAudioBlob();
|
|
3107
|
+
if (!blob)
|
|
3108
|
+
return '';
|
|
3109
|
+
return URL.createObjectURL(blob);
|
|
3110
|
+
};
|
|
3111
|
+
AudioRecorder.prototype.getAudioBase64 = function (blob) {
|
|
3112
|
+
return new Promise(function (resolve, reject) {
|
|
3113
|
+
var reader = new FileReader();
|
|
3114
|
+
reader.onerror = reject;
|
|
3115
|
+
reader.onload = function () { return resolve(reader.result); };
|
|
3116
|
+
reader.readAsDataURL(blob);
|
|
3117
|
+
});
|
|
3118
|
+
};
|
|
3119
|
+
AudioRecorder.prototype.getAudioInfo = function (blob) {
|
|
3120
|
+
var _this = this;
|
|
3121
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
3122
|
+
var audioContext, source, analyser, sampleRate, channels, bitDepth, format, error_2;
|
|
3123
|
+
var _a, _b;
|
|
3124
|
+
return __generator(this, function (_c) {
|
|
3125
|
+
switch (_c.label) {
|
|
3126
|
+
case 0:
|
|
3127
|
+
_c.trys.push([0, 2, , 3]);
|
|
3128
|
+
audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
3129
|
+
source = audioContext.createMediaStreamSource(this.stream);
|
|
3130
|
+
analyser = audioContext.createAnalyser();
|
|
3131
|
+
source.connect(analyser);
|
|
3132
|
+
sampleRate = audioContext.sampleRate;
|
|
3133
|
+
channels = ((_a = this.stream) === null || _a === void 0 ? void 0 : _a.getAudioTracks()[0].getSettings().channelCount) || 0;
|
|
3134
|
+
return [4 /*yield*/, this.getBitDepth(audioContext, blob)];
|
|
3135
|
+
case 1:
|
|
3136
|
+
bitDepth = _c.sent();
|
|
3137
|
+
format = this.getExtension((_b = this.mediaRecorder) === null || _b === void 0 ? void 0 : _b.mimeType);
|
|
3138
|
+
resolve(__assign(__assign({}, this.audioInfo), { sampleRate: sampleRate, channels: channels, bitDepth: bitDepth, format: format }));
|
|
3139
|
+
return [3 /*break*/, 3];
|
|
3140
|
+
case 2:
|
|
3141
|
+
error_2 = _c.sent();
|
|
3142
|
+
reject(error_2);
|
|
3143
|
+
return [3 /*break*/, 3];
|
|
3144
|
+
case 3: return [2 /*return*/];
|
|
3145
|
+
}
|
|
3146
|
+
});
|
|
3147
|
+
}); });
|
|
3148
|
+
};
|
|
3149
|
+
AudioRecorder.prototype.getBitDepth = function (audioContext, blob) {
|
|
3150
|
+
var _this = this;
|
|
3151
|
+
return new Promise(function (resolve, reject) {
|
|
3152
|
+
var reader = new FileReader();
|
|
3153
|
+
reader.onload = function (event) { return __awaiter(_this, void 0, void 0, function () {
|
|
3154
|
+
var arrayBuffer, channelData, maxAmplitude, i, isFloat;
|
|
3155
|
+
var _a;
|
|
3156
|
+
return __generator(this, function (_b) {
|
|
3157
|
+
switch (_b.label) {
|
|
3158
|
+
case 0: return [4 /*yield*/, audioContext.decodeAudioData((_a = event.target) === null || _a === void 0 ? void 0 : _a.result)];
|
|
3159
|
+
case 1:
|
|
3160
|
+
arrayBuffer = _b.sent();
|
|
3161
|
+
if (!arrayBuffer) {
|
|
3162
|
+
reject({
|
|
3163
|
+
error: '未能读取 Blob 数据'
|
|
3164
|
+
});
|
|
3165
|
+
}
|
|
3166
|
+
channelData = arrayBuffer.getChannelData(0);
|
|
3167
|
+
maxAmplitude = 0;
|
|
3168
|
+
for (i = 0; i < channelData.length; i++) {
|
|
3169
|
+
maxAmplitude = Math.max(maxAmplitude, Math.abs(channelData[i]));
|
|
3170
|
+
}
|
|
3171
|
+
isFloat = channelData.some(function (value) {
|
|
3172
|
+
return Math.abs(value) > 1.0 ||
|
|
3173
|
+
value.toString().includes('.');
|
|
3174
|
+
});
|
|
3175
|
+
resolve(isFloat ? 32 : 16);
|
|
3176
|
+
return [2 /*return*/];
|
|
3177
|
+
}
|
|
3178
|
+
});
|
|
3179
|
+
}); };
|
|
3180
|
+
reader.readAsArrayBuffer(blob);
|
|
3181
|
+
});
|
|
3182
|
+
};
|
|
3183
|
+
AudioRecorder.prototype.download = function (blob) {
|
|
3184
|
+
var _a;
|
|
3185
|
+
if (!blob)
|
|
3186
|
+
return null;
|
|
3187
|
+
var format = this.getExtension((_a = this.mediaRecorder) === null || _a === void 0 ? void 0 : _a.mimeType);
|
|
3188
|
+
var fileName = "recording_".concat(Date.now() + Math.random().toString(36).substring(2), ".").concat(format);
|
|
3189
|
+
var url = URL.createObjectURL(blob);
|
|
3190
|
+
var a = document.createElement('a');
|
|
3191
|
+
a.style.display = 'none';
|
|
3192
|
+
a.href = url;
|
|
3193
|
+
a.download = fileName;
|
|
3194
|
+
a.click();
|
|
3195
|
+
URL.revokeObjectURL(url);
|
|
3196
|
+
};
|
|
3197
|
+
AudioRecorder.prototype.getExtension = function (mimeType) {
|
|
3198
|
+
var mimeToExt = {
|
|
3199
|
+
'audio/webm': 'webm',
|
|
3200
|
+
'audio/webm;codecs=opus': 'webm',
|
|
3201
|
+
'audio/ogg': 'ogg',
|
|
3202
|
+
'audio/ogg;codecs=opus': 'ogg',
|
|
3203
|
+
'audio/mp4': 'mp4',
|
|
3204
|
+
'audio/mpeg': 'mp3',
|
|
3205
|
+
'audio/wav': 'wav',
|
|
3206
|
+
};
|
|
3207
|
+
if (mimeToExt[mimeType])
|
|
3208
|
+
return mimeToExt[mimeType];
|
|
3209
|
+
var mainType = mimeType.split(';')[0];
|
|
3210
|
+
return mimeToExt[mainType] || 'audio';
|
|
3211
|
+
};
|
|
3212
|
+
// 上传音频到服务器
|
|
3213
|
+
AudioRecorder.prototype.upload = function (url, options) {
|
|
3214
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
3215
|
+
var audioBlob, formData, format, fileName, response, data, error_3;
|
|
3216
|
+
var _a;
|
|
3217
|
+
return __generator(this, function (_b) {
|
|
3218
|
+
switch (_b.label) {
|
|
3219
|
+
case 0:
|
|
3220
|
+
audioBlob = this.getAudioBlob();
|
|
3221
|
+
if (!audioBlob)
|
|
3222
|
+
return [2 /*return*/, null];
|
|
3223
|
+
formData = new FormData();
|
|
3224
|
+
format = this.getExtension((_a = this.mediaRecorder) === null || _a === void 0 ? void 0 : _a.mimeType);
|
|
3225
|
+
fileName = "recording_".concat(Date.now() + Math.random().toString(36).substring(2), ".").concat(format);
|
|
3226
|
+
formData.append('audio', audioBlob, fileName);
|
|
3227
|
+
_b.label = 1;
|
|
3228
|
+
case 1:
|
|
3229
|
+
_b.trys.push([1, 4, , 5]);
|
|
3230
|
+
return [4 /*yield*/, fetch(url, options)];
|
|
3231
|
+
case 2:
|
|
3232
|
+
response = _b.sent();
|
|
3233
|
+
return [4 /*yield*/, response.json()];
|
|
3234
|
+
case 3:
|
|
3235
|
+
data = _b.sent();
|
|
3236
|
+
return [2 /*return*/, data];
|
|
3237
|
+
case 4:
|
|
3238
|
+
error_3 = _b.sent();
|
|
3239
|
+
throw new Error(error_3);
|
|
3240
|
+
case 5: return [2 /*return*/];
|
|
3241
|
+
}
|
|
3242
|
+
});
|
|
3243
|
+
});
|
|
3244
|
+
};
|
|
3245
|
+
return AudioRecorder;
|
|
3246
|
+
}());
|
|
3247
|
+
|
|
2980
3248
|
/**
|
|
2981
3249
|
* 匹配中文字母数字
|
|
2982
3250
|
*/
|
|
@@ -3002,4 +3270,4 @@ var email = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$
|
|
|
3002
3270
|
*/
|
|
3003
3271
|
var url = /^https?:\/\/[^\s/$.?#].[^\s]*$/;
|
|
3004
3272
|
|
|
3005
|
-
export { HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useCountDown, useDownload, zhLetterNumber };
|
|
3273
|
+
export { AudioRecorder, HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useCountDown, useDownload, zhLetterNumber };
|
package/dist/index.umd.js
CHANGED
|
@@ -1184,6 +1184,274 @@
|
|
|
1184
1184
|
return StreamRequest;
|
|
1185
1185
|
}());
|
|
1186
1186
|
|
|
1187
|
+
var AudioRecorder = /** @class */ (function () {
|
|
1188
|
+
function AudioRecorder(options) {
|
|
1189
|
+
this.options = {
|
|
1190
|
+
media: {
|
|
1191
|
+
audio: {
|
|
1192
|
+
channelCount: { ideal: 2 },
|
|
1193
|
+
sampleRate: { ideal: 44100 },
|
|
1194
|
+
echoCancellation: true,
|
|
1195
|
+
}
|
|
1196
|
+
},
|
|
1197
|
+
mediaRecorder: {
|
|
1198
|
+
mimeType: 'audio/mp4;codecs=mp4a.40.2',
|
|
1199
|
+
},
|
|
1200
|
+
maxTime: 0,
|
|
1201
|
+
};
|
|
1202
|
+
this.stream = null;
|
|
1203
|
+
this.mediaRecorder = null;
|
|
1204
|
+
this.audioChunks = [];
|
|
1205
|
+
this.audioInfo = {
|
|
1206
|
+
sampleRate: 0,
|
|
1207
|
+
channels: 0,
|
|
1208
|
+
format: null,
|
|
1209
|
+
bitDepth: 16,
|
|
1210
|
+
};
|
|
1211
|
+
this.timer = null;
|
|
1212
|
+
this.timerCount = 0;
|
|
1213
|
+
this.stopCallback = null;
|
|
1214
|
+
this.timerCallback = null;
|
|
1215
|
+
this.timerEndCallback = null;
|
|
1216
|
+
this.audioChunks = [];
|
|
1217
|
+
this.options = options;
|
|
1218
|
+
}
|
|
1219
|
+
// 开始录音
|
|
1220
|
+
AudioRecorder.prototype.start = function () {
|
|
1221
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1222
|
+
var _a, media, mediaRecorder, _b, maxTime, _c, error_1;
|
|
1223
|
+
var _this = this;
|
|
1224
|
+
return __generator(this, function (_d) {
|
|
1225
|
+
switch (_d.label) {
|
|
1226
|
+
case 0:
|
|
1227
|
+
_d.trys.push([0, 2, , 3]);
|
|
1228
|
+
_a = this.options, media = _a.media, mediaRecorder = _a.mediaRecorder, _b = _a.maxTime, maxTime = _b === void 0 ? 0 : _b;
|
|
1229
|
+
_c = this;
|
|
1230
|
+
return [4 /*yield*/, navigator.mediaDevices.getUserMedia({ audio: media.audio })];
|
|
1231
|
+
case 1:
|
|
1232
|
+
_c.stream = _d.sent();
|
|
1233
|
+
this.mediaRecorder = new MediaRecorder(this.stream, mediaRecorder);
|
|
1234
|
+
this.mediaRecorder.ondataavailable = function (event) {
|
|
1235
|
+
_this.audioInfo.format = event.data.type;
|
|
1236
|
+
_this.audioChunks.push(event.data);
|
|
1237
|
+
};
|
|
1238
|
+
this.mediaRecorder.onstop = function () {
|
|
1239
|
+
var _a;
|
|
1240
|
+
(_a = _this.stopCallback) === null || _a === void 0 ? void 0 : _a.call(_this, _this.audioChunks);
|
|
1241
|
+
_this.reset();
|
|
1242
|
+
};
|
|
1243
|
+
this.mediaRecorder.start();
|
|
1244
|
+
if (maxTime > 0) {
|
|
1245
|
+
this.startTimer({
|
|
1246
|
+
end: function () {
|
|
1247
|
+
_this.stop();
|
|
1248
|
+
}
|
|
1249
|
+
});
|
|
1250
|
+
}
|
|
1251
|
+
return [3 /*break*/, 3];
|
|
1252
|
+
case 2:
|
|
1253
|
+
error_1 = _d.sent();
|
|
1254
|
+
throw new Error(error_1);
|
|
1255
|
+
case 3: return [2 /*return*/];
|
|
1256
|
+
}
|
|
1257
|
+
});
|
|
1258
|
+
});
|
|
1259
|
+
};
|
|
1260
|
+
// 停止录音
|
|
1261
|
+
AudioRecorder.prototype.stop = function () {
|
|
1262
|
+
var _a;
|
|
1263
|
+
if (this.mediaRecorder) {
|
|
1264
|
+
this.reset();
|
|
1265
|
+
(_a = this.timerEndCallback) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
1266
|
+
this.mediaRecorder.stop();
|
|
1267
|
+
}
|
|
1268
|
+
};
|
|
1269
|
+
AudioRecorder.prototype.reset = function () {
|
|
1270
|
+
var _a, _b;
|
|
1271
|
+
this.clearTimer();
|
|
1272
|
+
this.audioChunks = [];
|
|
1273
|
+
(_b = (_a = this.stream) === null || _a === void 0 ? void 0 : _a.getTracks()) === null || _b === void 0 ? void 0 : _b.forEach(function (track) { return track.stop(); });
|
|
1274
|
+
};
|
|
1275
|
+
AudioRecorder.prototype.onStop = function (callback) {
|
|
1276
|
+
this.stopCallback = callback;
|
|
1277
|
+
};
|
|
1278
|
+
AudioRecorder.prototype.startTimer = function (options) {
|
|
1279
|
+
var _this = this;
|
|
1280
|
+
var _a = options || {}, next = _a.next, end = _a.end;
|
|
1281
|
+
this.timer = setInterval(function () {
|
|
1282
|
+
var _a, _b;
|
|
1283
|
+
next === null || next === void 0 ? void 0 : next();
|
|
1284
|
+
(_a = _this.timerCallback) === null || _a === void 0 ? void 0 : _a.call(_this, _this.timerCount);
|
|
1285
|
+
if (_this.timerCount === _this.options.maxTime) {
|
|
1286
|
+
_this.clearTimer();
|
|
1287
|
+
end === null || end === void 0 ? void 0 : end();
|
|
1288
|
+
(_b = _this.timerEndCallback) === null || _b === void 0 ? void 0 : _b.call(_this);
|
|
1289
|
+
}
|
|
1290
|
+
_this.timerCount++;
|
|
1291
|
+
}, 1000);
|
|
1292
|
+
};
|
|
1293
|
+
// 清除定时器
|
|
1294
|
+
AudioRecorder.prototype.clearTimer = function () {
|
|
1295
|
+
if (this.timer)
|
|
1296
|
+
clearInterval(this.timer);
|
|
1297
|
+
this.timer = null;
|
|
1298
|
+
this.timerCount = 0;
|
|
1299
|
+
};
|
|
1300
|
+
AudioRecorder.prototype.onTimer = function (callback) {
|
|
1301
|
+
this.timerCallback = callback;
|
|
1302
|
+
};
|
|
1303
|
+
AudioRecorder.prototype.onTimerEnd = function (callback) {
|
|
1304
|
+
this.timerEndCallback = callback;
|
|
1305
|
+
};
|
|
1306
|
+
AudioRecorder.prototype.getAudioBlob = function () {
|
|
1307
|
+
if (!this.mediaRecorder)
|
|
1308
|
+
return null;
|
|
1309
|
+
var type = this.getExtension(this.mediaRecorder.mimeType);
|
|
1310
|
+
return new Blob(this.audioChunks, { type: "audio/".concat(type) });
|
|
1311
|
+
};
|
|
1312
|
+
AudioRecorder.prototype.getAudioUrl = function () {
|
|
1313
|
+
var blob = this.getAudioBlob();
|
|
1314
|
+
if (!blob)
|
|
1315
|
+
return '';
|
|
1316
|
+
return URL.createObjectURL(blob);
|
|
1317
|
+
};
|
|
1318
|
+
AudioRecorder.prototype.getAudioBase64 = function (blob) {
|
|
1319
|
+
return new Promise(function (resolve, reject) {
|
|
1320
|
+
var reader = new FileReader();
|
|
1321
|
+
reader.onerror = reject;
|
|
1322
|
+
reader.onload = function () { return resolve(reader.result); };
|
|
1323
|
+
reader.readAsDataURL(blob);
|
|
1324
|
+
});
|
|
1325
|
+
};
|
|
1326
|
+
AudioRecorder.prototype.getAudioInfo = function (blob) {
|
|
1327
|
+
var _this = this;
|
|
1328
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
1329
|
+
var audioContext, source, analyser, sampleRate, channels, bitDepth, format, error_2;
|
|
1330
|
+
var _a, _b;
|
|
1331
|
+
return __generator(this, function (_c) {
|
|
1332
|
+
switch (_c.label) {
|
|
1333
|
+
case 0:
|
|
1334
|
+
_c.trys.push([0, 2, , 3]);
|
|
1335
|
+
audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
1336
|
+
source = audioContext.createMediaStreamSource(this.stream);
|
|
1337
|
+
analyser = audioContext.createAnalyser();
|
|
1338
|
+
source.connect(analyser);
|
|
1339
|
+
sampleRate = audioContext.sampleRate;
|
|
1340
|
+
channels = ((_a = this.stream) === null || _a === void 0 ? void 0 : _a.getAudioTracks()[0].getSettings().channelCount) || 0;
|
|
1341
|
+
return [4 /*yield*/, this.getBitDepth(audioContext, blob)];
|
|
1342
|
+
case 1:
|
|
1343
|
+
bitDepth = _c.sent();
|
|
1344
|
+
format = this.getExtension((_b = this.mediaRecorder) === null || _b === void 0 ? void 0 : _b.mimeType);
|
|
1345
|
+
resolve(__assign(__assign({}, this.audioInfo), { sampleRate: sampleRate, channels: channels, bitDepth: bitDepth, format: format }));
|
|
1346
|
+
return [3 /*break*/, 3];
|
|
1347
|
+
case 2:
|
|
1348
|
+
error_2 = _c.sent();
|
|
1349
|
+
reject(error_2);
|
|
1350
|
+
return [3 /*break*/, 3];
|
|
1351
|
+
case 3: return [2 /*return*/];
|
|
1352
|
+
}
|
|
1353
|
+
});
|
|
1354
|
+
}); });
|
|
1355
|
+
};
|
|
1356
|
+
AudioRecorder.prototype.getBitDepth = function (audioContext, blob) {
|
|
1357
|
+
var _this = this;
|
|
1358
|
+
return new Promise(function (resolve, reject) {
|
|
1359
|
+
var reader = new FileReader();
|
|
1360
|
+
reader.onload = function (event) { return __awaiter(_this, void 0, void 0, function () {
|
|
1361
|
+
var arrayBuffer, channelData, maxAmplitude, i, isFloat;
|
|
1362
|
+
var _a;
|
|
1363
|
+
return __generator(this, function (_b) {
|
|
1364
|
+
switch (_b.label) {
|
|
1365
|
+
case 0: return [4 /*yield*/, audioContext.decodeAudioData((_a = event.target) === null || _a === void 0 ? void 0 : _a.result)];
|
|
1366
|
+
case 1:
|
|
1367
|
+
arrayBuffer = _b.sent();
|
|
1368
|
+
if (!arrayBuffer) {
|
|
1369
|
+
reject({
|
|
1370
|
+
error: '未能读取 Blob 数据'
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
channelData = arrayBuffer.getChannelData(0);
|
|
1374
|
+
maxAmplitude = 0;
|
|
1375
|
+
for (i = 0; i < channelData.length; i++) {
|
|
1376
|
+
maxAmplitude = Math.max(maxAmplitude, Math.abs(channelData[i]));
|
|
1377
|
+
}
|
|
1378
|
+
isFloat = channelData.some(function (value) {
|
|
1379
|
+
return Math.abs(value) > 1.0 ||
|
|
1380
|
+
value.toString().includes('.');
|
|
1381
|
+
});
|
|
1382
|
+
resolve(isFloat ? 32 : 16);
|
|
1383
|
+
return [2 /*return*/];
|
|
1384
|
+
}
|
|
1385
|
+
});
|
|
1386
|
+
}); };
|
|
1387
|
+
reader.readAsArrayBuffer(blob);
|
|
1388
|
+
});
|
|
1389
|
+
};
|
|
1390
|
+
AudioRecorder.prototype.download = function (blob) {
|
|
1391
|
+
var _a;
|
|
1392
|
+
if (!blob)
|
|
1393
|
+
return null;
|
|
1394
|
+
var format = this.getExtension((_a = this.mediaRecorder) === null || _a === void 0 ? void 0 : _a.mimeType);
|
|
1395
|
+
var fileName = "recording_".concat(Date.now() + Math.random().toString(36).substring(2), ".").concat(format);
|
|
1396
|
+
var url = URL.createObjectURL(blob);
|
|
1397
|
+
var a = document.createElement('a');
|
|
1398
|
+
a.style.display = 'none';
|
|
1399
|
+
a.href = url;
|
|
1400
|
+
a.download = fileName;
|
|
1401
|
+
a.click();
|
|
1402
|
+
URL.revokeObjectURL(url);
|
|
1403
|
+
};
|
|
1404
|
+
AudioRecorder.prototype.getExtension = function (mimeType) {
|
|
1405
|
+
var mimeToExt = {
|
|
1406
|
+
'audio/webm': 'webm',
|
|
1407
|
+
'audio/webm;codecs=opus': 'webm',
|
|
1408
|
+
'audio/ogg': 'ogg',
|
|
1409
|
+
'audio/ogg;codecs=opus': 'ogg',
|
|
1410
|
+
'audio/mp4': 'mp4',
|
|
1411
|
+
'audio/mpeg': 'mp3',
|
|
1412
|
+
'audio/wav': 'wav',
|
|
1413
|
+
};
|
|
1414
|
+
if (mimeToExt[mimeType])
|
|
1415
|
+
return mimeToExt[mimeType];
|
|
1416
|
+
var mainType = mimeType.split(';')[0];
|
|
1417
|
+
return mimeToExt[mainType] || 'audio';
|
|
1418
|
+
};
|
|
1419
|
+
// 上传音频到服务器
|
|
1420
|
+
AudioRecorder.prototype.upload = function (url, options) {
|
|
1421
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1422
|
+
var audioBlob, formData, format, fileName, response, data, error_3;
|
|
1423
|
+
var _a;
|
|
1424
|
+
return __generator(this, function (_b) {
|
|
1425
|
+
switch (_b.label) {
|
|
1426
|
+
case 0:
|
|
1427
|
+
audioBlob = this.getAudioBlob();
|
|
1428
|
+
if (!audioBlob)
|
|
1429
|
+
return [2 /*return*/, null];
|
|
1430
|
+
formData = new FormData();
|
|
1431
|
+
format = this.getExtension((_a = this.mediaRecorder) === null || _a === void 0 ? void 0 : _a.mimeType);
|
|
1432
|
+
fileName = "recording_".concat(Date.now() + Math.random().toString(36).substring(2), ".").concat(format);
|
|
1433
|
+
formData.append('audio', audioBlob, fileName);
|
|
1434
|
+
_b.label = 1;
|
|
1435
|
+
case 1:
|
|
1436
|
+
_b.trys.push([1, 4, , 5]);
|
|
1437
|
+
return [4 /*yield*/, fetch(url, options)];
|
|
1438
|
+
case 2:
|
|
1439
|
+
response = _b.sent();
|
|
1440
|
+
return [4 /*yield*/, response.json()];
|
|
1441
|
+
case 3:
|
|
1442
|
+
data = _b.sent();
|
|
1443
|
+
return [2 /*return*/, data];
|
|
1444
|
+
case 4:
|
|
1445
|
+
error_3 = _b.sent();
|
|
1446
|
+
throw new Error(error_3);
|
|
1447
|
+
case 5: return [2 /*return*/];
|
|
1448
|
+
}
|
|
1449
|
+
});
|
|
1450
|
+
});
|
|
1451
|
+
};
|
|
1452
|
+
return AudioRecorder;
|
|
1453
|
+
}());
|
|
1454
|
+
|
|
1187
1455
|
/**
|
|
1188
1456
|
* 匹配中文字母数字
|
|
1189
1457
|
*/
|
|
@@ -1209,6 +1477,7 @@
|
|
|
1209
1477
|
*/
|
|
1210
1478
|
var url = /^https?:\/\/[^\s/$.?#].[^\s]*$/;
|
|
1211
1479
|
|
|
1480
|
+
exports.AudioRecorder = AudioRecorder;
|
|
1212
1481
|
exports.InputLegalityValidate = InputLegalityValidate;
|
|
1213
1482
|
exports.InputLengthValidate = InputLengthValidate;
|
|
1214
1483
|
exports.Mask = Mask;
|
|
@@ -1,20 +1,42 @@
|
|
|
1
1
|
interface AudioRecorderOptions {
|
|
2
|
-
|
|
2
|
+
media: MediaStreamConstraints;
|
|
3
|
+
mediaRecorder?: MediaRecorderOptions;
|
|
4
|
+
maxTime?: number;
|
|
5
|
+
}
|
|
6
|
+
interface TimerOptions {
|
|
7
|
+
next?: () => void;
|
|
8
|
+
end?: () => void;
|
|
9
|
+
}
|
|
10
|
+
interface AudioInfo {
|
|
3
11
|
sampleRate: number;
|
|
4
|
-
|
|
5
|
-
|
|
12
|
+
channels: number;
|
|
13
|
+
format: string | null;
|
|
14
|
+
bitDepth: number;
|
|
6
15
|
}
|
|
7
16
|
declare class AudioRecorder {
|
|
8
17
|
private options;
|
|
9
18
|
private stream;
|
|
10
19
|
private mediaRecorder;
|
|
11
20
|
private audioChunks;
|
|
21
|
+
private audioInfo;
|
|
22
|
+
private timer;
|
|
23
|
+
private timerCount;
|
|
24
|
+
private timerCallback;
|
|
25
|
+
private timerEndCallback;
|
|
12
26
|
constructor(options: AudioRecorderOptions);
|
|
13
27
|
start(): Promise<void>;
|
|
14
28
|
stop(): void;
|
|
15
29
|
reset(): void;
|
|
16
|
-
|
|
30
|
+
onStop(): void;
|
|
31
|
+
startTimer(options?: TimerOptions): void;
|
|
32
|
+
clearTimer(): void;
|
|
33
|
+
onTimer(callback: () => void): void;
|
|
34
|
+
onTimerEnd(callback: () => void): void;
|
|
35
|
+
getAudioBlob(): Blob | null;
|
|
17
36
|
getAudioUrl(): string;
|
|
18
|
-
getAudioBase64(): string
|
|
19
|
-
|
|
37
|
+
getAudioBase64(blob: Blob): Promise<string>;
|
|
38
|
+
getAudioInfo(): Promise<unknown>;
|
|
39
|
+
getWavBitDepth(blob: Blob): Promise<number>;
|
|
40
|
+
getExtension(mimeType: string): string;
|
|
41
|
+
upload(url: string, options?: RequestInit): Promise<any>;
|
|
20
42
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface AudioRecorderOptions {
|
|
2
|
+
media: MediaStreamConstraints;
|
|
3
|
+
mediaRecorder?: MediaRecorderOptions;
|
|
4
|
+
maxTime?: number;
|
|
5
|
+
}
|
|
6
|
+
interface TimerOptions {
|
|
7
|
+
next?: () => void;
|
|
8
|
+
end?: () => void;
|
|
9
|
+
}
|
|
10
|
+
export interface AudioInfo {
|
|
11
|
+
sampleRate: number;
|
|
12
|
+
channels: number;
|
|
13
|
+
format: string | null;
|
|
14
|
+
bitDepth: number;
|
|
15
|
+
}
|
|
16
|
+
export declare class AudioRecorder {
|
|
17
|
+
private options;
|
|
18
|
+
private stream;
|
|
19
|
+
private mediaRecorder;
|
|
20
|
+
private audioChunks;
|
|
21
|
+
private audioInfo;
|
|
22
|
+
private timer;
|
|
23
|
+
private timerCount;
|
|
24
|
+
private stopCallback;
|
|
25
|
+
private timerCallback;
|
|
26
|
+
private timerEndCallback;
|
|
27
|
+
constructor(options: AudioRecorderOptions);
|
|
28
|
+
start(): Promise<void>;
|
|
29
|
+
stop(): void;
|
|
30
|
+
reset(): void;
|
|
31
|
+
onStop(callback: (audioChunks: Blob[]) => void): void;
|
|
32
|
+
startTimer(options?: TimerOptions): void;
|
|
33
|
+
clearTimer(): void;
|
|
34
|
+
onTimer(callback: (count: number) => void): void;
|
|
35
|
+
onTimerEnd(callback: () => void): void;
|
|
36
|
+
getAudioBlob(): Blob | null;
|
|
37
|
+
getAudioUrl(): string;
|
|
38
|
+
getAudioBase64(blob: Blob): Promise<string>;
|
|
39
|
+
getAudioInfo(blob: Blob): Promise<unknown>;
|
|
40
|
+
getBitDepth(audioContext: AudioContext, blob: Blob): Promise<number>;
|
|
41
|
+
download(blob: Blob): null | undefined;
|
|
42
|
+
getExtension(mimeType: string): string;
|
|
43
|
+
upload(url: string, options?: RequestInit): Promise<any>;
|
|
44
|
+
}
|
|
45
|
+
export {};
|