• 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
Question by $$anonymous$$ · Mar 06, 2015 at 02:41 PM · c#wwwaudioclipaudioclip www load

Playing wav audio with integer samples

I am trying to request wav audio from Nuance NDEV TTS inside Unity and play back the audio. I was hoping that WWW.GetAudioClip(false, false, AudioType.WAV) would work, but it seems that it does not. There is no error when I try to play it (I am checking the isReadyToPlay property of the AudioClip), but I hear nothing and the length of the AudioClip is 0.

I have a suspicion about what the problem is. Previously, I was able to convert the response from Nuance into an array of integers and then to an array of floats within a Python script (with the aid of SciPy: http://nbviewer.ipython.org/github/mgeier/python-audio/blob/master/audio-files/audio-files-with-scipy-io.ipynb). I then sent this array of floats over HTTP to Unity and used AudioClip.SetData. This worked fine. So, I'm thinking maybe GetAudioClip only works with wav files in which the samples are floats, which seems odd since apparently wav files most commonly use integers (maybe floats are the norm for sound for games?).

The next step, then, is for me to figure out how to get an array of floats from a wav file in C#. It looks like NAudio might be a good possibility (http://naudio.codeplex.com/).

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by $$anonymous$$ · Mar 12, 2015 at 02:36 AM

It turns out I made a wrong assumption, which was that the response from Nuance was a WAV file. In fact, it's simply raw 16-bit PCM audio (no header). That is, every 2 bytes in the response is a 16-bit signed integer representing a sample. I wrote the following method to convert the binary response to an array of shorts and then to an array of floats. The audio plays back fine in Unity.

     float[] PCM2Floats(byte[] bytes)
     {
         // See pcm2float in https://github.com/mgeier/python-audio/blob/master/audio-files/utility.py
         float max = -(float)System.Int16.MinValue;
         float[] samples = new float[bytes.Length / 2];
         
         for (int i = 0; i < samples.Length; i++)
         {
             short int16sample = System.BitConverter.ToInt16(bytes, i * 2);
             samples[i] = (float)int16sample / max;
         }
         
         return samples;
     }


Comment
fredsa

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

WWW.GetAudioClip Freeze 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to get audioclip from UnityWebRequest 1 Answer

loading/streaming audioclip using www class [android] 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