Getting Started with Unity 8 - Sound System

1. Audio file parameter panel

  1. Force To  Mono : multi-channel to mono

    • Normalize: When forced to mono, it is normalized during mixing
  2. Load In Background: Loading in the background, without blocking the main thread, suitable for large sound effects

  3. Ambisonic: stereo reverberation sound

    Ideal for 360 video and XR applications

    Enable this option if the audio file contains ambisonic encoded audio

  4. Load Type: load type

    • Decompress On Load: no compression

      Fast loading, high memory usage, suitable for small sound effects

    • Compress In Memory: Compression

      Slow loading, small memory, only suitable for large sound files

    • Streaming: Streaming exists in memory and decoded when used

      Minimal memory usage, high CPU consumption, use performance for memory

  5. Preload Audio Data: preload audio

    After checking, enter the scene and load

    If it is not checked, it will be loaded when it is used for the first time

  6. Compression Format: compression method

    • PCM: audio is stored at the highest quality
    • Vorbis: smaller than PCM compression, depending on quality
    • ADPCM: Contains noise, sounds that will be played multiple times, such as collision sounds, strike sounds
  7. Quality: audio quality

    Determines the amount of compression to apply to compressed clips

    Not available for PCM / ADPCM / HEVAG formats

  8. Sample Rate Setting: Sample rate setting

    PCM/ADPCM allows automatic optimization or manual downsampling

    • Preserve Sample Rate: Keep the sample rate unchanged (default value)
    • Optimize Sample Rate: Automatically optimizes sample rate based on highest frequency content
    • Override Sample Rate: Allows manual override of the sample rate

2. Audio Source Audio Source

  1. Audio Clip: sound clip file (audio file)

  2. Output: output

    By default it will output directly to the audio listener in the scene

    Can be changed to output to mixer

  3. Mute: mute switch

  4. Bypass Effects: switch filter effect

  5. Bypass Listener Effects: Quickly switch all listeners

  6. Bypass Reverb Zones: Quickly switch all reverb zones

  7. Play On Awake: Automatically play when the object is created

  8. Loop: loop switch

  9. Priority: priority

    0 is the highest and the default is 128. The higher the priority, the less likely it will be covered by other sound effects

  10. Volume: volume size

  11. Pitch: pitch

  12. Stereo Pan: Stereo position for 2D sound

    Equivalent to left and right channels

  13. Spatial Blend: How much the audio is affected by 3D space

    0 is 2D, 1 is 3D, generally only use these two

  14. Reverb Zone Mix: Amount of output signal to the reverb zone

  15. 3D Sound Settings: Applied proportionally to the Spatial Blend parameters

    • Doppler Level: Doppler effect level

      generally do not change

    • Spread: Whether the spread angle is set to 3D stereo or multi-channel

    • Volume Rolloff: Sound decay speed

      • Logarithmic Rolloff

        Loud when close to audio source

        When moving away from the subject, the sound decreases rapidly

      • Linear Rolloff

        The farther the distance from the audio source is, the smaller the sound is heard, showing a linear relationship

      • Custom Rolloff

        The audio effect of the audio source is customized by the graph

  • Min / Max Distance

    Within the minimum distance, the sound maintains the maximum loudness

    Outside the maximum distance, the sound becomes 0

3. Audio Listener

​ Make sure there is one and only one Audio Listener script in the scene

4. Code control

(1) Control play, stop

audioSource = this.GetComponent<AudioSource>();

// 播放音效
audioSource.Play();

// 延迟播放 填写的是秒数
audioSource.PlayDelayed(5);

// 停止音效
audioSource.Stop();

// 暂停
audioSource.Pause();

// 停止暂停 和暂停后Play效果是一样的 都会继续播放现在的音效
audioSource.UnPause();

(2) Detect that the sound effect is played

// 如果你希望某一个音效播放完毕后 想要做什么事情
// 那就可以在Update生命周期函数中 不停的去检测 它的 该属性
// 如果是false就代表播放完毕了
if (audioSource.isPlaying) print("播放中");
else print("播放结束");

(3) Dynamically control sound playback

// 1.直接在要播放音效的对象上挂载脚本 控制播放

// 2.实例化挂载了音效源脚本的对象
// 这种方法 其实用的比较少
Instantiate(obj);

// 3.用一个AudioSource来控制播放不同的音效
AudioSource aus = this.gameObject.AddComponent<AudioSource>();
aus.clip = clip;
aus.Play();

// 潜在知识点 
// 一个GameObject可以挂载多个 音效源脚本AudioSource
// 使用时要注意 如果要挂载多个 那一定要自己管理他们 控制他们的播放 停止 不然 我们没有办法准确的获取
// 谁是谁

5. Code control microphone  Microphone

(1) Obtain device microphone information

string[] strs = Microphone.devices;
for (int i = 0; i < strs.Length; i++) print(strs[i]);

(2) Start recording

// 参数一:设备名 传空使用默认设备
// 参数二:超过录制长度后 是否重头录制
// 参数三:录制时长
// 参数四:采样率
clip = Microphone.Start(null, false, 10, 44100);

(3) End recording

Microphone.End(null);

// 第一次去获取 没有才添加 
AudioSource s = GetComponent<AudioSource>();
if (s == null)
    s = gameObject.AddComponent<AudioSource>();
s.clip = clip;
s.Play();

(4) Acquiring audio data for storage or transmission

// 规则 用于存储数组数据的长度 是用 声道数 * 剪辑长度
float[] f = new float[clip.channels * clip.samples];
clip.GetData(f, 0);
print(f.Length);
t.AddComponent<AudioSource>();
s.clip = clip;
s.Play();

Guess you like

Origin blog.csdn.net/weixin_53163894/article/details/131208026