Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Speaker update #319

Merged
merged 2 commits into from
Dec 25, 2024
Merged
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
34 changes: 33 additions & 1 deletion EXILED/Exiled.API/Features/Toys/Speaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

namespace Exiled.API.Features.Toys
{
using System.Collections.Generic;

using AdminToys;
using Enums;
using Exiled.API.Interfaces;
using UnityEngine;
using VoiceChat.Networking;
using VoiceChat.Playbacks;

/// <summary>
/// A wrapper class for <see cref="SpeakerToy"/>.
Expand Down Expand Up @@ -44,7 +48,7 @@ internal Speaker(SpeakerToy speakerToy)
public float Volume
{
get => Base.NetworkVolume;
set => Base.NetworkVolume = value;
set => Base.NetworkVolume = Mathf.Clamp01(value);
}

/// <summary>
Expand Down Expand Up @@ -86,6 +90,15 @@ public float MinDistance
set => Base.NetworkMinDistance = value;
}

/// <summary>
/// Gets or sets the controller ID of speaker.
/// </summary>
public byte ControllerId
{
get => Base.NetworkControllerId;
set => Base.NetworkControllerId = value;
}

/// <summary>
/// Creates a new <see cref="Speaker"/>.
/// </summary>
Expand All @@ -108,5 +121,24 @@ public static Speaker Create(Vector3? position, Vector3? rotation, Vector3? scal

return speaker;
}

/// <summary>
/// Plays audio through this speaker.
/// </summary>
/// <param name="message">An <see cref="AudioMessage"/> instance.</param>
/// <param name="targets">Targets who will hear the audio. If <c>null</c>, audio will be sent to all players.</param>
public static void Play(AudioMessage message, IEnumerable<Player> targets = null)
{
foreach (Player target in targets ?? Player.List)
target.Connection.Send(message);
}

/// <summary>
/// Plays audio through this speaker.
/// </summary>
/// <param name="samples">Audio samples.</param>
/// <param name="length">The length of the samples array.</param>
/// <param name="targets">Targets who will hear the audio. If <c>null</c>, audio will be sent to all players.</param>
public void Play(byte[] samples, int? length = null, IEnumerable<Player> targets = null) => Play(new AudioMessage(ControllerId, samples, length ?? samples.Length), targets);
}
}
Loading