Search Unity

Question Dissonance Noob wants to capture local microphone in Unity

Discussion in 'Audio & Video' started by shaldar, Nov 22, 2021.

  1. shaldar

    shaldar

    Joined:
    Dec 21, 2016
    Posts:
    9
    Hi, I am a Unity developer new to Dissonance and I'm getting familiar with workflow. I'm using Dissonance over PUN2.
    I want to capture the local microphone post dissonance sampling and feed it to a (local) audio-visualizer.
    I am looking for the most performant solution since audio sampling is quite CPU intensive ( I guess).
    I am considering the following solutions:
    1)
    - Use 2 broadcast triggers - one to the channel type "room" and the other to channel type "self".
    - Use "self" trigger and try to got the VoicePlayerState of the local player by attaching it as derived object from IDissonancePlayer
    - and to use that as a feed to my audioVisualizer animations

    2)
    - Simply have my own custom capture by modifying BasicMicrophoneCapture.cs to capture clip feed for my Audio Visualizer before DrainMicSamples() is called.
    - Attach that component to DissonanceComm

    3) Any third method that I'm missing.

    4) Ignore Dissonance components altogether and capture directly from the local Mic and feed it directly to the Visualizer. Will that affect Dissonance performance in any way?

    I suspect the 2nd method will be more performant but my chances of breaking something else shoots up.
    What do you suggest?
    Any sample code or scenes/inspector screenshots for hooking an audio visualizer to the local microphone post dissonance sampling will be very helpful.
     
    Nyarlathothep likes this.
  2. Nyarlathothep

    Nyarlathothep

    Joined:
    Jun 13, 2013
    Posts:
    398
    Dissonance has a built in system for accessing the raw audio stream (after preprocessing). So it's option #3 ;)

    Basically you just implement IMicrophoneSubscriber on a class, and then pass an instance of that class to DissonanceComms.SubscribeToRecordedAudio, Dissonance will then call the ReceiveMicrophoneData every time there is a new audio frame available (the frame size is configured in VoiceSettings).

    Only one thing can be accessing the mic at a time, so definitely don't do that!
     
    shaldar likes this.
  3. shaldar

    shaldar

    Joined:
    Dec 21, 2016
    Posts:
    9
    Thanks for the detailed response!
    I implemented the interface and attached it to DissonanceComms. I also wrote another piece of code that subscribed this instance calling
    GetComponent<DissonanceComms>().SubscribeToRecordedAudio(GetComponent<DissonanceAudioRecorder>());
    However, when I ran it I see that _readyForProcessing float array is always empty :(

    On debugging I find that IMicrophoneSubscriber.ReceiveMicrophoneData(.., ..) is never invoked, even though microphone is working.
    I debugged to see if subscription went through OK. I didn't see any issue other than _preprocessor being null in CapturePipelineManager.Subscribe(IMicrophoneSubscriber listener).

    Attached is my inspector setup in play mode. How can I get my _readyForProcessing populated with Mic audio data?


    upload_2021-11-24_21-28-5.png
     
  4. Nyarlathothep

    Nyarlathothep

    Joined:
    Jun 13, 2013
    Posts:
    398
    Are you in an active Dissonance session? The audio capture system doesn't run until a connection has been established.