Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using NAudio.Dsp;
using NAudio.Wave;
using Sendspin.SDK.Audio;
using SendspinClient.Services.Diagnostics;

namespace SendspinClient.Services.Audio;

/// <summary>
Expand All @@ -30,7 +28,6 @@ public sealed class DynamicResamplerSampleProvider : ISampleProvider, IDisposabl
private readonly ISyncCorrectionProvider? _correctionProvider;
private readonly WdlResampler _resampler;
private readonly ILogger? _logger;
private readonly IDiagnosticAudioRecorder? _diagnosticRecorder;
private readonly object _rateLock = new();
private readonly int _targetSampleRate;

Expand Down Expand Up @@ -135,20 +132,17 @@ public double PlaybackRate
/// Pass 0 or the source sample rate to perform only sync correction.
/// </param>
/// <param name="logger">Optional logger for debugging.</param>
/// <param name="diagnosticRecorder">Optional diagnostic recorder for audio capture.</param>
public DynamicResamplerSampleProvider(
ISampleProvider source,
ISyncCorrectionProvider? correctionProvider = null,
int targetSampleRate = 0,
ILogger? logger = null,
IDiagnosticAudioRecorder? diagnosticRecorder = null)
ILogger? logger = null)
{
ArgumentNullException.ThrowIfNull(source);

_source = source;
_correctionProvider = correctionProvider;
_logger = logger;
_diagnosticRecorder = diagnosticRecorder;

// Determine target sample rate - use source rate if not specified
_targetSampleRate = targetSampleRate > 0 ? targetSampleRate : source.WaveFormat.SampleRate;
Expand Down Expand Up @@ -262,10 +256,6 @@ public int Read(float[] buffer, int offset, int count)
// Resample
var outputGenerated = Resample(_sourceBuffer, inputRead, buffer, offset, count);

// Capture audio for diagnostic recording if enabled
// Zero overhead when disabled - null check is branch-predicted away
_diagnosticRecorder?.CaptureIfEnabled(buffer.AsSpan(offset, outputGenerated));

// If we didn't generate enough output, pad with silence and track underrun
if (outputGenerated < count)
{
Expand Down
11 changes: 1 addition & 10 deletions src/SendspinClient.Services/Audio/SoundTouchSampleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using NAudio.Wave;
using Sendspin.SDK.Audio;
using SoundTouch;
using SendspinClient.Services.Diagnostics;

namespace SendspinClient.Services.Audio;

/// <summary>
Expand All @@ -31,7 +29,6 @@ public sealed class SoundTouchSampleProvider : ISampleProvider, IDisposable
private readonly ISyncCorrectionProvider? _correctionProvider;
private readonly SoundTouchProcessor _processor;
private readonly ILogger? _logger;
private readonly IDiagnosticAudioRecorder? _diagnosticRecorder;
private readonly object _rateLock = new();
private readonly int _channels;

Expand Down Expand Up @@ -128,19 +125,16 @@ public double PlaybackRate
/// <param name="source">The upstream sample provider to read from.</param>
/// <param name="correctionProvider">Optional sync correction provider to subscribe to for rate change events.</param>
/// <param name="logger">Optional logger for debugging.</param>
/// <param name="diagnosticRecorder">Optional diagnostic recorder for audio capture.</param>
public SoundTouchSampleProvider(
ISampleProvider source,
ISyncCorrectionProvider? correctionProvider = null,
ILogger? logger = null,
IDiagnosticAudioRecorder? diagnosticRecorder = null)
ILogger? logger = null)
{
ArgumentNullException.ThrowIfNull(source);

_source = source;
_correctionProvider = correctionProvider;
_logger = logger;
_diagnosticRecorder = diagnosticRecorder;
_channels = source.WaveFormat.Channels;

// Output format matches input (SoundTouch doesn't change sample rate, just playback rate)
Expand Down Expand Up @@ -284,9 +278,6 @@ public int Read(float[] buffer, int offset, int count)
Array.Fill(buffer, 0f, offset + totalSamplesOutput, count - totalSamplesOutput);
}

// Capture audio for diagnostic recording if enabled
_diagnosticRecorder?.CaptureIfEnabled(buffer.AsSpan(offset, count));

return count;
}

Expand Down
13 changes: 3 additions & 10 deletions src/SendspinClient.Services/Audio/WasapiAudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Sendspin.SDK.Models;
using Sendspin.SDK.Synchronization;
using SendspinClient.Services.Models;
using SendspinClient.Services.Diagnostics;

namespace SendspinClient.Services.Audio;

Expand Down Expand Up @@ -52,7 +51,6 @@ public sealed class WasapiAudioPlayer : IAudioPlayer
private readonly ILogger<WasapiAudioPlayer> _logger;
private readonly SyncCorrectionStrategy _syncStrategy;
private readonly ResamplerType _resamplerType;
private readonly IDiagnosticAudioRecorder? _diagnosticRecorder;
private string? _deviceId;
private WasapiOut? _wasapiOut;
private AudioSampleProviderAdapter? _sampleProvider;
Expand Down Expand Up @@ -166,19 +164,16 @@ public bool IsMuted
/// WDL uses sinc interpolation, SoundTouch uses WSOLA algorithm.
/// Ignored when strategy is DropInsertOnly.
/// </param>
/// <param name="diagnosticRecorder">Optional diagnostic recorder for audio capture.</param>
public WasapiAudioPlayer(
ILogger<WasapiAudioPlayer> logger,
string? deviceId = null,
SyncCorrectionStrategy syncStrategy = SyncCorrectionStrategy.Combined,
ResamplerType resamplerType = ResamplerType.Wdl,
IDiagnosticAudioRecorder? diagnosticRecorder = null)
ResamplerType resamplerType = ResamplerType.Wdl)
{
_logger = logger;
_deviceId = deviceId;
_syncStrategy = syncStrategy;
_resamplerType = resamplerType;
_diagnosticRecorder = diagnosticRecorder;
}

/// <summary>
Expand Down Expand Up @@ -382,8 +377,7 @@ private void CreateResampler(ISampleProvider sourceProvider)
var soundTouch = new SoundTouchSampleProvider(
sourceProvider,
_correctionProvider,
_logger,
_diagnosticRecorder);
_logger);
_resamplerProvider = soundTouch;
_resamplerDisposable = soundTouch;
break;
Expand All @@ -394,8 +388,7 @@ private void CreateResampler(ISampleProvider sourceProvider)
sourceProvider,
_correctionProvider,
_deviceNativeSampleRate,
_logger,
_diagnosticRecorder);
_logger);
_resamplerProvider = wdl;
_resamplerDisposable = wdl;
break;
Expand Down
Loading
Loading