-
Notifications
You must be signed in to change notification settings - Fork 5
/
voice_typing
executable file
·71 lines (63 loc) · 2.45 KB
/
voice_typing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
# Copyright (C) 2023 by Henry Kroll III, www.thenerdshow.com
#
# With code contributions by Matthew Rensberry
#
# This is free software. You may redistribute it under the terms
# of the Apache license and the GNU General Public License Version
# 2 or at your option any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# fifo queue to hold temporary audio file names
audio_fifo=$(mktemp); rm "$audio_fifo" ; mkfifo "$audio_fifo"
## create a trap to remove temp files on untimely exit
cleanup() {
rm -f /tmp/tmp.txt "$audio_fifo"
}
trap cleanup 0
# function to process audio files from queue
trans(){
while read audio; do
# transcribe audio
whisper "$audio" --fp16 False --language English --model tiny\
--output_format txt --output_dir /tmp
# remove temporary audio file
rm -f "$audio"
output="$(</tmp/tmp.txt)"; rm -f /tmp/tmp.txt
# Type text to terminal, in background
# Thanks for watching! seems to occur frequently due to noise.
if [[ ${#output} > 15 ]] || [[ "$output" != "Thanks for watching!" ]] \
&& [[ "$output" != *'['* ]]; then
ydotool type "$output"
fi &
done < "$audio_fifo"
#cleanup
rm -f "$audio_fifo"
}
# record audio in background
while true; do
# Make temporary files to hold audio
tmp=$(mktemp)
# Remove it on exit
trap 'rm "$tmp" ; exit' INT
# Listen to the mic.
# The `1 0.2 4%` part of this command trims one clip of silence
# from the beginning. 4% of max volume is considered silence here.
# `1 1.0 2%` stops after a 1.0 second pause in speech.
# If recording never stops, increase sound threshold from 2%
# to 5% or more. This can happen with poor recording equipment
# or noisy environments with heaters and fans going.
rec -c 1 -r 22050 -t mp3 "$tmp" silence 1 0.2 4% 1 1.0 2%
# echo temporary audio file name to transcription queue
echo "$tmp"
done > "$audio_fifo" & #The `&` lets recording run in the background.
# run audio transciption handler
trans