Skip to content

Instantly share code, notes, and snippets.

@peterc
Last active April 5, 2019 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/7b9805e73ca0e1825594fa3bb3dd7e12 to your computer and use it in GitHub Desktop.
Save peterc/7b9805e73ca0e1825594fa3bb3dd7e12 to your computer and use it in GitHub Desktop.
CLI-based module music player that uses VLC
#!/usr/bin/env ruby
# playmod: plays module and other audio files in a directory using vlc
#
# on macOS: brew cask install vlc
require 'open3'
require 'io/console'
# Keep a copy of the child VLC process's PID around to terminate it nicely
# if we crash out
cpid = nil
# Trap CTRL+C and make sure we kill any child VLC process
trap ("SIGINT") do
Process.kill("SIGINT", cpid) if cpid
exit
end
# Go through every file in the directory by default
# (since module files have many different extensions, .s3m, .xm, .it, .mod, etc.)
Dir[ARGV.first || '*'].each do |file|
# Use UNIX `file` to identify the type of file
# Both MIME type (-I) and non-MIME type results are useful as
# .s3m files do not result in a audio/* MIME type for some reason..
type = IO.popen(["file", "-b", "-I", file]).read + IO.popen(["file", "-b", file]).read
next unless type =~ /audio|module|sound/i
# It's audio, so let's call vlc to play it
puts "Playing #{file}"
# --intf dummy means no GUI interface is displayed
# (cvlc works similarly but is not standard on macOS)
si, so, w = Open3.popen2e("vlc", "--intf", "dummy", "-q", file, "vlc://quit")
# Store the PID
cpid = w.pid
# Check STDIN in every second
# If 'enter' is pressed, skip the track
loop do
r = IO.select([STDIN], [], [], 1)
if r && r.first.first.read(1) == "\n"
Process.kill("SIGINT", cpid)
break
end
# If the current VLC ends, move on as well
if !w.alive?
break
end
end
end
@peterc
Copy link
Author

peterc commented Apr 5, 2019

Just run this in a folder full of .s3m, .mod, .xm, .it and similar files, and if you've got VLC installed, it'll play them nicely from the command line. Inspired by @bisqwit who does something similar.

@peterc
Copy link
Author

peterc commented Apr 5, 2019

Note that this script uses no unusual Ruby features and requires no third party gems, so should run on any modernish system that has Ruby installed.

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