I’m trying to send my soundcard audio to a HTTP stream with NAaudio so any browser on the LAN can open this one. Like an LAN Radiostation or something and use the default audio player from the browser. My sound is recorded with bytes, send, but the browser don’t let me play the stream.
public bool live = false;
public MMDeviceEnumerator MDE = new();
public MMDevice device_out;
public MMDevice device_in;
public WaveIn WaveIn = new();
public WaveOut WaveOut = new();
public BufferedWaveProvider BWP;
public string Port = "8572";
public HttpListener server;
public string ipAddress = "";
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MMDeviceEnumerator Audio_out = new();
MMDevice Devices_out = Audio_out.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
device_out = Devices_out;
BWP = new(new WaveFormat(44100, 16, 2));
VolumeWaveProvider16 volumeProvider = new(BWP);
WaveOut.Init(volumeProvider);
WaveOut.Play();
WaveIn = new WaveIn
{
WaveFormat = new WaveFormat(44100, 1)
};
MMDeviceEnumerator Audio_in = new();
MMDevice devices_in = MDE.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
device_in = devices_in;
WaveIn.StartRecording();
WaveIn.DataAvailable += AudioInput_DataAvailable;
}
private void AudioInput_DataAvailable(object? sender, WaveInEventArgs e)
{
float max = 0;
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
var sample32 = sample / 32768f;
if (sample32 < 0) sample32 = -sample32;
if (sample32 > max) max = sample32;
}
BWP.AddSamples(e.Buffer, 0, e.BytesRecorded);
}
public void StartServer()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString();
}
}
server = new();
server.Prefixes.Add("http://*:8572/");
server.Start();
clientTasks = new ConcurrentDictionary<string, Task>();
Task.Run(() => HandleRequests());
T_IP.Text = "http://" + ipAddress.ToString() + ":8572/stream";
}
private async Task HandleRequests()
{
while (server.IsListening)
{
HttpListenerContext context = await server.GetContextAsync();
var task = Task.Run(() => ProcessRequest(context));
clientTasks.TryAdd(context.Request.RemoteEndPoint.ToString(), task);
}
}
private async Task ProcessRequest(HttpListenerContext context)
{
HttpListenerResponse response = context.Response;
response.ContentType = "audio/wav";
response.StatusCode = 206;
response.StatusDescription = "OK";
response.ProtocolVersion = new Version("1.0");
byte[] buffer = new byte[BWP.BufferLength];
while (context.Response.OutputStream.CanWrite)
{
int bytesRead = BWP.Read(buffer, 0, buffer.Length);
await response.OutputStream.WriteAsync(buffer, 0, bytesRead);
context.Response.Headers.Add("Content-Range", $"bytes 0-{buffer.Length - 1}/{buffer.Length}");
await response.OutputStream.FlushAsync();
}
response.OutputStream.Close();
clientTasks.TryRemove(context.Request.RemoteEndPoint.ToString(), out _);
}
public void StartServer()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString(); // Lable in WPF Window
}
}
server = new();
server.Prefixes.Add("http://*:8572/");
server.Start();
clientTasks = new ConcurrentDictionary<string, Task>();
Task.Run(() => HandleRequests());
}
public void StopServer()
{
live = false;
server.Stop();
}
This is what I see after starting the server and navigate to local IP. Is there something wrong with sending it?