/// <summary> /// Update event /// </summary> protected override void ExampleUpdate() { try { base.ExampleUpdate(); if (UsePlotter) { if (null != m_plotData && m_plotData.Length > 0) { Vector2 pos = MaterialWave.mainTextureOffset; pos.x = Mic.GetPosition(); RendererWave.material.mainTextureOffset = pos / (float)m_plotData.Length; } } } catch (System.Exception ex) { Debug.Log(string.Format("Update exception={0}", ex)); } }
static void Filter() { var mic = new Mic(); var lowpassFilter = new LowpassFilter(); var equalizer = new Equalizer(); var highpassFilter = new HighpassFilter(); var speaker = new Speaker(); var cable1 = new Cable(); var cable2 = new Cable(); var cable3 = new Cable(); var cable4 = new Cable(); mic.OutputJacks[0].Connect(cable1); lowpassFilter.InputJacks[0].Connect(cable1); lowpassFilter.OutputJacks[0].Connect(cable2); equalizer.InputJacks[0].Connect(cable2); equalizer.OutputJacks[0].Connect(cable3); highpassFilter.InputJacks[0].Connect(cable3); highpassFilter.OutputJacks[0].Connect(cable4); speaker.InputJacks[0].Connect(cable4); mic.TurnOn(); speaker.TurnOn(); foreach (Equalizer.Frequencies frequency in Enum.GetValues(typeof(Equalizer.Frequencies))) { equalizer.SetFilter(frequency, 16); } while (true) { Console.ReadLine(); } }
private void Start() { if (!isLocalPlayer) { return; } // MIC SETUP Mic = Mic.Instance; Mic.StartRecording(k_MicFrequency, k_MicSegLenMS); gate = new VolumeGate(120, 1f, 5); // When the microphone is ready with a segment Mic.OnSampleReady += OnSampleReady; // Enable Mic speaking = true; // Find speakingIndicator //speakingIndicator = GameObject.FindGameObjectWithTag("speakingIndicator").GetComponent<Text>(); CmdSetChannels(Mic.Clip.channels); }
public LoRaPayloadJoinRequest(JoinEui joinEui, DevEui devEui, DevNonce devNonce, Mic mic) { MHdr = new MacHeader(MacMessageType.JoinRequest); AppEui = joinEui; DevEUI = devEui; DevNonce = devNonce; Mic = mic; }
// Use this for initialization void Start() { micManager = GameObject.Find("Mic Manager"); mic = micManager.GetComponent <Mic>(); }
/// <summary> /// Handle word detected event /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void WordDetectedHandler(object sender, WordDetection.WordEventArgs args) { // skip detection while recording if (m_recordingSample) { return; } if (null == args.Details || string.IsNullOrEmpty(args.Details.Label)) { m_command = Commands.Noise; return; } if (m_modeTimer > DateTime.Now) { m_command = Commands.Noise; return; } //Debug.Log(args.Details.Label); switch (m_mode) { case Modes.Set1: if (args.Details.Label == Commands.Go.ToString()) { AudioWordDetection.Words = m_set2; m_mode = Modes.Set2; m_modeTimer = DateTime.Now + TimeSpan.FromMilliseconds(1000); Mic.ClearData(); m_wordsChanged = true; m_command = Commands.Noise; } break; case Modes.Set2: if (args.Details.Label == Commands.Back.ToString()) { AudioWordDetection.Words = m_set1; m_mode = Modes.Set1; m_modeTimer = DateTime.Now + TimeSpan.FromMilliseconds(1000); Mic.ClearData(); m_wordsChanged = true; m_command = Commands.Noise; } else if (args.Details.Label == Commands.Action.ToString()) { AudioWordDetection.Words = m_set3; m_mode = Modes.Set3; m_modeTimer = DateTime.Now + TimeSpan.FromMilliseconds(2000); Mic.ClearData(); m_wordsChanged = true; m_command = Commands.Noise; } else if (args.Details.Label == Commands.Misc.ToString()) { AudioWordDetection.Words = m_set4; m_mode = Modes.Set4; m_modeTimer = DateTime.Now + TimeSpan.FromMilliseconds(2000); Mic.ClearData(); m_wordsChanged = true; m_command = Commands.Noise; } break; case Modes.Set3: case Modes.Set4: if (args.Details.Label == Commands.Back.ToString()) { AudioWordDetection.Words = m_set2; m_mode = Modes.Set2; m_modeTimer = DateTime.Now + TimeSpan.FromMilliseconds(1000); Mic.ClearData(); m_wordsChanged = true; m_command = Commands.Noise; } else if (args.Details.Label != Commands.Noise.ToString()) { AudioWordDetection.Words = m_set1; m_mode = Modes.Set1; m_modeTimer = DateTime.Now + TimeSpan.FromMilliseconds(2000); Mic.ClearData(); m_wordsChanged = true; m_command = Commands.Noise; } break; } m_command = (Commands)Enum.Parse(typeof(Commands), args.Details.Label, false); PlayAnimation(GetStateName(m_command)); }
void Run() { AudioBuffer buffer; AudioStreamer streamer; // MIC SETUP Mic = Mic.Instance; Mic.StartRecording(k_MicFrequency, k_MicSegLenMS); var channels = Mic.Clip.channels; var segLen = k_MicFrequency / 1000 * k_MicSegLenMS; var segCap = 1000 / k_MicSegLenMS; // Create an AudioBuffer using the Mic values buffer = new AudioBuffer( k_MicFrequency, channels, segLen, segCap ); // Use the buffer to create a streamer streamer = AudioStreamer.New(buffer, Source); // NETWORKING // On receiving a message from a peer, see if the tag is "audio", which denotes // that the data is an audio segment m_Node.OnGetPacket += delegate(ConnectionId cId, Packet packet, bool reliable) { var reader = new UniStreamReader(packet.Payload); switch (packet.Tag) { case "audio": var index = reader.ReadInt(); var segment = reader.ReadFloatArray(); if (OnGetVoiceSegment != null) { OnGetVoiceSegment(index, segment); } // If the streamer is getting filled, request for a packet skip streamer.Stream(index, segment); break; } }; gate = new VolumeGate(120, 1f, 5); VolumeGateVisualizer viz = VolumeGateVisualizer.New(gate); // When the microphone is ready with a segment Mic.OnSampleReady += (index, segment) => { // Return checks if (m_Node.NodeState == Node.State.Idle || m_Node.NodeState == Node.State.Uninitialized) { return; } if (!Speaking) { return; } // NOISE REMOVAL // Very primitive way to reduce the audio input noise // by averaging audio samples with a radius (radius value suggested: 2) int radius = 2; for (int i = radius; i < segment.Length - radius; i++) { float temp = 0; for (int j = i - radius; j < i + radius; j++) { temp += segment[j]; } segment[i] = temp / (2 * radius + 1); } if (!gate.Evaluate(segment)) { return; } // If Speaking is on, create a payload byte array for AirPeer // and send if (OnSendVoiceSegment != null) { OnSendVoiceSegment(index, segment); } m_Node.Send(Packet.From(m_Node).WithTag("audio").WithPayload( new UniStreamWriter() .WriteInt(index) .WriteFloatArray(segment) .Bytes )); }; }
/// <summary> /// Decode MDN and make sure the message was processed (allow for warnings) /// </summary> /// <param name="outboundMic">the outbound mic to verify against</param> /// <returns></returns> public bool IsOkOrWarning(Mic outboundMic) { HeaderList mdnFields = this.GetMdnFields(); /* * --------_=_NextPart_001_B096DD27.9007A6CE * Content-Type: message/disposition-notification * * Reporting-UA: AS2 eefacta Server (unimaze.com) * Original-Recipient: rfc822; SMP_2000000005 * Final-Recipient: rfc822; SMP_2000000005 * Original-Message-ID: a60d9982-680c-4f01-9ab4-9b5d5fb05f37 * Received-Content-MIC: ZMY/AoJb2JQS557MOATtc0EZdZQ=, sha1 * Disposition: automatic-action/MDN-sent-automatically; processed * * * --------_=_NextPart_001_B096DD27.9007A6CE-- */ // make sure we have a valid disposition string disposition = mdnFields["Disposition"]; if (disposition == null) { Log.Error("Unable to retreieve 'Disposition' from MDN"); return(false); } Log.DebugFormat("Decoding received disposition ({0})", disposition); As2Disposition as2Dis = As2Disposition.valueOf(disposition); // make sure we are in processed state if (!DispositionType.Processed.Equals(as2Dis.DispositionType)) { // Disposition: automatic-action/MDN-sent-automatically; failed/failure: sender-equals-receiver Log.Error("Failed or unknown state : " + disposition); return(false); } // check if the returned MIC matches our outgoing MIC (sha1 of payload), warn about mic mismatch string receivedMic = mdnFields["Received-Content-MIC"]; if (receivedMic == null) { Log.Error("MIC error, no Received-Content-MIC returned in MDN"); return(false); } if (!outboundMic.Equals(Mic.ValueOf(receivedMic))) { Log.Warn( "MIC mismatch, Received-Content-MIC was : " + receivedMic + " while Outgoing-MIC was : " + outboundMic); return(false); } // return when "clean processing state" : Disposition: automatic-action/MDN-sent-automatically; processed DispositionModifier modifier = as2Dis.getDispositionModifier(); if (modifier == null) { return(true); } // allow partial success (warning) if (DispositionModifierPrefix.Warning.Equals(modifier.GetPrefix())) { // Disposition: automatic-action/MDN-sent-automatically; processed/warning: duplicate-document Log.Warn("Returns with warning : " + disposition); return(true); } // Disposition: automatic-action/MDN-sent-automatically; processed/error: insufficient-message-security Log.Warn("MDN failed with disposition raw : " + disposition); Log.Warn("MDN failed with as2 disposition : " + as2Dis); return(false); }
public override bool CheckMic(string appKey) { return(Mic.ToArray().SequenceEqual(PerformMic(appKey))); }
private void OnDisable() { Mic mic = new Mic(); Debug.Log(mic.Speak()); }
// Use this for initialization void Start() { trock = GetComponent<Trock_Control>(); initO=(GameObject)Instantiate (GameInitPoint); init = initO.GetComponent<GameInit> (); life = new GameObject[Life_Count]; lifepoint = Life_Count; txt = txtobject.GetComponent<TextMesh>(); txt.text = ""; micscript = MicObject.GetComponent<Mic>(); }
/// <summary> /// GUI event /// </summary> protected override void OnGUI() { if (null == AudioWordDetection || null == Mic || string.IsNullOrEmpty(Mic.DeviceName)) { return; } #if !UNITY_XBOX360 && !UNITY_XBOXONE if (string.IsNullOrEmpty(m_webDevice)) { WebCamDevice[] devices = WebCamTexture.devices; GUILayout.BeginHorizontal(GUILayout.Width(Screen.width)); GUILayout.FlexibleSpace(); GUILayout.Label("Choose a camera:"); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); foreach (WebCamDevice device in devices) { GUILayout.BeginHorizontal(GUILayout.Width(Screen.width)); GUILayout.FlexibleSpace(); if (GUILayout.Button(device.name, GUILayout.MinHeight(40))) { m_webDevice = device.name; UpdateWebCam(); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } } #endif GUILayout.Label(string.Empty); Color backgroundColor = GUI.backgroundColor; for (int wordIndex = 0; wordIndex < AudioWordDetection.Words.Count; ++wordIndex) { if (m_wordsChanged) { m_wordsChanged = false; GUIUtility.ExitGUI(); } if (!m_recordingSample && AudioWordDetection.ClosestIndex == wordIndex) { GUI.backgroundColor = Color.red; } else { GUI.backgroundColor = backgroundColor; } WordDetails noise = GetWord(WORD_NOISE); if (null == noise) { continue; } if (wordIndex > 0) { GUI.enabled = null != noise.SpectrumReal; } GUILayout.BeginHorizontal(GUILayout.MinWidth(600)); WordDetails details = AudioWordDetection.Words[wordIndex]; if (GUILayout.Button("Play", GUILayout.Height(45), GUILayout.Width(75))) { if (null != details.Audio) { if (NormalizeWave) { GetComponent <AudioSource>().PlayOneShot(details.Audio, 0.1f); } else { GetComponent <AudioSource>().PlayOneShot(details.Audio); } } // show profile RefExample.OverrideSpectrumImag = true; RefExample.SpectrumImag = details.SpectrumReal; } if (wordIndex == 0) { GUILayout.Label(details.Label, GUILayout.Width(100), GUILayout.Height(45)); } else { details.Label = GUILayout.TextField(details.Label, GUILayout.Width(100), GUILayout.Height(45)); } Color oldColor = GUI.color; if (m_recordingSample) { GUI.color = Color.green; } GUILayout.Button(string.Format("{0}", (null == details.SpectrumReal) ? "not set" : "set"), GUILayout.Width(75), GUILayout.Height(45)); if (m_recordingSample) { GUI.color = oldColor; } Event e = Event.current; if (null != e) { Rect rect = GUILayoutUtility.GetLastRect(); bool overButton = rect.Contains(e.mousePosition); if (m_buttonIndex == -1 && m_timerStart == DateTime.MinValue && Input.GetMouseButton(0) && overButton) { //Debug.Log("Initial button down"); m_buttonIndex = wordIndex; m_startPosition = Mic.GetPosition(); m_timerStart = DateTime.Now + TimeSpan.FromSeconds(Mic.CaptureTime); m_recordingSample = true; } if (m_buttonIndex == wordIndex) { bool buttonUp = Input.GetMouseButtonUp(0); if (m_timerStart > DateTime.Now && !buttonUp) { //Debug.Log("Button still pressed"); m_recordingSample = true; } else if (m_timerStart != DateTime.MinValue && m_timerStart < DateTime.Now) { //Debug.Log("Button timed out"); SetupWordProfile(false); m_timerStart = DateTime.MinValue; m_buttonIndex = -1; m_recordingSample = false; Mic.ClearData(); } else if (m_timerStart != DateTime.MinValue && buttonUp && m_buttonIndex != -1) { //Debug.Log("Button is no longer pressed"); SetupWordProfile(true); m_timerStart = DateTime.MinValue; m_buttonIndex = -1; m_recordingSample = false; Mic.ClearData(); SaveProfile(details); } } } //GUILayout.Label(details.Score.ToString()); //GUILayout.Label(string.Format("{0}", details.GetMinScore(DateTime.Now - TimeSpan.FromSeconds(1)))); GUILayout.EndHorizontal(); if (wordIndex > 0) { GUI.enabled = null != noise.SpectrumReal; } GUILayout.Space(10); } GUI.backgroundColor = backgroundColor; GUI.enabled = true; }
public JoinRequestFrame(MacHeader mHdr, JoinEui joinEui, DevEui devEui, DevNonce devNonce, Mic mic, RadioMetadata radioMetadata) { MacHeader = mHdr; JoinEui = joinEui; DevEui = devEui; DevNonce = devNonce; Mic = mic; RadioMetadata = radioMetadata; }
/// <summary> /// GUI event /// </summary> protected override void OnGUI() { if (null == AudioWordDetection || null == Mic || string.IsNullOrEmpty(Mic.DeviceName)) { return; } GUILayout.Space(40); GUILayout.Label(string.Format("Active Command: {0}", m_command)); Color backgroundColor = GUI.backgroundColor; for (int wordIndex = 0; wordIndex < AudioWordDetection.Words.Count; ++wordIndex) { WordDetails details = AudioWordDetection.Words[wordIndex]; if (null == details) { continue; } if (AudioWordDetection.ClosestIndex == wordIndex) { GUI.backgroundColor = Color.red; } else { GUI.backgroundColor = backgroundColor; } if (details != GetWord(WORD_NOISE)) { GUI.enabled = (null != GetWord(WORD_NOISE) && null != GetWord(WORD_NOISE).SpectrumReal); } bool showRow = true; if (details == GetWord(WORD_NOISE)) { GUILayout.Label("First: Record a noise sample"); } else if (details == GetWord(WORD_PUSH_TO_TALK)) { showRow = false; } if (showRow) { GUILayout.BeginHorizontal(GUILayout.Width(300)); bool oldEnabled = GUI.enabled; GUI.enabled = null != details.Wave; if (GUILayout.Button("Play", GUILayout.Height(45))) { if (null != details.Audio) { if (NormalizeWave) { GetComponent <AudioSource>().PlayOneShot(details.Audio, 0.1f); } else { GetComponent <AudioSource>().PlayOneShot(details.Audio); } } // show profile RefExample.OverrideSpectrumImag = true; RefExample.SpectrumImag = details.SpectrumReal; } GUI.enabled = oldEnabled; if (wordIndex == 0) { GUILayout.Label(details.Label, GUILayout.Width(150), GUILayout.Height(45)); } else { details.Label = GUILayout.TextField(details.Label, GUILayout.Width(150), GUILayout.Height(45)); } GUILayout.Button(string.Format("{0}", (null == details.SpectrumReal) ? "Rec" : "Re-Rec"), GUILayout.Height(45)); } bool rectAvailable = false; Rect rect = new Rect(); Event e = Event.current; if (details == GetWord(WORD_PUSH_TO_TALK)) { if (AudioWordDetection.UsePushToTalk && null != e) { rect = new Rect(400, 250, 200, 200); rectAvailable = true; Color oldColor = GUI.backgroundColor; GUI.backgroundColor = Color.green; if (GUI.Button(rect, "Push To Talk\nHold Speak 1 Command")) { } GUI.backgroundColor = oldColor; } } else { if (null != e) { rect = GUILayoutUtility.GetLastRect(); rectAvailable = true; } } if (rectAvailable) { bool overButton = rect.Contains(e.mousePosition); if (m_buttonIndex == -1 && m_timerStart == DateTime.MinValue && Input.GetMouseButton(0) && overButton) { //Debug.Log("Initial button down"); m_buttonIndex = wordIndex; m_startPosition = Mic.GetPosition(); m_timerStart = DateTime.Now + TimeSpan.FromSeconds(Mic.CaptureTime); } if (m_buttonIndex == wordIndex) { bool buttonUp = Input.GetMouseButtonUp(0); if (m_timerStart > DateTime.Now && !buttonUp) { //Debug.Log("Button still pressed"); } else if (m_timerStart != DateTime.MinValue && m_timerStart < DateTime.Now) { //Debug.Log("Button timed out"); SetupWordProfile(false); m_timerStart = DateTime.MinValue; m_buttonIndex = -1; } else if (m_timerStart != DateTime.MinValue && buttonUp && m_buttonIndex != -1) { //Debug.Log("Button is no longer pressed"); SetupWordProfile(true); m_timerStart = DateTime.MinValue; m_buttonIndex = -1; } } } if (showRow) { #if UNITY_EDITOR GUI.enabled = null != details.Wave; if (GUILayout.Button("Save")) { string defaultName = string.Format("{0}.profile", details.Label); string path = EditorUtility.SaveFilePanel("Save profile", "Assets/", defaultName, "profile"); if (!string.IsNullOrEmpty(path)) { try { using ( FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite) ) { using (BinaryWriter bw = new BinaryWriter(fs)) { AudioWordDetection.SaveWord(bw, details); Debug.Log("Saved word"); } } } catch (Exception) { Debug.LogError("Failed to save word"); } } } GUI.enabled = true; if (GUILayout.Button("Load")) { string path = EditorUtility.OpenFilePanel("Load profile", "Assets/", "profile"); if (!string.IsNullOrEmpty(path)) { try { using ( FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) ) { using (BinaryReader br = new BinaryReader(fs)) { string oldLabel = details.Label; AudioWordDetection.LoadWord(br, details); Debug.Log("Loaded word"); details.Label = oldLabel; SetupWordProfile(details, false); } } } catch (Exception ex) { Debug.LogError(string.Format("Failed to load word: {0}", ex)); } } } #endif GUILayout.EndHorizontal(); if (details != GetWord(WORD_NOISE)) { GUI.enabled = true; } GUILayout.Space(10); } if (details == GetWord(WORD_NOISE)) { GUILayout.Label("Next: Record the voice command"); } } GUI.backgroundColor = backgroundColor; GUI.enabled = true; }
internal sealed record DataMessageKey(DevEui DevEui, Mic Mic, ushort FCnt);
/// <summary> /// GUI event /// </summary> protected override void OnGUI() { ExampleUpdate(); if (null == AudioWordDetection || null == Mic || string.IsNullOrEmpty(Mic.DeviceName)) { return; } DisplayProfileLoadSave(FILE_PROFILES); Color backgroundColor = GUI.backgroundColor; for (int wordIndex = 0; wordIndex < AudioWordDetection.Words.Count; ++wordIndex) { if (AudioWordDetection.ClosestIndex == wordIndex) { GUI.backgroundColor = Color.red; } else { GUI.backgroundColor = backgroundColor; } WordDetails noise = GetWord(WORD_NOISE); if (null == noise) { continue; } if (wordIndex > 0) { GUI.enabled = null != noise.SpectrumReal; } GUILayout.BeginHorizontal(); WordDetails details = AudioWordDetection.Words[wordIndex]; if (GUILayout.Button("Play", GUILayout.Height(45))) { if (null != details.Audio) { if (NormalizeWave) { audio.PlayOneShot(details.Audio, 0.1f); } else { audio.PlayOneShot(details.Audio); } } // show profile RefExample.OverrideSpectrumImag = true; RefExample.SpectrumImag = details.SpectrumReal; } if (wordIndex == 0) { GUILayout.Label(details.Label, GUILayout.Width(150), GUILayout.Height(45)); } else { details.Label = GUILayout.TextField(details.Label, GUILayout.Width(150), GUILayout.Height(45)); } GUILayout.Button(string.Format("{0}", (null == details.SpectrumReal) ? "not set" : "set"), GUILayout.Height(45)); Event e = Event.current; if (null != e) { Rect rect = GUILayoutUtility.GetLastRect(); bool overButton = rect.Contains(e.mousePosition); if (m_buttonIndex == -1 && m_timerStart == DateTime.MinValue && Input.GetMouseButton(0) && overButton) { //Debug.Log("Initial button down"); m_buttonIndex = wordIndex; m_startPosition = Mic.GetPosition(); m_timerStart = DateTime.Now + TimeSpan.FromSeconds(Mic.CaptureTime); } if (m_buttonIndex == wordIndex) { bool buttonUp = Input.GetMouseButtonUp(0); if (m_timerStart > DateTime.Now && !buttonUp) { //Debug.Log("Button still pressed"); } else if (m_timerStart != DateTime.MinValue && m_timerStart < DateTime.Now) { //Debug.Log("Button timed out"); SetupWordProfile(false); m_timerStart = DateTime.MinValue; m_buttonIndex = -1; } else if (m_timerStart != DateTime.MinValue && buttonUp && m_buttonIndex != -1) { //Debug.Log("Button is no longer pressed"); SetupWordProfile(true); m_timerStart = DateTime.MinValue; m_buttonIndex = -1; } } } GUILayout.Label(details.Score.ToString()); //GUILayout.Label(string.Format("{0}", details.GetMinScore(DateTime.Now - TimeSpan.FromSeconds(1)))); GUILayout.EndHorizontal(); if (wordIndex > 0) { GUI.enabled = null != noise.SpectrumReal; } GUILayout.Space(10); } GUI.backgroundColor = backgroundColor; }
public override byte[] PerformEncryption(string appSkey) { byte[] pt; if (!CfList.Span.IsEmpty) { pt = AppNonce.ToArray().Concat(NetID.ToArray()).Concat(DevAddr.ToArray()).Concat(DlSettings.ToArray()).Concat(RxDelay.ToArray()).Concat(CfList.ToArray()).Concat(Mic.ToArray()).ToArray(); } else { pt = AppNonce.ToArray().Concat(NetID.ToArray()).Concat(DevAddr.ToArray()).Concat(DlSettings.ToArray()).Concat(RxDelay.ToArray()).Concat(Mic.ToArray()).ToArray(); } Aes aes = new AesManaged(); aes.Key = ConversionHelper.StringToByteArray(appSkey); aes.IV = new byte[16]; aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.None; ICryptoTransform cipher; cipher = aes.CreateDecryptor(); var encryptedPayload = cipher.TransformFinalBlock(pt, 0, pt.Length); RawMessage = new byte[encryptedPayload.Length]; Array.Copy(encryptedPayload, 0, RawMessage, 0, encryptedPayload.Length); return(encryptedPayload); }
public void StartStopRec() { Mic method = new Mic(this._Mic); this.Invoke(method); }
public UniMicAudioInput(Mic mic) { this.mic = mic; mic.OnSampleReady += Mic_OnSampleReady; }
/// <summary> /// GUI event /// </summary> protected override void OnGUI() { if (null == AudioWordDetection || null == Mic || string.IsNullOrEmpty(Mic.DeviceName)) { return; } DisplayProfileLoadSave(FILE_PROFILES); GUILayout.Space(40); GUILayout.Label(string.Format("Active Command: {0}", m_command)); Color backgroundColor = GUI.backgroundColor; for (int wordIndex = 0; wordIndex < AudioWordDetection.Words.Count; ++wordIndex) { WordDetails details = AudioWordDetection.Words[wordIndex]; if (null == details) { continue; } if (AudioWordDetection.ClosestIndex == wordIndex) { GUI.backgroundColor = Color.red; } else { GUI.backgroundColor = backgroundColor; } if (details != GetWord(WORD_NOISE)) { GUI.enabled = (null != GetWord(WORD_NOISE) && null != GetWord(WORD_NOISE).SpectrumReal); } bool showRow = true; if (details == GetWord(WORD_NOISE)) { GUILayout.Label("First: Record a noise sample"); } else if (details == GetWord(WORD_PUSH_TO_TALK)) { showRow = false; } if (showRow) { GUILayout.BeginHorizontal(GUILayout.Width(300)); if (GUILayout.Button("Play", GUILayout.Height(45))) { if (null != details.Audio) { if (NormalizeWave) { audio.PlayOneShot(details.Audio, 0.1f); } else { audio.PlayOneShot(details.Audio); } } // show profile RefExample.OverrideSpectrumImag = true; RefExample.SpectrumImag = details.SpectrumReal; } if (wordIndex == 0) { GUILayout.Label(details.Label, GUILayout.Width(150), GUILayout.Height(45)); } else { details.Label = GUILayout.TextField(details.Label, GUILayout.Width(150), GUILayout.Height(45)); } GUILayout.Button(string.Format("{0}", (null == details.SpectrumReal) ? "Rec" : "Re-Rec"), GUILayout.Height(45)); } bool rectAvailable = false; Rect rect = new Rect(); Event e = Event.current; if (details == GetWord(WORD_PUSH_TO_TALK)) { if (null != e) { rect = new Rect(400, 250, 200, 200); rectAvailable = true; Color oldColor = GUI.backgroundColor; GUI.backgroundColor = Color.green; if (GUI.Button(rect, "Push To Talk\nHold Speak 1 Command")) { } GUI.backgroundColor = oldColor; } } else { if (null != e) { rect = GUILayoutUtility.GetLastRect(); rectAvailable = true; } } if (rectAvailable) { bool overButton = rect.Contains(e.mousePosition); if (m_buttonIndex == -1 && m_timerStart == DateTime.MinValue && Input.GetMouseButton(0) && overButton) { //Debug.Log("Initial button down"); m_buttonIndex = wordIndex; m_startPosition = Mic.GetPosition(); m_timerStart = DateTime.Now + TimeSpan.FromSeconds(Mic.CaptureTime); } if (m_buttonIndex == wordIndex) { bool buttonUp = Input.GetMouseButtonUp(0); if (m_timerStart > DateTime.Now && !buttonUp) { //Debug.Log("Button still pressed"); } else if (m_timerStart != DateTime.MinValue && m_timerStart < DateTime.Now) { //Debug.Log("Button timed out"); SetupWordProfile(false); m_timerStart = DateTime.MinValue; m_buttonIndex = -1; } else if (m_timerStart != DateTime.MinValue && buttonUp && m_buttonIndex != -1) { //Debug.Log("Button is no longer pressed"); SetupWordProfile(true); m_timerStart = DateTime.MinValue; m_buttonIndex = -1; } } } if (showRow) { GUILayout.Label(string.Format("{0:F2}", details.Score)); //GUILayout.Label(string.Format("{0}", details.GetMinScore(DateTime.Now - TimeSpan.FromSeconds(1)))); GUILayout.EndHorizontal(); if (details != GetWord(WORD_NOISE)) { GUI.enabled = true; } GUILayout.Space(10); } if (details == GetWord(WORD_NOISE)) { GUILayout.Label("Next: Record the voice command"); } } GUI.backgroundColor = backgroundColor; }