Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real-time processing left and right channel #485

Open
AaronSmithCoding opened this issue Feb 8, 2024 · 2 comments
Open

Real-time processing left and right channel #485

AaronSmithCoding opened this issue Feb 8, 2024 · 2 comments

Comments

@AaronSmithCoding
Copy link

Hey I was wondering if I want to listen to a specific sound card audio output (my running game)
The goal is to the process that audio and work out the amplitude of each channel

is this something that can be done with this library?

@markusschnepf
Copy link

i tried the same thing... a friend is deaf on one ear and i try to make a software that displays a marker when the left channel is louder than the right (sound coming from left) but the loopback recording does not work correctly.

i created a mp3/wav file that only plays a static sound on the left channel (made in audacity) but the code still gives me nearly identical values for the right and left volume/RMS:

`public partial class Form1 : Form
{
private static float treshold = 0;

private static Form1 _instance;
private WasapiLoopbackCapture waveIn;
private BackgroundWorker audioWorker;
private System.Timers.Timer timer;
private double leftRMS, rightRMS;

public static Form1 Instance
{
    get
    {
        if (_instance == null)
        {
            _instance = new Form1();
        }
        return _instance;
    }
}

public Form1()
{
    InitializeComponent();
    _instance = this;

}

private void OnDataAvailable(object sender, DataAvailableEventArgs e)
{
    if (!audioWorker.IsBusy)
    {
        audioWorker.RunWorkerAsync(e);
    }
}

private void AudioWorker_DoWork(object sender, DoWorkEventArgs e)
{
    DataAvailableEventArgs waveInEventArgs = (DataAvailableEventArgs)e.Argument;

    int bytesPerSample = 4; // 2 channels, 16 bits (2 bytes) per sample
    int bytesPerFrame = bytesPerSample * 2; // 2 channels

    double leftSumSquares = 0;
    double rightSumSquares = 0;
    int sampleCount = waveInEventArgs.ByteCount / bytesPerFrame;

    for (int index = 0; index < waveInEventArgs.ByteCount; index += bytesPerFrame)
    {
        short leftSample = BitConverter.ToInt16(waveInEventArgs.Data, index);
        short rightSample = BitConverter.ToInt16(waveInEventArgs.Data, index + bytesPerSample);

        leftSumSquares += leftSample * leftSample;
        rightSumSquares += rightSample * rightSample;
    }

    double leftRMS = Math.Sqrt(leftSumSquares / sampleCount);
    double rightRMS = Math.Sqrt(rightSumSquares / sampleCount);

    e.Result = new { LeftVolume = leftRMS / short.MaxValue, RightVolume = rightRMS / short.MaxValue };
}

private void AudioWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    dynamic result = e.Result;
    double leftVolume = result.LeftVolume;
    double rightVolume = result.RightVolume;

    Debug.WriteLine($"Left Volume: {leftVolume}, Right Volume: {rightVolume}");

    // Update labels based on channel volumes using BeginInvoke
    BeginInvoke((MethodInvoker)delegate
    {
        lblLeft.Enabled = leftVolume > rightVolume;
        lblRight.Enabled = rightVolume > leftVolume;
    });
}

private void button3_Click(object sender, EventArgs e)
{
    // Initialize audio capture
    waveIn = new WasapiLoopbackCapture();
    waveIn.DataAvailable += OnDataAvailable;
    waveIn.Initialize();
    waveIn.Start();

    // Initialize BackgroundWorker
    audioWorker = new BackgroundWorker();
    audioWorker.DoWork += AudioWorker_DoWork;
    audioWorker.RunWorkerCompleted += AudioWorker_RunWorkerCompleted;
}
}`

@markusschnepf
Copy link

since there is no proper documentation, i'll replace this libary with another one.
unfortunately, "NAudio" does not offer loopbackRecording :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants