


In AudioClip.Create, the length parameter should be a number of frames, not of samples.

One problem is that unity doesn't adopt this terminology, so the API is sometimes ambiguous. If the audio data is stereo, each frame will have 2 samples. 'Sample' refers to a single value, 'frame' to a single time unit. One problem I'm having though is that the AudioClips that I'm loading are 4 times as big as their filesize, and I'm not quite sure where to look on how to fix that. Loading a rather large file (101MB) takes about half a second, and half of that again comes from the AudioClip.Create function, so it's fast enough for my needs. However I don't know what you mean about number of frames.Īnd about allocations, I switched to using FileStream for reading bytes, however I don't quite see how it reduces number of allocations as I still need to read the entire file, if you could explain in detail what you mean that would be great.Īs it is now I think it's somewhat fast (It's certainly a lot faster then what it was). I also adopted your suggestion about using a backgroundworker for async loading, which seems to work great. Also made it throw some excptions when bit depth didn't match and etc. These are the most obvious points, really seem to know your stuff, where were you when I first created the thread?Īnyway I optimised the code a little bit, using your suggestion. Much faster to use Buffer.BlockCopy to copy from byte to Int16, and then to do the conversion to float sample by sample. Calling the OP's parser from the main thread will block everything for a potentially substantial amount of time if the file is large.NET has a very handy BackgroundWorker class which handles threading issues and is very nice to work with even without much prior experience in threading.Ĥ) So many allocations! Using the FileStream class could reduce that a LOT, enabling loading of larger files without trashing memory.ĥ) Performance: BitConverter is slow. ), number of frames etc.Ģ) A parser doesn't HAVE to support all formats, but should throw exceptions when it encounters formats it won't handleģ) Async is best. Sure! Time is scarce, if there's demand I'll happily post some code.ġ) First, one should parse the header and extract all important information: how many channels, what sample rate, bit depth( the OP wouldn't have had this problem had he parsed bit depth correctly - wav files can be 8, 16, 24 32 bits.
