Beispiel #1
0
        /// <summary>
        /// Loads Porcupine's shared library and creates an instance of wake word detection object.
        /// </summary>
        /// <param name="libraryPath">Absolute path to Porcupine's shared library.</param>
        /// <param name="modelFilePath">Absolute path to file containing model parameters.</param>
        /// <param name="keywordFilePath">Absolute path to keyword file containing hyper-parameters. If not present then 'keyword_file_paths' will be used.</param>
        /// <param name="sensitivity">Sensitivity parameter. A higher sensitivity value lowers miss rate at the cost of increased false alarm rate.
        /// For more information regarding this parameter refer to 'include/pv_porcupine.h'.
        /// If not present then 'sensitivities' is used.</param>
        /// <param name="keywordFilePaths"> List of absolute paths to keyword files. Intended to be used for multiple keyword scenario.
        /// This parameter is used only when 'keyword_file_path' is not set.</param>
        /// <param name="sensitivities"> List of sensitivity parameters. Intended to be used for multiple keyword scenario.
        /// This parameter is used only when 'sensitivity' is not set.</param>
        public Porcupine(string modelFilePath, string keywordFilePath = null,
                         float?sensitivity = null, IEnumerable <string> keywordFilePaths = null,
                         IEnumerable <float> sensitivities = null)
        {
            if (!File.Exists(LIBRARY_NAME + _extension))
            {
                throw new Exception($"the {LIBRARY_NAME} cannot be found.\nThis should be in the same folder as this or on a known path.");
            }
            if (keywordFilePath == null)
            {
                if (keywordFilePaths == null)
                {
                    throw new ArgumentNullException(nameof(keywordFilePaths));
                }

                if (sensitivities == null)
                {
                    throw new ArgumentNullException(nameof(sensitivities));
                }

                Status = pv_porcupine_multiple_keywords_init(modelFilePath, keywordFilePaths.Count(), keywordFilePaths.ToArray(), sensitivities.ToArray(), out _libraryPointer);
            }
            else
            {
                if (sensitivity == null)
                {
                    throw new ArgumentNullException(nameof(sensitivity));
                }

                Status = pv_porcupine_init(modelFilePath, keywordFilePath, sensitivity.Value, out _libraryPointer);
            }
        }
Beispiel #2
0
        public void MultipleKeywords()
        {
            var modelFilePath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "porcupine_params.pv"));

            Assert.IsTrue(File.Exists(modelFilePath), $"File.Exists(modelFilePath) --> {modelFilePath}");
            paths.ForEach(keywordFilePath => Assert.IsTrue(File.Exists(keywordFilePath), $"File.Exists(keywordFilePath) --> {keywordFilePath}"));

            Porcupine p = new Porcupine(modelFilePath, keywordFilePaths: paths, sensitivities: senses);

            Assert.AreEqual(PicoVoiceStatus.SUCCESS, p.Status, "the status of the creation of the recognition system has failed");
            WAVFile file = new WAVFile();

            file.Open("multiple_keywords.wav", WAVFile.WAVFileMode.READ);
            Assert.AreEqual(p.SampleRate(), file.AudioFormat.SampleRateHz, "The samplerate is not equal!!!");
            List <short> data = new List <short>();

            while (file.NumSamplesRemaining > 0)
            {
                data.Add(BitConverter.ToInt16(file.GetNextSample_ByteArray()));
            }
            int framecount = (int)Math.Floor((decimal)(data.Count / p.FrameLength()));
            var results    = new List <int>();

            for (int i = 0; i < framecount; i++)
            {
                int             start  = i * p.FrameLength();
                int             count  = p.FrameLength();
                List <short>    frame  = data.GetRange(start, count);
                PicoVoiceStatus status = p.ProcessMultipleKeywords(frame.ToArray(), out int result);
                if (result >= 0)
                {
                    results.Add(result);
                }
                Assert.AreEqual(PicoVoiceStatus.SUCCESS, status, "The status is not as expected");
            }

            var requiredRes = new[] { 8, 0, 1, 2, 3, 4, 5, 7, 8, 9 };

            Assert.AreEqual(requiredRes.Length, results.Count, $"expected results length are different expected {requiredRes.Length} got {results.Count}");
            for (var i = 0; i < results.Count; i++)
            {
                Assert.AreEqual(requiredRes[i], results[i], $"The result is not as expected, expected {requiredRes[i]} got {results[i]}");
            }

            p.Dispose();
        }