Advertisement
Guest User

Untitled

a guest
Feb 15th, 2022
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. using System;
  2. using Photon.Voice;
  3. using Photon.Voice.Unity;
  4. using UnityEngine;
  5. using ILogger = Photon.Voice.ILogger;
  6.  
  7. public class AudioSourceInputFactorySetter : VoiceComponent {
  8.     private AudioSourceInputFactory audioSourcePusher;
  9.  
  10.     [SerializeField]
  11.     private Recorder recorder;
  12.  
  13.     [SerializeField]
  14.     private AudioSource audioSource;
  15.  
  16.     protected override void Awake() {
  17.         this.audioSourcePusher = new AudioSourceInputFactory(this.audioSource, this.Logger);
  18.         this.recorder.SourceType = Recorder.InputSourceType.Factory;
  19.         this.recorder.InputFactory = this.InputFactory;
  20.         if (this.recorder.RequiresRestart) {
  21.             this.recorder.RestartRecording();
  22.         } else if (this.recorder.IsInitialized) {
  23.             this.recorder.StartRecording();
  24.         }
  25.         this.recorder.AutoStart = true;
  26.     }
  27.  
  28.     private IAudioDesc InputFactory() {
  29.         return this.audioSourcePusher;
  30.     }
  31. }
  32.  
  33. public class AudioSourceInputFactory : IAudioPusher<float> {
  34.  
  35.     private AudioSource audioSource;
  36.     private ILogger logger;
  37.  
  38.     private AudioOutCapture audioOutCapture;
  39.  
  40.     private int sampleRate;
  41.     private int channels;
  42.  
  43.     public int SamplingRate { get { return this.Error == null ? this.sampleRate : 0; } }
  44.     public int Channels { get { return this.Error == null ? this.channels : 0; } }
  45.     public string Error { get; private set; }
  46.  
  47.     public AudioSourceInputFactory(AudioSource aS, ILogger lg) {
  48.         try {
  49.             this.logger = lg;
  50.             this.audioSource = aS;
  51.             this.sampleRate = AudioSettings.outputSampleRate;
  52.             switch (AudioSettings.speakerMode) {
  53.                 case AudioSpeakerMode.Mono: this.channels = 1; break;
  54.                 case AudioSpeakerMode.Stereo: this.channels = 2; break;
  55.                 default:
  56.                     this.Error = string.Concat("Only Mono and Stereo project speaker mode supported. Current mode is ", AudioSettings.speakerMode);
  57.                     this.logger.LogError("AudioSourceInputFactory: {0}", this.Error);
  58.                     return;
  59.             }
  60.             if (!this.audioSource.enabled) {
  61.                 this.logger.LogWarning("AudioSourceInputFactory: AudioSource component disabled, enabling it.");
  62.                 this.audioSource.enabled = true;
  63.             }
  64.             if (!this.audioSource.gameObject.activeSelf) {
  65.                 this.logger.LogWarning("AudioSourceInputFactory: AudioSource GameObject inactive, activating it.");
  66.                 this.audioSource.gameObject.SetActive(true);
  67.             }
  68.             if (!this.audioSource.gameObject.activeInHierarchy) {
  69.                 this.Error = "AudioSource GameObject is not active in hierarchy, audio input can't work.";
  70.                 this.logger.LogError("AudioSourceInputFactory: {0}", this.Error);
  71.                 return;
  72.             }
  73.             this.audioOutCapture = this.audioSource.gameObject.GetComponent<AudioOutCapture>();
  74.             if (ReferenceEquals(null, this.audioOutCapture) || !this.audioOutCapture) {
  75.                 this.audioOutCapture = this.audioSource.gameObject.AddComponent<AudioOutCapture>();
  76.             }
  77.             if (!this.audioOutCapture.enabled) {
  78.                 this.logger.LogWarning("AudioSourceInputFactory: AudioOutCapture component disabled, enabling it.");
  79.                 this.audioOutCapture.enabled = true;
  80.             }
  81.         } catch (Exception e) {
  82.             this.Error = e.ToString();
  83.             if (this.Error == null) // should never happen but since Error used as validity flag, make sure that it's not null
  84.             {
  85.                 this.Error = "Exception in MicWrapperPusher constructor";
  86.             }
  87.             this.logger.LogError("AudioSourceInputFactory: {0}", this.Error);
  88.         }
  89.     }
  90.  
  91.     private float[] frame2 = Array.Empty<float>();
  92.  
  93.     private void AudioOutCaptureOnOnAudioFrame(float[] frame, int channelsNumber) {
  94.         if (channelsNumber != this.Channels) {
  95.             this.logger.LogWarning("AudioSourceInputFactory: channels number mismatch; expected:{0} got:{1}.", this.Channels, channelsNumber);
  96.         }
  97.         if (this.frame2.Length != frame.Length) {
  98.             this.frame2 = new float[frame.Length];
  99.         }
  100.         Array.Copy(frame, this.frame2, frame.Length);
  101.         this.pushCallback(frame);
  102.         Array.Clear(frame, 0, frame.Length);
  103.     }
  104.  
  105.     private Action<float[]> pushCallback;
  106.  
  107.     public void SetCallback(Action<float[]> callback, ObjectFactory<float[], int> bufferFactory) {
  108.         this.pushCallback = callback;
  109.         this.audioOutCapture.OnAudioFrame += this.AudioOutCaptureOnOnAudioFrame;
  110.     }
  111.  
  112.     public void Dispose() {
  113.         if (this.pushCallback != null && this.audioOutCapture != null) {
  114.             this.audioOutCapture.OnAudioFrame -= this.AudioOutCaptureOnOnAudioFrame;
  115.         }
  116.     }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement