示例#1
0
        public void TestSingleKeywordFr()
        {
            string        language = "fr";
            List <string> keywords = new List <string>()
            {
                "mon chouchou"
            };

            porcupine = Porcupine.FromKeywordPaths(
                ACCESS_KEY,
                GetKeywordPaths(language, keywords),
                GetModelPath(language)
                );

            RunTestCase(
                "mon_chouchou.wav",
                new List <int>()
            {
                0
            });
        }
示例#2
0
        public void TestSingleKeywordEs()
        {
            string        language = "es";
            List <string> keywords = new List <string>()
            {
                "manzana"
            };

            porcupine = Porcupine.FromKeywordPaths(
                ACCESS_KEY,
                GetKeywordPaths(language, keywords),
                GetModelPath(language)
                );

            RunTestCase(
                "manzana.wav",
                new List <int>()
            {
                0
            });
        }
示例#3
0
        public void TestSingleKeywordDe()
        {
            string        language = "de";
            List <string> keywords = new List <string>()
            {
                "heuschrecke"
            };

            porcupine = Porcupine.FromKeywordPaths(
                ACCESS_KEY,
                GetKeywordPaths(language, keywords),
                GetModelPath(language)
                );

            RunTestCase(
                "heuschrecke.wav",
                new List <int>()
            {
                0
            });
        }
示例#4
0
        public void TestWithNonAsciiModelName()
        {
            string        language = "es";
            List <string> keywords = new List <string>()
            {
                "murciélago",
            };

            porcupine = Porcupine.FromKeywordPaths(
                ACCESS_KEY,
                GetKeywordPaths(language, keywords),
                GetModelPath(language)
                );

            RunTestCase(
                "murciélago.wav",
                new List <int>()
            {
                0, 0
            });
        }
示例#5
0
        public void TestMultipleKeywordFr()
        {
            string        language = "fr";
            List <string> keywords = new List <string>()
            {
                "framboise",
                "mon chouchou",
                "parapluie"
            };

            porcupine = Porcupine.FromKeywordPaths(
                ACCESS_KEY,
                GetKeywordPaths(language, keywords),
                GetModelPath(language)
                );

            RunTestCase(
                "multiple_keywords_fr.wav",
                new List <int>()
            {
                0, 1, 0, 2
            });
        }
示例#6
0
        public void TestMultipleKeywordEs()
        {
            string        language = "es";
            List <string> keywords = new List <string>()
            {
                "emparedado",
                "leopardo",
                "manzana"
            };

            porcupine = Porcupine.FromKeywordPaths(
                ACCESS_KEY,
                GetKeywordPaths(language, keywords),
                GetModelPath(language)
                );

            RunTestCase(
                "multiple_keywords_es.wav",
                new List <int>()
            {
                0, 1, 2
            });
        }
示例#7
0
        public void TestMultipleKeywordDe()
        {
            string        language = "de";
            List <string> keywords = new List <string>()
            {
                "heuschrecke",
                "ananas",
                "leguan",
                "stachelschwein"
            };

            porcupine = Porcupine.FromKeywordPaths(
                ACCESS_KEY,
                GetKeywordPaths(language, keywords),
                GetModelPath(language)
                );

            RunTestCase(
                "multiple_keywords_de.wav",
                new List <int>()
            {
                1, 0, 2, 3
            });
        }
示例#8
0
        /// <summary>
        /// Reads through input file and, upon detecting the specified wake word(s), prints the detection timecode and the wake word.
        /// </summary>
        /// <param name="inputAudioPath">Required argument. Absolute path to input audio file.</param>
        /// <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="keywords">
        /// List of keywords (phrases) for detection. The list of available (default) keywords can be retrieved
        /// using `Porcupine.KEYWORDS`. If `keyword_paths` is set then this argument will be ignored.
        /// </param>
        public static void RunDemo(
            string inputAudioPath,
            string accessKey,
            string modelPath,
            List <string> keywordPaths,
            List <string> keywords,
            List <float> sensitivities)
        {
            Porcupine porcupine = null;

            try
            {
                // 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();

                using BinaryReader reader = new BinaryReader(File.Open(inputAudioPath, FileMode.Open));
                ValidateWavFile(reader, porcupine.SampleRate, 16, out short numChannels);

                // read audio and send frames to porcupine
                short[] porcupineFrame   = new short[porcupine.FrameLength];
                int     frameIndex       = 0;
                long    totalSamplesRead = 0;

                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
                while (reader.BaseStream.Position != reader.BaseStream.Length)
                {
                    totalSamplesRead++;
                    porcupineFrame[frameIndex++] = reader.ReadInt16();

                    if (frameIndex == porcupineFrame.Length)
                    {
                        int result = porcupine.Process(porcupineFrame);
                        if (result >= 0)
                        {
                            Console.WriteLine($"Detected {keywords[result]} at " +
                                              $"{Math.Round(totalSamplesRead / (double)porcupine.SampleRate, 2)} sec");
                        }
                        frameIndex = 0;
                    }

                    // skip right channel
                    if (numChannels == 2)
                    {
                        reader.ReadInt16();
                    }
                }
                stopWatch.Stop();
                double audioLen       = Math.Round(totalSamplesRead / (double)porcupine.SampleRate, 2);
                double realtimeFactor = Math.Round(audioLen / stopWatch.Elapsed.TotalSeconds, 2);
                Console.WriteLine($"Realtime factor: {realtimeFactor}x");
            }
            finally
            {
                porcupine?.Dispose();
            }
        }
示例#9
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();
                }
            }
        }