Server-side microphone capture in ASP.NET/ C#

Apr 10, 2012 at 5:48 PM

Hi NAudio-Users.

I'm trying to access a server-side microphone in ASP.NET / C#.

What I am planning to do:

Either with loading the page, or with clicking on a button, a C#-function becomes active. This gets a microphone-stream, and plays it back as an output stream. So if you go to the page and click the button, you hear what I talk into the microphone.

So that's the theory. Atm it does not work. Before I go on, I want to mention it is NOT about audio chat. Just about my microphone input being played live on the homepage. I perfectly know you use Flash or Silverlight for client-side microphone capture.

Sound output works. I can play from a .wav-file easily, on click on the homepage. The microphone is recognized. I used Response.Write to display the microphone's device number, the product name and so on on the homepage. So the server knows the micro. No exceptions are thrown. There is no sound, but no bugs found while debugging as well.

So I suppose there is a bug in my code. Just to mention: The device-number, here just put as 0, is correct. I tested it with other applications. The code is extremely simple and I used the NAudio-Tutorial's code from YouTube for loopback as an example, cause I'm just trying to figure out whether it works.

Here is the code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



using NAudio.Wave;

public partial class _Default : System.Web.UI.Page
{

    private NAudio.Wave.WaveIn sourceStream = null;
    private NAudio.Wave.DirectSoundOut waveOut = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Seite aufgerufen";

    }//protected void Page_Load(object sender, EventArgs e)

    protected void show(object sender, EventArgs e)
    {
        
    }

    protected void start(object sender, EventArgs e)
    {
        Label1.Text = "Aufnahme gestartet";

        int deviceNumber = 0;

        sourceStream = new NAudio.Wave.WaveIn();
        sourceStream.DeviceNumber = deviceNumber;

        sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);
        NAudio.Wave.WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(sourceStream);

        waveOut = new NAudio.Wave.DirectSoundOut();
        waveOut.Init(waveIn);

        sourceStream.StartRecording();

        waveOut.Play();  

    }//protected void Page_Load(object sender, EventArgs e)

    protected void stop(object sender, EventArgs e)
    {
        Label1.Text = "Aufnahme beendet";
        if (waveOut != null)
        {
            waveOut.Stop();
            waveOut.Dispose();
            waveOut = null;
        }//if (waveOut != null)
        if (sourceStream != null)
        {
            sourceStream.StopRecording();
            sourceStream.Dispose();
            sourceStream = null;
        }//if (sourceStream != null)
    }//protected void Page_Load(object sender, EventArgs e)



}//public partial class _Default : System.Web.UI.Page

 

And the code to display the page:

<%@ Page Title="Startseite" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Willkommen bei ASP.NET.
    </h2>
    <p>
        Weitere Informationen zu ASP.NET finden Sie auf <a href="http://www.asp.net" title="ASP.NET-Website">www.asp.net</a>.
    </p>
    <p>
        <a href="http://go.microsoft.com/fwlink/?LinkID=152368"
            title="MSDN-ASP.NET-Dokumente">Dokumentation finden Sie auch unter ASP.NET bei MSDN</a>.
     <p>
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br />
     <asp:Button ID="Button1" runat="server" Text="Start" OnClick = "start" /><br />
     <asp:Button ID="Button2" runat="server" Text="Start" OnClick = "stop" /><br />

     </p>
</asp:Content>

Best regards/ Thank you

Coordinator
Apr 26, 2012 at 4:05 PM

first of all, you are playing audio on the server, not on the clients. NAudio cannot be used to play audio in the browser of the people visiting your page.

Secondly, you can't feed WaveOut from WaveIn directly. You need to use a BufferedWaveProvider.

Mark