【平头哥RVB2601创意应用开发】第四篇 声音直方图
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">本篇测评实验的内容是将声音播放和显示结合起来,播放声音的同时屏幕同时显示声音强度的直方图。</font></span></span></span></span></span></p><p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">先上视频,再解释实现方法。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">视频</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify">ab5f2ef81c9665148465e742dd45ea4e<br />
</p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">本实验代码是基于</font><font face="Calibri">Offline</font><font face="宋体">版本的</font><font face="Calibri">musicplay</font><font face="宋体">例程开发的。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">该例程是将嵌入程序的</font><font face="Calibri">mp3</font><font face="宋体">声音数据(以数组形式保存)播放出来。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">例如实验中使用的</font><font face="Calibri">welcome_mp3</font><font face="宋体">声音数据就保存在</font><font face="Calibri">include/welcome_mp3.h</font><font face="宋体">中。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> <span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">数组里的数据完全是按照</font><font face="Calibri">mp3</font><font face="宋体">文件格式排布的。比如前四个字节表示声音数据段的帧头。感兴趣的朋友可以自己研究一下</font><font face="Calibri">mp3</font><font face="宋体">文件格式,对理解这个声音播放</font><font face="Calibri">demo</font><font face="宋体">还是很有帮助的。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="Calibri">MP3</font><font face="宋体">声音数据是压缩过的数据,需要先解码成</font><font face="Calibri">PCM</font><font face="宋体">后才能播放。</font><font face="Calibri">Demo</font><font face="宋体">中</font><font face="Calibri">mp3</font><font face="宋体">数据解码是使用</font><font face="Calibri">pvmp3dec</font><font face="宋体">开源的</font><font face="Calibri">mp3</font><font face="宋体">解码库来实现的。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">解码后的数据,送到</font><font face="Calibri">AV_AO_ALSA PCM</font><font face="宋体">输出驱动进行音频输出。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">在花了</font><font face="Calibri">N</font><font face="宋体">多小时的研究后,终于定位到了音频解码后数据所在。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="Calibri">Player.c</font><font face="宋体">文件中</font><font face="Calibri">_ptask()</font><font face="宋体">任务函数中,调用</font><font face="Calibri">ad_decode()</font><font face="宋体">对</font><font face="Calibri">mp3</font><font face="宋体">数据进行解码。解码后数据存放在</font><font face="Calibri">dframe</font><font face="宋体">数组中。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri"> rc = ad_decode(ad, dframe, &got_frame, &pkt);</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">接下来对音频数据进行处理,提取出声音的强度。下面的处理只是简单的对</font><font face="Calibri">PCM</font><font face="宋体">幅度值进行平均处理。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">if(dframe->linesize > 1024){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">tempmaxvalue = 0;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t j=0;j<16;j++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">blocknumtemp = 0;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">temptotal = 0;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t i=0;i<32;i++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">temp = *(dframe->data+1+j*64+i*2);</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">temp = temp << 8;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">temp += *(dframe->data+j*64+i*2);</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">temptotal += temp;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">blocknumtemp = temptotal;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">if(blocknumtemp > tempmaxvalue){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">tempmaxvalue = blocknumtemp; </span></span> </span></span></span></p>
<p align="justify" style="margin-left:112px; text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">if(tempmaxvalue >0){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t j=0;j<16;j++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">blocknum = (blocknumtemp * 8)/tempmaxvalue;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">下面是把这些代表声音强度的数据在在屏幕以直方图形式显示出来。</font></span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">void draw_block(uint8_t x, uint8_t y)</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">{</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t i = x; i >= x-4; i--){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t j=y; j<= y+7; j++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">buf = 1;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t j=y; j<= y+7; j++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">buf = 0;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t i = x; i >= x-3; i--){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">buf = 0;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">buf = 0;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">void draw_blocks()</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">{</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">uint8_t block_row, block_col, blockrownum,x,y;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t i=0;i<64;i++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(uint8_t j=0;j<128;j++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">buf = 0;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(block_col=1;block_col<= BLOCKCOLNUM;block_col++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">for(block_row=1;block_row<= blocknum;block_row++){</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">x = 63 - (block_row -1)*5;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">y = (block_col - 1)*8;</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">draw_block(x, y);</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"> </p>
<p align="justify" style="text-align:justify"><span style="font-size:10.5pt"><span style="text-justify:inter-ideograph"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:Calibri">}</span></span></span></span></span></p>
<p>想到了我的mp4,哈哈</p>
<p>请教一下,数组里的音频数据是怎么生成的?<br/>比如我有一段mp3音频,怎么生成对应的数据?</p> xinmeng_wit 发表于 2022-5-15 08:29
请教一下,数组里的音频数据是怎么生成的?比如我有一段mp3音频,怎么生成对应的数据?
<p>就是把mp3文件数据转换成U16数组;可以使用“DataToHex”工具,这个工具可以把文件生成数组保存到.h文件中。</p>
oxygen_sh 发表于 2022-5-15 10:01
就是把mp3文件数据转换成U16数组;可以使用“DataToHex”工具,这个工具可以把文件生成数组保 ...
<p>明白了,感谢</p>
页:
[1]