|
| 1 | +--- |
| 2 | +title: Using the EV3 Speaker |
| 3 | +group: hardware-standard |
| 4 | +--- |
| 5 | + |
| 6 | +* Table of Contents |
| 7 | +{:toc} |
| 8 | + |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +The EV3 uses pulse-width modulation (PWM) to drive an amplifier that produces the |
| 13 | +sounds that you hear through the speaker. It has two modes of operation. The first, |
| 14 | +we call **Tone** mode (aka **Beep** mode). In Tone mode, the PWM is set to a |
| 15 | +frequency in the [audible range][1]. The sound you hear is a square wave. |
| 16 | +The second mode is called **PCM Playback** mode. In this mode, the PWM is operated |
| 17 | +at a higher frequency. The high frequencies are (mostly) filtered out and we are |
| 18 | +able to reproduce a recorded sound. |
| 19 | + |
| 20 | +[1]: https://en.wikipedia.org/wiki/Audio_frequency |
| 21 | + |
| 22 | + |
| 23 | +## Tone Mode |
| 24 | + |
| 25 | +There are a couple of ways to control the speaker in Tone mode. Also worth an |
| 26 | +honorable mention: if you plug a USB keyboard into the EV3 and press |
| 27 | +<kbd>CTRL</kbd>+<kbd>G</kbd>, it will beep! |
| 28 | + |
| 29 | + |
| 30 | +### beep command |
| 31 | + |
| 32 | +The [beep man page][2] has all you need to know. |
| 33 | + |
| 34 | +[2]: https://manpages.debian.org/jessie/beep/beep.1.en.html |
| 35 | + |
| 36 | +Examples: |
| 37 | + |
| 38 | +```bash |
| 39 | +$ beep # 440 Hz for 200 ms |
| 40 | +$ beep -f 200 -l 500 # 220 Hz for 500 ms |
| 41 | +$ beep -f 262 -l 180 -d 20 -r 2 \ |
| 42 | +-n -f 392 -l 180 -d 20 -r 2 \ |
| 43 | +-n -f 440 -l 180 -d 20 -r 2 \ |
| 44 | +-n -f 392 -l 380 -d 20 \ |
| 45 | +-n -f 349 -l 180 -d 20 -r 2 \ |
| 46 | +-n -f 330 -l 180 -d 20 -r 2 \ |
| 47 | +-n -f 294 -l 180 -d 20 -r 2 \ |
| 48 | +-n -f 262 -l 400 # twinkle twinkle little star |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +### tone sysfs Attribute |
| 53 | + |
| 54 | +TODO: using the `tone` attribute is not really recommended. This needs to be |
| 55 | +updated with an example on how to use the Linux input subsystem instead. |
| 56 | + |
| 57 | +We have also provided a way to play tones directly using the sysfs attribute |
| 58 | +`/sys/devices/platform/snd-legoev3/tone`. |
| 59 | + |
| 60 | +Example: |
| 61 | + |
| 62 | +```bash |
| 63 | +$ cd /sys/devices/platform/snd-legoev3 |
| 64 | +$ echo 1000 10000 > tone # play a 1 KHz tone for 10 seconds |
| 65 | +$ echo 440 > tone # play a 440 Hz tone (endless mode) |
| 66 | +$ echo 0 > tone # stop the tone |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +## PCM Playback Mode |
| 71 | + |
| 72 | +PCM playback is implemented as an ALSA driver. This means that you can use just |
| 73 | +about any Linux program out there that can play sound. We have found the best |
| 74 | +compromise between sound quality and CPU usage is to use a sample rate 22050 Hz. |
| 75 | +16000 Hz sounds pretty good too. Of course the speaker is so small, the sound |
| 76 | +quality is just not going to be "high quality" no matter what. |
| 77 | + |
| 78 | +Example: Playing back a .wav file using `aplay`. |
| 79 | + |
| 80 | +```bash |
| 81 | +$ aplay my-file.wav |
| 82 | +Playing WAVE 'my-file.wav' : Signed 16 bit Little Endian, Rate 22050 Hz, Mono |
| 83 | +``` |
| 84 | + |
| 85 | +{% include /style/icon.html type="info" %} |
| 86 | +You must be a member of the `audio` group in order to use the sound driver. |
| 87 | +{: .alert .alert-info} |
| 88 | + |
| 89 | + |
| 90 | +## Volume control |
| 91 | + |
| 92 | +Again, there are a number of ways to do this. Currently, there is only a master |
| 93 | +volume control that controls both Tone mode and PCM Playback mode. |
| 94 | + |
| 95 | +### amixer Command |
| 96 | + |
| 97 | +Documentation: |
| 98 | +* [man page](https://manpages.debian.org/jessie/alsa-utils/amixer.1.en.html) |
| 99 | + |
| 100 | +Example: |
| 101 | + |
| 102 | +```bash |
| 103 | +$ amixer get Playback,0 # get current volume |
| 104 | +Simple mixer control 'Playback',0 |
| 105 | + Capabilities: volume volume-joined penum |
| 106 | + Playback channels: Mono |
| 107 | + Capture channels: Mono |
| 108 | + Limits: 0 - 256 |
| 109 | + Mono: 255 [100%] |
| 110 | +$ amixer set Playback,0 50% # set volume to 50% |
| 111 | +Simple mixer control 'Playback',0 |
| 112 | + Capabilities: volume volume-joined penum |
| 113 | + Playback channels: Mono |
| 114 | + Capture channels: Mono |
| 115 | + Limits: 0 - 256 |
| 116 | + Mono: 128 [50%] |
| 117 | +``` |
| 118 | + |
| 119 | +### alsamixer Command |
| 120 | + |
| 121 | +Graphical volume controls. |
| 122 | + |
| 123 | +``` |
| 124 | +┌───────────────────────────── AlsaMixer v1.0.25 ──────────────────────────────┐ |
| 125 | +│ Card: LEGO Mindstorms EV3 speaker F1: Help │ |
| 126 | +│ Chip: F2: System information │ |
| 127 | +│ View: F3:[Playback] F4: Capture F5: All F6: Select sound card │ |
| 128 | +│ Item: Playback Esc: Exit │ |
| 129 | +│ │ |
| 130 | +│ ┌──┐ │ |
| 131 | +│ │ │ │ |
| 132 | +│ │ │ │ |
| 133 | +│ │ │ │ |
| 134 | +│ │ │ │ |
| 135 | +│ │ │ │ |
| 136 | +│ │ │ │ |
| 137 | +│ │ │ │ |
| 138 | +│ │▒▒│ │ |
| 139 | +│ │▒▒│ │ |
| 140 | +│ │▒▒│ │ |
| 141 | +│ │▒▒│ │ |
| 142 | +│ │▒▒│ │ |
| 143 | +│ └──┘ │ |
| 144 | +│ 40 │ |
| 145 | +│ <Playback> │ |
| 146 | +│ │ |
| 147 | +└──────────────────────────────────────────────────────────────────────────────┘ |
| 148 | +``` |
| 149 | + |
| 150 | + |
| 151 | +## More Fun! |
| 152 | + |
| 153 | +### Text-To-Speech |
| 154 | + |
| 155 | +```bash |
| 156 | +$ espeak "hello, I am an EV3. |
| 157 | +> I like to talk because I am a robot. |
| 158 | +> Did you know that robots like to make sounds? |
| 159 | +> Beep. Boop. Dit. Dit. Meep. |
| 160 | +> I am just such a chatterbox." --stdout | aplay |
| 161 | +Playing WAVE 'stdin' : Signed 16 bit Little Endian, Rate 22050 Hz, Mono |
| 162 | +``` |
| 163 | + |
| 164 | +{% include /style/begin-panel.html type="info" heading="Tip" %} |
| 165 | + Add a function like this to your `~/.bashrc` file. |
| 166 | + |
| 167 | +```bash |
| 168 | +speak(){ |
| 169 | + espeak -a 200 -s 130 -v la --stdout "$@" | aplay --quiet |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +Then you can simply use: |
| 174 | + |
| 175 | +```bash |
| 176 | +$ speak "Thanks to GeekDude for this idea!" # @G33kDude |
| 177 | +``` |
| 178 | +{% include /style/end-panel.html %} |
| 179 | + |
| 180 | + |
| 181 | +### MP3 player |
| 182 | + |
| 183 | +```bash |
| 184 | +$ sudo apt-get update |
| 185 | +$ sudo apt-get install mpg123 # or mpg321 |
| 186 | +... |
| 187 | +$ mpg123 /home/ev3dev/Music/01-The\ Jumpin\'\ Jive.mp3 |
| 188 | +High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3 |
| 189 | + version 1.14.4; written and copyright by Michael Hipp and others |
| 190 | + free software (LGPL/GPL) without any warranty but with best wishes |
| 191 | + |
| 192 | +Directory: /home/ev3dev/Music/ |
| 193 | +Playing MPEG stream 1 of 1: 01-The Jumpin' Jive.mp3 ... |
| 194 | +
|
| 195 | +MPEG 1.0 layer III, 128 kbit/s, 44100 Hz joint-stereo |
| 196 | +Title: The Jumpin' Jive Artist: Cab Calloway |
| 197 | +Comment: Album: Minnie the Moocher [Universal] |
| 198 | +Year: 1989 Genre: Swing |
| 199 | +``` |
0 commit comments