Hey there,
I recently posted a question and kept digging further. I now got a mic working with FMOD in Unity, and am able to hear myself. There is some weird stuff happening still though.
When I talk thru the microphone, it sounds very distorted. Another odd thing that happens is the recording looping back. So after a little while, I hear the recording of before basically.
Feel like I’m getting closer to my endpoint, but currently stuck on the mic sounding distorted and the looping.
A complete different question, is there a way to tell if a microphone is picking up sounds? Like is there a possibility to just add a boolean that reacts to input to the microphone? I know it’s a possibility with just playing back sounds, I did it with the getPlaybackState in another script that reads data from an audio clip.
Hope to hear from you guys,
Niels
using UnityEngine;
using System;
using System.Text;
using System.Runtime.InteropServices;
public class MicSound : MonoBehaviour
{
public bool m_isrecording;
public FMOD.Sound m_Sound;
public string mic;
public int systemRate, speakerModeChannels;
public FMOD.SPEAKERMODE speakerMode = FMOD.SPEAKERMODE.RAW;
public FMOD.DRIVER_STATE driverState = FMOD.DRIVER_STATE.DEFAULT;
public int numDriv, numCon;
FMOD.RESULT m_Result;
FMOD.MODE soundMode = FMOD.MODE.OPENUSER | FMOD.MODE.LOOP_NORMAL;
FMOD.CREATESOUNDEXINFO exinfo;
public byte[] m_Bytes;
public FMOD.ChannelGroup channelGroup;
public FMOD.Channel channel;
void Start()
{
FMODUnity.RuntimeManager.CoreSystem.getRecordDriverInfo(0, out mic, 1, out Guid guid, out systemRate, out speakerMode, out speakerModeChannels, out driverState);
FMODUnity.RuntimeManager.CoreSystem.getRecordNumDrivers(out numDriv, out numCon);
exinfo.cbsize = Marshal.SizeOf(exinfo);
exinfo.numchannels = speakerModeChannels;
exinfo.format = FMOD.SOUND_FORMAT.PCM16;
exinfo.defaultfrequency = systemRate;
exinfo.length = (uint)(exinfo.defaultfrequency * exinfo.numchannels * sizeof(short)) * 8;
byte[] m_Bytes = new byte[exinfo.length + 1];
m_Result = FMODUnity.RuntimeManager.CoreSystem.createSound(m_Bytes, soundMode, ref exinfo, out m_Sound);
if (m_Result != FMOD.RESULT.OK)
{
UnityEngine.Debug.Log("failed to createSound: " + m_Result);
return;
}
m_Result = FMODUnity.RuntimeManager.CoreSystem.recordStart(0, m_Sound, false);
if (m_Result != FMOD.RESULT.OK)
{
UnityEngine.Debug.Log("failed to startrecord: " + m_Result);
}
else
{
m_isrecording = true;
UnityEngine.Debug.Log("Recording " + m_Result);
}
//test play
FMODUnity.RuntimeManager.CoreSystem.playSound(m_Sound, channelGroup, false, out channel);
}