• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 audioclip as wav/mp3/ogg... 1 Answer

turning off effects and music seperately one works the other wont? 1 Answer

Getting Multiple Streams From Microphone (VOIP & Recording) 0 Answers

Share audioclip through WhatsApp\Telegram 2 Answers

How to prevent the audio clip from playing louder? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges