Difference between revisions of "Audio setup"

From Raspberry Pi Min-Grant project
Jump to: navigation, search
 
(5 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
'''<span style="font-size:medium;">Audio Input</span>'''
 
'''<span style="font-size:medium;">Audio Input</span>'''
  
<span style="font-size:medium;"><span style="font-size:small;">In order to record audio, we need to connect a microphone to the Pi. If you connect a USB mic, you can ensure that the Pi recognizes it by typing<span style="font-family:courier new,courier,monospace;">lsusb</span> at the command line</span></span>
+
<span style="font-size:medium;"><span style="font-size:small;">In order to record audio, we need to connect a microphone to the Pi. If you connect a USB mic, you can ensure that the Pi recognizes it by typing&nbsp;<span style="font-family:courier new,courier,monospace;">lsusb</span> at the command line</span></span>
  
<code>pi@raspberrypi ~ $ lsusb</code>
+
<code>pi@raspberrypi ~ $ lsusb</code><br/><code>Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.<br/>Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub<br/>Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.<br/>Bus 001 Device 004: ID 0000:0538<br/>Bus 001 Device 006: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller<br/>Bus 001 Device 005: ID 1c4f:0002 SiGma Micro Keyboard TRACER Gamma Ivory</code>
 
 
<code><br/>Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.<br/>Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub<br/>Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.<br/>Bus 001 Device 004: ID 0000:0538<br/>Bus 001 Device 006: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller<br/>Bus 001 Device 005: ID 1c4f:0002 SiGma Micro Keyboard TRACER Gamma Ivory</code>
 
  
 
Here we have connected the C-Media USB Microphone.
 
Here we have connected the C-Media USB Microphone.
Line 15: Line 13:
 
<code>pi@raspberrypi ~ $ arecord -l</code>
 
<code>pi@raspberrypi ~ $ arecord -l</code>
  
​****&nbsp;<code>List of CAPTURE Hardware Devices ****</code>
+
<code>**** List of CAPTURE Hardware Devices ****</code>
  
 
<code>card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]</code>
 
<code>card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]</code>
Line 22: Line 20:
 
   
 
   
 
  Subdevice #0: subdevice #0
 
  Subdevice #0: subdevice #0
 +
 
   
 
   
 
  </code>
 
  </code>
 +
These device details will be useful in recording audio.
 +
 +
'''<font size="2">Installing SoX for audio recording</font>'''
 +
 +
SoX is a program for recording and manipulating audio. You can install it by typing&nbsp;
 +
 +
<span style="font-family:courier new,courier,monospace;">sudo apt-get install sox</span>
 +
 +
at the command line. There are two functions&nbsp;<span style="font-family:courier new,courier,monospace;">play</span> and <span style="font-family:courier new,courier,monospace;">rec</span> which can then be used for recording and playback. To record some speech, follow the following steps
 +
 +
#set the audio driver by typing <span style="font-family:courier new,courier,monospace;">export&nbsp;AUDIODRIVER=alsa</span>
 +
#set the recoding device by typing <span style="font-family:courier new,courier,monospace;">export&nbsp;AUDIODEV=hw:&lt;card number&gt;,&lt;device number&gt;</span>. The card and device numbers of the recording device are obtained from <span style="font-family:courier new,courier,monospace;">arecord -l. Using the result above we type export AUDIODEV=hw:1,0</span>
 +
#<font face="courier new, courier, monospace">Record some audio by typing <span style="font-family:courier new,courier,monospace;">rec -c 1 -r 8000 speech.wav trim 0 2&nbsp;</span></font>and pressing Enter. This command will record a single channel at 8kHz and save the resulting speech in <span style="font-family:courier new,courier,monospace;">speech.wav. The recording will last 2 seconds.</span>
 +
 +
'''<span style="font-size:medium;">Audio Output</span>'''
 +
 +
To playback the audio recorded, we follow the following steps
 +
 +
#Plug in a set of head phones in the audio jack.
 +
#Set the audio output to the headphones by typing <span style="font-family:courier new,courier,monospace;">amixer cset numid=3 1</span> at the command line .
 +
#Determine the playback device details using <span style="font-family:courier new,courier,monospace;">aplay -l&nbsp;</span>
 +
#Set the audio device using &nbsp;<span style="font-family:courier new,courier,monospace;">AUDIODEV=hw:&lt;card number&gt;,&lt;device number&gt;</span>.
 +
#Type play speech.wav
 +
#If the volume is too low type alsamixer and adjust the volume using the arrow keys.

Latest revision as of 14:48, 29 April 2015

For a number of experiments we will need to manipulate audio signals with the Raspberry Pi. Here we give a few hints on how to get audio to work.

Audio Input

In order to record audio, we need to connect a microphone to the Pi. If you connect a USB mic, you can ensure that the Pi recognizes it by typing lsusb at the command line

pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0000:0538
Bus 001 Device 006: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 005: ID 1c4f:0002 SiGma Micro Keyboard TRACER Gamma Ivory

Here we have connected the C-Media USB Microphone.

You can obtain the device details by typing arecord -l

pi@raspberrypi ~ $ arecord -l

**** List of CAPTURE Hardware Devices ****

card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]

 Subdevices: 1/1

Subdevice #0: subdevice #0



These device details will be useful in recording audio.

Installing SoX for audio recording

SoX is a program for recording and manipulating audio. You can install it by typing 

sudo apt-get install sox

at the command line. There are two functions play and rec which can then be used for recording and playback. To record some speech, follow the following steps

  1. set the audio driver by typing export AUDIODRIVER=alsa
  2. set the recoding device by typing export AUDIODEV=hw:<card number>,<device number>. The card and device numbers of the recording device are obtained from arecord -l. Using the result above we type export AUDIODEV=hw:1,0
  3. Record some audio by typing rec -c 1 -r 8000 speech.wav trim 0 2 and pressing Enter. This command will record a single channel at 8kHz and save the resulting speech in speech.wav. The recording will last 2 seconds.

Audio Output

To playback the audio recorded, we follow the following steps

  1. Plug in a set of head phones in the audio jack.
  2. Set the audio output to the headphones by typing amixer cset numid=3 1 at the command line .
  3. Determine the playback device details using aplay -l 
  4. Set the audio device using  AUDIODEV=hw:<card number>,<device number>.
  5. Type play speech.wav
  6. If the volume is too low type alsamixer and adjust the volume using the arrow keys.