/// <summary> /// Starts to stream the input of the current Mic device /// </summary> public void StartRecording(int sampleLen = 10) { StopRecording(); if (!Microphone.IsRecording(CurrentDeviceName)) { Debug.Log("[Mic] " + CurrentDeviceName + " was not started when starting recording, restarting mic."); StartMicrophone(); } IsRecording = true; SampleDurationMS = sampleLen; Sample = new float[AudioEncoding.samplerate / 1000 * SampleDurationMS * AudioClip.channels]; if (AudioClip) { StartCoroutine(ReadRawAudio()); // Make sure we seek before we start reading data Microphone.GetPosition(CurrentDeviceName); Debug.Log("[Mic] Started recording with " + CurrentDeviceName); if (OnStartRecording != null) { OnStartRecording.Invoke(); } } else { OnStartRecordingFailed.Invoke(); } }
public void StartRecording() { outputPath = Path.Combine(Environment.CurrentDirectory, GetNextFileName("record", ".wav")); writer = new WaveFileWriter(outputPath, waveIn.WaveFormat); OnStartRecording?.Invoke(); waveIn.StartRecording(); }
/// <summary> /// Starts to stream the input of the current Mic device /// </summary> public void StartRecording(int frequency, int sampleLen) { StopRecording(); IsRecording = true; Frequency = frequency; SampleDurationMS = sampleLen; Clip = Microphone.Start(CurrentDeviceName, true, 1, Frequency); Sample = new float[Frequency / 1000 * SampleDurationMS * Clip.channels]; m_AudioSource.clip = Clip; StartCoroutine(ReadRawAudio()); if (OnStartRecording != null) { OnStartRecording.Invoke(); } }
// ================================================ #region RECORDING /// <summary> /// Starts to stream the input of the current Mic device /// </summary> public void StartRecording(int sampleLen = 10) { // Cant start unless available if (!IsInputAvailable) { SafeStartMicrophone(); } // Still unavailable, exit if (!IsInputAvailable) { return; } // Stop recording if doing so StopRecording(); IsRecording = true; SampleDurationMS = sampleLen; Sample = new float[AudioEncoding.samplerate / 1000 * SampleDurationMS * AudioClip.channels]; if (AudioClip) { StartCoroutine(ReadRawAudio()); // Make sure we seek before we start reading data Microphone.GetPosition(CurrentDeviceName); Debug.Log("[Mic] Started recording with " + CurrentDeviceName); if (OnStartRecording != null) { OnStartRecording.Invoke(); } } else { OnStartRecordingFailed.Invoke(); } }
// Read raw audio protected virtual IEnumerator ReadRawAudio(int sampleDurationMS) { // Start recording OnStartRecording?.Invoke(); // Get data AudioClip micClip = GetMicClip(); string micDevice = GetMicName(); int micSampleRate = GetMicSampleRate(); // Setup sample int sampleTotal = AudioEncoding.samplerate / 1000 * sampleDurationMS * micClip.channels; float[] sample = new float[sampleTotal]; // All needed data int loops = 0; int readAbsPos = Microphone.GetPosition(micDevice); int prevPos = readAbsPos; int micTempTotal = micSampleRate / 1000 * sampleDurationMS * micClip.channels; int micDif = micTempTotal / sampleTotal; float[] temp = new float[micTempTotal]; // Continue reading while (micClip != null && Microphone.IsRecording(micDevice) && IsRecording) { bool isNewDataAvailable = true; while (isNewDataAvailable && micClip != null) { int currPos = Microphone.GetPosition(micDevice); if (currPos < prevPos) { loops++; } prevPos = currPos; var currAbsPos = loops * micClip.samples + currPos; var nextReadAbsPos = readAbsPos + micTempTotal; if (nextReadAbsPos < currAbsPos) { micClip.GetData(temp, readAbsPos % micClip.samples); // Fill sample & get level max float levelMax = 0; int sampleIndex = 0; for (int i = 0; i < temp.Length; i++) { float wavePeak = temp[i] * temp[i]; if (levelMax < wavePeak) { levelMax = wavePeak; } if (i % micDif == 0 && sampleIndex < sample.Length) { sample[sampleIndex] = temp[i]; sampleIndex++; } } _sampleCount++; OnSampleReady?.Invoke(_sampleCount, sample, levelMax); readAbsPos = nextReadAbsPos; } else { isNewDataAvailable = false; } } // Wait a moment yield return(null); } // Stop if (IsRecording) { StopRecording(); } }