The EV3 can also play sounds and music.
EV3 还可以播放声音和音乐。
We start with the simple program to say hello when pressing the center button.
我们从简单的程序开始,在按下中心按钮时打个招呼 。
Next we create a program which counts to three.
接下来,我们创建一个计数为 3 的程序。
There is another block called start sound. What is the difference?
还有另一个名为 start sound 的块。有什么区别?
Try to count like this.
试着像这样数数。
This block does not wait for the sound to finish. It starts immediately the next block and replaces the previous sound which barely has started with the new sound. Thus this program only plays the last sound (three).
此块不会等待声音完成。它立即开始下一个块,并替换之前几乎没有以新声音开始的声音。因此,该程序只播放最后一个声音 (3)。
To give the program time for the sound, we have to insert a wait block. This allows to play a sound precisely every second.
为了给程序提供声音的时间,我们必须插入一个等待块。这允许每秒精确地播放声音。
The stop all sounds block stops the currently running sound. If you press it while running one, two, three, it stops immeditly the current sound and plays the next one in the sequence.
stop all sounds 块会停止当前正在运行的声音。如果在运行 one、two、three 时按下它,它会立即停止当前声音并播放序列中的下一个声音。
Download: sound1.lmsp
下载: 声音 1.lmsp
With a loop we can repeat a sound a given number of times. For example we can repeat the sound LEGO three times.
通过 Loop,我们可以将声音重复给定次数。例如,我们可以重复声音 LEGO 三次。
We can also repeat as long a button is pressed.
只要按下一个按钮,我们也可以重复。
Finally it’s a bit more complicated to start repetition with a first button press and stop repetition with a second button press.
最后,从第一次按下按钮开始重复,通过第二次按下按钮停止重复稍微复杂一些。
We need to define a variable repeating which we initialize to 0. Then we enter a forever loop. Inside the loop we have an if block.
我们需要定义一个变量 repeating,并将其初始化为 0。然后我们进入一个永远的循环。在循环中,我们有一个 if 块。
If repeating = 1 then we play the sound.
如果 repeating = 1 则播放声音。
Next we program the button to toggle the variable repeating between the values 0 and 1. For our feedback, we also print this value to the screen.
接下来,我们对按钮进行编程,以在值 0 和 1 之间切换变量的重复 。为了得到我们的反馈,我们还将此值打印到屏幕上。
Download: sound2.lmsp
下载: 声音 2.lmsp
The start playing beep block starts a beep. With a keybord we can chose the pitch.
开始播放蜂鸣声块会发出蜂鸣声。使用 keybord,我们可以选择音高。
The sound will be playing continously. We can use a second button to stop the sound.
声音将持续播放。我们可以使用第二个按钮来停止声音。
There are 3 different ways to play a beep with only 1 button:
只有 3 个按钮即可播放哔哔声,有 3 种不同的方法:
The play bee function has two arguments:
play bee 函数有两个参数:
It allows to give a duration to the sound. In the followign example we play the sound for 0.2 seconds.
它允许为声音提供持续时间。在下面的示例中,我们播放声音 0.2 秒。
Buttons have two associated events:
按钮有两个关联的事件:
We can use these two events to program a button which plays a sound only while the button is being pressed.
我们可以使用这两个事件来编程一个按钮,该按钮仅在按下按钮时播放声音。
The last way is an on/off toggle button. This method needs a variable sound which is going to store the state of the sound:
最后一种方法是开/关切换按钮。此方法需要一个变量 sound,它将存储 sound 的状态:
We initialize the variable sound to 0 (off) at the start.
我们在开始时将变量 sound 初始化为 0 (off)。
When the button is pressed, we toggle the variable sound by using the expression sound = 1-sound
按下按钮时,我们切换可变声音 通过使用表达式 Sound = 1-Sound
Then we enter an if-else block:
然后我们输入一个 if-else 块:
Download: sound3.lmsp
下载: 声音 3.lmsp
We can control the volume and pitch of of a sound. First we start by creating two variables called intensity and pitch.
我们可以控制声音的音量和音高。首先,我们首先创建两个变量,分别称为 intensity 和 pitch。
We set intensity to 50 and pitch to 60. Then we enter a loop where we first display these two values to teh screen. Then we produce a short beep repeating every 0.5 second.
我们将 Intensity 设置为 50,将 pitch 设置为 60。然后我们进入一个循环,首先在屏幕上显示这两个值。然后我们发出每 0.5 秒重复一次的短促哔哔声。
Now we can use the 4 buttons to change the two variables pitch and intensity.
现在我们可以使用 4 个按钮来更改两个变量 pitch 和 intensity。
The pitch has been initalized to the value 60. These numbers correspond to:
音高已初始化为值 60。这些数字对应于:
60 C 61 C# 62 D 63 D# 64 E
We can use the rotary encoder to change pitch. In get the pitch in half-tone steps we:
我们可以使用旋转编码器来改变螺距。在 Get the pitch in half-tone steps 中,我们:
We can play beeps in sequence to play a melody. For example to play the music of this famous French folk song Frère Jacques
我们可以按顺序播放哔哔声来播放旋律。例如,播放这首著名的法国民歌 Frère Jacques 的音乐
It is quite straightforward to program the first measure. If we want 120 beats per minute (120 bpm) each beat must be 0.5 seconds.
对第一个度量进行编程非常简单。如果我们希望每分钟 120 次跳动 (120 bpm),则每次跳动必须为 0.5 秒。
If we want to change the tempo, then it would be better to code the duration of the beep with a variable. We create the variable t (time) and initialize it to 0.5 seconds. Also, we repeat the first 4 notes in a loop.
如果我们想改变速度,那么最好用变量来编码哔哔声的持续时间。我们创建变量 t (时间) 并将其初始化为 0.5 秒。此外,我们在循环中重复前 4 个音符。
With the up/down buttons we can select the tempo.
使用向上/向下按钮,我们可以选择速度。
Not all the notes have the same duration. The white ones are twice as long. We use the expression t*2 as the duration.
并非所有音符都具有相同的持续时间。白色的长是原来的两倍。我们使用表达式 t*2 作为持续时间。
On the other hand some other notes only have half the lenght. We use the expression t/2 for their duration.
另一方面,其他一些音符的长度只有一半。我们使用表达式 t/2 来表示它们的持续时间。
And this is the final part.
这是最后一部分。
Download: music.lmsp
下载: 音乐.lmsp