Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by JasonBennett · Oct 30, 2017 at 03:54 PM · audioaudiolistenergetoutputdata

Converting AudioListener.GetOutputData to a wav file

Hello @aldonaletto,

I've read your helpful forum posts about audio in Unity, and I'm wondering if you might be able to help me. (Anything would be appreciated!)

Not for lack of googling I am tearing my hair out trying to understand how GetOutputData works, and how to transform that data into a file. This is my first attempt wrangling with audio, so I might get some terminology wrong. Bear with me...

My task is to use the AudioListener as a simulated microphone in a simulated 3D environment, and then export the "recorded" audio as a wav file (or similar uncompressed audio format). My AudioSource clip is playing a 10 second wav file at 44100hz.

I imagined that if I capture the right number of samples for each frame and store them in a list, then that list should translate into the recorded audio. Instead it's... something else (sounds like the clicks of a record player stuck at the end of the record).

I'm obviously missing something about how GetOutputData() works; there is surprisingly little documentation / forum posts I can find that explain what that gets and how to use it. (Most of them are of the sort "GetOutputData() gets your output data and stores them in a float array based on how many samples you want", which might make sense to somebody who understands what that data is in the first place, but it does not make sense to me.)

This is the code attached to my main camera, which has the AudioListener as well as its own AudioSource component for capturing the "recorded" clip. The other AudioSource clip is playing on an empty object nearby.

 private float[] outputData;
 private List<float> outputValues;
 private float[] outputValuesFloat;
 
     public AudioClip thisAudio;
 
     // Use this for initialization
     void Start () {
        
         outputData = new float[512];
         Invoke("PlayData", 5);
         outputValues = new List<float>();
         thisAudio = AudioClip.Create("thisAudio", 110592, 1, 44000, false);
     }
     
     // Update is called once per frame
     void Update () {
         AudioListener.GetOutputData(outputData, 0);
         for (int i = 0; i < outputData.Length; i++)
         {
             outputValues.Add(outputData[i]);
         }
     }
 
     void PlayData()
     {
         
         outputValuesFloat = new float[outputValues.Count];
         int j = 0;
         foreach (float thisFloat in outputValues)
         {
             outputValuesFloat[j] = thisFloat;
             j++;
         }
         thisAudio.SetData(outputValuesFloat, 0);
         this.GetComponent<AudioSource>().clip = thisAudio;
         this.GetComponent<AudioSource>().Play();






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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Towerss · Jul 24, 2018 at 12:54 AM

Hi @curiousKoala, just wondering if you got to solve this? I am trying to record a WAV file using AudioListener.GetOutputData as well. Thanks.

Comment
Add comment · Share
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
avatar image
0

Answer by JasonBennett · Jul 24, 2018 at 02:26 PM

Hey Towerss,

I did end up solving it, by bypassing the AudioListener and doing a basic calculation of spatial audio myself. Then I just calculated the distance from my "listener" to the AudioSource, and used the AudioSource.GetOutputData with my custom spatial audio calculation applied.

In terms of writing the data to a WAV file, I used this script: https://gist.github.com/darktable/2317063

Shoutout to darktable for writing it :)

-Jason

Comment
Add comment · Show 4 · Share
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
avatar image Towerss · Jul 26, 2018 at 05:36 AM 0
Share

Thanks Jason for your reply. $$anonymous$$y issue was that the gameobject and the component AudioListener were hidden with a combination of a combination of [HideInInspector], HideFlags.HideInHierarchy, and HideFlags.HideInInspector. Once I figured out where to find them all good. Thanks :)

avatar image Lautaro-Arino · Aug 30, 2021 at 09:44 AM 0
Share

Hey Jason! Im in the exact same situation. Im trying to make sense on how to record the mixed audio through audio listener. At the moment i get sound data that resembles the audio but its very weird and garbled. I understand that you instead took the audio data from all audiosources playing and mixed them programmatically to get the audio data? Would you $$anonymous$$d sharing how? I agree that its weird that this function is not very well documented with and example. And i cant either seem to find someone explaining how its supposed to be used.

avatar image Towerss · Aug 30, 2021 at 12:50 PM 0
Share

@Lautaro-Arino This post in SO might be handy: https://stackoverflow.com/questions/50302315/save-audio-stream-float-frames-as-wav-with-c-sharp I do not have the source code I used, because it was a contract project, but I used that answer to actually save the audio file. hope it helps.

avatar image Lautaro-Arino · Aug 30, 2021 at 01:32 PM 0
Share

Thanks! But I'm looking to get the audio data out of the audio listener and play it back. I think I have the save to file covered. Do you have any other tip?

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

134 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 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

Read musice before it plays? 0 Answers

Can't hear audio in Unity 0 Answers

Audio coming through iPhone earpiece 0 Answers

How can I make the sounds work when I press a button? 0 Answers

Disabling the AudioListener works in Editor but not in Build. 2 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