Table of Contents

Getting Started

This guide walks through referencing the Audio Extension in an OpenPV application and using its main service, AudioManagerService, from your code. For background on what the extension provides, see the Introduction.

Table of Contents

Add the Extension to Your Project

Connect a Client

Wait for the Audio System to Be Ready

Read the Current Configuration

Switch the Active Input

Control Volume

Play an Audio Prompt

Tuner and A2B

Add the Extension to Your Project

Add the extension through the OpenPV Developer Toolkit's Plugin Manager (or by referencing the Ahsoka.Extensions.AudioManager package directly). Once added, its three services — AudioManagerService, A2BService, and TunerService — are registered with AutoStart behavior, so they start automatically with your application. There is nothing further to configure to start using them; you only need to create the client(s) you want to talk to.

Connect a Client

AudioManagerServiceClient connects to the default service configuration by convention. Constructing the client does not connect it — call Start() once you've wired up any notification handlers:

using Ahsoka.Services.AudioManager;

var audioClient = new AudioManagerServiceClient();
audioClient.Start();

If you're using Dispatcher for your application startup (the recommended approach for most OpenPV apps), the client's notifications — AudioSystemReady, VolumeChanged, PromptFinished, SourcePortChanged — will be delivered on your main thread automatically.

Wait for the Audio System to Be Ready

AudioManagerService initializes the underlying audio hardware asynchronously in the background rather than during construction, and it does not report itself as ready until that initialization completes. Because TunerService and A2BService share the same underlying hardware that AudioManagerService brings up, you should wait for its AudioSystemReady notification before enabling or otherwise interacting with the tuner or the A2B bus.

This only affects when you use the tuner and A2B clients, not when you create them — construct and Start() all three clients together up front, but hold off calling into the tuner/A2B clients until AudioSystemReady arrives on the AudioManagerServiceClient:

using Ahsoka.Services.AudioManager;
using Ahsoka.Services.Tuner;
using Ahsoka.Services.A2B;

var tunerClient = new TunerServiceClient();
tunerClient.Start();

var a2bClient = new A2BServiceClient();
a2bClient.Start();

var audioClient = new AudioManagerServiceClient();
audioClient.NotificationReceived += (sender, args) =>
{
    if (args.TransportId == AudioManagerMessageTypes.Ids.AudioSystemReady)
    {
        // Safe to start using tunerClient / a2bClient now.
        tunerClient.EnableTuner();
        a2bClient.Enable();
    }
};
audioClient.Start();

If your client connects after the service has already finished initializing, AudioManagerService sends AudioSystemReady directly to that new connection as soon as it connects, so you won't miss it by subscribing late.

Read the Current Configuration

GetConfiguration() returns the AudioManagerConfiguration that was packaged with your application, describing every card's inputs, outputs, and subwoofer setup:

AudioManagerConfiguration config = audioClient.GetConfiguration();

foreach (var card in config.CardConfigurations)
{
    Console.WriteLine($"Card {card.CardId}:");
    foreach (var input in card.Inputs)
        Console.WriteLine($"  Input {input.Id}: {input.Name} ({input.PortType})");
    foreach (var output in card.Outputs)
        Console.WriteLine($"  Output {output.Id}: {output.Name} ({output.PortType})");
}

Switch the Active Input

Use SetCurrentInput with the target card and port IDs from the configuration above:

var result = audioClient.SetCurrentInput(new SetPort
{
    CardId = 0,
    PortId = 1,
    DuckTimeInMs = 0
});

DuckTimeInMs lets the transition fade the previous source out rather than cutting it instantly. For a temporary source that should duck the current input and then restore it automatically — for example a chime or navigation voice prompt — use StartAlternateInput / StopAlternateInput instead:

audioClient.StartAlternateInput(new SetAlternatePort
{
    CardId = 0,
    PortId = 2,
    DuckTimeInMs = 250,
    DuckedVolume = 8000
});

// ... later, once the alternate source is done ...
audioClient.StopAlternateInput();

Control Volume

audioClient.SetVolume(new VolumeInfo
{
    CardId = 0,
    LeftChannelVolume = 20000,
    RightChannelVolume = 20000
});

VolumeInfo current = audioClient.GetVolume(new Card { CardId = 0 });

EQ bands are controlled the same way, via SetEqualizerInfo / GetEqualizerInfo with a list of EqualizerBand { Band, Db } entries.

Play an Audio Prompt

Audio prompts can be either .wav or .mp3 files. They're loaded from a Media folder next to your application's binary — place the files in a Media folder at your project root and mark each one to copy to the output directory:

<ItemGroup>
    <None Update="Media\DoorEntry.wav">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
</ItemGroup>

On the target device this resolves to a Media folder under the application's install root; on desktop it's the Media folder under your build output/working directory. List the available prompts, then play one by name:

AudioPromptList prompts = audioClient.GetAudioPrompts(new Card { CardId = 0 });

audioClient.PlayAudioPrompt(new AudioPrompt { Prompt = prompts.Prompts.First() });

Subscribe to the PromptFinished notification if you need to know when playback completes, or call StopAudioPrompt to cancel it early.

Tuner and A2B

For applications that need direct tuner or A2B control rather than going through AudioManagerService, construct and start the corresponding client the same way — see Wait for the Audio System to Be Ready for when it's safe to start using them:

using Ahsoka.Services.Tuner;
using Ahsoka.Services.A2B;

var tunerClient = new TunerServiceClient();
tunerClient.Start();

var a2bClient = new A2BServiceClient();
a2bClient.Start();

TunerServiceClient covers enabling the tuner, seeking/searching, selecting a frequency or DAB program, and background station scanning. A2BServiceClient covers enabling/resetting the bus and reading/writing peripheral nodes directly; for common amplifier tasks (master/zone volume, mute, balance), the A2BAmpPlugin client built on top of A2BService is usually simpler than talking to a peripheral's native protocol directly.