简介
OpenAtom OpenHarmony(以下简称“OpenHarmony”)是由开放原子开源基金会孵化及运营的开源项目,是面向全场景、全连接、全智能时代的智能物联网操作体系。
多媒体子体系是OpenHarmony体系中的核心子体系,为体系供给了相机、音频和视频等多媒体功用。多媒体子体系的音频模块、音频录音功用能够供给两套接口,一是由ohos.multimedia.media供给的AudioRecorder接口,能够直接设置录音保存的文件途径,在录制完毕今后自动生成对应的录音文件,代码编写比较简单;二是由ohos.multimedia.audio供给的AudioCapturer接口,能够获得录音过程中的PCM数据,并对数据进行处理。因为Capturer接口关于原始数据的处理愈加灵敏,今日就和咱们介绍经过Capturer接口完结录音变速的功用的办法。
作用展示
经过Capturer接口完结音频的录制,在录制过程中对PCM数据进行重采样完结声音的快放和慢放。
首要设置录音加快或许录音减速,设置完结今后点击“录音开端”按钮进行录音,点击“录音完毕”按钮中止录音,再经过点击“播映开端”对录音的音频进行播映,播映的音频是设置后的加快或许减速作用。
代码已经上传至SIG仓库,链接如下:
gitee.com/openharmony…
目录结构
调用流程
1.Start的结构层调用流程
- Read的结构层调用流程
源码剖析
1.首要看一下页面的布局,首要分为四个模块:
(1)设置录音加快
<div style="text-color: aqua;background-color: yellow;margin-bottom: 20fp;">
<text style="font-size: 30fp;">设置录音加快:</text>
</div>
<div class="container">
<button class="first" type="capsule" onclick="set_5_4">1.25倍速</button>
<button class="first" type="capsule" onclick="set_6_4">1.5倍速</button>
</div>
<div class="container">
<button class="first" type="capsule" onclick="set_7_4">1.75倍速</button>
<button class="first" type="capsule" onclick="set_8_4">2倍速</button>
</div>
(2)设置录音减速
<div style="text-color: aqua;background-color: yellow;margin-bottom: 20fp;margin-top: 20fp;">
<text style="font-size: 30fp;">设置录音减速:</text>
</div>
<div class="container">
<button class="first" type="capsule" onclick="set_3_4">0.75倍速</button>
<button class="first" type="capsule" onclick="set_2_4">0.5倍速</button>
</div>
(3)录音
<div style="text-color: aqua;background-color: yellow;margin-bottom: 20fp;margin-top: 20fp;">
<text style="font-size: 30fp;">录音:</text>
</div>
<div class="container">
<button class="first" type="capsule" onclick="record">录音开端</button>
<button class="first" type="capsule" onclick="recordstop">录音完毕</button>
</div>
(4)播映
<div style="text-color: aqua;background-color: yellow;margin-bottom: 20fp;margin-top: 20fp;">
<text style="font-size: 30fp;">播映:</text>
</div>
<div class="container">
<button class="first" type="capsule" onclick="play">播映开端</button>
<button class="first" type="capsule" onclick="playstop">播映完毕</button>
</div>
<div class="container">
<video if="{{ display }}" id="{{ videoId }}"
class="video"
src="{{url}}"
autoplay="{{ autoplay }}"
controls="{{ controlShow }}"
muted="false"
onseeked="seeked"
onprepared="prepared"
>
</video>
</div>
2.逻辑代码在JS中:
(1)首要经过AudioCapturer接口获取到PCM数据,再经过调用AudioCapturer的start接口来发动录音流程。
globalThis.capturer.start().then(function () {
console.log("gyf start");
globalThis.capturer.getBufferSize((err, bufferSize) => {
if (err) {
console.error('gyf getBufferSize error');
} else {
console.log("gyf bufferSize = " + bufferSize);
globalThis.getBuf(bufferSize);
}
});
});
(2)发动成功今后,getBuf会调用到getData函数,getData函数经过AudioCapturer的read办法来读取数据,成功读取到数据今后,经过handleBuffer函数对数据进行处理。handleBuffer函数的参数arrayBuffer便是经过read办法读取出来的pcm数据,在handleBuffer中对数据进行了快速播映或许慢速播映的处理。
//循环调用read,进行数据的读取
handleBuffer(arrayBuffer) {
console.log("gyf handleBuffer");
let result = new Uint8Array(arrayBuffer);
console.log("gyf handleBuffer ================== " + result);
let outData = this.test(result, up, down);
fileio.writeSync(globalThis.fd, outData.buffer);
globalThis.capturer.read(globalThis.bufSize, true).then(this.handleBuffer);
},
getData(bufSize) {
console.log("gyf getData");
globalThis.capturer.read(bufSize, true).then(this.handleBuffer);
},
getBuf(bufSize) {
console.log("gyf getBuf");
this.getData(bufSize);
},
(3)快速播映或许慢速播映是经过up和down两个办法的组合来完结的,down办法的原理是对PCM数据进行插值处理,在相邻两点间刺进down个采样点,up办法的原理是距离抽取,距离up个点进行抽取采样。
up(data, up) {
if (1 == up) {
return data;
}
let length = data.byteLength;
let upLength = Math.round(length / up);
var upData = new Uint8Array(upLength);
for (var i = 0, j = 0; i < length; ) {
if (j >= upLength) {
break;
}
upData[j] = data[i];
i += up;
j++;
}
return upData;
},
down(data, down) {
if (1 == down) {
return data;
}
let length = data.byteLength;
let downLength = Math.round(length * down);
var downData = new Uint8Array(downLength);
for (var i = 0, j = 0; i < length - 1; ) {
for (var k = 0; k < down; k++) {
downData[j] = data[i];
j++;
}
i++;
}
return downData;
},
(4)将down和up的办法组合调用,来完结1.25倍、1.5倍、1.75倍、2倍、0.75倍、0.5倍的速度播映。
test(data, up, down) {
let downData = this.down(data, down);
let upData = this.up(downData, up);
return upData;
},
(5)播映wav格局的音频文件,收集获取PCM数据,需求咱们依据设置的参数对pcm数据进行增加wav的头部信息,经过创建AudioCapturer实例的时候设置收集音频的参数,如采样率、通道数、采样格局等。
//音频收集初始化
var audioStreamInfo = {
samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_8000,
channels: audio.AudioChannel.CHANNEL_1,
sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_U8,
encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}
var audioCapturerInfo = {
source: audio.SourceType.SOURCE_TYPE_MIC,
capturerFlags: 1
}
var audioCapturerOptions = {
streamInfo: audioStreamInfo,
capturerInfo: audioCapturerInfo
}
let that = this;
audio.createAudioCapturer(audioCapturerOptions,(err, data) => {
if (err) {
console.error(`gyf AudioCapturer Created : Error: ${err.message}`);
}
else {
console.info('gyf AudioCapturer Created : Success : SUCCESS');
that.capturer = data;
}
});
(6)依据这些参数设置的信息需求对wav文件写入文件头,头信息一般包含44个字节,里面需求设置三个chunk的信息(RIFF chunk、fmt chunk、data chunk),详细的信息能够查看官网的介绍WAV文件格局介绍:
(www-mmsp.ece.mcgill.ca/Documents/A… )
//假定数据为1000秒钟的时间(8000 * 1000)
encodeWAV() {
var dataLen = 8000000;
var sampleRate = 8000;
var sampleBits = 8;
var buffer = new ArrayBuffer(44);
var data = new DataView(buffer);
var channelCount = 1; // 单声道
var offset = 0;
// 资源交换文件标识符
this.writeString(data, offset, 'RIFF'); offset += 4;
// 下个地址开端到文件尾总字节数,即文件巨细-8
data.setUint32(offset, 36 + dataLen, true); offset += 4;
// WAV文件标志
this.writeString(data, offset, 'WAVE'); offset += 4;
// 波形格局标志
this.writeString(data, offset, 'fmt '); offset += 4;
// 过滤字节,一般为 0x10 = 16
data.setUint32(offset, 16, true); offset += 4;
// 格局类别 (PCM方式采样数据)
data.setUint16(offset, 1, true); offset += 2;
// 通道数
data.setUint16(offset, channelCount, true); offset += 2;
// 采样率,每秒样本数,表示每个通道的播映速度
data.setUint32(offset, sampleRate, true); offset += 4;
// 波形数据传输率 (每秒平均字节数) 单声道每秒数据位数每样本数据位/8
data.setUint32(offset, channelCount * sampleRate * (sampleBits / 8), true); offset += 4;
// 快数据调整数 采样一次占用字节数 单声道每样本的数据位数/8
data.setUint16(offset, channelCount * (sampleBits / 8), true); offset += 2;
// 每样本数据位数
data.setUint16(offset, sampleBits, true); offset += 2;
// 数据标识符
this.writeString(data, offset, 'data'); offset += 4;
// 采样数据总数,即数据总巨细-44
data.setUint32(offset, dataLen, true); offset += 4;
return data;
},
总结
本文介绍了经过运用OpenHarmony音频模块的AudioCapturer接口完结录音功用。AudioCapturer接口关于原始数据的处理十分灵敏,能够对收集的数据进行插值/抽值的重采样处理,并将处理后的音频处理保存至本地文件。因为本地文件运用的是WAV格局,故在写数据前需求对WAV文件进行头部信息的增加,这些信息能够依据创建AudioCapturer时设置的参数来进行设置,以此保证头部信息的准确性,最后再经过应用层的video组件对音频数据进行播映。
希望这篇文章能为开发者供给一些新的思路,然后进行其他场景的拓宽,例如将获取到收集的数据经过这种方式完结语音辨认、语音转写等功用,在实践开发的过程中为OpenHarmony生态的发展贡献一份力量。