Birdsong analyser/Bat detector

In the past, I've listened to recordings of birdsong slowed down to 1/2 or 1/4 speed in Audacity and been amazed at the detail that you just can't pick out at normal speed. I reckon birds have a much faster reaction time than humans, and listening to their sounds slowed down gives a better impression of what they are hearing.

Anyway, I figured it ought to be possible to make a gadget that would slow down sounds but still work in real time, by recording without erasing into a looped buffer, playing it back at a slower speed, and only erasing the part that has been played back. The trade-off is that some sounds overlap, depending on how much you've slowed them down.

Looking for a suitable device to build the project on, the Pi seemed a good fit - I needed a decent amount of memory for the audio buffer, and a fast enough processor to be able to do all the DSP stuff. I also wanted it to be portable and battery powered so the Pi Zero fit the bill there - it has fewer 'extras' on the circuit board so is not as power-hungry as the other Pi models.

But there were a couple of problems with using the Pi. Firstly, the Raspbian OS startup time was way too high for a portable gadget, and secondly I needed to be working in realtime and Raspbian rather got in the way of that. So I decided to ditch the OS and work in 'bare metal' mode. Thanks to the work done by David Welch it didn't take too long to get up and running with a basic LED blinker to prove the concept. The Pi boots up in no time, and you have complete control of all processor and peripherals. Marvellous!

So, I needed a microphone, and since I was going to be lowering the pitch I wanted one that had as high a frequency cutoff as possible. I found one that claimed to work up to 80KHz - the SPH0641LU4H-1. It is a tiny 3.5mm x 2.6mm MEMS (Micro-Electro-Mechanical Systems) device, intended for surface-mount PCBs, but I made a kind of holder for it by hollowing out some stripboard and soldered some wires (individual strands from some multicore hookup wire) to the five pads on the mic. It was very fiddly, and a rather untidy job, but I got there in the end.

The device has a PDM (Pulse Density Modulation) output - that is a stream of 1's and 0's where the density of 1's indicates how high the sound pressure is. The stream is clocked out at up to 4.8MHz and needs to be filtered to reconstruct an audio signal, but luckily the Pi's processor has some hardware which can do some of that work.

I wrote some code that clocked the mics (I used two, for stereo processing) as fast as possible, performed the pitch-shifting and time-stretching using a long memory buffer, and generated a stereo analog signal using the two PWM outputs of the Pi. And after a fair bit of trial and error, the thing actually worked! Jangling a bunch of keys near the mic, slowed down an octave or two, sounded like clanging bells. Snapping my fingers sounded like a slow "Shhpop!" sound - and the reverberation of the room was also stretched out so it seemed like I was in a large hall.

I wanted to find out what the upper frequency limit of the gadget was, and luckily when I was looking up in my loft for something completely unrelated, I came across an adjustable ultrasonic oscillator that I'd made about 30 years ago. It still worked! With an oscilloscope I could measure its frequency, and I was pleased to discover that the microphones were sensitive up to 100 KHz - very impressive.

So eventually the time came to package the project up into a case - I had a suitable plastic project box, and made a housing for the mics (which are pretty delicate because of the thin wires) out of the lid of a Pritt glue stick, with some thin foam inside. The device is powered by a cheap 5V 'power bank', and can be recharged using a USB charger. It has a headphone output, and buttons to adjust the time-stretch ratio and the sensitivity. For good measure, I also added a button that puts it in a mode where the buffer is not cleared or overwritten, so you can constantly loop a snippet of audio and pitch-shift it at will.


I waited for evening time, when I knew bats would be flying at the back of the house, and took the gadget outside. I set it to drop the pitch by 4 octaves and therefore stretch time by a factor of 16. And it worked! I could hear descending chirps of pipistrelle bats as they flitted around the garden - and the stereo image made it possible to locate them by sound before seeing them. Echoes from buildings were stretched out too - I imagine it must be much closer to how the bats perceive their audio world.

So, I hooked the gadget up to my Zoom H1 recorder, and grabbed a 10-minute sample when the bats were reasonably active. I processed it a bit in Audacity - snipped out the silences, and filtered and de-noised it. Here is the result:

There seem to be three distinct types of call - I'm not sure if they come from different species of bat. The first one that can be heard, and the most common, is a pipistrelle, I think. There is a different, lower-pitched call which is first heard at about 48s and several times after that. And at 1m28s there is a third call at a relatively constant pitch. There is also a higher-pitched variant of the first 'pipistrelle' call at 54s and later - I don't know if this is a different species or just a different individual. Whenever I heard the lower-pitched call like the one at 48s, it seemed there were two bats apparently chasing each other or at least flying together, so perhaps it's a social call.

Well, that's the birdsong pitch-shifter that became a bat detector! I've had loads of fun making this project, and can see I'll enjoy using it too. Watch this space to find out what the next Pi project will be...