- Home /
How to split up AudioClip into chunks of 10ms?
Hi, So as part of a facial lip sync and animation package I need to spit up an Audio Clip into small chunks of 10ms, then convert them into a byte[] and feed it to the rest of the system.
Now Ive managed to find a way to convert the Audio clips to a byte[] but Im not sure how to slip up the Audio clip before hand. The audio clips are generated on the fly by an external system and so i can cut them up before feeding them to the program. Im not really sure where to even start with this, could I try and separate the byte[] into diffrent parts or is there a way to cut up and AudioClip on the fly ?
Any help is greatly appreciated, Thanks!
Answer by trakevital · Jan 15, 2021 at 12:11 PM
You can use the following code I made for trimming AudioClips:
  //Returns a trimmed version of the <originalClip>
     //originalClip - the originial clip
     //startPosSec - The starting position in seconds (eg: 1, 0.34, 2782.12, etc.)
     //lengthSec - The new length of the AudioClip in seconds (eg: 1, 0.34, 2782.12, etc.)
     private AudioClip TrimAudioClip( AudioClip originalClip, float startPosSec, float lengthSec)
     { 
         var originalClipSamples = new float[originalClip.samples];
         originalClip.GetData(originalClipSamples, 0);
 
         //converts startPosSec & takeAmountSec from seconds to sample amount
         int newStartPosSample = (int)(startPosSec * originalClip.frequency);
         int newLengthSecSample = (int)(lengthSec * originalClip.frequency);
 
         //gets the trimmed version of the orignalClipSamples
         var newClipSamples = originalClipSamples.Skip(newStartPosSample).Take(newLengthSecSample).ToArray();
 
         //generates a new empty clip and sets its data according to the newClipSamples
         AudioClip resClip = AudioClip.Create(originalClip.name, newClipSamples.Length, originalClip.channels, originalClip.frequency, false);
         resClip.SetData(newClipSamples, 0);
 
         return resClip;
     }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                