- Home /
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.
Format your code
Don't write everything in bold
If it's a genetic program$$anonymous$$g question, ask it on stack overflow
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131852im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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