Search in sources :

Example 1 with BinaryReader

use of cli.System.IO.BinaryReader 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());
    }
}
Also used : NSData(cli.MonoTouch.Foundation.NSData) NSError(cli.MonoTouch.Foundation.NSError) ALError(cli.OpenTK.Audio.OpenAL.ALError) BinaryReader(cli.System.IO.BinaryReader)

Example 2 with BinaryReader

use of cli.System.IO.BinaryReader in project playn by threerings.

the class IOSAssets method getBytesSync.

@Override
public byte[] getBytesSync(String path) throws Exception {
    String fullPath = resolvePath(path);
    // platform.log().debug("Loading bytes " + fullPath);
    BinaryReader reader = null;
    try {
        FileStream stream = new FileStream(fullPath, FileMode.wrap(FileMode.Open), FileAccess.wrap(FileAccess.Read), FileShare.wrap(FileShare.Read));
        reader = new BinaryReader(stream);
        return reader.ReadBytes((int) stream.get_Length());
    } finally {
        if (reader != null) {
            reader.Close();
        }
    }
}
Also used : FileStream(cli.System.IO.FileStream) BinaryReader(cli.System.IO.BinaryReader)

Aggregations

BinaryReader (cli.System.IO.BinaryReader)2 NSData (cli.MonoTouch.Foundation.NSData)1 NSError (cli.MonoTouch.Foundation.NSError)1 ALError (cli.OpenTK.Audio.OpenAL.ALError)1 FileStream (cli.System.IO.FileStream)1