using Adrenak.UniMic; using UnityEngine; namespace Adrenak.UniVoice.Outputs { /// /// An implementation of that plays /// peer audio using StreamedAUdioSource, which is included in UniMic /// [RequireComponent(typeof(StreamedAudioSource))] public class StreamedAudioSourceOutput : MonoBehaviour, IAudioOutput { const string TAG = "[StreamedAudioSourceOutput]"; public StreamedAudioSource Stream { get; private set; } [System.Obsolete("Cannot use new keyword to create an instance. Use the .New() method instead")] public StreamedAudioSourceOutput() { } /// /// Creates a new instance using the dependencies. /// public static StreamedAudioSourceOutput New() { var go = new GameObject("StreamedAudioSourceOutput"); DontDestroyOnLoad(go); var cted = go.AddComponent(); cted.Stream = go.GetComponent(); Debug.unityLogger.Log(LogType.Log, TAG, "StreamedAudioSource created"); return cted; } /// /// Feeds an incoming into the audio buffer. /// /// public void Feed(AudioFrame frame) { Stream.Feed(frame.frequency, frame.channelCount, Utils.Bytes.BytesToFloats(frame.samples), true); } /// /// Disposes the instance by deleting the GameObject of the component. /// public void Dispose() { Debug.unityLogger.Log(LogType.Log, TAG, "Disposing StreamedAudioSource"); Destroy(gameObject); } /// /// Creates instances /// public class Factory : IAudioOutputFactory { public IAudioOutput Create() { return New(); } } } }