Hi, i’m new to FMOD, I’m making a recording sound from mic and get the volume value of it.
Here the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FMODUnity;
public class RecordMic : MonoBehaviour
{
[Header("Choose a Microphone")]
public int RecordingDeviceIndex = 0;
[TextArea] public string RecordingDeviceName = null;
[Header("How Long In Seconds Before Recording Plays")]
public float Latency = 1f;
[Header("Choose A Key To Play/Pause/Add To Reverb To Recording")]
public KeyCode PlayAndPause;
public KeyCode ReverbOnOffSwitch;
[Header("Volume")]
public float volume;
// FMOD Objects
private FMOD.Sound sound;
private FMOD.CREATESOUNDEXINFO exinfo;
private FMOD.Channel channel;
private FMOD.Channel reverbChannel;
private FMOD.ChannelGroup channelGroup;
// Number of recording devices
private int numOfDriversConnected = 0;
private int numOfDrivers = 0;
// Device information
private System.Guid MicGUID;
private int SampleRate = 0;
private FMOD.SPEAKERMODE fmodSpeakerMode;
private int NumOfChannels = 0;
private FMOD.DRIVER_STATE driverState;
private FMOD.DSP playerDSP;
// Other Variables
private bool dspEnabled = false;
private bool playOrPause = true;
private bool playOkay = false;
void Start()
{
FMOD.RESULT result;
// Step 1: Check if any recording devices are available
result = RuntimeManager.CoreSystem.getRecordNumDrivers(out numOfDrivers, out numOfDriversConnected);
if (result != FMOD.RESULT.OK || numOfDriversConnected == 0)
{
Debug.LogError("No microphone connected or failed to get recording devices: " + result);
return;
}
Debug.Log("Microphones available: " + numOfDriversConnected);
// Step 2: Get information about the recording device
result = RuntimeManager.CoreSystem.getRecordDriverInfo(
RecordingDeviceIndex,
out RecordingDeviceName,
50,
out MicGUID,
out SampleRate,
out fmodSpeakerMode,
out NumOfChannels,
out driverState);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to get recording device info: " + result);
return;
}
// Step 3: Store relevant information into FMOD.CREATESOUNDEXINFO
exinfo.cbsize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(FMOD.CREATESOUNDEXINFO));
exinfo.numchannels = NumOfChannels;
exinfo.format = FMOD.SOUND_FORMAT.PCM16;
exinfo.defaultfrequency = SampleRate;
exinfo.length = (uint)SampleRate * sizeof(short) * (uint)NumOfChannels;
// Step 4: Create an FMOD Sound object to hold the recording
result = RuntimeManager.CoreSystem.createSound(
exinfo.userdata,
FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER,
ref exinfo,
out sound);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to create sound: " + result);
return;
}
// Step 5: Start recording into the Sound object
result = RuntimeManager.CoreSystem.recordStart(RecordingDeviceIndex, sound, true);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to start recording: " + result);
return;
}
// Step 6: Start a coroutine to play the sound after a delay
StartCoroutine(Wait());
}
IEnumerator Wait()
{
yield return new WaitForSeconds(0.002f);
FMOD.RESULT result;
// Create channel group
result = RuntimeManager.CoreSystem.createChannelGroup("Recording", out channelGroup);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to create channel group: " + result);
yield break;
}
// Play the sound
result = RuntimeManager.CoreSystem.playSound(sound, channelGroup, true, out channel);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to play sound: " + result);
yield break;
}
playOkay = true;
Debug.Log("Ready to play");
}
void Update()
{
volume = GetLoudness();
// Toggle play/pause on key press
if (playOkay)
{
if (Input.GetKeyDown(PlayAndPause))
{
playOrPause = !playOrPause;
FMOD.RESULT result = channel.setPaused(playOrPause);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to pause/unpause channel: " + result);
}
}
}
// Toggle reverb on key press
if (Input.GetKeyDown(ReverbOnOffSwitch))
{
FMOD.REVERB_PROPERTIES propOn = FMOD.PRESET.BATHROOM();
FMOD.REVERB_PROPERTIES propOff = FMOD.PRESET.OFF();
dspEnabled = !dspEnabled;
FMOD.RESULT result = RuntimeManager.CoreSystem.setReverbProperties(1, ref dspEnabled ? ref propOn : ref propOff);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to set reverb properties: " + result);
}
}
}
float GetLoudness()
{
float rms = 0f;
FMOD.RESULT result = channel.getDSP(0, out playerDSP);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to get DSP: " + result);
return rms;
}
playerDSP.setMeteringEnabled(true, true);
FMOD.DSP_METERING_INFO playerLevel;
FMOD.DSP_METERING_INFO playerInput;
result = playerDSP.getMeteringInfo(out playerInput, out playerLevel);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Failed to get metering info: " + result);
return rms;
}
float playerOutput = playerLevel.peaklevel[0] + playerLevel.peaklevel[1];
rms = playerOutput * 3;
return rms;
}
}
Here is my question:
How can I get the volume the mic recorded without play back ?
How to reduce latency ?
Addition question:
Is it posible to make a bot listen to the sound from Fmod 3D?