1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import audio from '@ohos.multimedia.audio' 16import deviceInfo from '@ohos.deviceInfo' 17import image from '@ohos.multimedia.image' 18import media from '@ohos.multimedia.media' 19import mediaLibrary from '@ohos.multimedia.mediaLibrary' 20import Logger from '../model/Logger' 21import MediaUtils from '../model/MediaUtils' 22// @ts-ignore 23import fs from '@ohos.file.fs' 24import mediaPlay from '../model/mediaPlay' 25 26class AudioCapturer { 27 private tag: string = 'qlw AudioCapture' 28 private static instance: AudioCapturer = new AudioCapturer() 29 private audioCapturer: audio.AudioCapturer = undefined 30 private fd: number = undefined 31 private isRecorder: boolean = false 32 private file: fs.File = undefined 33 34 35 async createAudioCapturer() { 36 let AudioStreamInfo = { 37 samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, 38 channels: audio.AudioChannel.CHANNEL_2, 39 sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, 40 encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW 41 } 42 let AudioCapturerInfo = { 43 source: audio.SourceType.SOURCE_TYPE_MIC, 44 capturerFlags: 0 45 } 46 let AudioCapturerOptions = { 47 streamInfo: AudioStreamInfo, 48 capturerInfo: AudioCapturerInfo 49 // @ts-ignore 50 } 51 this.audioCapturer = await audio.createAudioCapturer(AudioCapturerOptions) 52 } 53 54 async getFileFd() { 55 let filesDir = globalThis.abilityContext.filesDir 56 let path = filesDir + '/test.wav' 57 Logger.info(this.tag, `getFileFd path : ${path}`) 58 this.file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE | fs.OpenMode.TRUNC) 59 return this.file.fd 60 } 61 62 async startCapturer() { 63 try { 64 this.fd = await this.getFileFd() 65 Logger.info(this.tag, `fd : ${this.fd}`) 66 67 if (this.fd !== null) { 68 Logger.info(this.tag, `create audio fileAssets success fd : ${this.fd}`) 69 } 70 else { 71 Logger.info(this.tag, `create audio fileAssets error`) 72 } 73 74 let header = mediaPlay.encodeWAV(audio.AudioSamplingRate.SAMPLE_RATE_44100, audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, audio.AudioChannel.CHANNEL_2) 75 Logger.info(this.tag, `wav header length: ${header.buffer.byteLength}`) 76 fs.writeSync(this.fd, header.buffer) 77 78 this.audioCapturer.start(async (err) => { 79 if (err) { 80 Logger.info(this.tag, `Capture start failed`) 81 } else { 82 Logger.info(this.tag, `Capture start success`) 83 let bufferSize = await this.audioCapturer.getBufferSize() 84 Logger.info(this.tag, `audioCapture bufferSize: ${bufferSize}`) 85 this.isRecorder = true 86 while (this.isRecorder) { 87 Logger.info(this.tag, 'audioCapture: ---------READ BUFFER---------') 88 let buffer = await this.audioCapturer.read(bufferSize, true) 89 Logger.info(this.tag, 'audioCapture: ---------WRITE BUFFER---------') 90 fs.writeSync(this.fd, buffer) 91 } 92 } 93 }) 94 } catch (err) { 95 Logger.info(this.tag, `startCapturer fail err: ${err}, message: ${err.message}, code: ${err.code}`) 96 } 97 98 } 99 100 async stopCapturer() { 101 try { 102 await this.audioCapturer.stop() 103 this.isRecorder = false 104 Logger.info(this.tag, `stopCapturer success`) 105 } catch (err) { 106 Logger.info(this.tag, `stopCapturer fail err: ${err}, message: ${err.message}, code: ${err.code}`) 107 } 108 } 109 110 async releaseCapturer() { 111 try { 112 if (this.audioCapturer) { 113 await this.audioCapturer.release() 114 Logger.info(this.tag, `releaseCapturer success`) 115 } 116 if (this.file) { 117 fs.closeSync(this.file); 118 Logger.info(this.tag, `release file success`) 119 } 120 } catch (err) { 121 Logger.info(this.tag, `stopCapturer fail err: ${err}, message: ${err.message}, code: ${err.code}`) 122 } 123 } 124} 125 126export default new AudioCapturer()