mirror of
https://github.com/unclshura/ABStemPlayer.git
synced 2026-08-07 00:43:38 +00:00
Audio plays now
This commit is contained in:
parent
ea45bef297
commit
e08d5f8991
@ -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();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user