Raspberry Pi audio player with buttons

https://photos.app.goo.gl/qZJig6DzaLKR0eyD2

  • Get latest raspian image
    wget https://downloads.raspberrypi.org/raspbian_lite_latest

  • unzip

  • Mount sd and write it out:

      > diskutil list
      …
      dev/disk2 (external, physical):
      #:                       TYPE NAME                    SIZE       IDENTIFIER
      0:     FDisk_partition_scheme                        *15.9 GB    disk2
      1:             Windows_FAT_32 NO NAME                 15.9 GB    disk2s1
      …
    
      > diskutil unmountDisk /dev/disk2
      > sudo dd bs=1m if=/Users/viktor/Downloads/2017-09-07-raspbian-stretch-lite.img     of=/dev/rdisk2 conv=sync
    
  • enable ssh on the pi: touch /Volumes/boot/ssh (the pi boot image is called boot)

  • Also setup Wifi if you want that, pico /Volumes/boot/wpa_supplicant.conf

      network={
          ssid="YOUR_SSID"
          psk="YOUR_PASSWORD"
          key_mgmt=WPA-PSK
      }
    
  • eject > sudo diskutil eject /dev/rdisk2

Now we can SSH to the pie and do the rest from there.

I'm currently on a Wifi in an office I don't have router access to, so need to find the IP of the pi. I'm on mac so

First did:

  brew install nmap
  sudo nmap -sP 192.168.1.0/24 | awk '/^Nmap/{ip=$NF}/b8/{print ip}'

but was slow, but maybe that helped, dunno, but later tried:

   arp -na | grep -i b8:27:eb

which worked fine:

    ~ ❯❯❯ arp -na | grep -i b8:27:eb                                                                                                                                           ⏎
    ? (192.168.0.XXX) at b8:27:eb:XX:XX:XX on en0 ifscope [ethernet]

So then, log in with pi/raspberry over ssh. Next part is from tutorial over at adafruit:
https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi/install-audio

    sudo apt-get update
    sudo apt-get install alsa-utils mpg123
    sudo reboot

Relogin and then:

    sudo modprobe snd_bcm2835
    sudo amixer cset numid=3 1

Install python gpio stuff:

    sudo apt-get install python-dev python-rpi.gpio

Mixer settings:

    pi@raspberrypi:~ $ amixer controls
    numid=3,iface=MIXER,name='PCM Playback Route'
    numid=2,iface=MIXER,name='PCM Playback Switch'
    numid=1,iface=MIXER,name='PCM Playback Volume'
    numid=5,iface=PCM,name='IEC958 Playback Con Mask'
    numid=4,iface=PCM,name='IEC958 Playback Default'
    pi@raspberrypi:~ $ sudo alsactl store
    pi@raspberrypi:~ $ amixer cset numid=1 90%
    numid=1,iface=MIXER,name='PCM Playback Volume'
      ; type=INTEGER,access=rw---R--,values=1,min=-10239,max=400,step=0
      : values=-662
      | dBscale-min=-102.39dB,step=0.01dB,mute=1
    pi@raspberrypi:~ $ sudo alsactl store

Unfortunately, the pi's output simply sucks. Need to get separate audio dac and headphone amp. Ordered stuff from aliexpress.

Update 2018-01-25

Revisited this project and had forgotten most of it.

First off, I didn't actually have Wifi setup it seemed. But plugin in ethernet I could reach by

ssh pi@raspberry.local

Also, I've gotten one of these cheap USB sound cards in the mail.

It seemed autodiscovered. When plugging in:

sudo tail -100f /var/log/messages

