banovski

Pronunciation audio enhancement

Sep 25th, 2026 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.38 KB | Software | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Gets the path to an audio file provided as the first parameter. Resamples the
  4. # audio to 44100 Hertz, turns stereo into mono, boosts frequences above 6
  5. # kilohertz, normalizes to 0dBFS, strips silence and pads with 100 milliseconds
  6. # of silence at the beginning and at the end of the audio. The supported audio
  7. # formats are WAV, MP3, OGG.
  8.  
  9. ####### Functions #######
  10.  
  11. function is_installed()
  12. {
  13.     # Check if required apps are installed.
  14.     local app_name="$1"
  15.  
  16.     if command -v "$app_name" > /dev/null; then
  17.         return 0
  18.     else
  19.         return 1
  20.     fi
  21. }
  22.  
  23. function announce()
  24. {
  25.     # Make announcements with espeak or echo if espeak is not installed.
  26.     local message="$1"
  27.  
  28.     if is_installed "espeak"; then
  29.         espeak -v "whisper" -s 300 "$message"
  30.     else
  31.         echo "$message"
  32.     fi
  33. }
  34.  
  35. function leave()
  36. {
  37.     # Make the final announcement and exit with a specified exit status.
  38.     local message="$1"
  39.     local exit_status="$2"
  40.  
  41.     announce "$message"
  42.     exit "$exit_status"
  43. }
  44.  
  45. ####### Main #######
  46.  
  47. if ! is_installed "sox"; then
  48.     leave "SOX is not installed!" 1
  49. fi
  50.  
  51. temp_file=$(mktemp --suffix='.wav')
  52.  
  53. target_file="$1"
  54.  
  55. if ! [ -f "$target_file" ]; then
  56.     leave "The target file doesn't exist anymore!" 1
  57. fi
  58.  
  59. mime_type=$(file --mime-type -b "$target_file")
  60.  
  61. if [[ $mime_type != audio/mpeg && $mime_type != audio/ogg && $mime_type != audio/x-wav && $mime_type != audio/wav ]]; then
  62.     leave "Not MP3, WAV or OGG" 1
  63. fi
  64.  
  65. # Disable dithering because it may cause clipping, resample, convert to mono,
  66. # make headroom, amplify higher frequencies, normalize to 0 dBFS, remove
  67. # silence from the beginning and the end of the audio, pad the audio at the
  68. # end and at the beginning with silent segments 0.1 second long.
  69. 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
  70. sox "$temp_file" "$target_file" || leave "Couldn't reverse-convert the audio!" 1
  71. play "$target_file"
  72.  
  73. # Mark the target audio file with a "check" ("tick") icon and timestamp the
  74. # file to make sure the icon appears without making the file manager refresh.
  75. if is_installed "gio"; then
  76.     gio set "$target_file" -t stringv metadata::emblems emblem-default
  77.     touch "$target_file"
  78. else
  79.     announce "Gio is not installed! Can't mark the processed file!"
  80. fi
  81.  
Advertisement
Add Comment
Please, Sign In to add comment