Exemplo n.º 1
0
    def play_file(self, wav_file: str, volume: int, play_type: int) -> None:
        """
        Play a wav file and send a SoundCommand to the simulator with the given file url. :param string wav_file: The
        sound file path :param int volume: The play volume, in percent of maximum volume :param play_type: The
        behavior of ``play_file`` once playback has been initiated :type play_type:
        ``SoundConnector.PLAY_WAIT_FOR_COMPLETE``, ``SoundConnector.PLAY_NO_WAIT_FOR_COMPLETE`` or
        ``SoundConnector.PLAY_LOOP`` :return: returns ``None``
        """
        wave_read = wave.open(wav_file, 'rb')
        duration = wave_read.getnframes() / wave_read.getframerate()
        wave_obj = sa.WaveObject.from_wave_read(wave_read)
        wave_read.close()

        command = SoundCommand(f'Playing file: ``{wav_file}``', duration, "file")
        self.client_socket.send_command(command)

        if self.play_actual_sound:
            try:
                play_obj = wave_obj.play()
                if play_type == SoundConnector.PLAY_NO_WAIT_FOR_COMPLETE:
                    return

                play_obj.wait_done()  # Wait until sound has finished playing
                if play_type == SoundConnector.PLAY_LOOP:
                    self.play_file(wav_file, volume, play_type)

            except SimpleaudioError:
                print("An error occurred when trying to play a file. Ignoring to keep simulation running")
    def speak(self, message: str) -> Any:
        """
        Create and send a SoundCommand to be send to the simulator with the given text to speak.
        """

        command = SoundCommand(message)
        return self.client_socket.send_sound_command(command)
Exemplo n.º 3
0
    def test_process_sound_command(self):
        robot_sim = create_robot_sim()

        frames_per_second = get_simulation_settings(
        )['exec_settings']['frames_per_second']
        frames = int(5 * frames_per_second)

        message_processor = MessageProcessor(0, robot_sim)
        message_processor.process_sound_command(
            SoundCommand('A test is running at the moment!', 5, 'speak'))

        for i in range(frames - 1):
            soundJob = [
                i[1] for i in robot_sim.next_actuator_jobs()
                if i[0] == (0, 'speaker')
            ][0]
            self.assertIsNotNone(soundJob)

        message = [
            i[1] for i in robot_sim.next_actuator_jobs()
            if i[0] == (0, 'speaker')
        ][0]
        self.assertEqual(message, 'A test is \nrunning at\n the momen\nt!')

        jobs = robot_sim.next_actuator_jobs()
        self.assertEqual((jobs[0][1], jobs[1][1], jobs[2][1], jobs[3][1]),
                         (None, None, None, None))
    def _process_sound_command(self, d: dict) -> Any:
        """
        Deserialize the given dictionary into a SoundCommand and send it to the MessageProcessor.
        :param d: to process.
        """

        command = SoundCommand(d['message'])
        self.message_processor.process_sound_command(command)

        return None
Exemplo n.º 5
0
    def test_process_sound_command(self):
        robot_state = RobotState()

        frames_per_second = get_config().get_data()['exec_settings']['frames_per_second']
        frames = int(round((32 / 2.5) * frames_per_second))

        message_processor = MessageProcessor('left_brick', robot_state)
        message_processor.process_sound_command(SoundCommand('A test is running at the moment!'))

        for i in range(frames - 1):
            self.assertIsNotNone(robot_state.next_sound_job())

        message = robot_state.next_sound_job()
        self.assertEqual(message, 'A test is \nrunning at\n the momen\nt!')
        self.assertIsNone(robot_state.next_sound_job())
Exemplo n.º 6
0
    def _tts(self, text, espeak_opts, desired_volume: int) -> None:
        duration = (len(text.split()) / 200) * 60  # based on 200 words per minute as described in the tts docs

        command = SoundCommand(f'Saying: ``{text}``', duration, 'speak')
        self.client_socket.send_command(command)

        if self.play_actual_sound:
            try:
                engine = tts.init()
                engine.setProperty('volume', desired_volume / 100.0)
                engine.say(text)
                engine.runAndWait()
            except OSError as e:
                print(textwrap.dedent('''
                                        Warning, speak could not be executed. Speak makes use of the pyttsx3 library. This requires:
                                        - Windows users to install pypiwin32, installed by: pip install pypiwin32
                                        - Linux users to install espeak, installed by: sudo apt-get install espeak
                                        - Mac users do not need to install any additional software.
                                        '''))
                print('original exception', e)
            except RuntimeError as e:
                print("Warning: 'speak' called before last text-to-speech was handled")
                print(e)
Exemplo n.º 7
0
    def _linux_beep(self, tone_sequence) -> Any:
        for tone in tone_sequence:
            frequency = tone.get('frequency', 440.0)  # defaults to 440 hz which is the default of beep
            duration = tone.get('duration', 200) / 1000.0  # defaults to 200 ms which is the default of beep
            delay = tone.get('delay', 100)  # defaults to 100 ms which is the default of beep

            fs = 44100
            t = np.linspace(0, duration, int(duration * fs), False)
            note = np.sin(frequency * t * 2 * np.pi)

            audio = note * (2 ** 15 - 1) / np.max(np.abs(note))
            audio = audio.astype(np.int16)

            command = SoundCommand("Playing note with frequency: " + str(frequency), duration, "note")
            self.client_socket.send_command(command)

            if self.play_actual_sound:
                try:
                    # Start playback
                    play_obj = sa.play_buffer(audio, 1, 2, fs)
                    play_obj.wait_done()
                except SimpleaudioError:
                    print("An error occurred when trying to play a file. Ignoring to keep simulation running")
            sleep(delay / 1000.0)