Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 RoelfMik · May 11, 2014 at 07:16 PM · audiofrequencysamplerate

Why is the data I pass into OnAudioFilterRead() resulting in glitchy audio?

I am trying to read data from an AudioClip using GetData(), and play it using OnAudioFilterRead().

However, the audio that I am hearing has the wrong pitch/speed + distortion.

Here's my project (Google Drive), and below is the code in question:

 using UnityEngine;
 using System.Collections;
 
 public class myPlay : MonoBehaviour 
 {
 
     //I have an AudioSource that I can use to start/stop audio...
     public AudioSource myAudioSource;
 
     //I have an AudioClip that I want to play...
     public AudioClip myAudioClip;
 
     //myBuffer will containt the sample values that are to be played by OnAudioFilterRead()...
     float[] myBuffer;
 
     //_N is the offset for AudioClip.GetData()...
     int _N;
 
     //_Buffer determines if it is time to load new sample values into myBuffer...
     bool _Buffer;
     
     void Start() 
     {
 
         //I want to start at the first sample...
         _N = 0;
 
         //Determine outputSampleRate based on sample frequency of AudioClip...
         AudioSettings.outputSampleRate = myAudioClip.frequency;
 
         //Initialize myBuffer...
         myBuffer = new float[2048];
 
         //Load sample values from myAudioClip into myBuffer...
         myAudioClip.GetData(myBuffer, _N);
 
         //Raise _N, to update offset...
         _N += 2048;
 
         //Set _Buffer to false so it'll go through OnAudioFilterRead() before loading in a new set of sample values...
         _Buffer = false;
 
         //Start audio...
         myAudioSource.Play();
 
     }
 
     void Update()
     {
 
         //If it is time to load in new samples values...
         if (_Buffer)
         {
 
             //Load sample values from myAudioClip into myBuffer...
             myAudioClip.GetData(myBuffer, _N);
 
             //Raise _N, to update offset...
             _N += 2048;
 
             //Set _Buffer to false, to make sure it'll go through OnAudioFilterRead before loading in a new set of samples values...
             _Buffer = false;
 
         }
 
     }
 
     void OnAudioFilterRead(float[] data, int channels)
     {
 
         //Move all sample values contained in myBuffer tot data...
         for (int a = 0; a < 2048; a++)
         {
             
             data[a] = myBuffer[a];
 
         }
 
         //Set _Buffer to true, so a new set of sample values will be loaded into myBuffer...
         _Buffer = true;
         
     }
 
 }
 


Comment
Add comment · Show 3
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 gfoot · May 11, 2014 at 08:41 PM 1
Share

Why 2048? You should use data.Length ins$$anonymous$$d. Also be prepared for the number of channels to vary.

avatar image RoelfMik · May 11, 2014 at 09:03 PM 0
Share

Thank you for your response!

If I Debug.Log(data.Length) it gives me 2048. I believe this is the standard buffer size for 44.100 files. Is there a way to get the buffer size before OnAudioFilterRead() is called, so I can correctly size myBuffer[] based on the file I'm trying to play?

Is the interleaving of the channels different in the AudioClip than it is in OnAudioFilterRead()? How should I prepare for a varying number of channels?

avatar image RoelfMik · May 12, 2014 at 12:57 PM 0
Share

I changed: _N += data.Length, into: _N += 1024, and now it seems to work.

But, I don't get exactly how/why.

There are still some audible glitches now and then, but the pitch/playback speed is now correct, and the constant distortion is gone.

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class myPlay : $$anonymous$$onoBehaviour 
 {
     
     public AudioSource myAudioSource;
 
     public AudioClip myAudioClip;
 
     float[] myBuffer;
 
     int _N;
 
     bool _Buffer;
     
     void Start() 
     {
 
         _N = 0;
 
         AudioSettings.outputSampleRate = myAudioClip.frequency;
 
         myBuffer = new float[2048];
 
         myAudioClip.GetData(myBuffer, _N);
 
         _Buffer = false;
 
         myAudioSource.Play();
 
     }
 
     void Update()
     {
 
         if (_Buffer)
         {
 
             myAudioClip.GetData(myBuffer, _N);
         
             _Buffer = false;
 
         }
 
     }
 
     void OnAudioFilterRead(float[] data, int channels)
     {
 
         if (!_Buffer)
         {
 
             for (int a = 0; a < data.Length; a++)
             {
             
                 data[a] = myBuffer[a];
 
             }
 
             _N += 1024;
 
             _Buffer = true;
 
         }
 
     }
 
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sylafrs · Nov 06, 2015 at 10:52 AM

I guess it's a little too late, but the issue might come from the channels :)

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

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

22 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

Related Questions

how to downsampling audio data 0 Answers

Getting beats from a song using frequency range 1 Answer

Calculating reverberation time in Resonance Audio for Unity,Reverberation time in Resonance Audio (Unity) 0 Answers

How can I use spectrum data to create a frequency based beat detection? 1 Answer

Get the frequency from an AudioSource 1 Answer


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