internal void PlatformStart() { if (State == MicrophoneState.Started) { return; } _captureDevice = ALC.CaptureOpenDevice( Name, (uint)SampleRate, ALFormat.Mono16, GetSampleSizeInBytes(_bufferDuration)); CheckALCError("Failed to open capture device."); if (_captureDevice != IntPtr.Zero) { ALC.CaptureStart(_captureDevice); CheckALCError("Failed to start capture."); State = MicrophoneState.Started; } else { throw new AudioHardwareException("Failed to open capture device."); } }
static void RunGame() { // init picovoice platform string keywordPath = $"pico_chess_{_platform}.ppn"; string contextPath = $"chess_{_platform}.rhn"; using Picovoice picovoice = new Picovoice(keywordPath, WakeWordCallback, contextPath, InferenceCallback); DrawBoard("\n"); // create and start recording short[] recordingBuffer = new short[picovoice.FrameLength]; ALCaptureDevice captureDevice = ALC.CaptureOpenDevice(null, picovoice.SampleRate, ALFormat.Mono16, picovoice.FrameLength * 2); { ALC.CaptureStart(captureDevice); while (!_quitGame) { int samplesAvailable = ALC.GetAvailableSamples(captureDevice); if (samplesAvailable > picovoice.FrameLength) { ALC.CaptureSamples(captureDevice, ref recordingBuffer[0], picovoice.FrameLength); picovoice.Process(recordingBuffer); } Thread.Yield(); } // stop and clean up resources Console.WriteLine("Bye!"); ALC.CaptureStop(captureDevice); ALC.CaptureCloseDevice(captureDevice); } }
protected virtual void Dispose(bool disposing) { if (disposing) { _stillRunning = false; int sessionCount = 0; // NOTE: This is done in a way to avoid possible situations when the OpenALHardwareDeviceSession is already being dispose in another thread but doesn't hold the lock and tries to Unregister. do { lock (_lock) { if (_sessions.Count == 0) { break; } OpenALHardwareDeviceSession session = _sessions[_sessions.Count - 1]; session.Dispose(); sessionCount = _sessions.Count; } }while (sessionCount > 0); ALC.DestroyContext(_context); ALC.CloseDevice(_device); } }
protected virtual void Dispose(bool disposing) { if (!_isDisposed) { // Delete the buffer AL.DeleteBuffer(_bufferId); AL.DeleteSource(_sourceId); if (_context != ALContext.Null) { ALC.MakeContextCurrent(ALContext.Null); ALC.DestroyContext(_context); } _context = ALContext.Null; if (_device != IntPtr.Zero) { ALC.CloseDevice(_device); } _device = ALDevice.Null; _isDisposed = true; } }
private void Update() { ALC.MakeContextCurrent(_context); while (_stillRunning) { bool updateRequired = false; lock (_lock) { foreach (OpenALHardwareDeviceSession session in _sessions) { if (session.Update()) { updateRequired = true; } } } if (updateRequired) { _updateRequiredEvent.Set(); } // If it's not slept it will waste cycles. Thread.Sleep(10); } }
/// <inheritdoc cref="GetStringList(ALDevice, GetEnumerateAllContextStringList)"/> public static IEnumerable <string> GetStringList(GetEnumerateAllContextStringList param) { unsafe { byte *result = GetStringList(ALDevice.Null, param); return(ALC.ALStringListToList(result)); } }
private AudioContext() { var deviceName = findDeviceName(); device = ALC.OpenDevice(deviceName); ctx = ALC.CreateContext(device, (int[])null !); ALC.MakeContextCurrent(ctx); }
public void Destroy() { ALC.MakeContextCurrent(ALContext.Null); ALC.DestroyContext(_context); _context = ALContext.Null; ALC.CloseDevice(_device); _device = ALDevice.Null; }
public static void Init() { string deviceName = ALC.GetString(ALDevice.Null, AlcGetString.DefaultDeviceSpecifier); var device = ALC.OpenDevice(deviceName); var context = ALC.CreateContext(device, (int[])null); ALC.MakeContextCurrent(context); }
internal void Close() { foreach (var bufferedAudioSource in _bufferedAudioSources) { bufferedAudioSource.Close(); } ALC.DestroyContext(_alContext); ALC.CloseDevice(_alDevice); }
/// <summary> /// Lists available audio input devices. /// </summary> public static void ShowAudioDevices() { Console.WriteLine("Available audio devices: \n"); List <string> captureDeviceList = ALC.GetStringList(GetEnumerationStringList.CaptureDeviceSpecifier).ToList(); for (int i = 0; i < captureDeviceList.Count; i++) { Console.WriteLine($"\tDevice {i}: {captureDeviceList[i]}"); } }
public SoundManager(World world) { this.world = world; var audioDevice = ALC.OpenDevice(null); if (audioDevice.Handle != IntPtr.Zero) { var audioContext = ALC.CreateContext(audioDevice, new ALContextAttributes()); ALC.MakeContextCurrent(audioContext); } }
public void Init() { _device = ALC.OpenDevice(null); _attributes = new ALContextAttributes(); _context = ALC.CreateContext(_device, _attributes); ALC.MakeContextCurrent(_context); _bufferId = AL.GenBuffers(1)[0]; _sourceId = AL.GenSources(1)[0]; //If data is byte, use ALFormat.Stereo16. For float use ALFormat.StereoFloat32Ext switch (Path.GetExtension(_fileName)) { case ".ogg": _oggSoundData = DecodeSound.LoadNVorbisData(_fileName); AL.BufferData(_bufferId, MapFormat(_oggSoundData.Format), _oggSoundData.BufferData, _oggSoundData.BufferData.Length * sizeof(float), _oggSoundData.SampleRate); break; case ".mp3": _mp3AndWaveSoundData = DecodeSound.LoadMP3SharpData(_fileName); AL.BufferData(_bufferId, MapFormat(_mp3AndWaveSoundData.Format), _mp3AndWaveSoundData.BufferData, _mp3AndWaveSoundData.BufferData.Length, _mp3AndWaveSoundData.SampleRate); break; case ".wav": _mp3AndWaveSoundData = DecodeSound.LoadWaveFile(_fileName); //Helpers.SaveData(_mp3AndWaveSoundData.BufferData, 4, @"C:\Temp\tone-data.csv"); // Sends the buffer data to the sound card AL.BufferData(_bufferId, MapFormat(_mp3AndWaveSoundData.Format), _mp3AndWaveSoundData.BufferData, _mp3AndWaveSoundData.BufferData.Length, _mp3AndWaveSoundData.SampleRate); break; } // Bind the buffer to the source AL.Source(_sourceId, ALSourcei.Buffer, _bufferId); }
public void Create() { if (_device == ALDevice.Null) { _device = ALC.OpenDevice(null); } if (_context == ALContext.Null) { _context = ALC.CreateContext(_device, (int[])null); } ALC.MakeContextCurrent(_context); }
void Initialize() { device = ALC.OpenDevice(null); unsafe { context = ALC.CreateContext(device, (int *)null); ALC.MakeContextCurrent(context); } //Console.WriteLine(Alc.GetString(IntPtr.Zero, AlcGetString.AllDevicesSpecifier)); version = AL.Get(ALGetString.Version); vendor = AL.Get(ALGetString.Vendor); renderer = AL.Get(ALGetString.Extensions); }
public static void ValidateLoadUnmanagedDll() { Console.WriteLine($"Running {nameof(ValidateLoadUnmanagedDll)}..."); ALC alc = new ALC(); var asm = alc.LoadFromAssemblyPath(Assembly.GetExecutingAssembly().Location); Console.WriteLine(" -- Validate explicit load..."); // ALC implementation returns a fake handle value IntPtr ptr = NativeLibrary.Load(FakeNativeLibrary.Name, asm, null); alc.Validate(FakeNativeLibrary.Name); Assert.Equal(FakeNativeLibrary.Handle, ptr); alc.Reset(); ptr = IntPtr.Zero; bool success = NativeLibrary.TryLoad(FakeNativeLibrary.Name, asm, null, out ptr); Assert.True(success, $"NativeLibrary.TryLoad should have succeeded"); alc.Validate(FakeNativeLibrary.Name); Assert.Equal(FakeNativeLibrary.Handle, ptr); alc.Reset(); // ALC implementation calls NativeLibrary.TryLoad with a different name ptr = NativeLibrary.Load(FakeNativeLibrary.RedirectName, asm, null); alc.Validate(FakeNativeLibrary.RedirectName, FakeNativeLibrary.Name); Assert.Equal(FakeNativeLibrary.Handle, ptr); alc.Reset(); ptr = IntPtr.Zero; success = NativeLibrary.TryLoad(FakeNativeLibrary.RedirectName, asm, null, out ptr); Assert.True(success, $"NativeLibrary.TryLoad should have succeeded"); alc.Validate(FakeNativeLibrary.RedirectName, FakeNativeLibrary.Name); Assert.Equal(FakeNativeLibrary.Handle, ptr); alc.Reset(); Console.WriteLine(" -- Validate p/invoke..."); int addend1 = rand.Next(int.MaxValue / 2); int addend2 = rand.Next(int.MaxValue / 2); int expected = addend1 + addend2; int value = NativeSumInAssemblyLoadContext(alc, addend1, addend2); alc.Validate(NativeLibraryToLoad.InvalidName); Assert.Equal(expected, value); }
internal void CheckALCError(string operation) { ALCError error = ALC.GetErrorForDevice(_captureDevice); if (error == ALCError.NoError) { return; } string errorFmt = "OpenAL Error: {0}"; throw new AudioHardwareException(string.Format("{0} - {1}", operation, string.Format(errorFmt, error))); }
internal int GetQueuedSampleCount() { if (State == MicrophoneState.Stopped || BufferReady == null) { return(0); } Span <int> queuedSampleCountBuffer = stackalloc int[1]; ALC.GetInteger(_captureDevice, ALCGetInteger.CaptureSamples, 1, queuedSampleCountBuffer); CheckALCError("Failed to query capture samples."); return(queuedSampleCountBuffer[0]); }
internal static void CheckError(string message = "", params object[] args) { ALCError error; if ((error = ALC.GetError()) != ALCError.NoError) { if (args != null && args.Length > 0) { message = string.Format(message, args); } throw new InvalidOperationException(message + " (Reason: " + error.ToString() + ")"); } }
protected virtual void Dispose(bool disposing) { if (disposing) { _stillRunning = false; foreach (OpenALHardwareDeviceSession session in _sessions.Keys) { session.Dispose(); } ALC.DestroyContext(_context); ALC.CloseDevice(_device); } }
public void Dispose() { if (context != ALContext.Null) { ALC.MakeContextCurrent(ALContext.Null); ALC.DestroyContext(context); } context = ALContext.Null; if (device != IntPtr.Zero) { ALC.CloseDevice(device); } device = ALDevice.Null; }
public OpenALHardwareDeviceDriver() { _device = ALC.OpenDevice(""); _context = ALC.CreateContext(_device, new ALContextAttributes()); _updateRequiredEvent = new ManualResetEvent(false); _sessions = new List <OpenALHardwareDeviceSession>(); _stillRunning = true; _updaterThread = new Thread(Update) { Name = "HardwareDeviceDriver.OpenAL" }; _updaterThread.Start(); }
public static void ValidateLoadUnmanagedDll() { Console.WriteLine($"Running {nameof(ValidateLoadUnmanagedDll)}..."); Console.WriteLine(" -- Validate p/invoke..."); int addend1 = rand.Next(int.MaxValue / 2); int addend2 = rand.Next(int.MaxValue / 2); int expected = addend1 + addend2; ALC alc = new ALC(); int value = NativeSumInAssemblyLoadContext(alc, addend1, addend2); Assert.IsTrue(alc.LoadUnmanagedDllCalled, "AssemblyLoadContext.LoadUnmanagedDll should have been called."); Assert.AreEqual(expected, value, $"Unexpected return value for {nameof(NativeSum)}"); }
internal void PlatformStop() { if (State == MicrophoneState.Started) { ALC.CaptureStop(_captureDevice); CheckALCError("Failed to stop capture."); Update(); // to ensure that BufferReady doesn't get invoked after Stop() ALC.CaptureCloseDevice(_captureDevice); CheckALCError("Failed to close capture device."); _captureDevice = IntPtr.Zero; } State = MicrophoneState.Stopped; }
/// <summary> /// Destroys the AL context and closes the device, when they exist. /// </summary> private void DestroyContexts() { ALC.MakeContextCurrent(IntPtr.Zero); if (_context != IntPtr.Zero) { ALC.DestroyContext(_context); _context = IntPtr.Zero; } if (Device != IntPtr.Zero) { ALC.CloseDevice(Device); Device = IntPtr.Zero; } }
static OpenALContext() { //string deviceName = ALC.GetString(ALDevice.Null, AlcGetString.DefaultDeviceSpecifier); //foreach (var name in ALC.GetStringList(GetEnumerationStringList.DeviceSpecifier)) //{ // if (name.Contains("OpenAL Soft")) // { // deviceName = name; // } //} device = ALC.OpenDevice(null); context = ALC.CreateContext(device, (int[])null); ALC.MakeContextCurrent(context); }
internal unsafe int PlatformGetData <T>(Span <T> buffer) where T : unmanaged { int sampleCount = GetQueuedSampleCount(); sampleCount = Math.Min(buffer.Length * sizeof(T) / 2, sampleCount); // 16bit adjust if (sampleCount > 0) { var bufferBytes = MemoryMarshal.AsBytes(buffer); ALC.CaptureSamples(_captureDevice, bufferBytes, sampleCount); CheckALCError("Failed to capture samples."); return(sampleCount); } return(0); }
protected override void OnUnload() { GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindVertexArray(0); GL.UseProgram(0); GL.DeleteBuffer(_vertexBufferObject); GL.DeleteBuffer(_elementBufferObject); GL.DeleteVertexArray(_vertexArrayObject); ALC.DestroyContext(context); ALC.CloseDevice(device); base.OnUnload(); }
public OpenALBackend() { if (ALInstance != null) { throw new InvalidOperationException("Only one ALInstance can exist at a time."); } ALInstance = this; var audioDevice = ALC.OpenDevice(null); _context = ALC.CreateContext(audioDevice, new int[] { }); ALC.MakeContextCurrent(_context); _sourcePool = new ALSourcePool(); }
public Window(GameWindowSettings settings, NativeWindowSettings nativeSettings) : base(settings, nativeSettings) { device = ALC.OpenDevice(""); context = ALC.CreateContext(device, new ALContextAttributes() { Frequency = 44100 }); ALC.MakeContextCurrent(context); Console.WriteLine("Version : " + AL.Get(ALGetString.Version)); Console.WriteLine("Vendor : " + AL.Get(ALGetString.Vendor)); Console.WriteLine("Renderer: " + AL.Get(ALGetString.Renderer)); _gameboy = new Gameboy(() => { return(new AudioEmitter()); }); string path = "Roms/Games/Mario Deluxe.gbc "; string[] args = Environment.GetCommandLineArgs(); if (args.Length > 1) { path = args[1]; } if (!File.Exists(path)) { Console.WriteLine(path); Console.WriteLine("Invalid usage... GBSharp [rom]"); foreach (string s in args) { Console.WriteLine("S: " + s); } throw new Exception(); } Cartridge cartridge = Cartridge.Load(path); //Cartridge cartridge = GetNextTestRom(); //CartridgeLoader.LoadDataIntoMemory(_mmu, GetNextTestRom(), 0x00); _gameboy.LoadCartridge(cartridge); thread = new Thread(new ThreadStart(_gameboy.Run)); _isAlive = true; thread.Start(); }