- Documentation
- Tutorials
- Using the EV3 Speaker
- Introduction
- Tone Mode
- beep command
- tone sysfs Attribute
- PCM Playback Mode
- Volume control
- amixer Command
- alsamixer Command
- More Fun!
- Text-To-Speech
- MP3 player
Introduction
The EV3 uses pulse-width modulation (PWM) to drive an amplifier that produces thesounds that you hear through the speaker. It has two modes of operation. The first,we call Tone mode (aka Beep mode). In Tone mode, the PWM is set to afrequency in the audible range. The sound you hear is a square wave.The second mode is called PCM Playback mode. In this mode, the PWM is operatedat a higher frequency. The high frequencies are (mostly) filtered out and we areable to reproduce a recorded sound.
Tone Mode
There are a couple of ways to control the speaker in Tone mode. Also worth anhonorable mention: if you plug a USB keyboard into the EV3 and pressCTRL+G, it will beep!
beep command
The beep man page has all you need to know.
Examples:
$ beep # 440 Hz for 200 ms$ beep -f 200 -l 500 # 220 Hz for 500 ms$ beep -f 262 -l 180 -d 20 -r 2 \-n -f 392 -l 180 -d 20 -r 2 \-n -f 440 -l 180 -d 20 -r 2 \-n -f 392 -l 380 -d 20 \-n -f 349 -l 180 -d 20 -r 2 \-n -f 330 -l 180 -d 20 -r 2 \-n -f 294 -l 180 -d 20 -r 2 \-n -f 262 -l 400 # twinkle twinkle little star
tone sysfs Attribute
TODO: using the tone
attribute is not really recommended. This needs to beupdated with an example on how to use the Linux input subsystem instead.
We have also provided a way to play tones directly using the sysfs attribute/sys/devices/platform/snd-legoev3/tone
.
Example:
$ cd /sys/devices/platform/snd-legoev3$ echo 1000 10000 > tone # play a 1 KHz tone for 10 seconds$ echo 440 > tone # play a 440 Hz tone (endless mode)$ echo 0 > tone # stop the tone
PCM Playback Mode
PCM playback is implemented as an ALSA driver. This means that you can use justabout any Linux program out there that can play sound. We have found the bestcompromise between sound quality and CPU usage is to use a sample rate 22050 Hz.16000 Hz sounds pretty good too. Of course the speaker is so small, the soundquality is just not going to be “high quality” no matter what.
Example: Playing back a .wav file using aplay
.
$ aplay my-file.wavPlaying WAVE 'my-file.wav' : Signed 16 bit Little Endian, Rate 22050 Hz, Mono
You must be a member of the audio
group in order to use the sound driver.
Volume control
Again, there are a number of ways to do this. Currently, there is only a mastervolume control that controls both Tone mode and PCM Playback mode.
amixer Command
Documentation:
Example:
$ amixer get Playback,0 # get current volumeSimple mixer control 'Playback',0 Capabilities: volume volume-joined penum Playback channels: Mono Capture channels: Mono Limits: 0 - 256 Mono: 255 [100%]$ amixer set Playback,0 50% # set volume to 50%Simple mixer control 'Playback',0 Capabilities: volume volume-joined penum Playback channels: Mono Capture channels: Mono Limits: 0 - 256 Mono: 128 [50%]
alsamixer Command
Graphical volume controls.
┌───────────────────────────── AlsaMixer v1.0.25 ──────────────────────────────┐│ Card: LEGO Mindstorms EV3 speaker F1: Help ││ Chip: F2: System information ││ View: F3:[Playback] F4: Capture F5: All F6: Select sound card ││ Item: Playback Esc: Exit ││ ││ ┌──┐ ││ │ │ ││ │ │ ││ │ │ ││ │ │ ││ │ │ ││ │ │ ││ │ │ ││ │▒▒│ ││ │▒▒│ ││ │▒▒│ ││ │▒▒│ ││ │▒▒│ ││ └──┘ ││ 40 ││ <Playback> ││ │└──────────────────────────────────────────────────────────────────────────────┘
More Fun!
Text-To-Speech
$ espeak "hello, I am an EV3.> I like to talk because I am a robot.> Did you know that robots like to make sounds?> Beep. Boop. Dit. Dit. Meep.> I am just such a chatterbox." --stdout | aplayPlaying WAVE 'stdin' : Signed 16 bit Little Endian, Rate 22050 Hz, Mono
Tip
Add a function like this to your ~/.bashrc
file.
speak(){ espeak -a 200 -s 130 -v la --stdout "$@" | aplay --quiet}
Then you can simply use:
$ speak "Thanks to GeekDude for this idea!" # @G33kDude
MP3 player
$ sudo apt-get update$ sudo apt-get install mpg123 # or mpg321...$ mpg123 /home/ev3dev/Music/01-The\ Jumpin\'\ Jive.mp3 High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3version 1.14.4; written and copyright by Michael Hipp and othersfree software (LGPL/GPL) without any warranty but with best wishesDirectory: /home/ev3dev/Music/Playing MPEG stream 1 of 1: 01-The Jumpin' Jive.mp3 ...MPEG 1.0 layer III, 128 kbit/s, 44100 Hz joint-stereoTitle: The Jumpin' Jive Artist: Cab CallowayComment: Album: Minnie the Moocher [Universal]Year: 1989 Genre: Swing