public AudioClip StartImport(string mPath)
{
MPGImport.mpg123_init();
handle_mpg = MPGImport.mpg123_new(null, errPtr);
try
{
x = MPGImport.mpg123_open(handle_mpg, mPath);
MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding);
intRate = rate.ToInt32();
intChannels = channels.ToInt32();
intEncoding = encoding.ToInt32();
MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2);
MPGImport.mpg123_format_none(handle_mpg);
MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208);
Debug.Log("Getting ID3 info");
MPGImport.mpg123_id3v1 v1 = (MPGImport.mpg123_id3v1)Marshal.PtrToStructure(id3v1, typeof(MPGImport.mpg123_id3v1));
FrameSize = MPGImport.mpg123_outblock(handle_mpg);
byte[] Buffer = new byte[FrameSize];
lengthSamples = MPGImport.mpg123_length(handle_mpg);
myClip = AudioClip.Create(new String(v1.title), lengthSamples, intChannels, intRate, false);
int importIndex = 0;
while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done))
{
float[] fArray;
fArray = ByteToFloat(Buffer);
float offset = (importIndex * fArray.Length) / 2;
if (offset > lengthSamples)
{
Debug.LogWarning("[STED] MP3 file " + mPath + " is of an unexpected length and was truncated.");
break; // File was reported as shorter than it is. Salvage what we have and return.
}
myClip.SetData(fArray, (int)offset);
importIndex++;
}
}
catch (Exception ex)
{
// Attempt to dump any used memory before continuing.
// TODO: Still holds onto memory when repeatedy failing.
myClip.UnloadAudioData();
myClip = null;
throw ex;
}
finally
{
MPGImport.mpg123_close(handle_mpg);
}
return myClip;
}