コード例 #1
0
ファイル: MicDemo.cs プロジェクト: Picovoice/cheetah
        /// <summary>
        /// Creates an input audio stream and instantiates an instance of Cheetah object.
        /// </summary>
        /// <param name="accessKey">AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).</param>
        /// <param name="modelPath">Absolute path to the file containing model parameters. If not set it will be set to the default location.</param>
        /// <param name="endpointDurationSec">
        /// Duration of endpoint in seconds. A speech endpoint is detected when there is a segment of audio(with a duration specified herein) after
        /// an utterance without any speech in it. Set to `0` to disable
        /// </param>
        /// <param name="audioDeviceIndex">Optional argument. If provided, audio is recorded from this input device. Otherwise, the default audio input device is used.</param>
        public static void RunDemo(
            string accessKey,
            string modelPath,
            float endpointDurationSec,
            int audioDeviceIndex)
        {
            Cheetah cheetah = null;

            cheetah = Cheetah.Create(
                accessKey,
                modelPath,
                endpointDurationSec);

            PvRecorder recorder = PvRecorder.Create(audioDeviceIndex, cheetah.FrameLength);

            recorder.Start();
            List <short> audioFrame = new List <short>();


            Console.CancelKeyPress += (s, o) =>
            {
                Console.WriteLine("Stopping...");
            };


            Console.WriteLine($"\nUsing device: {recorder.SelectedDevice}");
            Console.WriteLine(">>> Press `CTRL+C` to exit:\n");

            try
            {
                while (true)
                {
                    short[] pcm = recorder.Read();

                    CheetahTranscript transcriptObj = cheetah.Process(pcm);
                    if (!String.IsNullOrEmpty(transcriptObj.Transcript))
                    {
                        Console.Write(transcriptObj.Transcript);
                    }
                    if (transcriptObj.IsEndpoint)
                    {
                        CheetahTranscript finalTranscriptObj = cheetah.Flush();
                        Console.WriteLine(finalTranscriptObj.Transcript);
                    }
                }
            }
            catch (CheetahActivationLimitException)
            {
                Console.WriteLine($"AccessKey '{accessKey}' has reached its processing limit.");
            }
            finally
            {
                cheetah.Dispose();
                recorder.Dispose();
            }
        }
コード例 #2
0
        /// <summary>
        /// Creates an input audio stream, instantiates an instance of Porcupine object, and monitors the audio stream for
        /// occurrencec of the wake word(s). It prints the time of detection for each occurrence and the wake word.
        /// </summary>
        /// <param name="accessKey">AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).</param>
        /// <param name="modelPath">Absolute path to the file containing model parameters. If not set it will be set to the default location.</param>
        /// <param name="keywordPaths">Absolute paths to keyword model files. If not set it will be populated from `keywords` argument.</param>
        /// <param name="sensitivities">
        /// Sensitivities for detecting keywords. Each value should be a number within [0, 1]. A higher sensitivity results in fewer
        /// misses at the cost of increasing the false alarm rate. If not set 0.5 will be used.
        /// </param>
        /// <param name="audioDeviceIndex">Optional argument. If provided, audio is recorded from this input device. Otherwise, the default audio input device is used.</param>
        /// <param name="outputPath">Optional argument. If provided, recorded audio will be stored in this location at the end of the run.</param>
        public static void RunDemo(
            string accessKey,
            string modelPath,
            List <string> keywordPaths,
            List <float> sensitivities,
            int audioDeviceIndex,
            string outputPath = null)
        {
            Porcupine    porcupine           = null;
            BinaryWriter outputFileWriter    = null;
            int          totalSamplesWritten = 0;

            // init porcupine wake word engine
            porcupine = Porcupine.FromKeywordPaths(accessKey, keywordPaths, modelPath, sensitivities);

            // get keyword names for labeling detection results
            List <string> keywordNames = keywordPaths.Select(k => Path.GetFileNameWithoutExtension(k).Split("_")[0]).ToList();

            // open stream to output file
            if (!string.IsNullOrWhiteSpace(outputPath))
            {
                outputFileWriter = new BinaryWriter(new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write));
                WriteWavHeader(outputFileWriter, 1, 16, 16000, 0);
            }

            Console.CancelKeyPress += (s, o) =>
            {
                Console.WriteLine("Stopping...");

                if (outputFileWriter != null)
                {
                    // write size to header and clean up
                    WriteWavHeader(outputFileWriter, 1, 16, 16000, totalSamplesWritten);
                    outputFileWriter.Flush();
                    outputFileWriter.Dispose();
                }
                porcupine?.Dispose();
            };

            // create and start recording
            using (PvRecorder recorder = PvRecorder.Create(deviceIndex: audioDeviceIndex, frameLength: porcupine.FrameLength))
            {
                Console.WriteLine($"Using device: {recorder.SelectedDevice}");
                Console.Write($"Listening for [{string.Join(' ', keywordNames.Select(k => $"'{k}'"))}]...\n");
                recorder.Start();

                while (true)
                {
                    short[] pcm = recorder.Read();

                    int result = porcupine.Process(pcm);
                    if (result >= 0)
                    {
                        Console.WriteLine($"[{DateTime.Now.ToLongTimeString()}] Detected '{keywordNames[result]}'");
                    }

                    if (outputFileWriter != null)
                    {
                        foreach (short sample in pcm)
                        {
                            outputFileWriter.Write(sample);
                        }
                        totalSamplesWritten += pcm.Length;
                    }
                    Thread.Yield();
                }
            }
        }
コード例 #3
0
ファイル: MicDemo.cs プロジェクト: Picovoice/rhino
        /// <summary>
        /// Creates an input audio stream, instantiates an instance of Rhino object, and infers the intent from spoken commands.
        /// </summary>
        /// <param name="accessKey">AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).</param>
        /// <param name="contextPath">
        /// Absolute path to file containing context model (file with `.rhn` extension). A context represents the set of
        /// expressions(spoken commands), intents, and intent arguments(slots) within a domain of interest.
        /// </param>
        /// <param name="modelPath">Absolute path to the file containing model parameters. If not set it will be set to the default location.</param>
        /// <param name="sensitivity">
        /// Inference sensitivity. It should be a number within [0, 1]. A higher sensitivity value results in
        /// fewer misses at the cost of (potentially) increasing the erroneous inference rate. If not set, the default value of 0.5 will be used.
        /// </param>
        /// <param name="requireEndpoint">
        /// If set to `true`, Rhino requires an endpoint (chunk of silence) before finishing inference.
        /// </param>
        /// <param name="audioDeviceIndex">Optional argument. If provided, audio is recorded from this input device. Otherwise, the default audio input device is used.</param>
        /// <param name="outputPath">Optional argument. If provided, recorded audio will be stored in this location at the end of the run.</param>
        public static void RunDemo(
            string accessKey,
            string contextPath,
            string modelPath,
            float sensitivity,
            bool requireEndpoint,
            int audioDeviceIndex,
            string outputPath = null)
        {
            Rhino        rhino               = null;
            BinaryWriter outputFileWriter    = null;
            int          totalSamplesWritten = 0;

            // init rhino speech-to-intent engine
            rhino = Rhino.Create(
                accessKey,
                contextPath,
                modelPath,
                sensitivity,
                requireEndpoint);

            // open stream to output file
            if (!string.IsNullOrWhiteSpace(outputPath))
            {
                outputFileWriter = new BinaryWriter(new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write));
                WriteWavHeader(outputFileWriter, 1, 16, 16000, 0);
            }

            Console.CancelKeyPress += (s, o) =>
            {
                Console.WriteLine("Stopping...");

                if (outputFileWriter != null)
                {
                    // write size to header and clean up
                    WriteWavHeader(outputFileWriter, 1, 16, 16000, totalSamplesWritten);
                    outputFileWriter.Flush();
                    outputFileWriter.Dispose();
                }
                rhino?.Dispose();
            };

            // create and start recording
            using (PvRecorder recorder = PvRecorder.Create(audioDeviceIndex, rhino.FrameLength))
            {
                recorder.Start();
                Console.WriteLine(rhino.ContextInfo);
                Console.WriteLine($"\nUsing device: {recorder.SelectedDevice}");
                Console.WriteLine("Listening...\n");

                while (true)
                {
                    short[] pcm         = recorder.Read();
                    bool    isFinalized = rhino.Process(pcm);
                    if (isFinalized)
                    {
                        Inference inference = rhino.GetInference();
                        if (inference.IsUnderstood)
                        {
                            Console.WriteLine("{");
                            Console.WriteLine($"  intent : '{inference.Intent}'");
                            Console.WriteLine("  slots : {");
                            foreach (KeyValuePair <string, string> slot in inference.Slots)
                            {
                                Console.WriteLine($"    {slot.Key} : '{slot.Value}'");
                            }
                            Console.WriteLine("  }");
                            Console.WriteLine("}");
                        }
                        else
                        {
                            Console.WriteLine("Didn't understand the command.");
                        }
                    }

                    if (outputFileWriter != null)
                    {
                        foreach (short sample in pcm)
                        {
                            outputFileWriter.Write(sample);
                        }
                        totalSamplesWritten += pcm.Length;
                    }
                    Thread.Yield();
                }
            }
        }