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
0
Question by jjennings1990 · Jan 28, 2014 at 12:54 AM · audiodata

Converting Float Data from audio clip to a string but only 1/4th of data is saved?

Hello All this is a more generic programming question versus and actual Unity programming question but I would love help figuring out why only a 4th of my data is being saved currently when I convert a float array to bytes , and that byte array then to a hexadecimal string .

I don't know if the issue is just my logic or even if its an issue with the data writing or retrieval so any and all help is appreciated

So I am retrieving an array of floats from my audio clip using the clip.GetData() function

I am then using the following function to convert those floats into bytes :

public byte[] ConvertFloatsToBytes(float[] audioData){ //** We create a new byte array to hold our clip data in bytes* byte[] bytes = new byte[audioData.Length * 4];

     for ( int i = 0 ; i <audioData.Length ; i++){
                     *//*** here we iterate though the existing data and place our bytes in a new array 

//***relative to the index of the audioData we are currently referencing
//** A.K.A AudioData[0]'s data will exist in indexes 0,1,2, and 3 of the byte array

         Buffer.BlockCopy(audioData,i,bytes,i*4,4);

  }

     return bytes;
 }


Then I use the following function to convert those bytes into hexadecimal values :

public static string ByteArrayToString(byte[] ba) { string hex = BitConverter.ToString(ba);

     Debug.Log("ba.length = " + ba.Length.ToString() +"hex string = " + hex);
     return hex.Replace("-","");
 }

when i get the data back I am using the following function to convert the data to bytes from our Hexadecimal values

public static byte[] StringToByteArray(String hex) { int NumberChars = hex.Length; byte[] bytes = new byte[NumberChars / 2]; for (int i = 0; i < NumberChars; i += 2) bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); return bytes; }

and lastly I use this function to convert my byte data back to float values

public float[] ConvertBytesToFloats(byte[] bytes){

             //*** Our float data should be a fourth of our byte arrays size , with 4 byte array elements holding the value of 1 float 

`` //*** array element float[] floats = new float[bytes.Length/4];

     for(int i = 0 ; i < floats.Length ; i++){
     *//**** we  populate our float array by combining the bytes of our byte array*
         Buffer.BlockCopy(bytes, i*4, floats, i, 4);

             *//*** at this point we should have fully converted our float array to and from hex decimal*
     return floats;
 }

I recently was given the save / load data management task by my team and considering we have very large arrays of audio data being generated not compressing these values is hammering me . I have found these functions and researched data management but i can't see where this issue is stemming from . if anybody with experience converting audio data to hex could help me i'd be very grateful ! As I said this code does work but only a fourth of our overall recording is actually saved and retrieved upon playing back the recording.

Comment
Add comment · Show 2
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 Benproductions1 · Jan 28, 2014 at 09:49 AM 0
Share
  1. Format your code

  2. Don't write everything in bold

  3. If it's a genetic program$$anonymous$$g question, ask it on stack overflow

avatar image gfoot · Jan 28, 2014 at 10:49 AM 0
Share

It's hard to read the code because random bits of it are not formatted properly - every line needs to have four spaces at the start to be formatted as code.

To find out where you're losing data, print out the lengths of the various arrays you're using and spot which one is not the right length.

I also don't understand why you're doing this at all - you're converting the floats into a hexadecimal string representation which is twice as big as the float array was to begin with.

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

20 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

Related Questions

AudioSource.GetOutputData example? 1 Answer

Render Audio Waveform to UI Image 0 Answers

WAV byte[] to AudioClip? 3 Answers

Unity Audio Output Data 0 Answers

Using raycast to sonify point cloud data and geometry of shapes 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