I had been looking a long time for a program that would start recording, but only save frames that contain sound which is ``louder'' than a specific threshold. I searched for programs. I found none that were open source. There weren't any closed source ones that run on linux or my n900 either... Only a few heavy programs for Windows. (Which were closed)
Why do I want this? Well.. very often I have some great ideas or just plain things I want to remember. When I'm walking in the forest, sitting in bath, cycling in the city... (Or just sitting in my room, music playing doesn't bother it) Then I can't just write it down. So this is kind of a Memo application, but instead of typing, you just say whatever you want to say, and it will record that. It will obviously have to be on all the time, but it will only actually save the useful samples.
So... I knew my phone could record because it had some "recorder" app. It worked quite well, but did not have this particular feature. I found the source, and it was written in C. It seemed to use GStreamer for recording, in combination with PulseAudio. (My phone uses PulseAudio)
I was messing a bit with GStreamer, but it was a bit overkill and didn't send any WAVE headers either. So I fell back to a default linux program. (It's probably on your desktop too, if you run Linux): asound. This program simply dumps the raw .wav file to the standard output. So I made a subprocess in my python script which piped all the wave output to my program. My program will read 8000 samples at a time, - exactly 1 second -and then process it. If it contains at least one high and low tone, it will write the entire sample of one second to the output file. Additionally, it will always record one extra second after a second that contain a loud sound. If that extra second also contains loud sounds, it will also capture the next second, and so on.
It was quite a task to do this, as I first didn't take a good look at my output data and started to do Fast Fourier Transformations, while I did not even need these.
Simply run the program by either making it executable (chmod +x the_file) or call it like "python the_file".
Obviously it will need a GUI and a system to manage all the output files, as it will currently override the last session. It should probably not write output to the file as it does now... More buffering would be more flash card friendly.
Some credits go to Benland100 for flaming/teasing me when I was using FFTs to do this. He put me on the right track.
E: Obviously this will work on any system that has asound and python.
python Code:
#!/usr/bin/env python
import subprocess
import wave
import time
#p = subprocess.Popen(['gst-launch-0.10', 'pulsesrc ! wavenc ! fdsink fd=1'], \
# stdout=subprocess.PIPE)
# Arecord just dumps the raw wav to stdout. We will use this
# to read from with out wave module.
p = subprocess.Popen(['arecord'], stdout=subprocess.PIPE)
# Open the pipe.
f = wave.open(p.stdout)
# Open file we will write to.
o = wave.open('test.wav', 'w')
o.setparams((1, 1, 8000, 0, 'NONE', 'not compressed'))
o.setnchannels(1)
high, lasthigh = False, False
# Print audio set up
print f.getparams()
while True:
# Read 1 second
a = f.readframes(8000)
b = [ord(x) for x in a]
_min, _max = min(b), max(b)
# Print bounds
print 'min', _min
print 'max', _max
# TODO: The gate should obviously be configurable.
if _max > 135 and _min < 120:
high = True
else:
high = False
# Write always if either is True.
if lasthigh or high:
o.writeframes(a)
lasthigh = high
f.close()
o.close()