Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Capturing Sound Card Audio with CSCore

Discussion in 'Scripting' started by GamesOnAcid, Jul 10, 2017.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    280
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using CSCore;
    4. using CSCore.SoundIn;
    5. using CSCore.Codecs.WAV;
    6.  
    7. public class SoundCapture : MonoBehaviour {
    8.     public WasapiCapture capture = new WasapiLoopbackCapture();
    9.     public WaveWriter w;
    10.    
    11.     void Start() {
    12.         w = new WaveWriter("dump.wav", capture.WaveFormat);
    13.     }
    14.  
    15.     void Update() {
    16.         capture.Initialize();
    17.         capture.DataAvailable += (s, e) => {
    18.                 w.Write(e.Data, e.Offset, e.ByteCount);
    19.             };
    20.         capture.Start();
    21.         Console.ReadKey();
    22.         capture.Stop();
    23.     }
    24. }
    25.  
    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.
     
  2. cspid

    cspid

    Joined:
    Apr 25, 2014
    Posts:
    30
    Any luck with this? Struggling with the same thing.
     
  3. morfaine

    morfaine

    Joined:
    Oct 1, 2015
    Posts:
    9