In Exercise 1 of Previous, the "CPU usage" of a Windows PC was regarded as a sensor value, and "sense" components were created. This time, we will practice the opposite direction of data flow, that is, the components of the "control" system. As in the last time, I don't know what kind of device you have, so I will explain "playing audio files" and "text speech" that do not require any device. That said, with Limotte, both features can be achieved by writing just a few lines of Python code.
Creates a function that sounds a chime-like sound effect when the switch is turned on. Let's prepare an "mp3 file" that contains the sound effect. Please refer to the following sites where you can download free sound sources. Sound Effect Lab Music-Note.jp DOVA-SYNDROME In this article, we have prepared "Bell.mp3" which sounds like "ping pong!". In addition, in this exercise, we will create a speech function using the voice function of Windows 10. Go to Windows "Settings", then "Time and Language", "Voice Recognition" menu, and use the "Select Voice" drop-down menu and "Voice Preview" button as shown in the screen below. Let's set the audio.
As in Exercise 1, create an app as follows.
First of all, when the switch is turned on, a function to play an audio file (mp3) is created at the station. First, create a new component. Set "I / O type" to "On / Off control". Next, select the "Layout" tab in the "Usage Page" tab. The default display items for "on / off control" have already been placed. You can switch to your favorite switch by clicking the expression icon as shown below. Now, in order to implement to play mp3 files when this switch is turned on, in the "Programs" tab in the "Configuration" tab, open the menu and select "Add existing file". Add the prepared mp3 file. Next, on the "Code" tab, add the following two lines to the control (self, data) function in the "output_control.py" file. To do. if data['value'] is True: self._sys.sound_on_station('Bell.mp3') That's it!
The function control (self, data) is an interface for telling the Python program what you have operated on the usage page. The function sound_on_station (file_name) is an API that allows a Python program to require the Limotte platform to play an audio file on the station's computer. By the way, if you use the function sound_in_browser (file_name), the sound will be played from all browsers connected to the station.
To start the app, press the "Save" button at the top of the screen, select the "Layout" tab in the "Usage Page" tab, and then press the "Start" button of the app. Every time the switch is turned from off to on, the station will play the specified sound effect.
Next, let's create a speech function. Add another component in the same app. Set "I / O type" to "Text control". When the "Layout" tab in the "Usage Page" tab is selected, one item is added in the expression format "Display and input text". In the following, the width is widened so that you can enter longer text, but you may implement it by other input methods by clicking the expression format selection icon. Finally, add the following line to the control (self, data) function of "output_control.py" as the source code. self._sys.speak_on_station(data['value'], 'ja') The function speak_on_station (text, lang) allows a Python program to send a given text to the Limotte platform in a specified language (eg,'ja' or'en-us') on the station's computer. An API that requires output. By the way, if you use the function speak_in_browser (text, lang), the sound will be output from all browsers connected to the station.
To start the app, press the "Save" button at the top of the screen, select the "Layout" tab in the "Usage Page" tab, and then press the "Start" button of the app. Enter the text you want to speak and the station will output voice.
In this exercise, we experienced the components of the "control" system. I want to learn that user operations on the usage page are communicated to Python programs by the control (self, data) function. Next time, we will deal with media (video and audio), which is one of the major features of the Limotte platform, and perform "face detection" on the images acquired from the camera.
Recommended Posts