public AudioFile(string path) { // Ensure file if (!File.Exists(path)) { throw new FileNotFoundException($"The audio file '{path}' does not exist or is an invalid path"); } Path = path; // Try to load the file NativeContent.AudioError error; (_handle, error) = NativeContent.AudioOpenFile(path); if (_handle == IntPtr.Zero || error != NativeContent.AudioError.NoError) { throw new ContentLoadException(path, $"audio file loading failed with {error}"); } // Get the file info NativeContent.AudioGetInfo(_handle, out var frames, out var rate, out var channels); if (rate < 8_000 || rate > 48_000) { throw new ContentLoadException(path, $"audio files must have sample rates in [8000, 48000] (actual {rate})"); } if (channels == 0 || channels > 2) { throw new ContentLoadException(path, $"audio files must have either 1 (mono) or 2 (stereo) channels (actual {channels})"); } FrameCount = frames; SampleRate = rate; Stereo = (channels == 2); AudioType = NativeContent.AudioGetType(_handle); }
public ImageFile(string path) { // Ensure file if (!File.Exists(path)) { throw new FileNotFoundException($"The image file '{path}' does not exist or is an invalid path"); } Path = path; // Try to load the file NativeContent.ImageError error; (_handle, error) = NativeContent.ImageOpenFile(path); if (_handle == IntPtr.Zero || error != NativeContent.ImageError.NoError) { throw new ContentLoadException(path, $"image file loading failed with {error}"); } // Get the file info (Width, Height) = NativeContent.ImageGetSize(_handle); Channels = NativeContent.ImageGetChannels(_handle); Type = NativeContent.ImageGetType(_handle); if (Channels == NativeContent.ImageChannels.Unknown) { throw new ContentLoadException(path, "image file has unsupported color channel count"); } }
private void dispose(bool disposing) { if (_handle != IntPtr.Zero) { NativeContent.AudioCloseFile(_handle); } _handle = IntPtr.Zero; }
// Loads the texture as RGBA data public ReadOnlySpan <Color> LoadDataRGBA() { var data = NativeContent.ImageLoadData(_handle, NativeContent.ImageChannels.RGBA); if ((data == null) || (Error != NativeContent.ImageError.NoError)) { throw new ContentLoadException(Path, $"image file data read failed with {Error}"); } return(new(data, (int)(Width * Height))); }
// Read frames - throws an exception on error, instead of returning 0 public ulong ReadFrames(ReadOnlySpan <short> buffer) { if (EOF) { return(0); } ulong fCount = (uint)buffer.Length / (Stereo ? 2u : 1u); ulong read = NativeContent.AudioReadFrames(_handle, fCount, buffer); if (read == 0) { throw new ContentLoadException(Path, $"audio file data read error ({Error})"); } return(read); }