Tanoda
AudioInput.cs
Go to the documentation of this file.
1/*
2* NatCorder
3* Copyright (c) 2020 Yusuf Olokoba.
4*/
5
6using System;
8using UnityEngine;
9using Object = UnityEngine.Object;
10
12{
16 public sealed class AudioInput : IDisposable
17 {
18 #region --Client API--
19
26 public AudioInput(IMediaRecorder recorder, IClock clock, AudioListener audioListener) : this(recorder, clock,
27 audioListener.gameObject)
28 {
29 }
30
38 public AudioInput(IMediaRecorder recorder, IClock clock, AudioSource audioSource, bool mute = false) : this(
39 recorder, clock, audioSource.gameObject, mute)
40 {
41 }
42
46 public void Dispose()
47 {
48 Object.Destroy(attachment);
49 }
50
51 #endregion
52
53
54 #region --Operations--
55
56 private readonly IMediaRecorder recorder;
57 private readonly IClock clock;
58 private readonly AudioInputAttachment attachment;
59 private readonly bool mute;
60
61 private AudioInput(IMediaRecorder recorder, IClock clock, GameObject gameObject, bool mute = false)
62 {
63 this.recorder = recorder;
64 this.clock = clock;
65 attachment = gameObject.AddComponent<AudioInputAttachment>();
66 attachment.sampleBufferDelegate = OnSampleBuffer;
67 this.mute = mute;
68 }
69
70 private void OnSampleBuffer(float[] data)
71 {
72 AndroidJNI.AttachCurrentThread();
73 recorder.CommitSamples(data, clock.timestamp);
74 if (mute)
75 Array.Clear(data, 0, data.Length);
76 }
77
78 private class AudioInputAttachment : MonoBehaviour
79 {
80 public Action<float[]> sampleBufferDelegate;
81
82 private void OnAudioFilterRead(float[] data, int channels)
83 {
84 sampleBufferDelegate?.Invoke(data);
85 }
86 }
87
88 #endregion
89 }
90}
Recorder input for recording audio frames from an AudioListener or AudioSource.
Definition: AudioInput.cs:17
AudioInput(IMediaRecorder recorder, IClock clock, AudioListener audioListener)
Create an audio recording input from a scene's AudioListener.
Definition: AudioInput.cs:26
AudioInput(IMediaRecorder recorder, IClock clock, AudioSource audioSource, bool mute=false)
Create an audio recording input from an AudioSource.
Definition: AudioInput.cs:38
void Dispose()
Stop recorder input and release resources.
Definition: AudioInput.cs:46
Clock for generating recording timestamps. Clocks are important for synchronizing audio and video tra...
Definition: IClock.cs:14
A recorder capable of recording video frames, and optionally audio frames, to a media output....
UnityEngine.Object Object