Mastodon

Saturday 17 April 2021

PiCorder (The Next Generation)

The original PiCorder worked, took ages to start recording and I couldn't hear what I was recording as it was being recorded, it was time for a rethink. 

I decided to throw away the original idea and add a small USB sound card, this is what I got in all for it, apart from the Raspberry Pi Zero W of course.

These are fantastic, reversible USB micro leads, not USB C, USB micro, they can go in either way around. 

I got them from Amazon, £7.99 for 2, there are probably other places so please shop around/
This is the mic I got, again from Amazon, £7.59, you can probably use any mic with a 3.5mm jack, I didn't have one so this did the job.
Nice little case for it all to go in, as it's transparent you can use the onboard LED as a sort of status thingy, Amazon again and £8.99


Then there's the sound card, there's quite a few of these about, I got two and they seem to work quite well to be fair. Again from Amazon at a cost of £7.59.


You don't have to get one of these but in my mind it makes the sound card fit better with the case. Cost for the two is £5.10 from Amazon.









Power was the next thing on my shopping list, I got one of these, mainly because I liked the look of it and if you press the test button on it then it turns the power back on, more on that later. I managed to get one from Morrisons at £9.99.













And last but not least apart from the Raspberry Pi itself was something to give me a bit of directionality without being too big and intrusive and was light. I looked at bullet mics and parabolic dishes and although the dish itself probably gives the best results I fell back on the idea of the really old hearing aids, basically a funnel. I found these in Poundland and managed to get some grey ones.

You need a micro SD card too of course, I got a 32GB one, I would suggest getting a decent make, I got some cheap ones and they gave up after about a week.




Put together will all the magic stuff from a glue gun and the old favourite Blu Tack and it looks like this, the special effect guys from Blakes 7 couldn't have done better. You have a headphone socket so that you can hear what you're recording and you can collapse the funnel back to make it less obtrusive. It's not brilliant but it works, the funnel does get rid of some outside noise and I'll include some sound samples later on.

I also added some small push buttons, top one is connected to GPIO 3 and ground, the bottom to GPIO 13 and ground. I soldered these directly to the board but you can break it and the warranty is void of course.







Time to put it all together now, I'm not going through how to install the OS onto the SD card, I used Raspbian Lite for it, don't really need a desktop environment and it's all set up by SSH so make sure that's enabled. 

We're going to have to get the sound card working first and change the playback and sound levels, run the following:

sudo nano /usr/share/alsa/alsa.conf

defaults.ctl.card 0

defaults.pcm.card 0

Change these to a 1 as shown below.

defaults.ctl.card 1

defaults.pcm.card 1



Run sudo alsamixer and you should see this, select the Speaker and Mic and up the levels and we are now ready to get on with the rest.






We're going to turn off bluetooth as this seemed to be the cause of some electrical noise.

Open up the /boot/config.txt file with:

sudo nano /boot/config.txt

Add the following to the bottom of the file

# Disable bluetooth
dtoverlay=disable-bt

# Allows power off and on
dtoverlay=gpio-shutdown

Now we remove the bluetooth software

sudo systemctl disable hciuart.service

sudo systemctl disable bluealsa.service

sudo systemctl disable bluetooth.service

 sudo apt-get purge bluez -y

sudo apt-get autoremove -y



Now reboot to continue, once it's back up you should find a press of the top button will shut the Pi down and once it's closed down completely the status lights on the charger will go off. Press the test button on the charger and the Pi will come back on.

I've made a small script called mtbootme which is in the bin folder, makes it easier to add things when it boots up, just run sudo nano /bin/mtbootme and add the following lines



# Turns the light off

echo 0 >/sys/class/leds/led0/brightness

# Makes the light flash

echo heartbeat >/sys/class/leds/led0/trigger

Save it and make it executable with sudo chmod +x /bin/mtbootme

Now we need a way of recording the sound and also of being able to monitor it while it's recording. There's probably a better way of doing it but I came up with this, there's about a second delay.



The bit of shell script I use checks for a file number, if it's not there it starts at 1, if it is there it adds a 1 to it. 

sudo nano /bin/mtcapture

Paste this lot in

#!/bin/bash

# shuts down the wifi

ifconfig wlan0 down

# Turns the LED on fully

echo 0 >/sys/class/leds/led0/brightness

echo 1 >/sys/class/leds/led0/brightness

# sorts the file number

FILE=/share/filenum

if [ -f "$FILE" ]; then

    echo "$FILE exists."

else

    echo "$FILE does not exist."

echo "0"> /share/filenum

fi

read newfile < /share/filenum

echo $newfile

newfile=$((newfile+1))

echo $newfile > /share/filenum

# Records the noises

arecord -f cd - | tee /share/AudioTux1-$newfile.wav | aplay -  &

echo $newfile

# updates the file number

echo $newfile > /share/filenum



Make it executable with sudo chmod +x /bin/mtcapture

That's how we start it recording, don't run this just yet as it will turn the wifi off and you'll lose your connection or rem out the wifi line while testing.



Now we do the stop recording file

sudo nano /bin/mtstop

#!/bin/bash

# Kill all recording services

pkill mtcapture

pkill mtcapture

pkill arecord

pkill aplay

# Starts up the wifi

ifconfig wlan0 up

# Flashes the green light so we know recording has stopped

echo 0 >/sys/class/leds/led0/brightness

echo heartbeat >/sys/class/leds/led0/trigger



Save it and make it executable again with sudo chmod +x /bin/mtstop



A bit of python now to monitor the stop start button.

sudo nano record.py

This lot goes in here.

# !/bin/python

import RPi.GPIO as GPIO

import time

from subprocess import call

import os

GPIO.setmode(GPIO.BCM)

GPIO.setup(13,GPIO.IN,pull_up_down=GPIO.PUD_UP)

flag = False

while True:

                input = GPIO.input(13)

                if(input == False):

                        if(flag == False):

                                os.system("/bin/mtcapture")

                                flag = True

                                time.sleep(0.4)

                        else:

                                os.system("/bin/mtstop")

                                flag = False



We don't need to make this executable to run it.

Finally we edit the rc.local file to get things working on startup


sudo nano /etc/rc.local


Add these two lines just before the exit 0 at the bottom

python2 /root/record.py &

/bin/mtbootme &


Save it and reboot and you're ready to go, one last thing I've done is to install samba so I can pull the files off it. All the recordings are saved in the directory /share.

Install Samba and MC, it's one of the first things I install as it makes navigating the structure and editing really easy.

sudo apt install samba mc

Once done edit the samba config file


sudo nano /etc/samba/smb.conf


Add this to the bottom[Shares]

path = /share

public = yes

browseable = yes

writeable = yes

delete readonly = yes

force create mode = 777

force directory mode = 777

writeable = yes

directory mode = 777

create mode = 777


Save it and restart samba with sudo service smbd restart


And you are now ready to go, once you've powered it up the green status LED should be flashing, plus some headphones into the socket and press the lower button and it should start recording, you'll hear a delayed sound in the headphones, press it again and it will stop.

It's not supposed to be a professional bit of equipment, it's small, it's light and it sort of works and more than anything it's a bit of fun and it will get you more involved with nature.

Advantages of the funnel at the end are that it's completely collapsible, you can put it in a pocket and fold it over as shown below and it still works. Disadvantages are that you look like a mad professor when you're out and about with it.


And finally some sample sounds, there's a bit of noise in the background which is from a nearby industrial unit,



No comments:

Post a Comment