Cabbage Logo
Back to Cabbage Site

Real Time Microphone Input With Unity Csound

Relatively new user with Cabbage,
For the last few days I have been trying to get Unity to accept inch input with the microphone in Csound for collision events, similar to the oscillator events when a sphere hits a cube, I’ve been struggling to get the C# code to work with Csound, I have logged the csound output and it registers the instrument with inch attached however there is no audio being output.

Any help on how to fix this would be greatly appreciated

Hi @Adzo94. You need to attach the microphone to the AudioSource. I don’t think you can do this through the GUI so you’ll need to add a script to the same GameObject the CsoundUnity script is attached to. Something like this does the trick for me:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

	// Use this for initialization
	void Start ()
    {
        foreach (var device in Microphone.devices)
        {
            Debug.Log("Name: " + device);
        }

        AudioSource audioSource = GetComponent<AudioSource>();
        audioSource.clip = Microphone.Start("Microphone Array (Realtek High Definition Audio)", true, 10, 44100);
        audioSource.Play();
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}

My Csound file then looks like this:

<Cabbage>
form caption("Test"), size(300, 200)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -m0d
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 64
nchnls = 2
0dbfs = 1

instr TEST
a1 inch 1
outs a1, a1
endin

</CsInstruments>
<CsScore>
i"TEST" 0 z
</CsScore>
</CsoundSynthesizer>

Hey Rory,

I have the script attached and made slight adjustments for my own device, however I am curious if you are meant to call on the csound file within the unity script.

I am getting the following error:

ArgumentException: Exception of type ‘System.ArgumentException’ was thrown.
Mic_Input_Tes.Start () (at Assets/Scripts/Mic_Input_Tes.cs:17)

I don’t follow? IN my simple example I have a GameObject, to which I’ve attached CsoundUnity component. I then add a script to the Cube (same as the one above). That script get the CsoundUnity component (which is an AudioSource) and sets the clip to be a microphone.

Can you share your script? The error is there on line 17 by the looks of things. It could be that Unity can’t open the mic. Try creating a new project, add a standard audio source and see if you can get the mic working there, outside of anything to do with Csound. If your mic access script works there, it should also work fine when dealing with CsoundUnity components.