Hi,
I am trying to get data of 4 microphones simultaneous through an audio interface having 4 channels.
I want to retrieve float data like the Unity AudioCLip.GetData () method.
I have made the code below and tried to save data in a csv but the results seem strange.
private const int maxChannels = 4;
private FMOD.System fmodSystem;
private FMOD.RESULT fmodResult;
private DriverData driverData = new DriverData();
private List<FMOD.Channel> fmodChannels = new List<FMOD.Channel>();
public class DriverData
{
public string driverName;
public int driverNameLentgh = 256;
public System.Guid guid;
public int systemRate;
public FMOD.SPEAKERMODE speakerMode;
public int speakerModeChannels;
public FMOD.DRIVER_STATE driverState;
}
private void Start()
{
InitInput();
}
private void InitInput()
{
fmodResult = FMOD.Factory.System_Create(out fmodSystem);
ResultCheck(fmodResult);
fmodResult = fmodSystem.init(maxChannels, FMOD.INITFLAGS.NORMAL, System.IntPtr.Zero);
ResultCheck(fmodResult);
}
public List<string> GetInputs()
{
List<string> devices = new List<string>();
int numSoundSources;
int numSoundSourcesConnected;
fmodResult = fmodSystem.getRecordNumDrivers(out numSoundSources, out numSoundSourcesConnected);
ResultCheck(fmodResult);
for (int i = 0; i < numSoundSources; i++)
{
fmodResult = fmodSystem.getRecordDriverInfo(
i,
out driverData.driverName,
driverData.driverNameLentgh,
out driverData.guid,
out driverData.systemRate,
out driverData.speakerMode,
out driverData.speakerModeChannels,
out driverData.driverState
);
ResultCheck(fmodResult);
devices.Add(driverData.driverName);
}
return devices;
}
public void SelectInput(string driverName)
{
int numSoundSources;
int numSoundSourcesConnected;
fmodResult = fmodSystem.getRecordNumDrivers(out numSoundSources, out numSoundSourcesConnected);
ResultCheck(fmodResult);
for (int i = 0; i < numSoundSources; i++)
{
fmodResult = fmodSystem.getRecordDriverInfo(
i,
out driverData.driverName,
driverData.driverNameLentgh,
out driverData.guid,
out driverData.systemRate,
out driverData.speakerMode,
out driverData.speakerModeChannels,
out driverData.driverState
);
ResultCheck(fmodResult);
if (driverData.driverName == driverName)
{
Debug.Log(
"NAME: <color=blue>" + driverData.driverName + "</color>" +
" | RATE: <color=blue>" + driverData.systemRate + "</color>" +
" | MODE: <color=blue>" + driverData.speakerMode + "</color>" +
" | CHANNELS: <color=blue>" + driverData.speakerModeChannels + "</color>" +
" | STATE: <color=blue>" + driverData.driverState + "</color>"
);
fmodSystem.setDriver(i);
break;
}
}
MicrophonesListener();
}
private void ResultCheck(FMOD.RESULT result)
{
if (result != FMOD.RESULT.OK)
Debug.LogError(result);
}
private void MicrophonesListener()
{
FMOD.ChannelGroup fmodChannelGroup = new FMOD.ChannelGroup();
for (int i = 0; i < driverData.speakerModeChannels; i++)
{
FMOD.Sound fmodSound;
FMOD.Channel fmodChannel;
FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
exinfo.cbsize = Marshal.SizeOf(typeof(FMOD.CREATESOUNDEXINFO));
exinfo.numchannels = driverData.speakerModeChannels;
exinfo.format = FMOD.SOUND_FORMAT.PCM16;
exinfo.defaultfrequency = driverData.systemRate;
exinfo.length = (uint)(exinfo.defaultfrequency * exinfo.numchannels * sizeof(ushort) * 0.5);
exinfo.channelorder = FMOD.CHANNELORDER.ALLMONO;
fmodResult = fmodSystem.createSound("Microphone_"+i, FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER, ref exinfo, out fmodSound);
ResultCheck(fmodResult);
fmodResult = fmodSystem.recordStart(i, fmodSound, true);
ResultCheck(fmodResult);
// Is it necessary to wait before playing sound?
fmodResult = fmodSystem.playSound(fmodSound, fmodChannelGroup, false, out fmodChannel);
fmodChannel.setVolume(0);
ResultCheck(fmodResult);
fmodChannels.Add(fmodChannel);
}
}
- Are microphones properly initialized?
- How can I correctly save the data of microphones in a csv file? (I did not put the code of the recording because it is very messy. I do not think to recover all the samples and sometimes I get NaN values.)
Many thanks for your reply.