Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Gets the path to an audio file provided as the first parameter. Resamples the
- # audio to 44100 Hertz, turns stereo into mono, boosts frequences above 6
- # kilohertz, normalizes to 0dBFS, strips silence and pads with 100 milliseconds
- # of silence at the beginning and at the end of the audio. The supported audio
- # formats are WAV, MP3, OGG.
- ####### Functions #######
- function is_installed()
- {
- # Check if required apps are installed.
- local app_name="$1"
- if command -v "$app_name" > /dev/null; then
- return 0
- else
- return 1
- fi
- }
- function announce()
- {
- # Make announcements with espeak or echo if espeak is not installed.
- local message="$1"
- if is_installed "espeak"; then
- espeak -v "whisper" -s 300 "$message"
- else
- echo "$message"
- fi
- }
- function leave()
- {
- # Make the final announcement and exit with a specified exit status.
- local message="$1"
- local exit_status="$2"
- announce "$message"
- exit "$exit_status"
- }
- ####### Main #######
- if ! is_installed "sox"; then
- leave "SOX is not installed!" 1
- fi
- temp_file=$(mktemp --suffix='.wav')
- target_file="$1"
- if ! [ -f "$target_file" ]; then
- leave "The target file doesn't exist anymore!" 1
- fi
- mime_type=$(file --mime-type -b "$target_file")
- if [[ $mime_type != audio/mpeg && $mime_type != audio/ogg && $mime_type != audio/x-wav && $mime_type != audio/wav ]]; then
- leave "Not MP3, WAV or OGG" 1
- fi
- # Disable dithering because it may cause clipping, resample, convert to mono,
- # make headroom, amplify higher frequencies, normalize to 0 dBFS, remove
- # silence from the beginning and the end of the audio, pad the audio at the
- # end and at the beginning with silent segments 0.1 second long.
- sox "$target_file" -D -r 44100 -c 1 "$temp_file" norm -6 treble +6 6k norm silence 1 0.1 -40d reverse silence 1 0.1 -40d reverse pad 0.1 0.1
- sox "$temp_file" "$target_file" || leave "Couldn't reverse-convert the audio!" 1
- play "$target_file"
- # Mark the target audio file with a "check" ("tick") icon and timestamp the
- # file to make sure the icon appears without making the file manager refresh.
- if is_installed "gio"; then
- gio set "$target_file" -t stringv metadata::emblems emblem-default
- touch "$target_file"
- else
- announce "Gio is not installed! Can't mark the processed file!"
- fi
Advertisement
Add Comment
Please, Sign In to add comment