Skip to content

Instantly share code, notes, and snippets.

@oivvio
Created February 11, 2017 19:30
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 oivvio/1ef9b6d9deb2bfa354ca8bfb83459ab5 to your computer and use it in GitHub Desktop.
Save oivvio/1ef9b6d9deb2bfa354ca8bfb83459ab5 to your computer and use it in GitHub Desktop.
# Loop five second sample from a random mp3 in a folder you specify
# Needs sox and coreutils that you can install with http://brew.sh/ (if you're on a mac)
# so you need to run theses two:
# brew install sox
# brew install coreutils
#
# run in terminal like so
# bash ./playrandomaudio.sh "/path/to/folder/with/mp3s"
#
# Get the folder from the command line
FOLDER="$1"
# Pick a random file in the folder
FILE=$(find "$FOLDER" -type f|grep -i mp3|shuf -n 1)
# Set clip duration
DURATION=5
# Figure out how long the mp3 is with the help of sox
LEN=$(sox "$FILE" -n stat 2>&1|grep seconds|perl -pe 's|.*?(\d+).*|\1|')
# Set start position to a random location
START=$(( RANDOM % $(($LEN - $DURATION)) ))
# Extract $DURATION seconds of audio start at $START with the help of sox
sox "$FILE" loop.mp3 trim $START $DURATION
# Play the file foreever.
while [ 1 ];do play -q loop.mp3;done
# Quit by holding Ctrl-C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment