• 音视频开发之旅(67) - 变速不变调之sonic源码分析


    目录

    1. 基音周期、浊音的概念
    2. Sonic源码分析
    3. 资料
    4. 收获

    上一篇我们学习了音频变速不变调的原理以及WSOLA波形相似叠加算法进行时域压扩处理。其中在寻找相似帧方面,Sonic采用AMDF(平均幅度差函数法)方法来进行寻找。

    一、基音周期、浊音的概念

    图片来自:[清音or浊音 ]

    1. 人体的发音器官可以分为三大部分:动力区 声源区 调音区
    2. 1.动力区—— 肺 、横膈膜、气管
    3. 肺部呼出的气流是语音的原动力。肺部呼出的气流,通过支气管到达喉头,作用于声带、咽腔、口腔 、鼻腔等发音器官。
    4. 2.声源区——喉头、声带
    5. 用手摸脖子那里的喉头,声带就位于喉头的后面,
    6. 声带是两片富有弹性的带状薄膜,两片声带之间的空隙叫声门。
    7. 从肺部呼出的气流通过关闭着的声门时,会引起声带振动而发出声音
    8. 如果你把手贴在脖子上喉的部位,发声时,手会感到轻微的震动,这是因为声带在振动。
    9. 嗓音的高低、粗细是由声带的松紧程度、呼出的气体多少决定的。
    10. 3.调音区————口腔、鼻腔、咽腔
    11. 调音区主要是口腔,鼻腔,咽腔三大部分,其中口腔主要包括唇、齿和舌头。(口腔后面是咽腔,咽头上通口腔、鼻腔,下接喉头。)
    12. 引用:[清音or浊音](https://zhuanlan.zhihu.com/p/374857199)

    浊音的发音过程是:来自肺部的气流冲击声门,造成声门的一张一合,形成一系列准周期的气流脉冲,经过声道(含口腔、鼻腔)的谐振及唇齿的辐射最终形成语音信号。故浊音波形呈现一定的准周期性。
    所谓基音周期,就是对这种准周期而言的,它反映了声门相邻两次开闭之间的时间间隔或开闭的频率

    基音周期是语音信号最重要的参数之一,但是基音的提取是比较困难的。
    主要体现在

    1. 1. 声门激励信号并不是一个完全的周期序列
    2. 2. 基音频率大多数情况是在100-200HZ,但是浊音信号往往啃根包含几十个谐波分量,而其基波分量往往不是最强的,造成基音检测时,把谐波当做了基波。
    3. 3. 基波周期的变化分为比较大,老年男性50 Hz,儿童和女性500 Hz
    4. 引用:[语音识别 08 基音周期的估算方法](https://zhuanlan.zhihu.com/p/454283094)

    基音检测的方法主要有自相关函数法,平均幅度差函数法等。而Sonic的实现采用的就是平均幅度差函数法,这也是sonic 变速不变调最重要的一步。

    二、Sonic源码分析

    sonic源码地址:https://github.com/waywardgeek/sonic
    可以看到它有两份实现Java版本(Sonic.java)和Cpp版本(Sonic.cpp),并且代码量都比较少,作者给出了性能对比,基本上也没什么差别。
    而android中大名鼎鼎的Exoplayer的变速不变调的实现就是基于Sonic.java,我们结合Exoplayer的实现来进行分析。

    主要有两个类SonicAudioProcessor和Sonic,其中SonicAudioProcessor是对Sonic做了一层封装为了适配Exoplayer的框架。

    1. public final class SonicAudioProcessor {
    2. private float speed;
    3. private float pitch;
    4. private Sonic sonic;
    5. private ByteBuffer buffer;
    6. private ShortBuffer shortBuffer;
    7. private ByteBuffer outputBuffer;
    8. public void setSpeed(float speed) {
    9. if (this.speed != speed) {
    10. this.speed = speed;
    11. ...
    12. flush();
    13. }
    14. }
    15. //速度发生变化后,重新初始化Sonic。
    16. private void flush() {
    17. ...
    18. sonic = new Sonic(
    19. mSampleRate,//输入采样率
    20. mChannelCount,//采样通道数
    21. speed,//速度
    22. pitch,//变调值,默认1.0f
    23. mSampleRate//输出采样率,一般不变
    24. );
    25. ...
    26. }
    27. //把Mediacodec解码音频后的Frame数据数据在给到AudioTrack.write之前,先给到Sonic进行变速处理
    28. public void queueInput(ByteBuffer inputBuffer) {
    29. ...
    30. ShortBuffer shortBuffer = inputBuffer.asShortBuffer();
    31. ...
    32. sonic.queueInput(shortBuffer);
    33. ...
    34. }
    35. // 紧接着调用Sonic变速处理后的数据给到AudioTrack进行write
    36. public ByteBuffer getOutput() {
    37. ...
    38. int outputSize = sonic.getOutputSize();
    39. buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
    40. shortBuffer = buffer.asShortBuffer();
    41. sonic.getOutput(shortBuffer);
    42. outputBuffer = buffer;
    43. ...
    44. return outputBuffer;
    45. }
    46. }

    可以看到SonicAudioProcessor就是AudioTrack和Sonic之前的一层封装层。把Mediacodec解码的音频frame数据在给到AudioTrack.write之前,先通过queueInput给到Sonic进行变速处理,然后通过getoutput获取处理后的数据再给到AudioTrack。

    下面我们重点看下Sonic的queueInput和getOutput的实现。

    1. public final class Sonic {
    2. private static final int MINIMUM_PITCH = 65;
    3. private static final int MAXIMUM_PITCH = 400;
    4. private static final int AMDF_FREQUENCY = 4000;
    5. private static final int BYTES_PER_SAMPLE = 2;
    6. public Sonic(
    7. int inputSampleRateHz, int channelCount, float speed, float pitch, int outputSampleRateHz) {
    8. this.inputSampleRateHz = inputSampleRateHz;
    9. this.channelCount = channelCount;
    10. this.speed = speed;
    11. this.pitch = pitch;
    12. rate = (float) inputSampleRateHz / outputSampleRateHz;
    13. minPeriod = inputSampleRateHz / MAXIMUM_PITCH;//最小的基音周期 44100/400
    14. maxPeriod = inputSampleRateHz / MINIMUM_PITCH;//最大的基音周期 44100/65
    15. maxRequiredFrameCount = 2 * maxPeriod;//最大的请求帧数 2* 44100/65 根据奈奎斯特采样定律,采样率为周期的2倍
    16. downSampleBuffer = new short[maxRequiredFrameCount];//下采样的buffer
    17. inputBuffer = new short[maxRequiredFrameCount * channelCount];
    18. outputBuffer = new short[maxRequiredFrameCount * channelCount];
    19. pitchBuffer = new short[maxRequiredFrameCount * channelCount];
    20. }
    21. public void queueInput(ShortBuffer buffer) {
    22. ...
    23. processStreamInput();
    24. }
    25. private void processStreamInput() {
    26. ...
    27. float s = speed / pitch;
    28. float r = rate * pitch;
    29. if (s > 1.00001 || s < 0.99999) {
    30. changeSpeed(s);
    31. }
    32. ...
    33. }
    34. private void changeSpeed(float speed) {
    35. ...
    36. int frameCount = inputFrameCount;
    37. int positionFrames = 0;
    38. do {
    39. //如果有保留的framecount,将inputbuffer 中保存的 positionFrames 个点的数据拷贝到 outputbuffer 中
    40. if (remainingInputToCopyFrameCount > 0) {
    41. positionFrames += copyInputToOutput(positionFrames);
    42. } else {
    43. //寻找基音周期
    44. int period = findPitchPeriod(inputBuffer, positionFrames);
    45. if (speed > 1.0) {
    46. //如果倍速 进行跳帧重采样
    47. positionFrames += period + skipPitchPeriod(inputBuffer, positionFrames, speed, period);
    48. } else {
    49. //如果慢速,则插入值
    50. positionFrames += insertPitchPeriod(inputBuffer, positionFrames, speed, period);
    51. }
    52. } while (positionFrames + maxRequiredFrameCount <= frameCount);
    53. removeProcessedInputFrames(positionFrames);
    54. }
    55. private int findPitchPeriod(short[] samples, int position) {
    56. //寻找基音周期,这是变速不变调的关键的一步,Sonic采用 AMDF方式寻找
    57. int period;
    58. int retPeriod;
    59. int skip = inputSampleRateHz > AMDF_FREQUENCY ? inputSampleRateHz / AMDF_FREQUENCY : 1;//采样率是否大于AMDF_FREQUENCY(4000),计算下采样时,跳过的采样点数量,这里的结果是5。为了提高效率,进行向下采样到4KHZ,然后用更窄的频率范围再做一次。
    60. downSampleInput(samples, position, skip);
    61. period = findPitchPeriodInRange(downSampleBuffer, 0, minPeriod / skip, maxPeriod / skip);
    62. if (skip != 1) {
    63. period *= skip;
    64. int minP = period - (skip * 4);
    65. int maxP = period + (skip * 4);
    66. if (minP < minPeriod) {
    67. minP = minPeriod;
    68. }
    69. if (maxP > maxPeriod) {
    70. maxP = maxPeriod;
    71. }
    72. downSampleInput(samples, position, 1);
    73. period = findPitchPeriodInRange(downSampleBuffer, 0, minP, maxP);
    74. }
    75. if (previousPeriodBetter(minDiff, maxDiff)) {
    76. retPeriod = prevPeriod;
    77. } else {
    78. retPeriod = period;
    79. }
    80. prevMinDiff = minDiff;
    81. prevPeriod = period;
    82. return retPeriod;
    83. }
    84. //寻找基音周期的 最终实现就在这里了
    85. private int findPitchPeriodInRange(short[] samples, int position, int minPeriod, int maxPeriod) {
    86. // Find the best frequency match in the range, and given a sample skip multiple. For now, just
    87. // find the pitch of the first channel.
    88. int bestPeriod = 0;
    89. int worstPeriod = 255;
    90. int minDiff = 1;
    91. int maxDiff = 0;
    92. position *= channelCount;
    93. for (int period = minPeriod; period <= maxPeriod; period++) {
    94. int diff = 0;
    95. for (int i = 0; i < period; i++) {
    96. short sVal = samples[position + i];
    97. short pVal = samples[position + period + i];
    98. diff += Math.abs(sVal - pVal);
    99. }
    100. // Note that the highest number of samples we add into diff will be less than 256, since we
    101. // skip samples. Thus, diff is a 24 bit number, and we can safely multiply by numSamples
    102. // without overflow.
    103. if (diff * bestPeriod < minDiff * period) {
    104. minDiff = diff;//计算最小差值
    105. bestPeriod = period;//对应对最佳基音周期
    106. }
    107. if (diff * worstPeriod > maxDiff * period) {
    108. maxDiff = diff;//记录最大的差值
    109. worstPeriod = period;//记录波形相似周期
    110. }
    111. }
    112. this.minDiff = minDiff / bestPeriod;//最小的差值 除以 最佳的基音周期,求得 采样点的平均最小差值
    113. this.maxDiff = maxDiff / worstPeriod;//最大差值 除以 波形相似周期,求得采样点的平均最大差值
    114. return bestPeriod;//返回最佳基音周期
    115. }
    116. //如果是倍速处理,跳过基音周期信号
    117. private int skipPitchPeriod(short[] samples, int position, float speed, int period) {
    118. // Skip over a pitch period, and copy period/speed samples to the output.
    119. int newFrameCount;
    120. if (speed >= 2.0f) {
    121. //大于等于2倍,不保留remainingInputToCopyFrameCount
    122. newFrameCount = (int) (period / (speed - 1.0f));
    123. } else {
    124. newFrameCount = period;
    125. //如果配速小于2倍,保留remainingInputToCopyFrameCount,采用线性插值法
    126. remainingInputToCopyFrameCount = (int) (period * (2.0f - speed) / (speed - 1.0f));
    127. }
    128. outputBuffer = ensureSpaceForAdditionalFrames(outputBuffer, outputFrameCount, newFrameCount);
    129. overlapAdd(
    130. newFrameCount,
    131. channelCount,
    132. outputBuffer,
    133. outputFrameCount,
    134. samples,
    135. position,
    136. samples,
    137. position + period);
    138. outputFrameCount += newFrameCount;
    139. return newFrameCount;
    140. }
    141. //如果是慢速(小于1.0)则进行插入基音周期信号
    142. private int insertPitchPeriod(short[] samples, int position, float speed, int period) {
    143. // Insert a pitch period, and determine how much input to copy directly.
    144. int newFrameCount;
    145. if (speed < 0.5f) {
    146. newFrameCount = (int) (period * speed / (1.0f - speed));
    147. } else {
    148. newFrameCount = period;
    149. remainingInputToCopyFrameCount = (int) (period * (2.0f * speed - 1.0f) / (1.0f - speed));
    150. }
    151. outputBuffer =
    152. ensureSpaceForAdditionalFrames(outputBuffer, outputFrameCount, period + newFrameCount);
    153. System.arraycopy(
    154. samples,
    155. position * channelCount,
    156. outputBuffer,
    157. outputFrameCount * channelCount,
    158. period * channelCount);
    159. overlapAdd(
    160. newFrameCount,
    161. channelCount,
    162. outputBuffer,
    163. outputFrameCount + period,
    164. samples,
    165. position + period,
    166. samples,
    167. position);
    168. outputFrameCount += period + newFrameCount;
    169. return newFrameCount;
    170. }
    171. //最后进行合帧叠加处理,到输出buffer
    172. private static void overlapAdd(
    173. int frameCount,
    174. int channelCount,
    175. short[] out,
    176. int outPosition,
    177. short[] rampDown,
    178. int rampDownPosition,
    179. short[] rampUp,
    180. int rampUpPosition) //rampUpPosition=rampDownPosition+基音周期值
    181. {
    182. for (int i = 0; i < channelCount; i++) {
    183. int o = outPosition * channelCount + i;
    184. int u = rampUpPosition * channelCount + i;
    185. int d = rampDownPosition * channelCount + i;
    186. for (int t = 0; t < frameCount; t++) {
    187. //把起始帧和基音周期帧的帧相加,这里采样线性插值
    188. out[o] = (short) ((rampDown[d] * (frameCount - t) + rampUp[u] * t) / frameCount);
    189. o += channelCount;
    190. d += channelCount;
    191. u += channelCount;
    192. }
    193. }
    194. }
    195. }

    详细说明见上述代码注释,基本流程总结如下:

    1. 首先确定一个最大和最小的基音周期范围(和采样率有关系的一个经验值)
    2. 通过findPitchPeriod找到基音周期大小,为了提高效率,先进行下采样到4KHZ,然后用更窄的频率范围再做一次。寻找基音周期的方法就是:在 range 范围内遍历每个帧与起始帧的 AMDF 值,值最小的帧与起始帧的距离则是基因周期
    3. 根据倍速还是慢速分别进行跳过部分基音周期信号或者进行插入基音周期信号,
    4. 进行合帧叠加输出到outputBuffer

    调用以及log输出

    1. sonicAudioProcessor.queueInput(audioData);
    2. outData = sonicAudioProcessor.getOutput();
    3. Log.i(TAG, " inputDataLength="+audioData.limit()+ " inputData="+ Arrays.toString(audioData.array()));
    4. Log.i(TAG, " outDataLength="+outData.limit()+ " outData="+ Arrays.toString(outData.array()));
    5. --->0.5倍速时
    6. inputDataLength=4096
    7. outDataLength=8096 //--》不是恒定的
    8. --->1.5倍速时
    9. inputDataLength=4096
    10. outDataLength=2844 //--》不是恒定的
    11. --->2倍速时
    12. inputDataLength=4096
    13. outDataLength=2020 //--》不是恒定的

    可以看到0.5倍速时,进行了插值处理;大于1倍数时进行了采样。这个的实现是

    1. do {
    2. //如果有保留的framecount,将inputbuffer 中保存的 positionFrames 个点的数据拷贝到 outputbuffer 中
    3. if (remainingInputToCopyFrameCount > 0) {
    4. positionFrames += copyInputToOutput(positionFrames);
    5. } else {
    6. //寻找基音周期
    7. int period = findPitchPeriod(inputBuffer, positionFrames);
    8. //找到基音周期后,变速的处理,重点时下面的skipPitchPeriod和insertPitchPeriod
    9. if (speed > 1.0) {
    10. positionFrames += period + skipPitchPeriod(inputBuffer, positionFrames, speed, period);
    11. } else {
    12. positionFrames += insertPitchPeriod(inputBuffer, positionFrames, speed, period);
    13. }
    14. }
    15. } while (positionFrames + maxRequiredFrameCount <= frameCount);

    skipPitchPeriod的实现用下图说明

    insertPitchPeriod 的实现用下图说明

    由此可见,变速不变调不是简单的改变采样率,而是首先要找到基音周期,然后根据不同的倍速情况进行分帧、下采样或者插值、合帧以及remainingInputToCopyFrameCount等处理。其中Sonic再寻找基音周期时采用 AMDF方式。
    那么soundtouch又是如何实现的呐?我们下一篇来对其进行分析

    三、资料

    音频变速变调 -sonic 源码分析
    语音识别 08 基音周期的估算方法

    四、收获

    通过本篇的学习

    1. 了解了人是如何发生的,以及什么是基音周期
    2. 分析Exoplayer的Sonic变速不变调的实现
    3. 分析Sonic的通过平均幅度差函数法寻找基音周期的实现
    4. 分析变速的实现原理

    感谢你的阅读
    下一篇我们继续通过源码分析另外一种变速不变调的实现:Soundtouch,欢迎关注公众号“音视频开发之旅”,一起学习成长。
    欢迎交流

  • 相关阅读:
    Graph WaveNet:用于时空图建模的图神经网络结构
    Github相关知识
    算法竞赛进阶指南 基本算法 0x07 贪心
    git学习——第5节 远程仓库
    【电脑讲解】文件夹怎么设置密码
    CSP-J2022普及组题解T2:解密
    【Command模式】C++设计模式——命令模式
    Redis三种模式——主从复制,哨兵模式,集群
    网络安全入门必知的靶场!
    前端发布项目后,解决缓存的老版本文件问题
  • 原文地址:https://blog.csdn.net/u011570979/article/details/126301910