Audio plays now

This commit is contained in:
Alexander Shabarshov 2026-07-04 13:17:14 +01:00
parent ea45bef297
commit e08d5f8991
2 changed files with 10 additions and 6 deletions

View File

@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using NAudio.CoreAudioApi; using NAudio.CoreAudioApi;
using NAudio.Dmo;
using NAudio.Wave; using NAudio.Wave;
namespace AudioCore.Impl; namespace AudioCore.Impl;
@ -11,6 +12,7 @@ public sealed class WasapiOutputDevice : IAudioOutputDevice, IDisposable
private readonly BufferedWaveProvider _buffer; private readonly BufferedWaveProvider _buffer;
private readonly WaveFormat _mixFormat = null!; private readonly WaveFormat _mixFormat = null!;
private readonly ByteBufferPool _pool; private readonly ByteBufferPool _pool;
private readonly bool _isFloat;
public int SampleRate { get; } public int SampleRate { get; }
public int Channels { get; } public int Channels { get; }
@ -51,16 +53,18 @@ public sealed class WasapiOutputDevice : IAudioOutputDevice, IDisposable
// Use the system mix format (required for shared mode) // Use the system mix format (required for shared mode)
_mixFormat = device.AudioClient.MixFormat; _mixFormat = device.AudioClient.MixFormat;
SampleRate = _mixFormat.SampleRate; SampleRate = _mixFormat.SampleRate;
_isFloat = _mixFormat.Encoding == WaveFormatEncoding.Extensible &&
((WaveFormatExtensible)_mixFormat).SubFormat == AudioMediaSubtypes.MEDIASUBTYPE_IEEE_FLOAT;
// Create buffer in mix format // Create buffer in mix format
_buffer = new BufferedWaveProvider(_mixFormat) _buffer = new BufferedWaveProvider(_mixFormat)
{ {
BufferLength = _mixFormat.AverageBytesPerSecond * 4, // 4 seconds BufferLength = _mixFormat.AverageBytesPerSecond / 2, // 0.5 seconds
DiscardOnBufferOverflow = false DiscardOnBufferOverflow = false
}; };
// Event-driven WASAPI mode (true = event mode) // Event-driven WASAPI mode (true = event mode)
_out = new WasapiOut(device, AudioClientShareMode.Shared, true, 10); _out = new WasapiOut(device, AudioClientShareMode.Shared, false, 10);
_out.Init(_buffer); _out.Init(_buffer);
} }
@ -72,7 +76,7 @@ public sealed class WasapiOutputDevice : IAudioOutputDevice, IDisposable
// Convert float -> PCM16 or float passthrough depending on mix format // Convert float -> PCM16 or float passthrough depending on mix format
byte[] bytes; byte[] bytes;
if (_mixFormat.Encoding == WaveFormatEncoding.IeeeFloat) if (_isFloat)
{ {
// Device supports float32 directly // Device supports float32 directly
bytes = MemoryMarshal.AsBytes(samples).ToArray(); bytes = MemoryMarshal.AsBytes(samples).ToArray();

View File

@ -72,9 +72,9 @@ public sealed class Pipeline_Integration_Tests
$"\"{outFlac}\"", $"\"{outFlac}\"",
RedirectStandardInput = true, RedirectStandardInput = true,
RedirectStandardError = true, RedirectStandardError = true,
UseShellExecute = false, UseShellExecute = false,
CreateNoWindow = true, CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden, WindowStyle = ProcessWindowStyle.Hidden,
}; };
using var ff = Process.Start(psi); using var ff = Process.Start(psi);