### Handle SharpAudioException Errors Source: https://context7.com/feliwir/sharpaudio/llms.txt Catch SharpAudioException to implement fallback logic when backend audio API calls fail. This example demonstrates forcing XAudio2 and falling back to OpenAL if unavailable. ```csharp using System; using SharpAudio; try { // Force XAudio2 on a non-Windows machine — will throw or return null var engine = AudioEngine.CreateXAudio() ?? throw new SharpAudioException("XAudio2 is not available on this platform."); var buffer = engine.CreateBuffer(); // ... } catch (SharpAudioException ex) { Console.Error.WriteLine($"Audio error: {ex.Message}"); // Graceful fallback using var fallback = AudioEngine.CreateOpenAL(); if (fallback != null) Console.WriteLine("Fell back to OpenAL successfully."); } ``` -------------------------------- ### Monitor SoundStream Playback State Source: https://context7.com/feliwir/sharpaudio/llms.txt Subscribe to PropertyChanged events on SoundStream to receive asynchronous notifications for state and position changes. This example shows reacting to state transitions like Playing, Paused, and Stop. ```csharp using System; using System.ComponentModel; using System.IO; using System.Threading; using SharpAudio; using SharpAudio.Codec; using var engine = AudioEngine.CreateDefault(); using var soundStream = new SoundStream(File.OpenRead("track.mp3"), engine); // React to state and position changes soundStream.PropertyChanged += (sender, e) => { var ss = (SoundStream)sender; if (e.PropertyName == nameof(SoundStream.State)) Console.WriteLine($"State → {ss.State}"); else if (e.PropertyName == nameof(SoundStream.Position)) Console.Write($"\r{ss.Position:mm\\:ss} / {ss.Duration:mm\\:ss} "); }; soundStream.Play(); // State → Playing Thread.Sleep(5000); soundStream.Play(); // State → Paused (toggle) Thread.Sleep(1000); soundStream.Play(); // State → Playing soundStream.Stop(); // State → Stop // Expected console output: // State → Playing // 00:00 / 03:42 00:01 / 03:42 ... (position ticks) // State → Paused // State → Playing // State → Stop ``` -------------------------------- ### Create and Play Audio with AudioSource Source: https://context7.com/feliwir/sharpaudio/llms.txt Demonstrates creating an AudioSource, uploading PCM data to a buffer, and playing it. Ensure to dispose of AudioSource and Buffer when done. ```csharp using System; using System.Threading; using SharpAudio; using var engine = AudioEngine.CreateDefault(); var buffer = engine.CreateBuffer(); var source = engine.CreateSource(); // Upload PCM data (440 Hz beep, 1 s) var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; var samples = new short[44100]; for (int i = 0; i < samples.Length; i++) samples[i] = (short)(32760 * Math.Sin(2 * Math.PI * 440.0 / samples.Length * i)); buffer.BufferData(samples, format); // Queue the buffer and start playback source.QueueBuffer(buffer); source.Volume = 0.8f; // 0.0 (silent) – 1.0 (full) source.Looping = false; source.Play(); Console.WriteLine($"Buffers queued: {source.BuffersQueued}"); while (source.IsPlaying()) Thread.Sleep(50); source.Stop(); // explicit stop (no-op if already finished) source.Flush(); // clear queued buffers (useful before seeking) source.Dispose(); buffer.Dispose(); ``` -------------------------------- ### AudioEngine - Create engine instances Source: https://context7.com/feliwir/sharpaudio/llms.txt The AudioEngine is the entry point for all audio operations. Static factory methods are available to create engine instances, automatically selecting the appropriate backend for the current platform or allowing explicit selection. The engine is IDisposable and should be managed within a using block. ```APIDOC ## AudioEngine — Create engine instances `AudioEngine` is the entry point for all audio operations. Static factory methods select the appropriate backend for the current platform, or a specific one can be requested explicitly. The engine is `IDisposable`; wrap it in a `using` block to release native resources. ```csharp using SharpAudio; // Automatically picks XAudio2 on Windows, OpenAL elsewhere using var engine = AudioEngine.CreateDefault(); if (engine == null) throw new Exception("No audio backend available on this platform."); Console.WriteLine($"Backend: {engine.BackendType}"); // e.g. "XAudio2" or "OpenAL" // Force a specific backend using var alEngine = AudioEngine.CreateOpenAL(); // returns null if OpenAL unavailable using var xa2Engine = AudioEngine.CreateXAudio(); // returns null if XAudio2 unavailable // Supply custom options (sample rate, channel count) var options = new AudioEngineOptions(sampleRate: 48000, sampleChannels: 2); using var customEngine = AudioEngine.CreateDefault(options); ``` ``` -------------------------------- ### Create and Play Raw Audio Buffer Source: https://github.com/feliwir/sharpaudio/blob/master/README.md Demonstrates the low-level interface for creating an audio engine, buffer, and source, then populating a buffer with generated audio samples and playing it. ```csharp var engine = AudioEngine.CreateDefault(); var buffer = engine.CreateBuffer(); var source = engine.CreateSource(); // Play a 1s long sound at 440hz AudioFormat format; format.BitsPerSample = 16; format.Channels = 1; format.SampleRate = 44100; float freq = 440.0f; var size = format.SampleRate; var samples = new short[size]; for (int i = 0; i < size; i++) { samples[i] = (short)(32760 * Math.Sin((2 * Math.PI * freq) / size * i)); } buffer.BufferData(samples, format); source.QueueBuffer(buffer); source.Play(); ``` -------------------------------- ### Create AudioEngine Instances Source: https://context7.com/feliwir/sharpaudio/llms.txt Use static factory methods to create an AudioEngine. The engine is IDisposable and should be wrapped in a using block. Custom options can be supplied for sample rate and channel count. ```csharp using SharpAudio; // Automatically picks XAudio2 on Windows, OpenAL elsewhere using var engine = AudioEngine.CreateDefault(); if (engine == null) throw new Exception("No audio backend available on this platform."); Console.WriteLine($"Backend: {engine.BackendType}"); // e.g. "XAudio2" or "OpenAL" // Force a specific backend using var alEngine = AudioEngine.CreateOpenAL(); // returns null if OpenAL unavailable using var xa2Engine = AudioEngine.CreateXAudio(); // returns null if XAudio2 unavailable // Supply custom options (sample rate, channel count) var options = new AudioEngineOptions(sampleRate: 48000, sampleChannels: 2); using var customEngine = AudioEngine.CreateDefault(options); ``` -------------------------------- ### Play Sound File with Codec Package Source: https://github.com/feliwir/sharpaudio/blob/master/README.md Shows how to use the high-level interface from the SharpAudio.Codec package to load and play sound files like MP3s. ```csharp var engine = AudioEngine.CreateDefault(); var soundStream = new SoundStream(File.OpenRead("test.mp3"), engine); soundStream.Volume = 0.5f; soundStream.Play(); ``` -------------------------------- ### AudioCapture — Record audio input Source: https://context7.com/feliwir/sharpaudio/llms.txt Shows how to use AudioCapture for recording audio input from devices. It covers creating an AudioCapture instance with custom options and checking the available backend type. ```APIDOC ## AudioCapture — Record audio input `AudioCapture` mirrors `AudioEngine` for input devices. On Windows it uses MediaFoundation; on other platforms it falls back to OpenAL capture. ```csharp using SharpAudio; // Capture at 16 kHz mono (speech-friendly settings) var captureOpts = new AudioCaptureOptions(sampleRate: 16000, sampleChannels: 1); using var capture = AudioCapture.CreateDefault(captureOpts); // or explicitly: using var alCapture = AudioCapture.CreateOpenAL(captureOpts); using var mfCapture = AudioCapture.CreateMediaFoundation(captureOpts); if (capture == null) Console.WriteLine("No capture backend available."); else Console.WriteLine($"Capture backend: {capture.BackendType}"); ``` ``` -------------------------------- ### SoundSink - Low-level audio sink with optional raw sample access Source: https://context7.com/feliwir/sharpaudio/llms.txt Demonstrates how to use SoundSink for low-level audio output and intercept raw PCM data using ISoundSinkReceiver. It shows setting up a sink, connecting a SoundStream, and basic playback control. ```APIDOC ## SoundSink — Low-level audio sink with optional raw sample access `SoundSink` is the bridge between decoded PCM bytes and a `BufferChain`/`AudioSource`. It runs its own thread and accepts audio via `Send(byte[])`. Use `ISoundSinkReceiver` to intercept the raw PCM data (e.g., for visualization or recording). ```csharp using System; using System.IO; using SharpAudio; using SharpAudio.Codec; // Custom receiver that logs buffer sizes class DebugReceiver : ISoundSinkReceiver { public void Receive(byte[] buffer) => Console.WriteLine($"[SoundSink] received {buffer.Length} bytes"); public void Dispose() { } } using var engine = AudioEngine.CreateDefault(); var sink = new SoundSink(engine, submixer: null, receiver: new DebugReceiver()); // Wire a SoundStream to the shared sink using var soundStream = new SoundStream(File.OpenRead("music.ogg"), sink, autoDisposeSink: false); soundStream.Play(); // sink.Source exposes the underlying AudioSource for direct control sink.Source.Volume = 0.5f; sink.Dispose(); // stops playback thread ``` ``` -------------------------------- ### Audio3DEngine — Spatial audio positioning Source: https://context7.com/feliwir/sharpaudio/llms.txt Demonstrates how to use Audio3DEngine for spatial audio positioning. It includes setting listener position and orientation, creating audio sources, and positioning them in 3D space. ```APIDOC ## Audio3DEngine — Spatial audio positioning `Audio3DEngine` positions a listener and individual sources in 3-D space using `System.Numerics.Vector3`. Retrieve an instance via `engine.Create3DEngine()`. ```csharp using System; using System.Numerics; using System.Threading; using SharpAudio; using var engine = AudioEngine.CreateDefault(); var audio3d = engine.Create3DEngine(); // Position the listener (camera / player head) at origin, facing –Z audio3d.SetListenerPosition(new Vector3(0, 0, 0)); audio3d.SetListenerOrientation( top: Vector3.UnitY, // up vector front: -Vector3.UnitZ // forward vector ); // Create two sources: one left, one right var buffer = engine.CreateBuffer(); var leftSrc = engine.CreateSource(); var rightSrc = engine.CreateSource(); var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; var samples = new short[44100]; for (int i = 0; i < samples.Length; i++) samples[i] = (short)(32760 * Math.Sin(2 * Math.PI * 440.0 / samples.Length * i)); buffer.BufferData(samples, format); leftSrc.QueueBuffer(buffer); rightSrc.QueueBuffer(buffer); audio3d.SetSourcePosition(leftSrc, new Vector3(-10, 0, 0)); // 10 m to the left audio3d.SetSourcePosition(rightSrc, new Vector3( 10, 0, 0)); // 10 m to the right leftSrc.Play(); Thread.Sleep(1500); rightSrc.Play(); Thread.Sleep(1500); ``` ``` -------------------------------- ### AudioBuffer.BufferData - Upload PCM samples Source: https://context7.com/feliwir/sharpaudio/llms.txt `AudioBuffer` transfers raw PCM data to the audio hardware. Three overloads accept arrays, spans, or raw memory pointers. Create buffers via `engine.CreateBuffer()`. ```APIDOC ## AudioBuffer.BufferData — Upload PCM samples `AudioBuffer` transfers raw PCM data to the audio hardware. Three overloads accept arrays, spans, or raw memory pointers. Create buffers via `engine.CreateBuffer()`. ```csharp using System; using SharpAudio; using var engine = AudioEngine.CreateDefault(); using var buffer = engine.CreateBuffer(); // --- Array overload: generate a 440 Hz sine wave (1 second, mono, 16-bit) --- var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; int sampleCount = format.SampleRate; // 1 second worth var samples = new short[sampleCount]; for (int i = 0; i < sampleCount; i++) samples[i] = (short)(32760 * Math.Sin(2 * Math.PI * 440.0 / sampleCount * i)); buffer.BufferData(samples, format); // --- Span overload: slice an existing array --- var span = samples.AsSpan(0, sampleCount / 2); // first 0.5 seconds buffer.BufferData(span, format); // --- IntPtr overload: interop with unmanaged memory --- unsafe { fixed (short* ptr = samples) { buffer.BufferData((IntPtr)ptr, sampleCount * sizeof(short), format); } } Console.WriteLine($"Buffer format: {buffer.Format.SampleRate} Hz, {buffer.Format.Channels} ch"); ``` ``` -------------------------------- ### AudioCapture: Record audio input Source: https://context7.com/feliwir/sharpaudio/llms.txt AudioCapture provides audio input functionality, falling back to OpenAL capture on non-Windows platforms. Configure capture options like sample rate and channels before creating the capture instance. ```csharp using SharpAudio; // Capture at 16 kHz mono (speech-friendly settings) var captureOpts = new AudioCaptureOptions(sampleRate: 16000, sampleChannels: 1); using var capture = AudioCapture.CreateDefault(captureOpts); // or explicitly: using var alCapture = AudioCapture.CreateOpenAL(captureOpts); using var mfCapture = AudioCapture.CreateMediaFoundation(captureOpts); if (capture == null) Console.WriteLine("No capture backend available."); else Console.WriteLine($"Capture backend: {capture.BackendType}"); ``` -------------------------------- ### SoundSink: Low-level audio playback with raw sample access Source: https://context7.com/feliwir/sharpaudio/llms.txt Use SoundSink for direct PCM byte playback to an AudioSource. Implement ISoundSinkReceiver to intercept raw PCM data for visualization or recording. Ensure the AudioEngine and SoundSink are disposed to stop playback threads. ```csharp using System; using System.IO; using SharpAudio; using SharpAudio.Codec; // Custom receiver that logs buffer sizes class DebugReceiver : ISoundSinkReceiver { public void Receive(byte[] buffer) => Console.WriteLine($"[SoundSink] received {buffer.Length} bytes"); public void Dispose() { } } using var engine = AudioEngine.CreateDefault(); var sink = new SoundSink(engine, submixer: null, receiver: new DebugReceiver()); // Wire a SoundStream to the shared sink using var soundStream = new SoundStream(File.OpenRead("music.ogg"), sink, autoDisposeSink: false); soundStream.Play(); // sink.Source exposes the underlying AudioSource for direct control sink.Source.Volume = 0.5f; sink.Dispose(); // stops playback thread ``` -------------------------------- ### Upload PCM Samples to AudioBuffer Source: https://context7.com/feliwir/sharpaudio/llms.txt AudioBuffer transfers raw PCM data to the audio hardware using one of three overloads: arrays, spans, or raw memory pointers. Buffers are created via engine.CreateBuffer(). ```csharp using System; using SharpAudio; using var engine = AudioEngine.CreateDefault(); using var buffer = engine.CreateBuffer(); // --- Array overload: generate a 440 Hz sine wave (1 second, mono, 16-bit) --- var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; int sampleCount = format.SampleRate; // 1 second worth var samples = new short[sampleCount]; for (int i = 0; i < sampleCount; i++) samples[i] = (short)(32760 * Math.Sin(2 * Math.PI * 440.0 / sampleCount * i)); buffer.BufferData(samples, format); // --- Span overload: slice an existing array --- var span = samples.AsSpan(0, sampleCount / 2); // first 0.5 seconds buffer.BufferData(span, format); // --- IntPtr overload: interop with unmanaged memory --- unsafe { fixed (short* ptr = samples) { buffer.BufferData((IntPtr)ptr, sampleCount * sizeof(short), format); } } Console.WriteLine($"Buffer format: {buffer.Format.SampleRate} Hz, {buffer.Format.Channels} ch"); ``` -------------------------------- ### Describe PCM Sample Layout Source: https://context7.com/feliwir/sharpaudio/llms.txt AudioFormat describes the encoding of raw PCM data, including sample rate, channels, and bits per sample. This is required when uploading samples to an AudioBuffer. ```csharp using SharpAudio; var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; Console.WriteLine($"Bytes/sample : {format.BytesPerSample}"); // 2 Console.WriteLine($"Bytes/second : {format.BytesPerSecond}"); // 88200 ``` -------------------------------- ### Play Audio from In-Memory Stream with SoundStream Source: https://context7.com/feliwir/sharpaudio/llms.txt Demonstrates playing audio from an in-memory stream, such as an embedded resource. SoundStream automatically detects the audio format from the stream. ```csharp using System; using System.IO; using System.Reflection; using SharpAudio; using SharpAudio.Codec; using var engine = AudioEngine.CreateDefault(); // Load embedded resource (as used in tests) var asm = Assembly.GetExecutingAssembly(); using var stream = asm.GetManifestResourceStream("MyApp.Assets.bach.wav"); using var soundStream = new SoundStream(stream, engine); // Inspect format after opening Console.WriteLine($"Sample rate : {soundStream.Format.SampleRate}"); // 44100 Console.WriteLine($"Channels : {soundStream.Format.Channels}"); // 2 Console.WriteLine($"Duration : {soundStream.Duration.Seconds} s"); // 54 soundStream.Play(); ``` -------------------------------- ### Audio3DEngine: Spatial audio positioning Source: https://context7.com/feliwir/sharpaudio/llms.txt Position audio sources and listeners in 3D space using Vector3. Set listener position and orientation, then create buffers and sources. Assign positions to sources and play them to achieve spatial audio effects. ```csharp using System; using System.Numerics; using System.Threading; using SharpAudio; using var engine = AudioEngine.CreateDefault(); var audio3d = engine.Create3DEngine(); // Position the listener (camera / player head) at origin, facing –Z audio3d.SetListenerPosition(new Vector3(0, 0, 0)); audio3d.SetListenerOrientation( top: Vector3.UnitY, // up vector front: -Vector3.UnitZ // forward vector ); // Create two sources: one left, one right var buffer = engine.CreateBuffer(); var leftSrc = engine.CreateSource(); var rightSrc = engine.CreateSource(); var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; var samples = new short[44100]; for (int i = 0; i < samples.Length; i++) samples[i] = (short)(32760 * Math.Sin(2 * Math.PI * 440.0 / samples.Length * i)); buffer.BufferData(samples, format); leftSrc.QueueBuffer(buffer); rightSrc.QueueBuffer(buffer); audio3d.SetSourcePosition(leftSrc, new Vector3(-10, 0, 0)); // 10 m to the left audio3d.SetSourcePosition(rightSrc, new Vector3( 10, 0, 0)); // 10 m to the right leftSrc.Play(); Thread.Sleep(1500); rightSrc.Play(); Thread.Sleep(1500); ``` -------------------------------- ### Configure AudioEngine Parameters Source: https://context7.com/feliwir/sharpaudio/llms.txt AudioEngineOptions controls the target sample rate and channel count for audio resampling. Default values are 44100 Hz stereo. Custom options can be passed during engine creation. ```csharp using SharpAudio; // Default construction (44100 Hz, 2 channels) var defaultOpts = new AudioEngineOptions(); // Custom construction var opts = new AudioEngineOptions(sampleRate: 48000, sampleChannels: 1); Console.WriteLine(opts.SampleRate); // 48000 Console.WriteLine(opts.SampleChannels); // 1 using var engine = AudioEngine.CreateOpenAL(opts); ``` -------------------------------- ### Submixer — Group sources for shared volume control Source: https://context7.com/feliwir/sharpaudio/llms.txt Illustrates the use of Submixer for grouping audio sources and applying shared volume control. It shows creating a submixer, setting its volume, and assigning sources to it. ```APIDOC ## Submixer — Group sources for shared volume control A `Submixer` acts as an audio bus: sources assigned to it inherit the mixer's volume. Create one via `engine.CreateSubmixer()` and pass it to `engine.CreateSource(submixer)`. ```csharp using System; using System.Threading; using SharpAudio; using var engine = AudioEngine.CreateDefault(); // Direct source (full volume) var directSource = engine.CreateSource(); // Submixer bus at 50 % volume var submixer = engine.CreateSubmixer(); submixer.Volume = 0.5f; var mixedSource = engine.CreateSource(submixer); var buffer = engine.CreateBuffer(); var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; var samples = new short[44100]; for (int i = 0; i < samples.Length; i++) samples[i] = (short)(32760 * Math.Sin(2 * Math.PI * 440.0 / samples.Length * i)); buffer.BufferData(samples, format); directSource.QueueBuffer(buffer); MixedSource.QueueBuffer(buffer); directSource.Play(); // plays at 100 % Thread.Sleep(1500); MixedSource.Play(); // plays at 50 % (submixer volume) Thread.Sleep(1500); submixer.Dispose(); ``` ``` -------------------------------- ### Submixer: Group sources for shared volume control Source: https://context7.com/feliwir/sharpaudio/llms.txt Use Submixer as an audio bus to group sources for shared volume control. Create a Submixer via engine.CreateSubmixer(), set its volume, and pass it to engine.CreateSource() for sources that should be affected by the mixer's volume. ```csharp using System; using System.Threading; using SharpAudio; using var engine = AudioEngine.CreateDefault(); // Direct source (full volume) var directSource = engine.CreateSource(); // Submixer bus at 50 % volume var submixer = engine.CreateSubmixer(); submixer.Volume = 0.5f; var mixedSource = engine.CreateSource(submixer); var buffer = engine.CreateBuffer(); var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; var samples = new short[44100]; for (int i = 0; i < samples.Length; i++) samples[i] = (short)(32760 * Math.Sin(2 * Math.PI * 440.0 / samples.Length * i)); buffer.BufferData(samples, format); directSource.QueueBuffer(buffer); MixedSource.QueueBuffer(buffer); directSource.Play(); // plays at 100 % Thread.Sleep(1500); MixedSource.Play(); // plays at 50 % (submixer volume) Thread.Sleep(1500); submixer.Dispose(); ``` -------------------------------- ### Gapless Streaming with BufferChain Source: https://context7.com/feliwir/sharpaudio/llms.txt Utilizes BufferChain for gapless audio streaming by managing a pool of AudioBuffer objects. The QueueData method is a convenience for buffering and queuing. ```csharp using System; using System.Threading; using SharpAudio; using var engine = AudioEngine.CreateDefault(); var chain = new BufferChain(engine); var source = engine.CreateSource(); source.Play(); var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; int chunkSamples = 4410; // 100 ms chunks // Stream 5 chunks (0.5 s total) for (int chunk = 0; chunk < 5; chunk++) { var samples = new short[chunkSamples]; for (int i = 0; i < chunkSamples; i++) samples[i] = (short)(32760 * Math.Sin(2 * Math.PI * 440.0 / chunkSamples * i)); // BufferData rotates through the internal pool and returns the filled buffer var buf = chain.BufferData(samples, format); // QueueData is a convenience that combines BufferData + QueueBuffer chain.QueueData(source, samples, format); Thread.Sleep(80); } ``` -------------------------------- ### AudioEngineOptions - Configure engine parameters Source: https://context7.com/feliwir/sharpaudio/llms.txt `AudioEngineOptions` controls the target sample rate and channel count used when the engine resamples audio. Defaults to 44100 Hz stereo. ```APIDOC ## AudioEngineOptions — Configure engine parameters `AudioEngineOptions` controls the target sample rate and channel count used when the engine resamples audio. Defaults to 44 100 Hz stereo. ```csharp using SharpAudio; // Default construction (44100 Hz, 2 channels) var defaultOpts = new AudioEngineOptions(); // Custom construction var opts = new AudioEngineOptions(sampleRate: 48000, sampleChannels: 1); Console.WriteLine(opts.SampleRate); // 48000 Console.WriteLine(opts.SampleChannels); // 1 using var engine = AudioEngine.CreateOpenAL(opts); ``` ``` -------------------------------- ### AudioFormat - Describe PCM sample layout Source: https://context7.com/feliwir/sharpaudio/llms.txt `AudioFormat` is a value type that describes how raw PCM data is encoded. It is required when uploading samples to an `AudioBuffer`. ```APIDOC ## AudioFormat — Describe PCM sample layout `AudioFormat` is a value type that describes how raw PCM data is encoded. It is required when uploading samples to an `AudioBuffer`. ```csharp using SharpAudio; var format = new AudioFormat { SampleRate = 44100, Channels = 1, BitsPerSample = 16 }; Console.WriteLine($"Bytes/sample : {format.BytesPerSample}"); // 2 Console.WriteLine($"Bytes/second : {format.BytesPerSecond}"); // 88200 ``` ``` -------------------------------- ### High-Level File Playback with SoundStream Source: https://context7.com/feliwir/sharpaudio/llms.txt Uses SoundStream for high-level audio file playback, supporting various formats like MP3 and WAV. It manages its own decoding thread and auto-detects file formats. ```csharp using System; using System.IO; using System.Threading; using SharpAudio; using SharpAudio.Codec; using var engine = AudioEngine.CreateDefault(); // --- Play an MP3 file --- using var soundStream = new SoundStream(File.OpenRead("music.mp3"), engine); Console.WriteLine($"Format : {soundStream.Format.SampleRate} Hz, " + $"{soundStream.Format.Channels} ch, " + $"{soundStream.Format.BitsPerSample}-bit"); Console.WriteLine($"Duration : {soundStream.Duration}"); Console.WriteLine($"Streamed : {soundStream.IsStreamed}"); soundStream.Volume = 0.75f; // 0.0 – 1.0 soundStream.Play(); // toggle: Paused → Playing // Poll current position while playing while (soundStream.IsPlaying) { Console.Write($"\rPosition : {soundStream.Position:mm\:ss}"); Thread.Sleep(200); } Console.WriteLine($"\nFinal state: {soundStream.State}"); // TrackFinished // --- Seek example --- soundStream.Play(); // restart Thread.Sleep(3000); soundStream.TrySeek(TimeSpan.FromSeconds(10)); // jump to 10 s // --- Stop early --- soundStream.Stop(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.