IEnumerator DoRecording()
	{
		Debug.Log("Recording");
		audio.clip = Microphone.Start(null, false, 5, 8000);
		yield return new WaitForSeconds(5);
		Debug.Log("Playing");
		audio.Play();
		Microphone.End(null);

		float[] clipData = new float[audio.clip.samples * audio.clip.channels];
		audio.clip.GetData(clipData, 0);
		
		//Format to 8KHz sampling rate
		WaveGen.WaveFormatChunk format = new WaveGen().MakeFormat(audio.clip);
		
		string filename = "recordedSpeech.wav";
		FileStream stream = File.OpenWrite(filename);
		
		new WaveGen().Write(clipData, format, stream);
		stream.Close();

        ATT_MSSDK.Speechv3.SpeechResponse response = SpeechToTextService(filename, "Generic", "audio/wav");
        string speechOutput = response.Recognition.NBest[0].ResultText;
        Debug.Log(speechOutput);

		string text = speechOutput.ToLower();
		
		if (text.Contains("red"))
		{
			gameObject.renderer.material.color = Color.red;
		}
		
		if (text.Contains("green"))
		{
			gameObject.renderer.material.color = Color.green;
		}
		
		if (text.Contains("blue"))
		{
			gameObject.renderer.material.color = Color.blue;
		}
	}
    public void RequestSpeech(AudioClip audio, GameObject receiver, string callback)
    {
        float[] clipData = new float[audio.samples * audio.channels];
        audio.GetData(clipData, 0);
        WaveGen.WaveFormatChunk format = new WaveGen().MakeFormat(audio);
        
        try
        {
            string filename = GetTempFileName() + ".wav";
            FileStream stream = File.OpenWrite(filename);
            new WaveGen().Write(clipData, format, stream);
            stream.Close();

            Debug.Log("Request Start time: " + DateTime.Now.ToLongTimeString());

            if (requestFactory == null)
            {
                requestFactory = BuildRequestFactory(RequestFactory.ScopeTypes.Speech);
            }

            if (clientToken ==  null)
            {
                clientToken = GetAccessToken();
            }

            if (null != clientToken)
            {
                requestFactory.ClientCredential = clientToken;
            }

            ATT_MSSDK.Speechv3.SpeechResponse response = SpeechToTextService(filename, "Generic", "audio/wav");
            string speechOutput = response.Recognition.NBest[0].ResultText;
            if (clientToken == null)
            {
                clientToken = requestFactory.ClientCredential;
                SaveAccessToken();
            }

            Debug.Log("Response received time: " + DateTime.Now.ToLongTimeString());
            showProcess = false;
            Debug.Log("response: " + speechOutput);
            File.Delete(filename);
        }
        catch (System.Exception e)
        {
            Debug.LogError(e);
        }
    }