public static class Audio
{
public static WaveIn waveIn = new WaveIn();
private static BufferedWaveProvider player;
private static WaveFormat wf = new WaveFormat(16000, 1);
public static void AddSamples(byte[] bytes)
{
player.AddSamples(bytes, 0, bytes.Length);
}
private static void OnDataAvailable(object? sender, WaveInEventArgs args)
{
float max = 0;
for (int index = 0; index < args.BytesRecorded; index += 2)
{
short sample = (short)((args.Buffer[index + 1] << 8) |
args.Buffer[index + 0]);
var sample32 = sample / 32768f;
if (sample32 < 0) sample32 = -sample32;
if (sample32 > max) max = sample32;
}
if (100 * max > 10)
{
Network.client.Send(new Packet() { VCAudioBuffer = args.Buffer, VCPacketDataIdentifier = PacketIdentifier.AudioStream, VCSessionKey = Network.KEY }.GetPacketDataStream());
}
}
public static void Init()
{
waveIn.BufferMilliseconds = 50;
waveIn.DeviceNumber = 0;
waveIn.WaveFormat = wf;
waveIn.DataAvailable += OnDataAvailable;
waveIn.StartRecording();
var wo = new WaveOutEvent();
BufferedWaveProvider pro = new BufferedWaveProvider(wf);
pro.DiscardOnBufferOverflow = true;
wo.Init(pro);
wo.Play();
player = pro;
}
}
public static class Audio
{
public static WaveIn waveIn = new WaveIn();
private static BufferedWaveProvider player;
private static WaveFormat wf = new WaveFormat(16000, 1);
public static void AddSamples(byte[] bytes)
{
player.AddSamples(bytes, 0, bytes.Length);
}
private static void OnDataAvailable(object? sender, WaveInEventArgs args)
{
float max = 0;
for (int index = 0; index < args.BytesRecorded; index += 2)
{
short sample = (short)((args.Buffer[index + 1] << 8) |
args.Buffer[index + 0]);
var sample32 = sample / 32768f;
if (sample32 < 0) sample32 = -sample32;
if (sample32 > max) max = sample32;
}
if (100 * max > 10)
{
Network.client.Send(new Packet() { VCAudioBuffer = args.Buffer, VCPacketDataIdentifier = PacketIdentifier.AudioStream, VCSessionKey = Network.KEY }.GetPacketDataStream());
}
}
public static void Init()
{
waveIn.BufferMilliseconds = 50;
waveIn.DeviceNumber = 0;
waveIn.WaveFormat = wf;
waveIn.DataAvailable += OnDataAvailable;
waveIn.StartRecording();
var wo = new WaveOutEvent();
BufferedWaveProvider pro = new BufferedWaveProvider(wf);
pro.DiscardOnBufferOverflow = true;
wo.Init(pro);
wo.Play();
player = pro;
}
}