unity Audio基本知识 一

基本知识:

1.unity只能播放44khz(16位)的音频吗?

答:不是,unity默认把所有的音频数据,重新采样到44khz的文件中,但这个取决于你的压缩格式,如果你的音频是48khz(24位)的音频,你希望不被重新采样,以原有的频率输出,则需要做的就是更改采样设置:

 

 

  • Preserve Sample Rate - 默认情况下,此设置使采样率保持不变,保留原有的速率
  • Optimize Sample Rate - 该设置根据分析的最高频率内容自动优化采样率。
  • Override Sample Rate - 此设置允许手动重写采样率,并可用于丢弃频率内容。

注意:高频率的音频,文件大,但是质量好,适合短音频,低采样率的音频,文件小,质量稍差,适合长音频

 

2.常用的API

 2.1 AudioSetting:

获得音频的播放模式(是单声道还是多声道还是环绕声)

获得mixer的当前输出速率

获得当前的音频配置AudioSettings.GetConfiguration

当前音频设置更改后触发的事件AudioSettings.OnAudioConfigurationChanged

当全局音频设置被更改时,通过audiosetings调用的委托。当操作系统控制面板改变采样率或默认输出设备的改变,例如当插入一个HDMI监视器或USB耳机。也会调用

通过AudioSettings.Reset设置音频配置,

比如:

   AudioConfiguration _audioConfig = AudioSettings.GetConfiguration ();
        _audioConfig.speakerMode = AudioSettings.driverCapabilities;
        AudioSettings.Reset (_audioConfig);

 2.2 AudioConfiguration 音频配置

    public AudioSpeakerMode speakerMode;  设置音频的声道

    public int dspBufferSize;dsp缓存大小
    public int sampleRate;//设置采样率
    public int numRealVoices;//设置游戏中同时可以播放的声音的数量
    public int numVirtualVoices//游戏中管理声音的最大数量。超过这个限度,声音就会停止播放。

 

2.3 AudioSpeakerMode 枚举,单声道还是多声道

     2.4Microphone

      public static string[] devices;//返回设备上可用的麦克风设备,返回名字数组

      public static void End(string deviceName);停止记录,如果传递为null或者空字符串,则为默认设备,下面的都一样

      public static void GetDeviceCaps(string deviceName, out int minFreq, out int maxFreq);//获得设备的支持的采样频率,如果最 大和最小当中有0,说明该设备支持任意采样频率

      public static int GetPosition(string deviceName);得到当前音频的位置,就是说当前音频采样的位置,返回值是以时间来返回的,可以用来延迟播放,如果不需要延迟,就和0比较,比如:while(!Microphone.GetPosition(null)>0){};audiosource.play();

  不大于0表示还没有采样,如果延迟3ms就让Microphone.GetPosition(null)>3,再播放

    public static bool IsRecording(string deviceName);//麦克风是否正在记录

    public static AudioClip Start(string deviceName, bool loop, int lengthSec, int frequency);//麦克风开始记录,第一个为null或者空字符串,表示默认设备,第三个参数是录制多长时间,第二个参数表示到了指定时间,是否还继续录制,相当于录制了同时录制两种声音,第四个参数是录制的采样频率,比如44100,返回值是一个音频文件

 注意:访问麦克风的时候会有相应的权限提示

  

 3.如果在本游戏中关闭了声音,但是想要听到其它应用播放的声音,怎么做?

   https://support.unity3d.com/hc/en-us/articles/206972423-Enable-selectively-the-Override-iPod-Music-player-option-from-code

4.怎么把audioclip打包到assetbundle中?

   创建一个空物体,添加audiosource组件,把要打包的audioclip添加上去,然后打包这个空物体

5.怎么让unity实时回放麦克风输入的话?

 

6.如果有很多音频,不想为每一个音频都新建一个空物体,怎么办?

 可以在一个物体上挂载多个AudioSource组件,然后通过PlayOneShot播放

7.音频有延迟怎么办?

音频延迟25ms一下,就认为是正常的,在iOS上延迟大概20ms,但是在安卓上大概在200-300之间,解决的方法一个是在projectsetting->audio->DSP Buffer Size->Best Latency ,或者使用插件

8.怎么优化音频,使性能更好?

1.对于长音乐,一般是背景音

  1. 使用 Load Type “Streaming” 、 Compression Format “Vorbis”. 这种组合将使用最少的内存,但是需要一些CPU能力和磁盘I/O吞吐量
  2. 使用Load Type “Compressed In Memory” and Compression Format “Vorbis”. 和上面的方法唯一的区别是需要一些内存来存放音频文件.你可以调节Quality slider 来调节压缩的比率,通常 100%太高了. 推荐使用 70 %. 但是,如果你有两个以上的环境音,长音乐,这会消耗大量的CPU资源

2.对于短音乐

  1. 对于经常播放的段音乐, 使用Decompress On Load 和PCM 或者 ADPCM 的压缩格式. 选用 PCM压缩格式,在需要的时候不需要解压,所以加载的比较快.  ADPCM需要解压,比 Vorbis要快一些
  2. 对于经常播放,大小中等的音频, 使用Compressed In Memory 和ADPCM 压缩格式. ADPCM比原生的 PCM耗时要少大概3.5倍,且它的解压缩算法要比 Vorbis.更少的消耗CPU资源
  3. 对于不经常播放,比较小的音频,使用 Compressed In Memory and ADPCM. 原因同上
  4. 对于不经常播放,中等大小的音频, 使用 Compressed In Memory and Vorbis Compression Format.

 

 

 

 

