Example #1
0
class AudioConsumerTest(unittest.TestCase):
    """
    AudioConsumerTest
    """
    def setUp(self):
        self.loop = RecognizerLoop()
        self.queue = Queue()
        self.recognizer = MockRecognizer()
        self.consumer = AudioConsumer(self.loop.state, self.queue, self.loop,
                                      MycroftSTT(),
                                      self.loop.wakeup_recognizer,
                                      self.loop.wakeword_recognizer)

    def __create_sample_from_test_file(self, sample_name):
        root_dir = dirname(dirname(dirname(__file__)))
        filename = join(root_dir, 'unittests', 'client', 'data',
                        sample_name + '.wav')
        wavfile = WavFile(filename)
        with wavfile as source:
            return AudioData(source.stream.read(), wavfile.SAMPLE_RATE,
                             wavfile.SAMPLE_WIDTH)

    def test_word_extraction(self):
        """
        This is intended to test the extraction of the word: ``mycroft``.
        The values for ``ideal_begin`` and ``ideal_end`` were found using an
        audio tool like Audacity and they represent a sample value position of
        the audio. ``tolerance`` is an acceptable margin error for the distance
        between the ideal and actual values found by the ``WordExtractor``
        """
        # TODO: implement WordExtractor test without relying on the listener
        return

        audio = self.__create_sample_from_test_file('weather_mycroft')
        self.queue.put(audio)
        tolerance = 4000
        ideal_begin = 70000
        ideal_end = 92000

        monitor = {}
        self.recognizer.set_transcriptions(["what's the weather next week"])

        def wakeword_callback(message):
            monitor['pos_begin'] = message.get('pos_begin')
            monitor['pos_end'] = message.get('pos_end')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.read()

        actual_begin = monitor.get('pos_begin')
        self.assertIsNotNone(actual_begin)
        diff = abs(actual_begin - ideal_begin)
        self.assertTrue(diff <= tolerance,
                        str(diff) + " is not less than " + str(tolerance))

        actual_end = monitor.get('pos_end')
        self.assertIsNotNone(actual_end)
        diff = abs(actual_end - ideal_end)
        self.assertTrue(diff <= tolerance,
                        str(diff) + " is not less than " + str(tolerance))

    @unittest.skip('Disabled while unittests are brought upto date')
    def test_wakeword_in_beginning(self):
        self.queue.put(self.__create_sample_from_test_file('weather_mycroft'))
        self.recognizer.set_transcriptions(["what's the weather next week"])
        monitor = {}

        def callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', callback)
        self.consumer.read()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("what's the weather next week", utterances[0])

    @unittest.skip('Disabled while unittests are brought upto date')
    def test_wakeword(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.recognizer.set_transcriptions(["silence"])
        monitor = {}

        def callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', callback)
        self.consumer.read()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("silence", utterances[0])

    def test_ignore_wakeword_when_sleeping(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.recognizer.set_transcriptions(["not detected"])
        self.loop.sleep()
        monitor = {}

        def wakeword_callback(message):
            monitor['wakeword'] = message.get('utterance')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.read()
        self.assertIsNone(monitor.get('wakeword'))
        self.assertTrue(self.loop.state.sleeping)

    def test_wakeup(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft_wakeup'))
        self.loop.sleep()
        self.consumer.read()
        self.assertFalse(self.loop.state.sleeping)

    @unittest.skip('Disabled while unittests are brought upto date')
    def test_stop(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.consumer.read()

        self.queue.put(self.__create_sample_from_test_file('stop'))
        self.recognizer.set_transcriptions(["stop"])
        monitor = {}

        def utterance_callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', utterance_callback)
        self.consumer.read()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("stop", utterances[0])

    @unittest.skip('Disabled while unittests are brought upto date')
    def test_record(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.consumer.read()

        self.queue.put(self.__create_sample_from_test_file('record'))
        self.recognizer.set_transcriptions(["record"])
        monitor = {}

        def utterance_callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', utterance_callback)
        self.consumer.read()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("record", utterances[0])
class AudioConsumerTest(unittest.TestCase):
    """
    AudioConsumerTest
    """
    def setUp(self):
        self.loop = RecognizerLoop()
        self.queue = Queue()
        self.recognizer = MockRecognizer()

        self.consumer = AudioConsumer(
            self.loop.state,
            self.queue,
            self.loop,
            self.loop.wakeup_recognizer,
            self.loop.ww_recognizer,
            RemoteRecognizerWrapperFactory.wrap_recognizer(self.recognizer, 'google'),
            self.loop.wakeup_prefixes,
            self.loop.wakeup_words)

    def __create_sample_from_test_file(self, sample_name):
        root_dir = dirname(dirname(dirname(__file__)))
        filename = join(root_dir, 'test', 'client', 'data', sample_name + '.wav')
        wavfile = WavFile(filename)
        with wavfile as source:
            return AudioData(source.stream.read(), wavfile.SAMPLE_RATE, wavfile.SAMPLE_WIDTH)

    def test_audio_pos_front_back(self):
        audio = self.__create_sample_from_test_file('mycroft_in_utterance')
        self.queue.put(audio)
        TRUE_POS_BEGIN = 69857 + int(WakewordExtractor.TRIM_SECONDS * audio.sample_rate * audio.sample_width)
        TRUE_POS_END = 89138 - int(WakewordExtractor.TRIM_SECONDS * audio.sample_rate * audio.sample_width)

        TOLERANCE_RANGE_FRAMES = WakewordExtractor.MAX_ERROR_SECONDS * audio.sample_rate * audio.sample_width

        monitor = {}
        self.recognizer.set_transcriptions(["what's the weather next week", ""])

        def wakeword_callback(message):
            monitor['pos_begin'] = message.get('pos_begin')
            monitor['pos_end'] = message.get('pos_end')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.try_consume_audio()

        pos_begin = monitor.get('pos_begin')
        self.assertIsNotNone(pos_begin)
        diff = abs(pos_begin - TRUE_POS_BEGIN)
        self.assertTrue(diff <= TOLERANCE_RANGE_FRAMES, str(diff) + " is not less than " + str(TOLERANCE_RANGE_FRAMES))

        pos_end = monitor.get('pos_end')
        self.assertIsNotNone(pos_end)
        diff = abs(pos_end - TRUE_POS_END)
        self.assertTrue(diff <= TOLERANCE_RANGE_FRAMES, str(diff) + " is not less than " + str(TOLERANCE_RANGE_FRAMES))

    def test_wakeword_in_beginning(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        monitor = {}
        self.recognizer.set_transcriptions(["what's the weather next week", ""])

        def callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', callback)
        self.consumer.try_consume_audio()
        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("what's the weather next week", utterances[0])

    def test_wakeword_in_phrase(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        monitor = {}
        self.recognizer.set_transcriptions(["he can do other stuff too", "what's the weather in cincinnati"])

        def callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', callback)
        self.consumer.try_consume_audio()
        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 2)
        self.assertEquals("he can do other stuff too", utterances[0])
        self.assertEquals("what's the weather in cincinnati", utterances[1])

    def test_call_and_response(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        monitor = {}
        self.recognizer.set_transcriptions(["mycroft",""])

        def wakeword_callback(message):
            monitor['wakeword'] = message.get('utterance')

        def utterance_callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.try_consume_audio()

        self.assertIsNotNone(monitor.get('wakeword'))

        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.recognizer.set_transcriptions(["what's the weather next week", ""])
        self.loop.once('recognizer_loop:utterance', utterance_callback)
        self.consumer.try_consume_audio()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("what's the weather next week", utterances[0])

    def test_ignore_wakeword_when_sleeping(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.loop.sleep()
        monitor = {}
        self.recognizer.set_transcriptions(["", ""])

        def wakeword_callback(message):
            monitor['wakeword'] = message.get('utterance')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.try_consume_audio()

        self.assertIsNone(monitor.get('wakeword'))
        self.assertTrue(self.loop.state.sleeping)
class AudioConsumerTest(unittest.TestCase):
    """
    AudioConsumerTest
    """

    def setUp(self):
        self.loop = RecognizerLoop()
        self.queue = Queue()
        self.recognizer = MockRecognizer()

        self.consumer = AudioConsumer(
            self.loop.state,
            self.queue,
            self.loop,
            self.loop.wakeup_recognizer,
            self.loop.mycroft_recognizer,
            RemoteRecognizerWrapperFactory.wrap_recognizer(
                self.recognizer, 'google'))

    def __create_sample_from_test_file(self, sample_name):
        root_dir = dirname(dirname(dirname(__file__)))
        filename = join(
            root_dir, 'test', 'client', 'data', sample_name + '.wav')
        wavfile = WavFile(filename)
        with wavfile as source:
            return AudioData(
                source.stream.read(), wavfile.SAMPLE_RATE,
                wavfile.SAMPLE_WIDTH)

    def test_word_extraction(self):
        """
        This is intended to test the extraction of the word: ``mycroft``.
        The values for ``ideal_begin`` and ``ideal_end`` were found using an
        audio tool like Audacity and they represent a sample value position of
        the audio. ``tolerance`` is an acceptable margin error for the distance
        between the ideal and actual values found by the ``WordExtractor``
        """

        # TODO: implement WordExtractor test without relying on the listener
        return

        audio = self.__create_sample_from_test_file('weather_mycroft')
        self.queue.put(audio)
        tolerance = 4000
        ideal_begin = 70000
        ideal_end = 92000

        monitor = {}
        self.recognizer.set_transcriptions(["what's the weather next week"])

        def wakeword_callback(message):
            monitor['pos_begin'] = message.get('pos_begin')
            monitor['pos_end'] = message.get('pos_end')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.read_audio()

        actual_begin = monitor.get('pos_begin')
        self.assertIsNotNone(actual_begin)
        diff = abs(actual_begin - ideal_begin)
        self.assertTrue(
            diff <= tolerance,
            str(diff) + " is not less than " + str(tolerance))

        actual_end = monitor.get('pos_end')
        self.assertIsNotNone(actual_end)
        diff = abs(actual_end - ideal_end)
        self.assertTrue(
            diff <= tolerance,
            str(diff) + " is not less than " + str(tolerance))

    def test_wakeword_in_beginning(self):
        self.queue.put(self.__create_sample_from_test_file('weather_mycroft'))
        self.recognizer.set_transcriptions(["what's the weather next week"])
        monitor = {}

        def callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', callback)
        self.consumer.read_audio()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("what's the weather next week", utterances[0])

    def test_wakeword(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.recognizer.set_transcriptions(["silence"])
        monitor = {}

        def callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', callback)
        self.consumer.read_audio()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("silence", utterances[0])

    def test_ignore_wakeword_when_sleeping(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.recognizer.set_transcriptions(["not detected"])
        self.loop.sleep()
        monitor = {}

        def wakeword_callback(message):
            monitor['wakeword'] = message.get('utterance')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.read_audio()
        self.assertIsNone(monitor.get('wakeword'))
        self.assertTrue(self.loop.state.sleeping)

    def test_wakeup(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft_wakeup'))
        self.loop.sleep()
        self.consumer.read_audio()
        self.assertFalse(self.loop.state.sleeping)

    def test_stop(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.consumer.read_audio()

        self.queue.put(self.__create_sample_from_test_file('stop'))
        self.recognizer.set_transcriptions(["stop"])
        monitor = {}

        def utterance_callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', utterance_callback)
        self.consumer.read_audio()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("stop", utterances[0])

    def test_record(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.consumer.read_audio()

        self.queue.put(self.__create_sample_from_test_file('record'))
        self.recognizer.set_transcriptions(["record"])
        monitor = {}

        def utterance_callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', utterance_callback)
        self.consumer.read_audio()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("record", utterances[0])
Example #4
0
class AudioConsumerTest(unittest.TestCase):
    """
    AudioConsumerTest
    """
    def setUp(self):
        self.loop = RecognizerLoop()
        self.queue = Queue()
        self.recognizer = MockRecognizer()

        self.consumer = AudioConsumer(
            self.loop.state, self.queue, self.loop,
            self.loop.wakeup_recognizer, self.loop.ww_recognizer,
            RemoteRecognizerWrapperFactory.wrap_recognizer(
                self.recognizer,
                'google'), self.loop.wakeup_prefixes, self.loop.wakeup_words)

    def __create_sample_from_test_file(self, sample_name):
        root_dir = dirname(dirname(dirname(__file__)))
        filename = join(root_dir, 'test', 'client', 'data',
                        sample_name + '.wav')
        wavfile = WavFile(filename)
        with wavfile as source:
            return AudioData(source.stream.read(), wavfile.SAMPLE_RATE,
                             wavfile.SAMPLE_WIDTH)

    def test_audio_pos_front_back(self):
        audio = self.__create_sample_from_test_file('mycroft_in_utterance')
        self.queue.put(audio)
        TRUE_POS_BEGIN = 69857 + int(WakewordExtractor.TRIM_SECONDS *
                                     audio.sample_rate * audio.sample_width)
        TRUE_POS_END = 89138 - int(WakewordExtractor.TRIM_SECONDS *
                                   audio.sample_rate * audio.sample_width)

        TOLERANCE_RANGE_FRAMES = (WakewordExtractor.MAX_ERROR_SECONDS *
                                  audio.sample_rate * audio.sample_width)

        monitor = {}
        self.recognizer.set_transcriptions(
            ["what's the weather next week", ""])

        def wakeword_callback(message):
            monitor['pos_begin'] = message.get('pos_begin')
            monitor['pos_end'] = message.get('pos_end')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.try_consume_audio()

        pos_begin = monitor.get('pos_begin')
        self.assertIsNotNone(pos_begin)
        diff = abs(pos_begin - TRUE_POS_BEGIN)
        self.assertTrue(
            diff <= TOLERANCE_RANGE_FRAMES,
            str(diff) + " is not less than " + str(TOLERANCE_RANGE_FRAMES))

        pos_end = monitor.get('pos_end')
        self.assertIsNotNone(pos_end)
        diff = abs(pos_end - TRUE_POS_END)
        self.assertTrue(
            diff <= TOLERANCE_RANGE_FRAMES,
            str(diff) + " is not less than " + str(TOLERANCE_RANGE_FRAMES))

    def test_wakeword_in_beginning(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        monitor = {}
        self.recognizer.set_transcriptions(
            ["what's the weather next week", ""])

        def callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', callback)
        self.consumer.try_consume_audio()
        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("what's the weather next week", utterances[0])

    def test_wakeword_in_phrase(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        monitor = {}
        self.recognizer.set_transcriptions(
            ["he can do other stuff too", "what's the weather in cincinnati"])

        def callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:utterance', callback)
        self.consumer.try_consume_audio()
        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 2)
        self.assertEquals("he can do other stuff too", utterances[0])
        self.assertEquals("what's the weather in cincinnati", utterances[1])

    def test_call_and_response(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        monitor = {}
        self.recognizer.set_transcriptions(["mycroft", ""])

        def wakeword_callback(message):
            monitor['wakeword'] = message.get('utterance')

        def utterance_callback(message):
            monitor['utterances'] = message.get('utterances')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.try_consume_audio()

        self.assertIsNotNone(monitor.get('wakeword'))

        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.recognizer.set_transcriptions(
            ["what's the weather next week", ""])
        self.loop.once('recognizer_loop:utterance', utterance_callback)
        self.consumer.try_consume_audio()

        utterances = monitor.get('utterances')
        self.assertIsNotNone(utterances)
        self.assertTrue(len(utterances) == 1)
        self.assertEquals("what's the weather next week", utterances[0])

    def test_ignore_wakeword_when_sleeping(self):
        self.queue.put(self.__create_sample_from_test_file('mycroft'))
        self.loop.sleep()
        monitor = {}
        self.recognizer.set_transcriptions(["", ""])

        def wakeword_callback(message):
            monitor['wakeword'] = message.get('utterance')

        self.loop.once('recognizer_loop:wakeword', wakeword_callback)
        self.consumer.try_consume_audio()

        self.assertIsNone(monitor.get('wakeword'))
        self.assertTrue(self.loop.state.sleeping)