Skip to content

Instantly share code, notes, and snippets.

@martindevans
Last active August 21, 2019 15:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save martindevans/2ab034c885cf0c038db8fda471336596 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using NAudio.Wave;
using UnityEngine;
namespace Dissonance.Audio.Capture
{
public class BasicFileStreamingCapture
: MonoBehaviour, IMicrophoneCapture
{
public bool IsRecording { get; private set; }
public TimeSpan Latency { get; private set; }
private readonly List<IMicrophoneSubscriber> _subscribers = new List<IMicrophoneSubscriber>();
private WaveFormat _format = new WaveFormat(48000, 1);
private readonly float[] _silence = new float[960];
private float _elapsedTime;
public WaveFormat StartCapture(string name)
{
IsRecording = true;
Latency = TimeSpan.FromMilliseconds(0);
return _format;
}
public void StopCapture()
{
IsRecording = false;
}
public void Subscribe(IMicrophoneSubscriber listener)
{
_subscribers.Add(listener);
}
public bool Unsubscribe(IMicrophoneSubscriber listener)
{
return _subscribers.Remove(listener);
}
public bool UpdateSubscribers()
{
_elapsedTime += Time.unscaledDeltaTime;
while (_elapsedTime > 0.02f)
{
_elapsedTime -= 0.02f;
foreach (var subscriber in _subscribers)
{
subscriber.ReceiveMicrophoneData(new ArraySegment<float>(_silence), _format);
}
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment