2026-02-22 14:21:56 +01:00

76 lines
2.5 KiB
C#

#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
using System;
using System.Threading;
using System.Runtime.InteropServices;
namespace Photon.Voice.MacOS
{
public class AudioInReader : IAudioReader<float>
{
const string lib_name = "AudioIn";
[DllImport(lib_name)]
private static extern IntPtr Photon_Audio_In_CreateReader(int deviceID);
[DllImport(lib_name)]
private static extern void Photon_Audio_In_Destroy(IntPtr handler);
[DllImport(lib_name)]
private static extern bool Photon_Audio_In_Read(IntPtr handle, float[] buf, int len);
IntPtr audioIn;
public AudioInReader(int deviceID, ILogger logger)
{
// initialization in a separate thread to avoid 0.5 - 1 sec. pauses in main thread execution
var t = new Thread(() =>
{
lock (this)
{
try
{
audioIn = Photon_Audio_In_CreateReader(deviceID);
}
catch (Exception e)
{
Error = e.ToString();
if (Error == null) // should never happen but since Error used as validity flag, make sure that it's not null
{
Error = "Exception in AudioInReader constructor";
}
logger.Log(LogLevel.Error, "[PV] AudioInReader: " + Error);
}
}
});
Util.SetThreadName(t, "[PV] MacOSAudioInReaderCtr");
t.Start();
}
public int Channels { get { return 1; } }
public int SamplingRate { get { return 44100; } }
public string Error { get; private set; }
public void Dispose()
{
// disopse in a separate thread to avoid pauses in main thread execution
var t = new Thread(() =>
{
lock (this)
{
if (audioIn != IntPtr.Zero)
{
Photon_Audio_In_Destroy(audioIn);
audioIn = IntPtr.Zero;
}
}
});
Util.SetThreadName(t, "[PV] MacOSAudioInReaderDisp");
t.Start();
}
public bool Read(float[] buf)
{
return audioIn != IntPtr.Zero && Photon_Audio_In_Read(audioIn, buf, buf.Length);
}
}
}
#endif