Gapless looping MP3 music in Unity 3D
关于unity下 播放无缝循环的音乐 我们会发现一个问题,就是音乐制作团队提供的文件,使用PC或是MAC的一些特殊的第三方播放器 可以听到 该文件是完美无缝循环的音乐,但是放到unity里时,却发现结尾处会卡停一下,或是播放不完整。下面的内容就是解释这是怎么回事,要如何解决呢?
一般游戏开发团队 最常用的格式MP3。他体积小 兼容IOS 或是 安卓又非常好,但是他有个致命的BUG,就是音乐不能完美循环或是完整播放。下面先说说为什么MP3有这样的BUG。
Gapless looping MP3 music in Unity 3D

Recently I have been struggling with my background music. As it appears MP3 music does not loop very well. Natively MP3 sounds have a little space at the start and end of the track.I thought that when you add a MP3 sound to your game, the new Unity setting called *Gapless looping* would solve that for you automatically.
The problem in my case was that the property was disabled.
After searching for answers on Unity questions and answers, Google etc I have found the solution myself!

The trick is to export your music as WAV. Meaning that it will be added to Unity as an uncompressed audio file. Next, select the audio file and watch the properties in the inspector. As you can see the drop down shows WAV and most of the other options are grayed out. Now, select MPEG from the drop down. By doing that we are telling Unity to compress the WAV file.
Watch what happens with the other properties: they become enabled!
Next, select Gapless looping and the other settings. If you are using the file as background music you might want to set priority to 0 for instance to prevent the sound from being swapped out. Also it probably doesn*t make sense to have 3D background music as the volume and panning does not apply to the position of your listener.
Finally click Apply and Unity will start creating the MP3 file for you. Add an empty game object to your game, add an Audio Source component to it and select the Audio clip. Make sure looping is enabled and start your game!

The music should loop perfectly. So to summarize: don*t import MP3 files into Unity but instead import WAV and let Unity create the MP3 for you. That way you can create nice looping background music.

The problem (I think) is, that mp3 has "header" data that it stores at the beginning and the end of the file because it is a compressed data format. This header data is represented as a silent gap in the audio file, usually at the beginning or end of the file.
(本文是跟Channel游戏音乐 美国总监 讨论的结果,所以该篇文章英文占了很大的内容)我用中文简短概述上面的问题。这个问题出在:mp3在开始和结束播放时都会储存一些标头数据("header" data),因为MP3格式是一个压缩数据格式。标头数据显示为一段无声的空隙,一般出现在文件起始或结尾处。所以再unity下 会出现循环时卡壳的情况。
Yi (奇异)should not be importing MP3 into Unity as the MP3 files already have this header data embedded into the file and it will not loop properly.
WAV audio, however, does not have this header data because it is uncompressed. According to the link above, the audio should be imported into Unity as WAV and then converted within Unity to MP3 upon import. There is also a "gapless looping" checkbox on this importer that will remove the header data from the MP3 file that Unity created. This way, when Unity imports uncompressed WAV audio, it will convert the audio to MP3 and also remove the header data from the file so that the file will then loop seamlessly.
所以不建议直接把MP3导入到Unity里,因为mp3文件已经产生了这些标头数据,无法正确循环播放,但是WAV文件就没有这种标头数据,因为WAV格式未经压缩。应使用WAV格式音频文件导入到Unity,再在Unity内部转换成MP3导入软件上有一个“无缝循环”的选项,可以去除Unity在MP3文件里产生的标头数据,这样就可以通过用Unity导入未压缩的WAV文件,并转为MP3、去除MP3里的标头数据,即可实现无缝循环。
If Yi is importing MP3 directly into Unity then it is importing an MP3 file that already has header data in it, and possibly redundantly converting it to MP3 again and adding additional header data. We should be importing uncompressed WAV files into Unity and letting Unity handle the MP3 conversion and the gapless looping feature, rather than importing MP3 directly to Unity.
如果直接把MP3导入Unity就无法避免产生标头数据,并且可能Unity还会再把它转成MP3,在此过程中又产生新的标头数据。所以应先把未压缩的WAV文件导入Unity,再让Unity将之转化为MP3,并实现无缝循环,而非直接导入MP3文件。当然音乐的部分,如果选择wav格式,最终untiy打包也会把wav的音乐一起打包,从而增大了容量。经过测试使用OGG作为源文件,导入untiy后,untiy自处理压缩后的得到的 无论是mpeg 还是 ogg ,完美无缝的循环表现都很不错,跟wav的表现几乎完全一样。音效部分, wav在untiy中是无解压过程的,所以音效的部分我们选择wav源文件没问题, 如果是音乐或是音效 要使用无缝LOOP的时候,建议使用OGG作为源文件 。并采用硬件播放方式从而减少内存的损耗。


最后补充:格式的选择 只是平台和引擎下的表现能力。但是要完美无缝循环 还是需要先得到完美无缝循环的音乐音效文件,Channel游戏音乐欢迎您 到我们的音频团队 来制作您最想要的声音体验。


游戏音乐制作,游戏配音,游戏音效制作,Channe游戏音乐工作室

 

 

 

 

 

 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TO_ZRG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值