This is a tutorial to record a .wav file, using ALSA -arecord when a button a pressed, and then upload the recorded file to Soundcloud.com , as shown in the video below
Here are the steps you will have to follow -
#1 Setting up USB audio sound card on the BeagleBone
Connect a mic and speaker to the USB sound card and SSH into the BeagleBone to run the commands below. And using a LAN cable connect your BeagleBone to your home network router.( the soundcard i am using is called HDE 7.1 Channel Usb External Sound Card Audio Adapter: Amazon.ca: Electronics )
#2 ALSA- utils should be installed by default if you bought your BeagleBone recently,if not run the following command just in case
root@beaglebone:~#apt-get update
root@beaglebone:~#apt-get install alsa-base alsa-utils
#3 Check if your sound card is detected using the command
root@beaglebone:~#lsusb
you should see USB sound card in the output ie C-Media Electronics, Inc. CM108 Audio Controller
#4 Check the default adapter from which sound will be outputted
root@beaglebone:~# aplay -l
As you see in the output below the sound is defaulted to output from the HDMI port, that is HDMI is maked as card 0
**** List of PLAYBACK Hardware Devices ****
card 0: Black [TI BeagleBone Black], device 0: HDMI nxp-hdmi-hifi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
#5 Modify the uEnv.txt file to disable HDMI
Navigate to the /boot partition
root@beaglebone:~#cd /boot
and modify the uEnv.txt file and add the line below at the end of the file
root@beaglebone:~#nano uEnv.txt
optargs=capemgr.disable_partno=BB-BONELT-HDMI,BB-BONELT-HDMIN
#6 Make the USB sound card as the default by modifying the file below
root@beaglebone:~# nano /etc/modprobe.d/alsa-base.conf
In the file modify the value below from -2 to 0
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=0
Now if you run the aplay -l command you should see only one card
root@beaglebone:~#aplay -l
#7 Test recording and playing a file
create a folder called recordtest and then run the following command to test recording a wav file for 10 seconds
root@beaglebone:~#arecord -D plughw:0 --duration=10 -f cd -vv testsound.wav
and to play file with a speaker connected use
root@beaglebone:~#aplay testsound.wav
Note: you have have to adjust the volume of your speaker and mic use the command below
root@beaglebone:~#alsamixer
#8 Install the Soundcloud python wrapper
There are a couple of ways you can do this, i prefer to use pip, that is
root@beaglebone:~#pip install soundcloud
Here is the github link if you in a mood of manually installing it - https://github.com/soundcloud/soundcloud-python
Note: As of 05 March 2016 , as I am writing this blog there is dependency missing as part of the soundcloud repo use the command below to install it
root@beaglebone:~# pip install six
#9 Getting the auth token from Soundcloud
To upload the file to soundclound.com , we will have to get a auth-token which we will use in the final program.
Here start by creating a soundcloud account if you dont have one and go to the Developer console --> Your App --> and Register a new application
Make a client id and client secret, which you will have to modify in the python code below
import soundcloud
client = soundcloud.Client(client_id ='XXXXXXXXXXXXXXXXXXXXX',
client_secret ='XXXXXXXXXXXXXXXXXXXXXXX',
username = 'XXXXXX',
password = 'XXXXXXXXX')
print client.access_token
to run the program use
root@beaglebone:~#python userAuthToken.py
make a note o the access_token , which you will require as part of the next piece of python code. As part of the final deployment/project you will have to refresh the token regularly and if possible store it in database/file.
#10 Breadboard the circuit
For the circuit you will need 2 buttons and 2 LED's, in addition to you will also need 2 1kohms and 330 ohms resistors
Connect the LEDS via 330 ohms resistor to pin P9_11 and P9_12
And the button using the 1K ohms pull down resistors to P9_13 and P9_15
#Button and LED connections
recordLED = "P9_11"
uploadLED = "P9_12"
recordButton = "P9_13"
uploadButton = "P9_15"
#11 Modify the python program with your token to record and upload to soundcloud
Modify the soundcloudToken value as part of the program attached below with the value you received as part of step 9 and run it using
root@beaglebone:~#python soundcloudRecordUpload.py
#12 Modify the Name and description of the uploaded file on soundclound.com
Now once you done you can modify the details on Soundcloud, upload a fancy image and add it to your favorite play-list ..