Skip to content

Instantly share code, notes, and snippets.

@machinaut
Created November 30, 2017 06:29
Show Gist options
  • Save machinaut/1bc3ec89c898321a80c3c7cfa40b232b to your computer and use it in GitHub Desktop.
Save machinaut/1bc3ec89c898321a80c3c7cfa40b232b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import subprocess
import glob
import os
import shutil
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN)
def call(cmd):
print(' '.join(cmd))
subprocess.call(cmd)
def mount_drives():
for c in 'abcd':
mnt = '/mnt/tunebox' + c
if not os.path.ismount(mnt):
call(['mount', mnt])
def merge_sounds():
''' Return True if we have a sound file to play '''
found = []
for f in glob.glob('/mnt/tunebox*/*'):
if f.endswith('.wav'):
found.append(f)
try:
os.remove('/tmp/out.wav')
except OSError:
pass
if len(found) == 1:
call(['cp'] + found + ['/tmp/out.wav'])
return True
elif len(found) > 1:
call(['sox', '-m'] + found[:2] + ['/tmp/out1.wav'])
shutil.copyfile('/tmp/out1.wav', '/tmp/out.wav')
if len(found) > 2:
for i in range(2, len(found)):
call(['sox', '-m', '/tmp/out%d.wav' % (i - 1), found[i], '/tmp/out%d.wav' % i])
shutil.copyfile('/tmp/out%d.wav' % i, '/tmp/out.wav')
return True
return False
def play_sounds():
mount_drives()
if merge_sounds():
call(['play', '/tmp/out.wav'])
def main():
while True:
time.sleep(0.1)
if GPIO.input(18):
# Try again to make sure
time.sleep(0.01)
if GPIO.input(18):
play_sounds()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment