This article assumes that you have already successfully installed your Wolfson audio card - if you haven't done that yet, there is an excellent blog by cstanton at http://www.element14.com/community/community/raspberry-pi/raspberry-pi-accessories/wolfson_pi/blog/2014/03/14/can-you-hear-the-wolfson-calling-setting-up-and-using-the-wolfson-audio-card
Once you have your card set up you will want to start recording with it. Like may people, I run my card 'headless' via a Putty shell, and therefore I don't have a screen attached to control recording or playback. therefore i want to be able to record and playback straight from the card - i.e. without using players like Audacity. My eventual aim is to have a recorder controlled from my phone, but as a starting point I want to be able to at least record and playback with a minimum of effort.
This post explains how to make a recording and play it back, and how the recording parameters can be amended. Its only a basic guide but should help get you started - and there is an attached tar which contains an example of working record and playback scripts.
amixer recording control
The distribution comes with the amixer function, which is a bit like a command line mixing desk. There is some documentation available at http://www.linuxcommand.org/man_pages/amixer1.html and I used this to experiment with recording settings.
When recording sound there are two main settings to worry about - the 'gain' control which affects how much signal comes into the device, and the 'level' control which determines how much of that signal gets recorded. The gain should be set so that you get a clear signal - to much and it will be distorted, too little and there will be a lot of hiss and noise. Once the signal level is correct you then use the level control to decide how loud you want the recording to be. This second option really has more relevance with multi-channel recording - for instance if you want one source quieter than another.
The distribution comes packaged with some scripts in the use-cases directory - these allow you to set up the control for different types of record/playback. In my case I want to record in the onboard mics, and then pay back through the headphone jack.
Displaying current values and control parameters
Current values for a device can be be obtained by using the command amixer cget name='xxxxxxx', where xxxxxx is the name of the control.
For example, to get the current input gain settings use amixer cget name='IN2L Digital Volume' which returns the following :
From this we can see the current value (i.e. 128) and also the settings for this control - i.e.
- Minimum value -64.0 dB
- Increment value 0.50 dB
- Max value 191 (which works out at around 31.5dB - i.e. -64 + (191/2)
So with a current value of 128, the input gain is set to (-64 + 128/2) - i.e. 0dB.
Setting Control Values
Control levels are set using command amixer -Dhw:0 cset name='xxxxxxx' nnn where
- xxxxxxx is the name of the control
- nnn is the value to assign
I.e. to set the input gain for the left hand onboard mic to 3dB us
amixer -Dhw:0 cset name='IN2L Digital Volume' 130
(NB value of 128 is 0dB for this control as explained above).
Gain Control
The gain for the on board mics is set using devices IN2L and IN2R (i.e. for left and right).
The control name for the onboard mic gain is IN2L Digital Volume (for the left channel) and IN2R Digital Volume
Using command amixer cget name='IN2L Digital Volume' to get the current gain
Input Volume
The recording input level is set using devices AIF1TX1 Input 1 Volume and AIF1TX2 Input 1 Volume (i.e. for left and right).
The recording device has to be assigned to each control and then he level set.
E.g. to set up the left hand onboard mic (IN2L)
amixer -Dhw:0 cset name='AIF1TX2 Input 1' IN2L amixer -Dhw:0 cset name='AIF1TX2 Input 1 Volume' 32
Preparing to record
Before a recording is made the appropriate commands must be set up to enable the correct devices and set the levels. The following example prepares for recording using the onboard mics, with input gain set at 5db, and the input volume at 8dB.
#!/bin/bash #Record from onboard DMICs # Gain Control # 128 = 0db, increments in steps of 0.5db (starting from -64db) # Max = 191 (i.e. +32db) # (to see current setting, and documentation of values, use command "amixer cget name='IN2L Digital Volume'" ) # amixer -Dhw:0 cset name='IN2L Digital Volume' 138 amixer -Dhw:0 cset name='IN2R Digital Volume' 138 # # Input level : 32 = 0db, increments in steps of 1.0db, starting at -32db # Max setting = 48 (=16Db) # (to see current setting, and documentation of values, use command "amixer cget name='AIF1TX2 Input 1 Volume'" ) # amixer -Dhw:0 cset name='AIF1TX1 Input 1' IN2R amixer -Dhw:0 cset name='AIF1TX1 Input 1 Volume' 40 amixer -Dhw:0 cset name='AIF1TX2 Input 1' IN2L amixer -Dhw:0 cset name='AIF1TX2 Input 1 Volume' 40 amixer -Dhw:0 cset name='DMIC Switch' on
Making a recording
To make a recording :
- Initalise the sound card values
- Set the recording parameters
- Run the recording command
The distribution comes with a 'reset' script which will reset all the values.
Recording is achieved using the command
arecord -Dhw:0 -r 44100 -c 2 -f S32_LE output_file
e.g.
# Make sure card is set up to record from the onboard Mic use_case_scripts/RESET.sh use_case_scripts/Record_from_DMIC.sh # record to file arecord -Dhw:0 -r 44100 -c 2 -f S32_LE music/petemp.wav
PlayBack
To playback the recording use the command
aplay -Dhw:0 -r 44100 -c 2 -f S32_LE recorded file
Prior to playback the playback medium (e.g. the headset) must be enabled. The output volumes can be adjusted in the same way as the recording levels.
Example recording configuration
The attached tar file contains examples of recording and playback set up scripts, plus a couple of scripts which will record and playback. The recorded file is created in sub directory 'music'.
To install
- Navigate to a directory on the pi where you want to install the example
- Copy the attached file to that location (e.g. using ftp)
- Un-tar using command
tar -xvf ~/recording_example.tar
This will create directory recording and 2 sub directories use_cases and music
To make a recording run pe_record.sh. To play back use pe_playback.sh. The recorded file is created as petemp.wav in sub directory music.
For more details, look inside the individual script files !