Capturing Sound Card Audio with CSCore

So, I’m about ready to call it and give up right here. I’m trying to make a sound visualizer, but in order to do that I need to capture the feed coming from the sound card, so whatever audio is being pushed to the speakers I can capture and visualize. Unfortunately, I don’t know how to do that even remotely. I found a couple things (namely CSCore and NAudio) and I am currently running experiments with CSCore. My current capture script looks like this:

using UnityEngine;
using System;
using CSCore;
using CSCore.SoundIn;
using CSCore.Codecs.WAV;

public class SoundCapture : MonoBehaviour {
    public WasapiCapture capture = new WasapiLoopbackCapture();
    public WaveWriter w;
   
    void Start() {
        w = new WaveWriter("dump.wav", capture.WaveFormat);
    }

    void Update() {
        capture.Initialize();
        capture.DataAvailable += (s, e) => {
                w.Write(e.Data, e.Offset, e.ByteCount);
            };
        capture.Start();
        Console.ReadKey();
        capture.Stop();
    }
}

but that returns a bunch of errors (like a NullReferenceException and NotImplementedException) so I don’t know what to do anymore. If you guys have any suggestions on changes I can make to the code, or other libraries I can use to better understand capturing sound from the sound card, let me know. Thank you.

Any luck with this? Struggling with the same thing.

This might help if anyone stumbles across this post:

https://www.reddit.com/r/Unity3D/comments/4xoshl/i_made_a_simple_visualization_example_using_the/

3 Likes