public SoundBuffer() { AL10.alGenBuffers(1, out _handle); ALUtils.CheckALError("unable to create sound buffer"); if (_handle == 0) { throw new AudioException("Unable to create OpenAL sound buffer"); } }
private void dispose(bool disposing) { if (!_isDisposed) { if (_handle != 0 && !AudioEngine.IsShutdown) { AL10.alDeleteBuffers(1, ref _handle); ALUtils.CheckALError("unable to destroy sound buffer"); _handle = 0; } _isDisposed = true; } }
internal void SetData(IntPtr data, AudioFormat fmt, uint hz, uint size) { AL10.alBufferData(_handle, (int)fmt, data, (int)size, (int)hz); ALUtils.CheckALError("unable to set audio buffer data"); AL10.alGetBufferi(_handle, AL10.AL_BITS, out int bits); AL10.alGetBufferi(_handle, AL10.AL_CHANNELS, out int channels); AL10.alGetBufferi(_handle, AL10.AL_SIZE, out int unpackedSize); Format = fmt; DataSize = (uint)unpackedSize; Duration = TimeSpan.FromSeconds((double)(unpackedSize / ((bits / 8) * channels)) / hz); }
public static void Initialize() { // Populate the device lists PlaybackDevice.PopulateDeviceList(); if (PlaybackDevice.Devices.Count == 0) { throw new AudioException("There are no audio playback devices available"); } LDEBUG("Available audio playback devices:"); foreach (var dev in PlaybackDevice.Devices) { LDEBUG($" {dev.Identifier}"); } // Open the default playback device Device = ALC10.alcOpenDevice(PlaybackDevice.Devices[0].Identifier); ALUtils.CheckALCError(); if (Device == IntPtr.Zero) { throw new AudioException("Unable to open default audio playback device"); } // Create the al context and set it as active Context = ALC10.alcCreateContext(Device, new int[2] { 0, 0 }); // Two 0s tell OpenAL no special attribs ALUtils.CheckALCError(); if (Context == IntPtr.Zero) { throw new AudioException("Unable to create audio context"); } ALC10.alcMakeContextCurrent(Context); ALUtils.CheckALCError(); // Generate audio sources AL10.alGenSources(MAX_SOURCE_COUNT, s_allSources); ALUtils.CheckALError("unable to generate audio sources"); foreach (var src in s_allSources) { s_availableSources.Push(src); } // Report LINFO("Started OpenAL audio engine."); LINFO($" Device: {PlaybackDevice.Devices[0].Identifier}."); }
public static void Shutdown() { // Destroy the sources AL10.alDeleteSources(MAX_SOURCE_COUNT, s_allSources); ALUtils.CheckALError("unable to free audio sources"); s_availableSources.Clear(); s_usedSources.Clear(); // Destroy the context, and then close the device ALC10.alcMakeContextCurrent(IntPtr.Zero); ALUtils.CheckALCError(); ALC10.alcDestroyContext(Context); ALUtils.CheckALCError(); Context = IntPtr.Zero; ALC10.alcCloseDevice(Device); ALUtils.CheckALCError(); Device = IntPtr.Zero; IsShutdown = true; // Report LINFO("Shutdown OpenAL audio engine."); }
internal static void PopulateDeviceList() { string[] dnames = ALUtils.GetALCString(ALC11.ALC_ALL_DEVICES_SPECIFIER, 0).Split('\n'); s_devices.AddRange(dnames.Select(name => new PlaybackDevice(name))); }