using FMODUnity; using UnityEngine; using System.Collections; using System.IO; using System; using System.Runtime.InteropServices; using _Scripts.GUI; public class RecordMic : MonoBehaviour { //public variables [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 Reverb To Recording")] //public KeyCode PlayAndPause; //public KeyCode ReverbOnOffSwitch; //FMOD Objects public FMOD.Sound sound; public FMOD.CREATESOUNDEXINFO exinfo; public FMOD.Channel channel; public FMOD.ChannelGroup channelGroup; //How many recording devices are plugged in for us to use. private int numOfDriversConnected = 0; private int numofDrivers = 0; //Info about the device we're recording with. private Guid MicGUID; private int SampleRate = 0; private FMOD.SPEAKERMODE FMODSpeakerMode; private int NumOfChannels = 0; private FMOD.DRIVER_STATE driverState; //Other variables. private bool dspEnabled = false; //private bool playOrPause = true; //private bool playOkay = false; private bool isRecording = false; private bool isPlaying = false; public void InitiateSound() { //Step 2: Get all of the information we can about the recording device (or driver) that we're // going to use to record with. RuntimeManager.CoreSystem.getRecordDriverInfo(RecordingDeviceIndex, out RecordingDeviceName, 50, out MicGUID, out SampleRate, out FMODSpeakerMode, out NumOfChannels, out driverState); //Next we want to create an "FMOD Sound Object", but to do that, we first need to use our //FMOD.CREATESOUNDEXINFO variable to hold and pass information such as the sample rate we're //recording at and the num of channels we're recording with into our Sound object. //Step 3: Store relevant information into FMOD.CREATESOUNDEXINFO variable. exinfo.cbsize = 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 * 30; //Step 4: Create an FMOD Sound "object". This is what will hold our voice as it is recorded. RuntimeManager.CoreSystem.createSound(exinfo.userdata, FMOD.MODE.LOOP_OFF | FMOD.MODE.OPENUSER, ref exinfo, out sound); } IEnumerator Wait() { yield return new WaitForSeconds(Latency); RuntimeManager.CoreSystem.playSound(sound, channelGroup, true, out channel); //playOkay = true; Debug.Log("Ready To Play"); } public void StartRecording() { if (isRecording || isPlaying) return; InitiateSound(); //Step 5: Start recording through our chosen device into our Sound object. RuntimeManager.CoreSystem.recordStart(RecordingDeviceIndex, sound, true); // Step 6: Start a Corutine that will tell our Sound object to play after a ceratin amount of time. isRecording = true; Debug.LogError("Recording Started"); } public void StopRecording() { RuntimeManager.CoreSystem.recordStop(RecordingDeviceIndex); isRecording = false; StartCoroutine(Wait()); Debug.LogError("Recording Stopped"); } public void PlayRecording() { if (isRecording) StopRecording(); if (!isPlaying) { channel.setPaused(false); isPlaying = true; } } public void PauseRecording() { if (isRecording) return; if (isPlaying) { channel.setPaused(true); isPlaying = false; } } public void ClearAudioRecording() { sound.release(); channel.setPaused(true); isPlaying = false; } }