Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheCatProblem · Mar 28, 2018 at 03:11 PM · audiosourcegetoutputdata

AudioSource scales audio amplitude by 0.7071 (square root of 0.5)

When an AudioSource plays an AudioClip, it appears to scale the audio data by the square root of 0.5 (0.70710678...), i.e., a value of 1.0 becomes 0.7071, 0.5 becomes 0.3536, etc. This corresponds to a reduction in gain of 3 dB (20*log10( sqrt(1/2) / 1 ) = -3.0103); -3 dB is a common value for limiters in audio production.

For example, given a wave file with a sample rate of 48000 Hz (which matches AudioSettings.outputSampleRate on my machine) containing one second of a 1 Hz sine wave, the data returned by the AudioClip match the wave file (apart from some minor compression artifacts) but the AudioSource data are scaled. Here's a plot (the ranges are limited by the amount of data that GetData and GetOutputData can return):

Audio data

This behaviour doesn't seem to be documented; can it be disabled?

For reference, here's the code I used to collect the data plotted above:

 using System.IO;
 using UnityEngine;
 
 public class AudioTest : MonoBehaviour
 {
     public AudioSource audioSource; // Should loop, play on awake, and be 2D.
     public AudioClip audioClip; // Should be the same as the clip specified in the AudioSource's parameters.
 
     private const int SampleCount = 16384; // Should be a power of two for AudioSource.GetOutputData().
     private float[] _samples;
 
     private bool _hasSavedOutput = false;
 
     void Awake()
     {
         _samples = new float[SampleCount];
     }
 
     void Start ()
     {
         print("Sample rate of output device is " + AudioSettings.outputSampleRate + " Hz.");
         audioClip.GetData(_samples, 0);
         OutputSamples("fromClip");
     }
     
     void Update ()
     {
         if (!_hasSavedOutput)
         {
             audioSource.GetOutputData(_samples, 0);
 
             // First couple calls to GetOutputData return no data.
             // Apparently this is by design (see https://issuetracker.unity3d.com/issues/audiosource-dot-getoutputdata-returns-an-empty-array-the-first-time-it-is-called).
             if (!Mathf.Approximately(_samples[0], 0.0f))
             {
                 _hasSavedOutput = true;
                 OutputSamples("fromSource");
             }
         }
     }
 
     void OutputSamples(string filename)
     {
         string output = string.Empty;
 
         foreach (float value in _samples)
         {
             output += value + "\n";
         }
 
         StreamWriter writer = new StreamWriter("C:/TestFolder/" + filename + ".txt");
         writer.Write(output);
         writer.Close();
     }
 }


plotaudiodata-output.png (37.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What does 'float[] sample' in GetOutputData represent? 2 Answers

Question Related to GetSpectrumData and GetOutputData 0 Answers

Multiple audiosources 1 Answer

Play AudioSource in editor 0 Answers

Distortion of sound when doing audio.stop() 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges