Oakrey.Audio
2.0.4
dotnet add package Oakrey.Audio --version 2.0.4
NuGet\Install-Package Oakrey.Audio -Version 2.0.4
<PackageReference Include="Oakrey.Audio" Version="2.0.4" />
<PackageVersion Include="Oakrey.Audio" Version="2.0.4" />
<PackageReference Include="Oakrey.Audio" />
paket add Oakrey.Audio --version 2.0.4
#r "nuget: Oakrey.Audio, 2.0.4"
#:package Oakrey.Audio@2.0.4
#addin nuget:?package=Oakrey.Audio&version=2.0.4
#tool nuget:?package=Oakrey.Audio&version=2.0.4
Oakrey.Audio
A Windows .NET library built on NAudio for audio recording,
playback, synthesis, mixing, and signal analysis. The central type is FFTAudioSample, which
wraps any ISampleProvider and adds an online STFT/FFT pipeline, reactive state tracking via
IObservable<State>, and spectrum accumulation into SpectraList.
Main features
- FFT / STFT pipeline -
FFTAudioSamplefeeds every read throughSampleAggregatorand accumulates frequency spectra inSpectraList.ShortTimeFourierTransformationprovides a standalone STFT entry point. - Playback -
Playerloads any audio file orISampleProvider, supports configurable start/length offsets, volume control, output device selection, andSaveToFile. - Synthesis -
Synthesizersgenerates sine, square, sawtooth, pink noise, and sweep waveforms at configurable frequency and gain, ready to play or compare. - Mixing -
Mixer(playable) andSampleCreators.Mixer(analysis-only) wrap NAudio'sMixingSampleProviderwith automatic format conversion onAddAudioStream. - Recording -
FileRecorderstreams microphone input directly to a WAV file.ObservableRecorderpublishes a completedFFTAudioSampleviaIObservable<FFTAudioSample>onStopRecording. - Audio comparators -
ComparatorSNRSegcomputes segmented SNR with Hann windowing and configurable overlap.ComparatorFwSNRSegcomputes frequency-weighted segmented SNR.ComparatorLogLikelihoodRatiocomputes LLR using a Levinson-Durbin AR model. - Device enumeration -
AudioDevicesEnumeratorlists all WaveIn / WaveOut devices by name and resolves device indices for recording and playback. - File I/O -
WaveAudioFileAccessloads a region of a WAV file into anFFTAudioSampleand saves anFFTAudioSampleback to a WAV file.
Architecture
classDiagram
class FFTAudioSample {
+SpectraList ListOfFrequencySpectra
+IObservable~State~ Status
+bool GetSpectra
+Play() Pause() Stop() Resume() Record()
}
class AudioBasePlayable {
+byte Volume
+Play(deviceName)
+SaveToFile(filename)
}
class RecorderBase {
+TimeSpan RecordTime
+bool SpectrumComputing
+StartRecording()
+StopRecording()
}
FFTAudioSample <|-- AudioBasePlayable
FFTAudioSample <|-- RecorderBase
AudioBasePlayable <|-- Player
AudioBasePlayable <|-- Players_Mixer
AudioBasePlayable <|-- Synthesizers
RecorderBase <|-- FileRecorder
RecorderBase <|-- ObservableRecorder
class ComparatorBase
ComparatorBase <|-- ComparatorSNRSeg
ComparatorBase <|-- ComparatorFwSNRSeg
ComparatorBase <|-- ComparatorLogLikelihoodRatio
Project structure
| Path | Purpose |
|---|---|
FFTAudioSample.cs |
Core type: ISampleProvider + online FFT + reactive State |
ISpectra.cs |
Interface for FFT data and frequency index resolution |
State.cs |
Playing / Paused / Stopped / Recording enum |
Players/Player.cs |
File and ISampleProvider playback with offset support |
Players/Synthesizers.cs |
Playable signal generators (sine, square, sawtooth, sweep, pink noise) |
Players/Mixer.cs |
Playable multi-input mixer |
Players/FileRecorder.cs |
Microphone to WAV file recorder |
Players/ObservableRecorder.cs |
Microphone recorder that publishes FFTAudioSample on stop |
Players/AudioBasePlayable.cs |
Base for all playable types (WaveOut management, volume, device) |
Players/RecorderBase.cs |
Base for all recorder types (WaveIn management, spectrum toggle) |
SampleCreators/ |
Mixer (analysis), Synthesizers (analysis), WaveAudioFileAccess |
AudioComparators/ |
ComparatorSNRSeg, ComparatorFwSNRSeg, ComparatorLogLikelihoodRatio |
AudioLib/ |
SampleAggregator, ShortTimeFourierTransformation |
Mathematic/ |
Vector, Matrix, NumericArrayExtensions, NumericExtensions |
AudioDevicesEnumerator.cs |
WaveIn / WaveOut device discovery |
SpectraList.cs, SampleList.cs |
Spectrum and sample collection types |
Requirements
- .NET 10 or higher
- Windows only (
net10.0-windows, usesWaveInEvent/WaveOutEvent) - NAudio 2.2.1
Installation
dotnet add package Oakrey.Audio
Or via the Visual Studio NuGet Package Manager: search for Oakrey.Audio.
Example usage
Play a WAV file on a specific output device
using Oakrey.Audio.Players;
using Player player = Player.Create("speech.wav");
player.PlaybackStart = TimeSpan.FromSeconds(2);
player.PlaybackLength = TimeSpan.FromSeconds(5);
player.Volume = 80;
player.Play("Speakers (Realtek High Definition Audio)");
Record microphone input to a file
using Oakrey.Audio.Players;
using FileRecorder recorder = FileRecorder.Create("capture.wav", "Microphone Array");
recorder.RecordTime = TimeSpan.FromSeconds(10);
recorder.StartRecording();
// ... wait ...
recorder.StopRecording();
Reactive recording with FFT spectrum
using Oakrey.Audio;
using Oakrey.Audio.Players;
using ObservableRecorder recorder = ObservableRecorder.Create("Microphone Array");
recorder.SpectrumComputing = true;
using IDisposable sub = recorder.SampleRecorded.Subscribe(sample =>
{
SpectraList spectra = sample.ListOfFrequencySpectra;
Console.WriteLine($"Captured {spectra.Count} spectra frames");
});
recorder.StartRecording();
await Task.Delay(TimeSpan.FromSeconds(3));
recorder.StopRecording();
Compare two audio samples with segmented SNR
using Oakrey.Audio;
using Oakrey.Audio.AudioComparators;
using Oakrey.Audio.SampleCreators;
FFTAudioSample reference = WaveAudioFileAccess.LoadFromFile("ref.wav", TimeSpan.Zero, TimeSpan.FromSeconds(5));
FFTAudioSample distorted = WaveAudioFileAccess.LoadFromFile("dist.wav", TimeSpan.Zero, TimeSpan.FromSeconds(5));
ComparatorSNRSeg comparator = new(reference, distorted);
float snr = comparator.Analyze(CancellationToken.None);
Console.WriteLine($"SNR: {snr:F2} dB");
Synthesize and play a sine wave
using Oakrey.Audio.Players;
using Synthesizers synth = Synthesizers.CreateSine(TimeSpan.FromSeconds(3), frequency: 440.0);
synth.Volume = 60;
synth.Play();
Development notes
FFTAudioSampleinherits fromDisposableBase(Oakrey.Disposables). Always dispose instances created withnewor factory methods.- The FFT window length is fixed at 2048 samples. Spectrum frames are accumulated in
SpectraListonly whileGetSpectra = true. RecorderBase.RecordTime = TimeSpan.Zeromeans unlimited recording duration.AudioDevicesEnumeratorincludes device index-1(the Windows default device mapper) in its enumeration; index-1is valid for WaveIn/WaveOut.ComparatorLogLikelihoodRatiouses a Levinson-Durbin AR model internally; AR order defaults apply and the computation supportsCancellationToken.- The library targets
net10.0-windowsexclusively; it cannot be used in cross-platform or server-side (Linux/macOS) builds.
License
MIT - Copyright (c) Oakrey 2016-present. Repository: https://dev.azure.com/oakrey/OpenPackages/_git/Drivers
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows7.0 is compatible. |
-
net10.0-windows7.0
- NAudio (>= 2.3.0)
- NAudio.WinForms (>= 2.3.0)
- Oakrey.Disposables (>= 2.0.0)
- System.Reactive (>= 6.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.