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
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
a25cdf80b2
commit
b0c9fd9cdc
1 changed files with 4599 additions and 1 deletions
52
samples/Hello/HelloSdlMixer.cs
Normal file
52
samples/Hello/HelloSdlMixer.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* HelloSdlMixer
|
||||
*
|
||||
* Opens the default audio device with Sdl.Mixer, plays a half-second 440 Hz tone and waits for it
|
||||
* to finish. Runs headless (no window).
|
||||
*/
|
||||
|
||||
#:property AllowUnsafeBlocks=true
|
||||
#:package Ahwoo.Brutal.Sdl@2026.10.2
|
||||
#:package Ahwoo.Brutal.Sdl.Mixer@2026.10.2
|
||||
|
||||
using Brutal.SdlApi;
|
||||
using Brutal.SdlApi.Mixer;
|
||||
|
||||
const uint DEFAULT_PLAYBACK_DEVICE = 0xFFFFFFFFu; // SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
|
||||
|
||||
Sdl.Init.Initialize(SdlInitFlags.Audio);
|
||||
SdlMixer.Init();
|
||||
try
|
||||
{
|
||||
Run();
|
||||
}
|
||||
finally
|
||||
{
|
||||
SdlMixer.Quit();
|
||||
Sdl.Init.Quit();
|
||||
}
|
||||
|
||||
// Unsafe for the null and &spec pointer arguments.
|
||||
static unsafe void Run()
|
||||
{
|
||||
// Disposed in reverse when Run returns: the track, the tone, then the mixer.
|
||||
using var mixer = SdlMixer.CreateMixerDevice(DEFAULT_PLAYBACK_DEVICE, null); // null accepts the device's format.
|
||||
SdlAudioSpec spec;
|
||||
mixer.GetFormat(&spec);
|
||||
Console.WriteLine(
|
||||
$"Mixer: {spec.freq} Hz, {spec.channels} channel(s), {Sdl.PInvoke.GetAudioFormatName(spec.format)}"
|
||||
);
|
||||
|
||||
// A generated sine wave, so there is no audio file to ship.
|
||||
using var tone = mixer.CreateSineWaveAudio(440, 0.25f, 500);
|
||||
using var track = mixer.CreateTrack();
|
||||
track.SetAudio(tone);
|
||||
track.Play(0);
|
||||
Console.WriteLine($"Playing a 440 Hz sine wave for {tone.FramesToMS(tone.GetDuration())} ms");
|
||||
|
||||
// Playback happens on SDL's audio thread; poll until the track runs out, with a safety timeout.
|
||||
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(3);
|
||||
while (track.Playing() && DateTime.UtcNow < deadline)
|
||||
Thread.Sleep(10);
|
||||
Console.WriteLine("Done");
|
||||
}
|
||||
Loading…
Reference in a new issue