vmc (6908B)
1 #!/bin/bash 2 # 3 # DESCRIPTION 4 # 5 # Given a sentence file and (optionally) prerecorded audio files, produce 6 # a voice model in a specified location. Indicate whether to record 7 # (-newrecordings) or import (-importrecordings) the audio files. 8 # 9 # The statistical language model is now produced separately, run 'lmc' to 10 # see what the parameters are. 11 # 12 # There are two modes of operation: 1) adapt an existing model to use new 13 # audio data and 2) create a new model from scratch. In either case, 14 # there are three options for sourcing the audio: 1) record new audio, 2) 15 # import audio from a folder, or 3) add new recordings to the previously 16 # recorded audio data ('addrecordings', below). 17 # 18 # If importing audio, be aware that the model names of the acoustic m 19 # models used must match exactly. 20 # 21 # If creating a new model, there is a copy of the standard acoustic voice 22 # model stored in /opt/vmc/lib/en-us. Copy it into the home directory 23 # before use. (sudo cp /opt/vmc/lib/en-us ~/Documents/) 24 # 25 # If importing audio files, the associated text file with sentences must 26 # be located in the folder with the audio, and follow the naming and 27 # internal formatting standards. 28 # 29 # The [reps] variable at the end specifies how many times to request a 30 # recording of each entry in the sentence file. It is optional, as it is 31 # placed at the end of the list of parameters and the script will not 32 # fail if it is not specified. 33 # 34 # USAGE | EXAMPLES ------------------------------------------------------------ 35 # 36 # vmc en-us \ 37 # -adapt /extant/model/location \ 38 # -addrecordings /audio/files/location /dictation/file/location.txt 5 39 # 40 # vmc en-us \ 41 # -create /place/to/put/model \ 42 # -newrecordings /place/to/put/audio/files /dictation/file/location.txt 5 43 # 44 # vmc en-us \ 45 # -adapt /extant/model/location \ 46 # -importrecordings /audio/files/location 47 # 48 # vmc [ -remove OR -uninstall ] 49 # 50 # VARIABLES =================================================================== 51 52 model_name="$1" # i.e. 'en-us' 53 model_location="$3" # i.e. /usr/local/lib/python2.7/dist-packages/pocketsphinx/model/en-us 54 audio_folder="$5" # i.e. ~/.psyche/audio 55 56 if [ $4 == '-newrecordings' ] || [ $4 == '-addrecordings' ]; then 57 dict_file="$6"; reps="$7" 58 elif [ $1 == '-remove' ] || [ $1 == '-uninstall' ]; then 59 echo -n "" 60 else 61 echo "Bad options given. Run: 'nano /usr/local/bin/vmc'."; exit 1 62 fi 63 64 current_number_of_recordings="0" # changed below, if there are any. 65 66 # FUNCTIONS =================================================================== 67 68 make_sure_model_files_exist() { 69 70 for model_file in /opt/vmc/lib/en-us/* 71 do 72 local filename=$(basename $model_file) 73 if [ ! -f "$model_location/$filename" ] 74 then 75 sudo cp $model_file "$model_location/$filename" 76 fi 77 done 78 79 } 80 81 # COMMANDS ==================================================================== 82 83 # remove vmc ------------------------------------------------------------------ 84 85 if [ $1 == '-remove' ]; then 86 sudo rm -rf /opt/vmc 87 sudo rm -f /usr/local/bin/vmc 88 sudo rm -f /usr/local/bin/lmc 89 echo "vmc removed." 90 exit 0 91 fi 92 93 # misc. housekeeping ---------------------------------------------------------- 94 95 sudo ls 1>/dev/null # get sudo 96 97 export LD_LIBRARY_PATH=/usr/local/lib # include sphinx library location 98 # I need to do this any time I want to use the manually downloaded CMU Sphinx. 99 # http://jrmeyer.github.io/installation/2016/01/08/Installing-CMU-Sphinx-on-Ubuntu.html 100 101 if [ $2 == '-create' ] && [ ! -d "$model_location" ]; then 102 echo "Press [enter] to confirm writing new acoustic model at:" 103 echo "$model_location"; read 104 mkdir $model_location; cp -r /opt/vmc/lib/en-us/* "$model_location/"; 105 elif [ $2 == '-create' ] && [ -d "$model_location" ]; then 106 echo -n "MODEL already exists at this directory! Press [enter] to overwrite."; read 107 sudo rm -r $model_location 108 mkdir $model_location; cp -r /opt/vmc/lib/en-us/* "$model_location/"; 109 fi 110 111 # record new audio files ------------------------------------------------------ 112 113 if [ $4 = '-addrecordings' ]; then # find how many recordings were already made 114 current_number_of_recordings=$(ls $audio_folder/$model_name*.wav | tail -1 | sed 's/[^0-9]*//g' | bc -l); 115 echo $current_number_of_recordings 116 else 117 sudo mkdir -p $audio_folder 118 current_number_of_recordings='0' 119 fi 120 121 if [ $4 = '-newrecordings' ] && [ -d "$audio_folder" ] && [ "$(ls -A $audio_folder)" ]; then 122 echo -n "AUDIO already exists at this directory! Press [enter] to overwrite."; read 123 sudo rm -r $audio_folder; mkdir $audio_folder 124 fi 125 126 if [ $4 = '-newrecordings' ] || [ $4 = '-addrecordings' ]; then # record audio into folder. 127 128 sudo python3 /opt/vmc/lib/getaudio.py \ 129 $dict_file \ 130 $audio_folder \ 131 $reps \ 132 $model_name \ 133 $current_number_of_recordings 134 sudo chown -R $USER: $audio_folder # bug: dir had root ownership. 135 echo "Audio files saved into $audio_folder. They can be reused." 136 fi 137 138 # MOVE CONFLICTING LIB FILES INTO LIB TEMPORARILY ----------------------------- 139 140 bash /opt/vmc/lib/move_ps_files.sh -into_lib 141 142 # Make sure all the files exist that are needed ------------------------------- 143 144 make_sure_model_files_exist 145 146 # PRODUCE DERIVATIVE FILES ---------------------------------------------------- 147 # These are recreated for all audio files, regardless of whether this is a new 148 # collection of recordings or just adding to an old one. Simpler that way. 149 150 echo -e "\nProducing sentence file derivatives..." 151 sudo python3 /opt/vmc/lib/format_text.py \ 152 $dict_file \ 153 $model_name \ 154 $audio_folder \ 155 $reps \ 156 $current_number_of_recordings 157 158 echo "Producing audio file derivatives..." 159 sudo bash /opt/vmc/lib/acousticfiles.sh \ 160 "$audio_folder" \ 161 "$model_location" \ 162 "$audio_folder/$model_name.fileids" 163 164 # CREATE MODELS --------------------------------------------------------------- 165 166 echo "Creating voice model..." 167 sudo bash /opt/vmc/lib/voicemodel.sh $model_name $model_location $audio_folder 168 169 echo "Conflicting CMU Sphinx files moved into backup." 170 171 # FIX ROOT PERMISSIONS -------------------------------------------------------- 172 # I really hope this isn't horrible practice, but it's the most straightforward 173 # way to make sure the wrong stuff isn't left owned by root 174 sudo chown -R $USER: $model_location 175 sudo chown -R $USER: $audio_folder 176 177 # MOVE CONFLICTING LIBS OUT OF THE WAY ---------------------------------------- 178 179 bash /opt/vmc/lib/move_ps_files.sh -out_of_lib 180 181 # DONE ------------------------------------------------------------------------ 182 echo "Process complete." 183 echo -e "ACOUSTIC VOICE MODEL: \t $model_location" 184 echo -e "VOICE RECORDINGS: \t $audio_folder" 185