public void StopMelody() { Debug.Log("Finished Melody"); if (!CurrentMelody.IsEmpty) { var pos = Microphone.GetPosition(MicrophoneDevice); Microphone.End(MicrophoneDevice); // trim the audio clip var clip = CurrentMelody.Audio; var data = new float[pos]; var trimmedClip = AudioClip.Create("Trimmed Microphone", pos, clip.channels, clip.frequency, false); clip.GetData(data, 0); trimmedClip.SetData(data, 0); CurrentMelody.Audio = trimmedClip; CurrentMelody.IsFinished = true; ParticleController.AddMelody(CurrentMelody); } // we immediatelly start a new melody // the recording starts with first key press StartNewMelody(); }