1

I own a Snapstream Firefly IR remote control with a USB receiver. Connecting the receiver and using NOOBS to perform a fresh install of Raspbian, the Raspberry Pi receives input from the remote control without additional configuration. For instance, I can open a terminal and press the "A" button on the remote and "a" is output to the terminal window. Pressing the up button on the remote scrolls through the bash history as it would if pressing the up arrow on a keyboard.

After installing ir-keytable (sudo apt-get install ir-keytable) I can view the input from the remote. Pressing the OK button looks like this:

$ ir-keytable -t
Testing events. Please, press CTRL-C to abort.
1467816132.220648: event type EV_MSC(0x04): scancode = 0x1e
1467816132.220648: event type EV_KEY(0x01) key_down: KEY_OK(0x0001)
1467816132.220648: event type EV_SYN(0x00).
1467816132.220757: event type EV_KEY(0x04) key_up: KEY_OK(0x0001)
1467816132.220757: event type EV_SYN(0x00).

I want the Raspberry Pi to play a sound when I click a button on the remote. I want to be able to play various sounds associated with various buttons on the remote.

Is it necessary to install and configure lirc in order to accomplish my goal? I've been fighting with lirc for 3 days and I'd like to bypass it if it isn't the simplest solution.

How can I play a sound (wav, mp3) on my Raspberry Pi using my remote without using lirc?

  • Lirc is used to convert ir signals to keyboard events. Your USB device already does this. You need keyboard event handler to generate sounds. – PaulF8080 Jul 6 '16 at 16:33
2

Since your remote already sends normal keyboard commands, I'd recommend something simple that maps keypresses to play sound files.

The following simple Python script will do that. It uses termios to read a single character at a time, then calls out to ffmpeg's ffplay to play an audio file in response.

#!/usr/bin/python

import sys
import subprocess
import tty, termios

# The map of files ot play when something is pressed
map = {}
# This maps 'a', 'b', and 'c' to some random sound files
map[97] = 'coughing.mp3'
map[98] = 'cabinetdoorclosing.mp3'
map[99] = 'scifi9.mp3'
# This maps <enter> to exit the application
map[13] = 'EXIT'

# A simple implementation of getch
# Note: This won't work on Windows
def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(fd)
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

# The world's hackiest way to play an MP3, requires 
# ffplay (part of ffmpeg) to be installed
def play(path):
    subprocess.Popen(['ffplay', '-autoexit', '-nodisp', path]).wait()

# Main worker loop
while True:
    press = ord(getch())
    if press in map:
        if map[press] == 'EXIT':
            print "Exiting now"
            break
        else:
            print "Playing " + map[press] + " ..."
            play(map[press])
    else:
        print "Nothing to do for [" + str(press) + "]"
  • Great! Raspbian comes with omxplayer installed by default, so replacing subprocess.Popen(['ffplay', '-autoexit', '-nodisp', path]).wait() with subprocess.Popen(['omxplayer', path]).wait() removes a dependency. – Joel Harris Jul 6 '16 at 18:36
  • 1
    It looks like the remote USB is sending scan codes (MSC) events and the ir-keytable is mapping the codes. You might want to do the scan code handling yourself. That way you won't beep keyboard codes. – PaulF8080 Jul 6 '16 at 22:33
  • @PaulF8080 Thanks for the suggestion. I was able to come up with this – Joel Harris Jul 7 '16 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.