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 dmitry_kovalchuk · Apr 19, 2016 at 02:54 PM · soundsound effects

AudioClip calculates length wrong

Hi everyone !

When I'm loading clips from web I've got this issue.

  private IEnumerator WaitAudioclipLoad(string shortCode, string url)
         {
             WWW ww = new WWW(url);
             yield return ww;
             if (ww.error != null)
             {
                 Debug.Log("ErrorOccured :" + ww.error);
                 OnAudioclipLoaded(shortCode, null);
             }
             else
             {
                 var audioClip = ww.GetAudioClip(false, false, AudioType.MPEG);
                 audioClip.LoadAudioData();
 
                 File.WriteAllBytes(Application.dataPath + "/" + shortCode + ".mp3", ww.bytes);
                 Debug.Log("\n\n\n");
                 Debug.Log("channels         " + audioClip.channels);
                 Debug.Log("frequency        " + audioClip.frequency);
                 Debug.Log("length           " + audioClip.length);
                 Debug.Log("loadState        " + audioClip.loadState);
                 Debug.Log("loadType         " + audioClip.loadType);
                 Debug.Log("preloadAudioData " + audioClip.preloadAudioData);
                 Debug.Log("samples          " + audioClip.samples);
 
                 Debug.Log("\n\n\n");
                 CacheProvider.CacheProvider.Instance.AddToCache(shortCode, ww.bytes, audioClip);
                 OnAudioclipLoaded(shortCode, audioClip);
             }
             ww.Dispose();
         }

i load the clip via www ( and wait til the clip is loaded). ater that I save the clip locally ( exaple is for editor) and dump AC data to console.

and some strange things I sow in console:
1) if frequency is 44100Hz - length is normal alt text

2) if frequency is 24000Hz - length is multiplied by two alt text

Is there any way to get normal lenght ?

screen-shot-2016-04-19-at-171618.png (26.9 kB)
screen-shot-2016-04-19-at-171603.png (27.0 kB)
Comment
Add comment · Show 1
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 Cherno · Apr 19, 2016 at 05:02 PM 0
Share

$$anonymous$$inda hacky, but you could just multiply the length by clip's Frequency / 44100Hz (if you can access the frequency via script).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 20, 2016 at 01:06 AM

I don't see any error here. Your first clip has 662400 samples at 44.1 kHz and your second clip has 774144 samples at 24 kHz. Just calculate it yourself:

 662400 / 44100 == 15.02040 seconds

 774144 / 24000 == 32.256 seconds

Also keep in mind that mp3 can only be used on Android / iOS and maybe on windows phone as well. On all other platforms you can't dynamically load mp3s. See the documentation for more details. I'm also not sure if you can actually access any data of mp3s on Android as Unity just uses the hardware player to play mp3. Have you actually tried playing those clips? Also for which platform do you develop?

Comment
Add comment · Show 2 · 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 dmitry_kovalchuk · Apr 20, 2016 at 08:44 AM 0
Share

Hi. I develop now for iOS & android. so stream/download mp3 files is good for now.

I don't see any error here. Your first clip has 662400 samples at 44.1 kHz and your second clip has 774144 samples at 24 kHz

but the problem is that the second track (774144 samples at 24 kHz) really has 16.128 seconds when I save the file locally - any music player says that track has 16 second length. Even if I drop mp3 file in unity's assets folder - editor in preview displays 16.128 seconds lenght. but if I load file via www - I receive double length 32.256s

avatar image Bunny83 dmitry_kovalchuk · Apr 21, 2016 at 09:43 PM 0
Share

Well, the only thing that's possible is, that it only detects a single channel. As you can see at the top the audio file has 1 channel. That means it's interpreted as mono audio. If it's actually a stereo audio file the time would be half the size. The actual formula for the clip length is:

 length = nSampled / (freq * nChannel).

So either it's format isn't recognised correctly or the file is otherwise corrupted such as that the header contains wrong data.

As said, as far as i know Unity only supports mp3 strea$$anonymous$$g on mobile. I don't think you can actually load an mp3 to access the decompressed PC$$anonymous$$ samples. Also you usually can only play one mp3 at a time as it's played by the native (hardware) mp3 decoder on those devices.

I have never really tested this. $$anonymous$$aybe that has changed but i doubt it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make walking sound effects? 0 Answers

3D sound crackling problem 0 Answers

How do I make enemies play a sound when they die? 1 Answer

how to make an sliding sound effect? 2 Answers

Question about copyright on sound effects. 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