mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 12:38:24 +01:00
samples: add Hello/Demo samples for most BRUTAL packages (#19)
Add samples/, a collection of single-file .NET 10 apps run with `dotnet run <file>.cs`. Hello/ has one minimal sample per package that has a public API, many of them headless; Demo/ has fuller programs that combine packages into a more interesting application. samples/README.md indexes every sample and covers the layout, error handling and per-OS requirements.
This commit is contained in:
parent
4ac91e105e
commit
7a3b72d843
1 changed files with 4599 additions and 1 deletions
43
samples/Hello/HelloOpenAl.cs
Normal file
43
samples/Hello/HelloOpenAl.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* HelloOpenAl
|
||||
*
|
||||
* Opens the default audio device with OpenAL and plays a generated one-second 440 Hz tone.
|
||||
*/
|
||||
|
||||
#:package Ahwoo.Brutal.OpenAl@2026.10.2
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
using Brutal.OpenAlApi;
|
||||
|
||||
using var device = Alc.OpenDevice(); // Default output device.
|
||||
Run(device);
|
||||
|
||||
static void Run(Device device)
|
||||
{
|
||||
using var context = device.CreateContext();
|
||||
context.MakeCurrent();
|
||||
|
||||
Console.WriteLine($"Opened device: {device.Specifier}");
|
||||
|
||||
// One second of a 440 Hz sine wave as 16-bit mono PCM.
|
||||
const int SAMPLE_RATE = 44_100;
|
||||
var pcm = new short[SAMPLE_RATE];
|
||||
for (var i = 0; i < pcm.Length; i++)
|
||||
pcm[i] = (short)(Math.Sin(2 * Math.PI * 440 * i / SAMPLE_RATE) * short.MaxValue * 0.5);
|
||||
|
||||
var buffer = Al.GenBuffer();
|
||||
buffer.SetData(AlFormat.Mono16, MemoryMarshal.AsBytes<short>(pcm), SAMPLE_RATE);
|
||||
|
||||
var source = Al.GenSource();
|
||||
source.Buffer = buffer;
|
||||
source.Play();
|
||||
|
||||
Console.WriteLine("Playing a 440 Hz tone...");
|
||||
while (source.State == AlSourceState.Playing)
|
||||
Thread.Sleep(10);
|
||||
|
||||
// Buffers and sources aren't IDisposable, so delete them by hand.
|
||||
Al.DeleteSources([source]);
|
||||
Al.DeleteBuffers([buffer]);
|
||||
Context.MakeCurrent(null); // Release the context before it is destroyed.
|
||||
}
|
||||
Loading…
Reference in a new issue