Groups - Axwabo/SecretLabNAudio GitHub Wiki

Speaker Groups

To play the same audio through multiple speakers, create a SpeakerToyGroup - this way they will share one ControllerId

The AudioPlayerExtensions and SpeakerToyExtensions have extension methods and properties to make this easier.

Each group has a controller speaker. If the controller is destroyed/pooled, all child speakers will be pooled. You can manually destroy the group, and choose whether to return speakers to the pool or destroy them altogether.

Example
using SecretLabNAudio.Core.Extensions;
using SecretLabNAudio.Core.Pools;
using UnityEngine;

AudioPlayerPool.RentDefault(Vector3.up * 300)
    .UseFile("/path/to/music.wav")
    // 2 extra speakers
    .CloneOutput(new Vector3(10, 300, 0), new Vector3(-10, 300, 0))
    .PoolOnEnd();

CloneOutput calls GetOrCreateGroup - this will return the existing group if present, and create one if necessary.

The Group IsGrouped IsGroupController IsGroupMaster extension properties on AudioPlayers and SpeakerToys provide information about the group.

SpeakerToyGroup

This class stores the controller and children. It cannot be instantiated by the consumer, use extension methods to create them.

When a child is added to the group, its ControllerId will be set to that of the group's controller.

When the controller is destroyed, the IsDestroyed property will be set to true. After this, the instance cannot be used, and will throw an ObjectDisposedException if trying to modify the group or access the controller.

You can add existing speakers to the group with the Add method.

Remove children from the group by calling Remove - the controller cannot be removed. To "remove" the controller from a group, you must destroy the group.

Caution

If the adding a speaker that's already part of another group, an InvalidOperationException will be thrown. Remove the child from its group or ungroup/destroy the group if the speaker is the group's controller.

If you want to disband (get rid of) a group, there are two options:

  • Ungroup marks the group as destroyed while leaving each speaker as is
  • Destroy returns all speakers to the pool, or destroys them altogether
    • Pass false to the pool parameter to destroy speakers

SpeakerToy Extensions

  • CreateGroup makes a group, but will throw an exception if the speaker is already grouped
  • GetOrCreateGroup gets the existing one, or creates a group

You can pass a delegate to CreateGroup to configure it, e.g. add speakers.

Group Extensions

Use the AddFromPool extension method to get a speaker from the pool and add it to the group.

The All static extension property returns a new HashSet of all groups.

The SpeakersPerGroup static extension property returns an IEnumerable of IGrouping, grouping grouped speakers by their groups.

The GroupedSpeakers static extension property returns an IEnumerable containing all grouped speakers.

Caution

Do not modify groups, and don't remove speakers from a group while enumerating the SpeakersPerGroup or GroupedSpeakers properties.

Tip

To personalize each speaker in a group, call AddPersonalizationToAll on the group, and store the results in a list. Then, you can call personalization methods on the collection.

⚠️ **GitHub.com Fallback** ⚠️