Skip to content

Commit d55e742

Browse files
committed
formatting, trace recording device to console
1 parent 91d53b6 commit d55e742

File tree

1 file changed

+140
-141
lines changed

1 file changed

+140
-141
lines changed

Examples/ServiceExamples/Scripts/ExampleStreaming.cs

Lines changed: 140 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -24,145 +24,144 @@
2424

2525
public class ExampleStreaming : MonoBehaviour
2626
{
27-
private int m_RecordingRoutine = 0;
28-
29-
30-
private string m_MicrophoneID = null;
31-
private AudioClip m_Recording = null;
32-
private int m_RecordingBufferSize = 2;
33-
private int m_RecordingHZ = 22050;
34-
35-
36-
private SpeechToText m_SpeechToText = new SpeechToText();
37-
38-
void Start()
39-
{
40-
LogSystem.InstallDefaultReactors();
41-
Log.Debug("ExampleStreaming", "Start();");
42-
43-
Active = true;
44-
45-
StartRecording();
46-
}
47-
48-
public bool Active
49-
{
50-
get { return m_SpeechToText.IsListening; }
51-
set {
52-
if ( value && !m_SpeechToText.IsListening )
53-
{
54-
m_SpeechToText.DetectSilence = true;
55-
m_SpeechToText.EnableWordConfidence = false;
56-
m_SpeechToText.EnableTimestamps = false;
57-
m_SpeechToText.SilenceThreshold = 0.03f;
58-
m_SpeechToText.MaxAlternatives = 1;
59-
m_SpeechToText.EnableContinousRecognition = true;
60-
m_SpeechToText.EnableInterimResults = true;
61-
m_SpeechToText.OnError = OnError;
62-
m_SpeechToText.StartListening( OnRecognize );
63-
}
64-
else if ( !value && m_SpeechToText.IsListening )
65-
{
66-
m_SpeechToText.StopListening();
67-
}
68-
}
69-
}
70-
71-
private void StartRecording()
72-
{
73-
if (m_RecordingRoutine == 0)
74-
{
75-
UnityObjectUtil.StartDestroyQueue();
76-
m_RecordingRoutine = Runnable.Run(RecordingHandler());
77-
}
78-
}
79-
80-
private void StopRecording()
81-
{
82-
if (m_RecordingRoutine != 0)
83-
{
84-
Microphone.End(m_MicrophoneID);
85-
Runnable.Stop(m_RecordingRoutine);
86-
m_RecordingRoutine = 0;
87-
}
88-
}
89-
90-
private void OnError( string error )
91-
{
92-
Active = false;
93-
94-
Log.Debug("ExampleStreaming", "Error! {0}", error);
95-
}
96-
97-
private IEnumerator RecordingHandler()
98-
{
99-
m_Recording = Microphone.Start(m_MicrophoneID, true, m_RecordingBufferSize, m_RecordingHZ);
100-
yield return null; // let m_RecordingRoutine get set..
101-
102-
if (m_Recording == null)
103-
{
104-
StopRecording();
105-
yield break;
106-
}
107-
108-
bool bFirstBlock = true;
109-
int midPoint = m_Recording.samples / 2;
110-
float[] samples = null;
111-
112-
while (m_RecordingRoutine != 0 && m_Recording != null)
113-
{
114-
int writePos = Microphone.GetPosition(m_MicrophoneID);
115-
if (writePos > m_Recording.samples || !Microphone.IsRecording(m_MicrophoneID))
116-
{
117-
Log.Error("MicrophoneWidget", "Microphone disconnected.");
118-
119-
StopRecording();
120-
yield break;
121-
}
122-
123-
if ((bFirstBlock && writePos >= midPoint)
124-
|| (!bFirstBlock && writePos < midPoint))
125-
{
126-
// front block is recorded, make a RecordClip and pass it onto our callback.
127-
samples = new float[midPoint];
128-
m_Recording.GetData(samples, bFirstBlock ? 0 : midPoint);
129-
130-
AudioData record = new AudioData();
131-
record.MaxLevel = Mathf.Max(samples);
132-
record.Clip = AudioClip.Create("Recording", midPoint, m_Recording.channels, m_RecordingHZ, false);
133-
record.Clip.SetData(samples, 0);
134-
135-
m_SpeechToText.OnListen(record);
136-
137-
bFirstBlock = !bFirstBlock;
138-
}
139-
else
140-
{
141-
// calculate the number of samples remaining until we ready for a block of audio,
142-
// and wait that amount of time it will take to record.
143-
int remaining = bFirstBlock ? (midPoint - writePos) : (m_Recording.samples - writePos);
144-
float timeRemaining = (float)remaining / (float)m_RecordingHZ;
145-
146-
yield return new WaitForSeconds(timeRemaining);
147-
}
148-
149-
}
150-
151-
yield break;
152-
}
153-
154-
private void OnRecognize(SpeechRecognitionEvent result)
155-
{
156-
if (result != null && result.results.Length > 0)
157-
{
158-
foreach (var res in result.results)
159-
{
160-
foreach (var alt in res.alternatives)
161-
{
162-
string text = alt.transcript;
163-
Log.Debug("ExampleStreaming", string.Format("{0} ({1}, {2:0.00})\n", text, res.final ? "Final" : "Interim", alt.confidence));
164-
}
165-
}
166-
}
167-
}
27+
private int m_RecordingRoutine = 0;
28+
private string m_MicrophoneID = null;
29+
private AudioClip m_Recording = null;
30+
private int m_RecordingBufferSize = 2;
31+
private int m_RecordingHZ = 22050;
32+
33+
private SpeechToText m_SpeechToText = new SpeechToText();
34+
35+
void Start()
36+
{
37+
LogSystem.InstallDefaultReactors();
38+
Log.Debug("ExampleStreaming", "Start();");
39+
40+
Active = true;
41+
42+
StartRecording();
43+
}
44+
45+
public bool Active
46+
{
47+
get { return m_SpeechToText.IsListening; }
48+
set
49+
{
50+
if (value && !m_SpeechToText.IsListening)
51+
{
52+
m_SpeechToText.DetectSilence = true;
53+
m_SpeechToText.EnableWordConfidence = false;
54+
m_SpeechToText.EnableTimestamps = false;
55+
m_SpeechToText.SilenceThreshold = 0.03f;
56+
m_SpeechToText.MaxAlternatives = 1;
57+
m_SpeechToText.EnableContinousRecognition = true;
58+
m_SpeechToText.EnableInterimResults = true;
59+
m_SpeechToText.OnError = OnError;
60+
m_SpeechToText.StartListening(OnRecognize);
61+
}
62+
else if (!value && m_SpeechToText.IsListening)
63+
{
64+
m_SpeechToText.StopListening();
65+
}
66+
}
67+
}
68+
69+
private void StartRecording()
70+
{
71+
if (m_RecordingRoutine == 0)
72+
{
73+
UnityObjectUtil.StartDestroyQueue();
74+
m_RecordingRoutine = Runnable.Run(RecordingHandler());
75+
}
76+
}
77+
78+
private void StopRecording()
79+
{
80+
if (m_RecordingRoutine != 0)
81+
{
82+
Microphone.End(m_MicrophoneID);
83+
Runnable.Stop(m_RecordingRoutine);
84+
m_RecordingRoutine = 0;
85+
}
86+
}
87+
88+
private void OnError(string error)
89+
{
90+
Active = false;
91+
92+
Log.Debug("ExampleStreaming", "Error! {0}", error);
93+
}
94+
95+
private IEnumerator RecordingHandler()
96+
{
97+
Log.Debug("ExampleStreaming", "devices: {0}", Microphone.devices);
98+
m_Recording = Microphone.Start(m_MicrophoneID, true, m_RecordingBufferSize, m_RecordingHZ);
99+
yield return null; // let m_RecordingRoutine get set..
100+
101+
if (m_Recording == null)
102+
{
103+
StopRecording();
104+
yield break;
105+
}
106+
107+
bool bFirstBlock = true;
108+
int midPoint = m_Recording.samples / 2;
109+
float[] samples = null;
110+
111+
while (m_RecordingRoutine != 0 && m_Recording != null)
112+
{
113+
int writePos = Microphone.GetPosition(m_MicrophoneID);
114+
if (writePos > m_Recording.samples || !Microphone.IsRecording(m_MicrophoneID))
115+
{
116+
Log.Error("MicrophoneWidget", "Microphone disconnected.");
117+
118+
StopRecording();
119+
yield break;
120+
}
121+
122+
if ((bFirstBlock && writePos >= midPoint)
123+
|| (!bFirstBlock && writePos < midPoint))
124+
{
125+
// front block is recorded, make a RecordClip and pass it onto our callback.
126+
samples = new float[midPoint];
127+
m_Recording.GetData(samples, bFirstBlock ? 0 : midPoint);
128+
129+
AudioData record = new AudioData();
130+
record.MaxLevel = Mathf.Max(samples);
131+
record.Clip = AudioClip.Create("Recording", midPoint, m_Recording.channels, m_RecordingHZ, false);
132+
record.Clip.SetData(samples, 0);
133+
134+
m_SpeechToText.OnListen(record);
135+
136+
bFirstBlock = !bFirstBlock;
137+
}
138+
else
139+
{
140+
// calculate the number of samples remaining until we ready for a block of audio,
141+
// and wait that amount of time it will take to record.
142+
int remaining = bFirstBlock ? (midPoint - writePos) : (m_Recording.samples - writePos);
143+
float timeRemaining = (float)remaining / (float)m_RecordingHZ;
144+
145+
yield return new WaitForSeconds(timeRemaining);
146+
}
147+
148+
}
149+
150+
yield break;
151+
}
152+
153+
private void OnRecognize(SpeechRecognitionEvent result)
154+
{
155+
if (result != null && result.results.Length > 0)
156+
{
157+
foreach (var res in result.results)
158+
{
159+
foreach (var alt in res.alternatives)
160+
{
161+
string text = alt.transcript;
162+
Log.Debug("ExampleStreaming", string.Format("{0} ({1}, {2:0.00})\n", text, res.final ? "Final" : "Interim", alt.confidence));
163+
}
164+
}
165+
}
166+
}
168167
}

0 commit comments

Comments
 (0)