Skip to content

Instantly share code, notes, and snippets.

@gannebamm
Created November 3, 2017 09:22
Show Gist options
  • Save gannebamm/6407d5cb9d62f23eefc8e6e7581d741d to your computer and use it in GitHub Desktop.
Save gannebamm/6407d5cb9d62f23eefc8e6e7581d741d to your computer and use it in GitHub Desktop.
record audio samples via alsaaudio for the ASSOS project - creates bad chunks of data
### configuration file for assos_listen
# upload
upload = False
# config for this sensor
sensorID = "al_03"
# sampling rate & chunk size
chunkSize = 1024
samplingRate = 44100 #44100 # 44100 needed for Aves sampling
# choices=[4000, 8000, 16000, 32000, 44100] :: default 16000
# sample length in seconds
sampleLength = 10
# configuration for assos_store container
ftp_server_ip = "192.168.0.157"
username = "sensor"
password = "sensor"
# storage on assos_listen device
storagePath = "/home/pi/assos_listen_pi/storage/"
import wave
import datetime
import signal
import ftplib
import sys
import os
#### alsaaudio since pyaudio gave IOError: [Errno Input overflowed] -9981
import alsaaudio
import numpy as np
import array
import time
# configuration for assos_listen
import config
# run the audio capture and send sound sample processes
# in parallel
from multiprocessing import Process
# CONFIG
# Set attributes: Mono, 16 bit little endian samples
FORMAT = alsaaudio.PCM_FORMAT_S16_LE
CHANNELS = 1
# read from config.py
CHUNK = config.chunkSize
RATE = config.samplingRate
RECORD_SECONDS = config.sampleLength
# HELPER FUNCTIONS
# write to ftp
def uploadFile(filename):
print("start uploading file: " + filename)
# connect to container
ftp = ftplib.FTP(config.ftp_server_ip, config.username, config.password)
# write file
ftp.storbinary('STOR '+filename, open(filename, 'rb'))
# close connection
ftp.quit()
print("finished uploading: " +filename)
# write to sd-card
def storeFile(filename,recorder,frames):
print("writing file: " + filename)
wf = wave.open(filename, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(2)
wf.setframerate(RATE)
wf.writeframes(frames)
wf.close()
#print(filename + " written")
# abort the sampling process
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
# MAIN FUNCTION
def recordAudio(recorder):
sampleNumber = 0
while (True):
sampleNumber = sampleNumber +1
print("*** recording #" + str(sampleNumber))
# %Y-%m-%d_%H%M%S_%Z is the by monitoR used time.source formatting
# UTC is the default timezone
# ___%f could be usefull for localisation algorithms
startDateTimeStr = datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S_UTC--%f")
fileName = str(config.sensorID) + "_" + startDateTimeStr + ".wav"
wf = wave.open(fileName, 'w')
wf.setnchannels(CHANNELS)
wf.setsampwidth(2)
wf.setframerate(RATE)
for i in range(0, 1000*RECORD_SECONDS/22):
l, data = recorder.read()
wf.writeframes(data)
wf.close()
print(fileName + " written")
if (config.upload == True):
# since waiting for the upload to finish will take some time
# and we do not want to have gaps in our sample
# we start the upload process in parallel
print("start uploading...")
uploadProcess = Process(target=uploadFile, args=(fileName,))
uploadProcess.start()
# ENTRYPOINT FROM CONSOLE
if __name__ == '__main__':
recorder = alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)
recorder.setchannels(CHANNELS)
recorder.setrate(RATE)
recorder.setformat(FORMAT)
recorder.setperiodsize(CHUNK)
# directory to write and read files from
os.chdir(config.storagePath)
# abort by pressing C
signal.signal(signal.SIGINT, signal_handler)
print('\n\n--------------------------\npress Ctrl+C to stop the recording')
# start recording
recordAudio(recorder)
@gannebamm
Copy link
Author

gannebamm commented Nov 3, 2017

For an example of a file with bad chunks see: https://drive.google.com/file/d/1LemPtyblKuZ-0ExHZCrbmFWE-JvhDKJi/view?usp=sharing
The error is not following any kind of schema. These are the four sensors I used to capture audio data and the erroneous files: https://drive.google.com/file/d/1Qkw4rbZ_GT-3RrFOqrFXX2uw6JUuUAzl/view?usp=sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment