forked from ITP-xStory/uniFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microphoneCapture.cs
85 lines (71 loc) · 2.54 KB
/
microphoneCapture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof (AudioSource))]
public class microphoneCapture : MonoBehaviour {
//A boolean that flags whether there's a connected microphone
private bool micConnected = false;
//The maximum and minimum available recording frequencies
private int minFreq;
private int maxFreq;
//A handle to the attached AudioSource
private AudioSource goAudioSource;
//Use this for initialization
void Start()
{
//Check if there is at least one microphone connected
if(Microphone.devices.Length <= 0)
{
//Throw a warning message at the console if there isn't
Debug.LogWarning("Microphone not connected!");
}
else //At least one microphone is present
{
//Set 'micConnected' to true
micConnected = true;
//Get the default microphone recording capabilities
Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
//According to the documentation, if minFreq and maxFreq are zero, the microphone supports any frequency...
if(minFreq == 0 && maxFreq == 0)
{
//...meaning 44100 Hz can be used as the recording sampling rate
maxFreq = 44100;
}
//Get the attached AudioSource component
goAudioSource = this.GetComponent<AudioSource>();
}
}
void OnGUI()
{
//If there is a microphone
if(micConnected)
{
//If the audio from any microphone isn't being captured
if(!Microphone.IsRecording(null))
{
//Case the 'Record' button gets pressed
if(GUI.Button(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Record"))
{
//Start recording and store the audio captured from the microphone at the AudioClip in the AudioSource
goAudioSource.clip = Microphone.Start(null, true, 20, maxFreq);
}
}
else //Recording is in progress
{
//Case the 'Stop and Play' button gets pressed
if(GUI.Button(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Stop and Play!"))
{
Microphone.End(null); //Stop the audio recording
goAudioSource.Play(); //Playback the recorded audio
}
GUI.Label(new Rect(Screen.width/2-100, Screen.height/2+25, 200, 50), "Recording in progress...");
}
}
else // No microphone
{
//Print a red "Microphone not connected!" message at the center of the screen
GUI.contentColor = Color.red;
GUI.Label(new Rect(Screen.width/2-100, Screen.height/2-25, 200, 50), "Microphone not connected!");
}
}
}