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 /
  • Help Room /
avatar image
0
Question by jan_fechner · Nov 08, 2015 at 11:32 AM · audioclipsampling

Get PCM data from an mp3 track with a desired number of samples - AudioClip.GetData and mpg123

I've been trying to retrieve the raw PCM data from a music track stored in an mp3 format using the AudioClip.GetData function. Unfortunately the returned array is filled with 0s.

The track, however, is played properly: the data is loaded in the AudioClip using SetData with the output of a function from the mpg123 library as a parameter.

Here's a fragment of @KoningStoma's code that I used to test it.

         myClip = AudioClip.Create ("myClip", lengthSamples, intChannels, intRate, false);
         
         int importIndex = 0;
 
         //List<float> trackPCM = new List<float>(); 
         while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done)) 
         {
             float[] fArray;
             fArray = ByteToFloat (Buffer);
 
             //trackPCM.AddRange(fArray);
             myClip.SetData (fArray, (importIndex*fArray.Length)/2);
             
             importIndex++;                 
         }
         //float [] trackPCMArray = trackPCM.ToArray ();
         //myClip.SetData (trackPCMArray, 0);
 
         MPGImport.mpg123_close (handle_mpg);
         
         audioSource.clip = myClip;
         audioSource.loop = false;
         audioSource.Play ();
 
         float[] dataCopy = new float [numSamples];
         myClip.GetData ( dataCopy , 0 );

I've read the reference pages for both mpg123 and GetData but I don't fully understand what's the proper way to obtain the raw PCM data sampled across particular intervals.

I'll need more than 8192 samples so getSpectrumData() isn't an option, getData() simply returns 0s and mpg123_read() yields an array with channels * track sampling frequency * time samples which is way more than I need.

Is there a different way to get the raw PCM data of a track with a custom number of samples using any of these functions?

If not, would manually sampling the array obtained from mpg123_read() be a good solution?

Thank you in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ChurrosBllack · Sep 01, 2016 at 01:50 PM

It may be a stupid question but are you using a compressed Audio File? Cause if so, you need to choose "Decompress on Load" in the Load Type of your clip :)

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

32 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

Related Questions

Save and Load Audio from web 0 Answers

Audio playing at hightspeed and simoultanesly various times! 1 Answer

How to play audio only if i stay within a collider, and not only once 1 Answer

How to create buttons that toggle the audio clip playing for a game object? 0 Answers

Playing random audio clip using mouse cursor ? 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