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
1
Question by Rainault · Jan 01, 2013 at 11:37 PM · audioaudioclip

Receiving float[][] instead of float[] for AudioClip.PCMReaderCallback

I'm playing around with making a runtime-generated AudioClip, but I'm running into a strange issue where I'm receiving a float[][] for the sample buffer on my PCMReaderCallback, when I should be receiving a float[].

The code is pretty simple:

 using UnityEngine;
 using System;
 
 public class RuntimeAudioClip : MonoBehaviour
 {
    private AudioClip _virtualClip;
 
    void Start()
    {
       _virtualClip = AudioClip.Create("test", 1, 1, 1, false, true, OnAudioRead);
       audio.clip = _virtualClip;
    }
 
    void OnAudioRead(float[] data)
    {
       if (!(data is float[]))
       {
          throw new Exception(string.Format("expected '{0}', got '{1}'",
             typeof(float[]), data.GetType()));
       }
    }
 }

I'm rather befuddled. Changing the type of the "data" parameter causes a compile-time error, since it doesn't match the signature of the PCMReaderCallback delegate.

I verified that this breaks on a minimally-built project in both Unity 3.5 and Unity 4. It's easy to repro this yourself. Just create a new project, attach the above script to an Empty Object, attach an AudioSource component, and run the game. You'll hit the exception immediately.

Any ideas?

Comment
Add comment · Show 5
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 Rainault · Jan 02, 2013 at 12:10 AM 1
Share

The interesting thing is that you can still write to the data array as though it were a float[]. I ran into this issue when I tried to use Array.Copy to copy samples from a local float[] buffer to the float[] data array. I hit an ArrayType$$anonymous$$ismatchException.

I created a local float[][] reference to the data array using the sledgehammer cast approach: (float[][])(object)data. After writing some samples to the array, I attached the $$anonymous$$onoDevelop debugger and tried to inspect values through the float[][] reference. Then Unity crashed. I described the situation in the crash report, so hopefully someone at Unity can help explain what's going on.

avatar image bdubs · Jul 26, 2013 at 03:58 PM 1
Share

Has there been a solution to this one yet? I'm seeing the same thing and would like to know what the best approach is to handle this.

avatar image Jamora · Jul 26, 2013 at 04:03 PM 1
Share

If you need access to the data, you can use OnAudioFilterRead, as described here Generating Procedural Audio. As to the OP's question; I have no idea.

avatar image gregzo · Jul 26, 2013 at 08:49 PM 1
Share

PC$$anonymous$$ReaderCallbacks are super buggy and undocumented. I was advised by Unity's previous audio dev not to go near them for audio generation - use OnAudioFilterRead.

avatar image Tony-Lovell · Mar 04, 2016 at 08:51 AM 1
Share

Happy to report the PC$$anonymous$$ReaderCallback did exactly what I needed in fairly straightforward manner on Unity 5.3 (and it did indeed work to just handle a "float[]" parameter, as documented).

Those wishing to use it might be warned that the callback comes from a low level thread and not the main Unity thread; if you wish to supply audio frames from other threads, you will need threadsafe means of handing it in to your callback.

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

11 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

Related Questions

Breathing volume increases as player gets more tired 0 Answers

Beginners troubles with audiosource 0 Answers

Triggering multiple audio clips to play in sequence 1 Answer

Unitywebrequest returns fmod error 1 Answer

OnTriggerExit stop audio 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