Jan 25 10:49:32 raspberrypi kernel: [   47.300558] usb 1-1.4: new full-speed USB device number 5 using dwc_otg
Jan 25 10:49:32 raspberrypi kernel: [   47.447967] usb 1-1.4: New USB device found, idVendor=1b3f, idProduct=2008
Jan 25 10:49:32 raspberrypi kernel: [   47.447976] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Jan 25 10:49:32 raspberrypi kernel: [   47.447980] usb 1-1.4: Product: USB Audio Device
Jan 25 10:49:32 raspberrypi kernel: [   47.447984] usb 1-1.4: Manufacturer: GeneralPlus
Jan 25 10:49:32 raspberrypi kernel: [   47.478837] input: GeneralPlus USB Audio Device as /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.3/0003:1B3F:2008.0002/input/input1
Jan 25 10:49:32 raspberrypi mtp-probe: checking bus 1, device 5: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4"
Jan 25 10:49:32 raspberrypi kernel: [   47.540863] hid-generic 0003:1B3F:2008.0002: input,hidraw0: USB HID v2.01 Device [GeneralPlus USB Audio Device] on usb-3f980000.usb-1.4/input3
Jan 25 10:49:32 raspberrypi mtp-probe: bus: 1, device: 5 was not an MTP device
Jan 25 10:51:22 raspberrypi pulseaudio[683]: [pulseaudio] server-lookup.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Jan 25 10:51:22 raspberrypi pulseaudio[683]: [pulseaudio] main.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11


Great stuff!

So I'm using this python script top react to buttons and plau sound now and I need to add the output module. Listing modules:

pi@raspberrypi:~ $ aplay -L
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
sysdefault:CARD=ALSA
    bcm2835 ALSA, bcm2835 ALSA
    Default Audio Device
dmix:CARD=ALSA,DEV=0
    bcm2835 ALSA, bcm2835 ALSA
    Direct sample mixing device
dmix:CARD=ALSA,DEV=1
    bcm2835 ALSA, bcm2835 IEC958/HDMI
    Direct sample mixing device
dsnoop:CARD=ALSA,DEV=0
    bcm2835 ALSA, bcm2835 ALSA
    Direct sample snooping device
dsnoop:CARD=ALSA,DEV=1
    bcm2835 ALSA, bcm2835 IEC958/HDMI
    Direct sample snooping device
hw:CARD=ALSA,DEV=0
    bcm2835 ALSA, bcm2835 ALSA
    Direct hardware device without any conversions
hw:CARD=ALSA,DEV=1
    bcm2835 ALSA, bcm2835 IEC958/HDMI
    Direct hardware device without any conversions
plughw:CARD=ALSA,DEV=0
    bcm2835 ALSA, bcm2835 ALSA
    Hardware device with all software conversions
plughw:CARD=ALSA,DEV=1
    bcm2835 ALSA, bcm2835 IEC958/HDMI
    Hardware device with all software conversions
sysdefault:CARD=Device
    USB Audio Device, USB Audio
    Default Audio Device
front:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    Front speakers
surround21:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    Direct sample mixing device
dsnoop:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    Direct sample snooping device
hw:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    Direct hardware device without any conversions
plughw:CARD=Device,DEV=0
    USB Audio Device, USB Audio
    Hardware device with all software conversions

I can then use these names, like hw:CARD=Device as param to aplay:

aplay -Dhw:CARD=Device woman.wav

Fine!

So complete script is now:

#!/usr/bin/env python

import RPi.GPIO as GPIO
from time import sleep
import subprocess
import logging
import os
import signal


GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)
GPIO.setup(3, GPIO.IN)
GPIO.setup(4, GPIO.IN)
GPIO.setup(17, GPIO.IN)
GPIO.setup(27, GPIO.IN)

proc = None

def playFile(file):
    global proc
    if(proc != None):
        logging.critical('killing')
        os.kill(proc.pid, signal.SIGINT)
        proc.terminate()
    # proc = subprocess.Popen(['omxplayer', '-o', 'local', file])
    # proc = subprocess.Popen(['mpg123', '-q', file])
    sleep(0.1);
    proc = subprocess.Popen(['aplay', '-Dhw:CARD=Device', file + '.wav'])

while True:
    sleep(0.1);

    if (GPIO.input(2) == False):
        playFile('distant')

    if (GPIO.input(3) == False):
        playFile('boy')

    if (GPIO.input(4) == False):
        playFile('approval')

    if (GPIO.input(17) == False):
        playFile('swimmer')

    if (GPIO.input(27) == False):
        # playFile('woman')
        proc.terminate()


I remember I had some glitches when changing songs, I'll report back.

One more thing. I'm just using ssh to work with the PI but I'll want my local editor. So I'm doing:

> cat > audio-player3.py

Then paste my editor in terminal and hit ctrl-D

For some reason I think I have used more involved ways before.

Update:
USB sound card sounds just fine