Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.