Hi,
There isn’t much more to take into consideration other than what you have pointed out. Which method you end up using will solely depend on the functionality you want to give the user.
Scott’s video is a great resource to get started with, but there are some changes we will want to make ourselves.
• Includes required
using FMODUnity;
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.IO;
• Extra functions used
ERRCHECK()
/// <summary>
/// Use to debug FMOD functions with explanation of what the function was doing with an both success and failed messages
/// </summary>
/// <param name="result"></param>
/// <param name="failed"></param>
/// <param name="success"></param>
private void ERRCHEK(FMOD.RESULT result, string failed, string success)
{
#if UNITY_EDITOR
if (result.ToString().Length != 0)
{
if (result != FMOD.RESULT.OK)
Debug.Log(failed + " " + result);
else
Debug.Log(success + " " + result);
}
#endif
}
/// <summary>
/// Override of no success message is required
/// </summary>
/// <param name="result"></param>
/// <param name="failed"></param>
private void ERRCHEK(FMOD.RESULT result, string failed)
{
//Will only call debugs if in editor to save on calls in build
#if UNITY_EDITOR
if (result.ToString().Length != 0)
{
if (result != FMOD.RESULT.OK)
Debug.Log(failed + " " + result);
}
#endif
}
SetState()
/// <summary>
/// Sets the state of the recording
/// </summary>
/// <param name="currentState">Options: Recording, Stopped, Restart</param>
private void SetState(string currentState)
{
switch(currentState)
{
case "Recording":
{
recording = true;
stopped = false;
killed = false;
#if UNITY_EDITOR
Debug.Log("RECORDING");
#endif
break;
}
case "Stopped":
{
recording = false;
stopped = true;
killed = true;
soundActive = false;
#if UNITY_EDITOR
Debug.Log("STOPPED");
#endif
break;
}
case "Restart":
{
recording = false;
stopped = false;
killed = false;
soundActive = true;
#if UNITY_EDITOR
Debug.Log("RESTARTING");
#endif
break;
}
}
}
• You will need two systems Core System, one to record and one to write to a .wav
file
Creating Systems
//System Variables
private FMOD.System micRecordSystem, wavWriterSystem;
private string micRecordData = "MICTEST";
private string wavWiterData = "WAVWRITER";
//Creating systems to record and output mic input -------------------------------------------------------------
GCHandle micRecordDataPtr = GCHandle.Alloc(micRecordData);
GCHandle wavWriterDataPrt = GCHandle.Alloc(wavWiterData);
FMOD.RESULT result;
FMOD.OUTPUTTYPE currentOutput;
//Creating the mic system solely for recording mic input
result = FMOD.Factory.System_Create(out micRecordSystem);
ERRCHEK(result, "Failed to create mic sysytem");
//Initialize mic record system
resusystemcRecordSystem.init(100, FMOD.INITFLAGS.NORMAL, GCHandle.ToIntPtr(micRecordDataPtr));
ERRCHEK(result, "Mic system int failed");
//Checking current output for the system
result = micRecordSystem.getOutput(out currentOutput);
ERRCHEK(result, "Failed to check output", "Mic system current ouput " + currentOutput.ToString() + " with result");
FMOD.RESULT wavResult; //Just for this snip
//Creating the wav writer system
wavResult= FMOD.Factory.System_Create(out wavWriterSystem);
ERRCHEK(wavResult, "Failed to create wav system");
//Initialize the wav writer system
wavResult= wavWriterSystem.init(100, FMOD.INITFLAGS.NORMAL, GCHandle.ToIntPtr(wavWriterDataPrt));
ERRCHEK(wavResult, "Failed to init wav write system");
//Assigning output of Wav system to nosound, till we are ready to record sound
wavResult= wavWriterSystem.setOutput(FMOD.OUTPUTTYPE.NOSOUND);
wavWriterSystem.getOutput(out currentOutput);
ERRCHEK(wavResult, "Failed to change wav output", "Wav system current output " + currentOutput.ToString() + " with result ");
//-------------------------------------------------------------------------------------------------------------
• Because we are creating these systems ourselves, we are responsible for updating and releasing them
• Call in Update()
Update systems
//Updating both the created systems
if (!killed)
{
micRecordSystem.update();
wavWriterSystem.update();
}
Release()
//REMEMBER TO RELEASE SYSTEMS SO THERE ARENT MEMORY LEAKS, this will get called if you stop the Editor running or if the object is destroyed
private void OnDestroy()
{
micRecordSystem.release();
wavWriterSystem.release();
}
• We can follow Scott’s example of how to set up and retrieve the recordingDevice
from our new system
• When creating the new sound
don’t use the FMODUnity.RuntimeManger.CoreSystem
you will want to you’re your recordingSystem
• When beginning the recording, input will be recorded on the recordingSystem
, while the wavWriterSystem
will only be responsible for outputting the file
Start Recording
//START RECORDING----------------------------------------------------------------------------------------------
if (Input.GetKeyDown(KeyCode.R) && !recording && soundActive)
{
SetState("Recording");
FMOD.RESULT result;
result = micRecordSystem.recordStart(recordingDeviceIndex, sound, true);
ERRCHEK(result, "Failed to start recording");
//Playing the sound to the mic record system
result = micRecordSystem.playSound(sound, channelGroup, false, out channel);
ERRCHEK(result, "Failed to play sound on mic system");
//Playing the sound on the wav system
result = wavWriterSystem.playSound(sound, channelGroup, false, out wavChannel);
ERRCHEK(result, "Failed to play sound on wav system");
//Start wav system writing to file, make sure this only happens once
result = wavWriterSystem.setOutput(FMOD.OUTPUTTYPE.WAVWRITER);
ERRCHEK(result, "Wav system failed to start writing to file", "Wav system now writing to file");
}
//-------------------------------------------------------------------------------------------------------------
• When the recording is done, we will need to stop writing to the .wav
file so it can be used as a source for our Programmer Sound
.
Stop Recording
//STOP RECORDING-----------------------------------------------------------------------------------------------
if (Input.GetKeyDown(KeyCode.S) && !stopped && recording)
{
SetState("Stopped");
//Stopping the recording
FMOD.RESULT result = micRecordSystem.recordStop(recordingDeviceIndex);
ERRCHEK(result, "Failed to stop recording on mic system");
//Should stop WASAPI starving------------------------------------------------------------------------------
result = wavChannel.stop();
ERRCHEK(result, "Failed to stop wav channel");
result = channel.stop();
ERRCHEK(result, "Failed to stop mic channel");
//---------------------------------------------------------------------------------------------------------
result = sound.release();
ERRCHEK(result, "Failed to release sound");
//Stop the wav system writing to file
result = wavWriterSystem.setOutput(FMOD.OUTPUTTYPE.NOSOUND);
ERRCHEK(result, "Failed to change wav system output");
}
//-------------------------------------------------------------------------------------------------------------
• If you want to do another recording, you will have to recreate the sound
and delete the .wav
file so it can’t be created again.
Recreate Sound
//RECREATE SOUND-----------------------------------------------------------------------------------------------
if (Input.GetKeyDown(KeyCode.L) && killed && stopped)
{
SetState("Restart");
//Only need to recreate the sound
FMOD.RESULT result = micRecordSystem.createSound("Sound", FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER,
ref exinfo, out sound);
ERRCHEK(result, "Failed to recreate sound");
//Removing old wav so that the new one can be recorded
if (filePath.text.Length != 0)
{
File.Delete(filePath.text);
#if UNITY_EDITOR
Debug.Log("Deleted .wav file");
#endif
}
}
//-------------------------------------------------------------------------------------------------------------
I’ll include the entire file. But it’s a good idea to try to set it up yourself so you understand how it works!
RecordMic.txt (9.4 KB)
I have also included how to use programmer sounds
ProgrammerSounds.txt (3.6 KB)
Hope this helps!