开源的音频处理库 SoundTouch 实现变声技术

jopen 13年前
     <img style="width:103px;height:103px;" border="0" alt="开源的音频处理库 SoundTouch 实现变声技术" src="https://simg.open-open.com/show/8ccf4c5501aac85c1a0e3dddadff16fb.jpg" width="144" height="144" />    <br /> SoundTouch是一个开源的音频处理库,主要实现包含变速、变调、变速同时变调等三个 功能模块,能够对媒体流实时操作,也能对音频文件操作。采用32位浮点或者16位定点,支持单声道或者双声道,采样率范围为8k~48k。    <br />    <br />    <p>要在ios中实现一个变声技术,而且又要要求能在iphone 3g上也能运行,所以自带的一些api就显得比较麻烦,因此决定使用soundtouch开源库,该库可以实现变声效果,包括可以调节声音的频率而不改变 声音的长度,大家可以使用openal试试,改变频率之后,声音就会变短了。</p>    <p>呵呵,有了这个库,其实我们就可以山寨一个talkingtom了,记住我不是叫大家去山寨,这里只是谈谈学习的技术。还是要创新才好啊。</p>    <p> </p>    <p>过程大概如下:</p>    <p>首先下载soundtouch开源库,这个可以到项目主页去找了,http://www.surina.net/soundtouch/。</p>    <p>下载之后解压,将“soundtouch/source/SoundTouch”中的文件全部拷贝到要添加的ios项目中,暂时记不清出要不要修改其他的参数了,如果又任何错误则欢迎讨论,应该是可以直接编译的,加入之后如下图。</p>    <p><a href="https://simg.open-open.com/show/680f248eb6d14be0239abe0a9788e7a1.jpg" target="_blank"><img title="[项目总结]在ios中使用soundtouch库实现变声" border="0" alt="开源的音频处理库 SoundTouch 实现变声技术" src="https://simg.open-open.com/show/680f248eb6d14be0239abe0a9788e7a1.jpg" width="222" height="424" /></a></p>    <p>因为soundtouch是一个c++编写的开源库,所以要在ios中使用,就要和object-c混编了,该改后缀名的就改了吧。</p>    <p>下面我们要修改音频文件主要调整的几个函数如下了:</p>    <ol>     <li><span><span>mSoundTouch.setSampleRate(sampleRate);</span><span>//设置声音的采样频率</span><span> <wbr /></span></span> </li>     <li><span>mSoundTouch.setChannels(channels);<span>//设置声音的声道</span><span> <wbr /></span></span> </li>     <li><span>m_SoundTouch.setTempoChange(tempoDelta); <wbr /><span>//这个就是传说中的变速不变调</span><span> <wbr /></span></span> </li>     <li><span>m_SoundTouch.setPitchSemiTones(pitchDelta);<span>//设置声音的pitch</span><span> <wbr /></span></span> </li>     <li><span>mSoundTouch.setRateChange(rateDelta);<span>//设置声音的速率</span><span> <wbr /></span></span> </li>     <li><span><span>// <wbr />quick是一个bool变量,USE_QUICKSEEK具体有什么用我暂时也不太清楚。</span><span> <wbr /></span></span> </li>     <li><span>mSoundTouch.setSetting(SETTING_USE_QUICKSEEK, <wbr />quick); <wbr /></span> </li>     <li><span><span>// <wbr />noAntiAlias是一个bool变量,USE_AA_FILTER具体有什么用我暂时也不太清楚。</span><span> <wbr /></span></span> </li>     <li><span>mSoundTouch.setSetting(SETTING_USE_AA_FILTER, <wbr />!(noAntiAlias)); <wbr /></span> </li>    </ol>    <p>其中mSoundTouch是一个soundtouch对象了。至于要怎么变就看你怎么调节参数了,soundtouch的特点好像就是吧声音变得卡通了,这个按照自己的需求去选择吧,也还有其他的一些开源库了。</p>    <p>在文末会给大家提供一个soundtouch整合iphone自带的SpeakHere的例子了,但是不知道能不能上传,好像有点儿大,如果不能上传就到http://weibo.com/yarin上找我吧。</p>    <p>最后需要注意的两个问题:</p>    <p>已开始我编译出来,声音很奇怪也有噪音很大,还是花了很多时间来找原因,但是都无果,突然想起前面我们说过,它同时支持32位浮点和16位定点,默 认时采用32位浮点的,我将其改为16位定点数的,哈哈,对了,效果很好,更改方式在STTypes.h文件中找到FLOAT_SAMPLES宏,注释 掉,并打开INTEGER_SAMPLES宏,如下:</p>    <ol>     <li><span><span>#if <wbr />!(INTEGER_SAMPLES <wbr />|| <wbr />FLOAT_SAMPLES)</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr />Choose <wbr />either <wbr />32bit <wbr />floating <wbr />point <wbr />or <wbr />16bit <wbr />integer <wbr />sampletype</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr />by <wbr />choosing <wbr />one <wbr />of <wbr />the <wbr />following <wbr />defines, <wbr />unless <wbr />this <wbr />selection <wbr /></span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr />has <wbr />already <wbr />been <wbr />done <wbr />in <wbr />some <wbr />other <wbr />file.</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>////</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr />Notes:</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr />- <wbr />In <wbr />Windows <wbr />environment, <wbr />choose <wbr />the <wbr />sample <wbr />format <wbr />with <wbr />the</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr /> <wbr /> <wbr />following <wbr />defines.</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr />- <wbr />In <wbr />GNU <wbr />environment, <wbr />the <wbr />floating <wbr />point <wbr />samples <wbr />are <wbr />used <wbr />by <wbr /></span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr /> <wbr /> <wbr />default, <wbr />but <wbr />integer <wbr />samples <wbr />can <wbr />be <wbr />chosen <wbr />by <wbr />giving <wbr />the <wbr /></span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr /> <wbr /> <wbr />following <wbr />switch <wbr />to <wbr />the <wbr />configure <wbr />script:</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />./configure <wbr />--enable-integer-samples</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr /> <wbr /> <wbr />However, <wbr />if <wbr />you <wbr />still <wbr />prefer <wbr />to <wbr />select <wbr />the <wbr />sample <wbr />format <wbr />here <wbr /></span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr /> <wbr /> <wbr />also <wbr />in <wbr />GNU <wbr />environment, <wbr />then <wbr />please <wbr />#undef <wbr />the <wbr />INTEGER_SAMPLE</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>/// <wbr /> <wbr /> <wbr />and <wbr />FLOAT_SAMPLE <wbr />defines <wbr />first <wbr />as <wbr />in <wbr />comments <wbr />above.</span><span> <wbr /></span></span> </li>     <li><span><span> <wbr /> <wbr /> <wbr /> <wbr />#define <wbr />INTEGER_SAMPLES <wbr /> <wbr /> <wbr /> <wbr /> <wbr />1 <wbr /> <wbr /> <wbr /> <wbr />//< <wbr />16bit <wbr />integer <wbr />samples</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /> <wbr /> <wbr /><span>//#define <wbr />FLOAT_SAMPLES <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />1 <wbr /> <wbr /> <wbr /> <wbr />//< <wbr />32bit <wbr />float <wbr />samples</span><span> <wbr /></span></span> </li>     <li><span> <wbr /> <wbr /></span> </li>     <li><span><span> <wbr />#endif</span><span> <wbr /></span></span> </li>    </ol>    <p>另外一个问题就是在模拟器上测试都完全无误了,这个时候放到真机设备上,直接报错,退出程序,又一个花了我不少时间的问题,最终发现,在真机上需要将声道改变为单声道,如下,在自己设置的时候使用:</p>    <ol>     <li><span><span>mSoundTouch.setChannels(2);</span><span>//1则为单声道</span><span> <wbr /></span></span> </li>    </ol>    <p>大功告成,当然了如果你要继续了解soundtouch库,那么这里(http://blog.csdn.net/leilu2008/article/details/6540575)又一系列的参考资料。</p>