©2017 YOKE ApS. GPLv3 licensed.
This is a simple Node.js app for the Klogskabet audio module. It wraps an mpg123
process to play the MP3 audio files assigned to it in the Klogskabet CMS.
The code specifically targets a Raspberry Pi 3 running Raspbian "stretch", using a JustBoom DAC hat for audio output.
- Raspbian "stretch"
- Node.js 8.x & npm
mpg123
NOTE: The following assumes you're running a regular Raspberry Pi with a passwordless-sudo pi
user account. The config files for this app assumes the app will be installed in /home/pi/klogskabet-audio-player/
.
Of course it also assumes that the audio module has been built as specified.
First, if you haven't already, install the JustBoom DAC hardware and enable its device tree overlay (see also JustBoom's docs), by editing /boot/config.txt
(e.g. with $ sudo nano /boot/config.txt
):
- Change the line
dtparam=audio=on
todtparam=audio=off
- Add the line
dtoverlay=justboom-dac
Next, install dependencies (Node.js 8.x, device-tree compiler, and mpg123
):
$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
$ sudo apt-get update
$ sudo apt-get install -y nodejs build-essential device-tree-compiler mpg123
Download or clone this source code, and cd into the /home/pi/klogskabet-audio-player
directory (if you've downloaded a .zip from GitHub, you may have to rename the directory to get rid of the branch name).
$ cd klogskabet-audio-player
Compile the device-tree-overlay for the buttons, and move it to /boot/overlays/
:
$ dtc -W no-unit_address_vs_reg -@ -I dts -O dtb -o ./devicetree/klogskabet-audio-buttons.dtbo ./devicetree/klogskabet-audio-buttons.dts
$ sudo cp ./devicetree/klogskabet-audio-buttons.dtbo /boot/overlays/klogskabet-audio-buttons.dtbo
$ sudo chown root:root /boot/overlays/klogskabet-audio-buttons.dtbo
Install the overlay by editing /boot/config.txt
again and add the line dtoverlay=klogskabet-audio-buttons
.
Install npm packages
$ npm install
Set up the device's ID for use with the CMS by copying the example config, and editing it:
$ cp config/device.js.example config/device.js
$ nano config/device.js
Reboot the Raspberry Pi:
$ sudo reboot
Test the setup by issuing:
$ sudo node index.js
Provided everything works, perform these final two steps to make the app start automatically and manage log files:
Install the logrotate configuration:
$ sudo cp ./config/klogskabet-audio-player.logrotate /etc/logrotate.d/klogskabet-audio-player.logrotate
$ sudo chmod 0644 /etc/logrotate.d/klogskabet-audio-player.logrotate
$ sudo chown root:root /etc/logrotate.d/klogskabet-audio-player.logrotate
And install the systemd service script:
$ sudo cp ./config/klogskabet-audio-player.service /lib/systemd/system/klogskabet-audio-player.service
$ sudo systemctl daemon-reload
$ sudo systemctl enable klogskabet-audio-player.service
Reboot the Pi to ensure it starts up automatically. If you need to stop it when it's running as a service, you can issue:
$ sudo systemctl stop klogskabet-audio-player.service
And conversely, to start it again:
$ sudo systemctl start klogskabet-audio-player.service
The app loads a playlist of MP3 files from the Klogskabet CMS, and loops through it.
Three GPIO-connected buttons allow users to skip to the next/previous track, and play/pause the current track.
The player is rather simple:
Load and play the given file
Load - but don't auto-play - the given file
Toggle pause. Note that pausing might seem "delayed" slightly. This is mpg123's fault; it doesn't clear the audio buffer when pausing, so anything already in there will play.
Quit the mpg123 process. Note that if this is called, the player object becomes pretty usable.
A number describing the state:
0
: Stopped (no track loaded)
1
: Paused (track loaded)
2
: Playing
Whether anything is being played.
Get elapsed time in seconds.
Get remaining time in seconds.
State changed to playing.
State changed to paused.
Playback stopped.
State (playing/paused/stopped) changed. Listeners receive the new state number. This is emitted before the specfic paused
/playing
/stopped
events.
Elapsed/remaining time updated. Listeners receive both values as numbers. The values may be negative, indicating nothing's playing.
mpg123 spat out an error. Listeners receive the error text.
The version number in package.json
should be bumped for new releases (and npm install
should be run to update package-lock.json
before committing!).
- Pausing is "delayed" slightly, as mpg123 doesn't clear already-buffered audio when pausing, so that has to play through.
Adhere to the git-flow
model for branching etc..
Added server check-in to report IP addresses, and more robust file downloading.
Initial release.
See COPYING