use of cli.OpenTK.Audio.OpenAL.ALError in project playn by threerings.
the class CAFLoader method load.
public static void load(String path, int bufferId) {
// mmap (if possible) the audio file for efficient reading/uploading
NSError[] err = new NSError[1];
NSData data = NSData.FromFile(path, NSDataReadingOptions.wrap(READ_OPTS), err);
if (err[0] != null) {
throw new RuntimeException(err[0].ToString());
}
// read the CAFF metdata to find out the audio format and the data offset/length
BinaryReader br = new BinaryReader(data.AsStream());
if (!new String(br.ReadChars(4)).equals("caff"))
throw new RuntimeException("Input file not CAFF: " + path);
// rest of caf file header
br.ReadBytes(4);
CAFDesc desc = null;
int offset = 8, dataOffset = 0, dataLength = 0;
do {
String type = new String(br.ReadChars(4));
int size = (int) BitConverter.ToInt64(reverse(br.ReadBytes(8)), 0);
offset += 12;
if (type.equals("data")) {
dataOffset = offset;
dataLength = size;
} else if (type.equals("desc")) {
desc = new CAFDesc(br.ReadBytes(size));
if ("ima4".equalsIgnoreCase(desc.formatID))
throw new RuntimeException("Cannot use compressed CAFF. " + "Use AIFC for compressed audio on iOS.");
} else {
br.ReadBytes(size);
}
offset += size;
} while (dataOffset == 0);
br.Close();
// upload the audio data to OpenAL straight from the mmap'd file
AL.BufferData(bufferId, desc.GetALFormat(), IntPtr.Add(data.get_Bytes(), dataOffset), dataLength, (int) desc.sampleRate);
// now dispose the mmap'd file to free up resources
data.Dispose();
// finally freak out if OpenAL didn't like what we sent it
ALError error = AL.GetError();
if (error.Value != ALError.NoError) {
throw new RuntimeException(error.ToString());
}
}
Aggregations