Skip to content

Commit

Permalink
Release 5.0.6 - See CHANGELOG.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tiredofit committed Aug 15, 2020
1 parent dc3d57c commit 28ac83d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 48 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 5.0.6 2020-08-15 <dave at tiredofit dot ca>

### Changed
- Fix for FOP2 not starting up
- Add debug statements to watson-transcription script
- Repair issues with watson transcription


## 5.0.5 2020-08-14 <hobbit378@github>

### Fixed
Expand Down
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ RUN echo "Package: libxml2*" > /etc/apt/preferences.d/libxml2 && \
cd /usr/src/asterisk/ && \
make distclean && \
contrib/scripts/get_mp3_source.sh && \
\
cd /usr/src/asterisk && \
./configure \
--with-jansson-bundled \
Expand Down
4 changes: 3 additions & 1 deletion install/etc/cont-init.d/10-freepbx
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,11 @@ if var_true "$ENABLE_FOP" ; then
mkdir -p /var/log/fop
chown -R asterisk. /var/log/fop
sed -i "s/manager_host=.*/manager_host=127.0.0.1/g" /usr/local/fop2/fop2.cfg
silent service fop2 restart
/usr/local/fop2/fop2_server -D
fi

chown asterisk:asterisk /usr/sbin/watson-transcription

silent service apache2 restart

if var_true "$ENABLE_XMPP" ; then
Expand Down
116 changes: 70 additions & 46 deletions install/usr/sbin/watson-transcription
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# 16/07/2015 - V1.5 - Handle natively GSM WAV (thanks to Michael Munger)
# 01/08/2020 - V2.0 - Modded for revised IBM Watson URLs and Docker tiredofit/docker-freepbx
# 01/08/2020 - V2.1 - Further tweaks to integrate within tiredofit/docker-freepbx
# 15/08/2020 - V2.2 - Add Debug statements (Set environment variable CONTAINER_LOG_LEVEL=DEBUG to see)

# Requires
# dos2unix
Expand All @@ -21,16 +22,22 @@
source /assets/functions/00-container
source /assets/defaults/10-freepbx

print_debug "Set Path"
# set PATH
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

print_debug "Create Temporary Directory"
# create a temporary directory and cd to it
TMPDIR=$(mktemp -d)
cd "$TMPDIR"
print debug "Now working in ${TMPDIR}"

# dump the stream to a temporary file
cat >> stream.org
print_debug "Dumping Stream to Temporary File"
cat >>stream.org
print_debug "Temporary file is $(du -k "stream.org" | cut -f 1) kilobytes"

print_debug "Getting Boundaries"
# get the boundary
BOUNDARY=$(grep "boundary=" stream.org | cut -d'"' -f 2)

Expand All @@ -40,98 +47,115 @@ BOUNDARY=$(grep "boundary=" stream.org | cut -d'"' -f 2)
# stream.part2 - body of the message
# stream.part3 - attachment in base64 (WAV file)
# stream.part4 - footer of the message
print_debug "Cutting file into parts"
awk '/'$BOUNDARY'/{i++}{print > "stream.part"i}' stream.org

# if mail is having no audio attachment (plain text)
PLAINTEXT=$(cat stream.part1 | grep 'plain')
if [ "$PLAINTEXT" != "" ]
then
if [ "$PLAINTEXT" != "" ]; then
print_debug "File has no audio attachment"

# prepare to send the original stream
cat stream.org > stream.new

cat stream.org >stream.new
print_debug "Sending to new Stream"
# else, if mail is having audio attachment
else

print_debug "File has audio attachment"
# cut the attachment into parts
# stream.part3.head - header of attachment
# stream.part3.wav.base64 - wav file of attachment (encoded base64)
sed '7,$d' stream.part3 > stream.part3.wav.head
sed '1,6d' stream.part3 > stream.part3.wav.base64
print_debug "Cutting Attachment into parts"
sed '7,$d' stream.part3 >stream.part3.wav.head
sed '1,6d' stream.part3 >stream.part3.wav.base64

# convert the base64 file to a wav file
print_debug "Converting Base64 information into WAV file"
dos2unix -o stream.part3.wav.base64
base64 -di stream.part3.wav.base64 > stream.part3.wav
base64 -di stream.part3.wav.base64 >stream.part3.wav

# convert wave file (GSM encoded or not) to PCM wav file
print_debug "Convert WAV file to PCM"
sox stream.part3.wav stream.part3-pcm.wav

if var_true "$ENABLE_VM_TRANSCRIBE" ; then

CURL_OPTS=""
# Do not change the API_USERNAME
API_USERNAME="apikey"
# Set the API_PASSWORD to relevant $ENV variable
# IBM Watson PAYG account with speech transcription enabled - is required
# More info at https://www.ibm.com/uk-en/cloud/watson-speech-to-text
# See also http:https://nerdvittles.com/?page_id=25616 for more info on how to setup the IBM account
API_PASSWORD="$VM_TRANSCRIBE_APIKEY"
# Set the voice model to a valid IBM model
# All models available via https://cloud.ibm.com/docs/speech-to-text?topic=speech-to-text-models
API_MODEL=$VM_TRANSCRIBE_MODEL

curl -s "$CURL_OPTS" -k -u $API_USERNAME:"$API_PASSWORD" -X POST \
--limit-rate 40000 \
--header "Content-Type: audio/wav" \
--data-binary @stream.part3.wav \
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true&model=$VM_TRANSCRIBE_MODEL" 1>audio.txt

# Extract transcript results from JSON response
TRANSCRIPT=$(cat audio.txt | grep transcript | sed 's#^.*"transcript": "##g' | sed 's# "$##g')
if var_true "$ENABLE_VM_TRANSCRIBE"; then
print_debug "Voicemail Transcription Enabled"
CURL_OPTS=""
# Do not change the API_USERNAME
API_USERNAME="apikey"
# Set the API_PASSWORD to relevant $ENV variable
# IBM Watson PAYG account with speech transcription enabled - is required
# More info at https://www.ibm.com/uk-en/cloud/watson-speech-to-text
# See also http:https://nerdvittles.com/?page_id=25616 for more info on how to setup the IBM account
API_PASSWORD="$VM_TRANSCRIBE_APIKEY"
# Set the voice model to a valid IBM model
# All models available via https://cloud.ibm.com/docs/speech-to-text?topic=speech-to-text-models
API_MODEL=$VM_TRANSCRIBE_MODEL
print_debug "Using API_MODEL ${VM_TRANSCRIBE_MODEL}"

print_debug "Sending Payload to Watson"
curl -s "$CURL_OPTS" -k -u $API_USERNAME:$API_PASSWORD -X POST \
--limit-rate 40000 \
--header "Content-Type: audio/wav" \
--data-binary @stream.part3.wav \
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?continuous=true&model=$VM_TRANSCRIBE_MODEL" 1>audio.txt

print_debug "Extracting Transcript Results"
# Extract transcript results from JSON response
TRANSCRIPT=$(cat audio.txt | grep transcript | sed 's#^.*"transcript": "##g' | sed 's# "$##g')
else
print_debug "Skipping Watson Transcription"
fi

# convert PCM wav file to mp3 file
# -b 24 is using CBR, giving better compatibility on smartphones (you can use -b 32 to increase quality)
# -V 2 is using VBR, a good compromise between quality and size for voice audio files
print_debug "Converting WAV to MP3"
lame -m m -b 24 stream.part3-pcm.wav stream.part3.mp3

# convert back mp3 to base64 file
base64 stream.part3.mp3 > stream.part3.mp3.base64
print_debug "Converting MP3 to Base64"
base64 stream.part3.mp3 >stream.part3.mp3.base64

# generate the new mp3 attachment header
# change Type: audio/x-wav or audio/x-WAV to Type: audio/mpeg
# change name="msg----.wav" or name="msg----.WAV" to name="msg----.mp3"
sed 's/x-[wW][aA][vV]/mpeg/g' stream.part3.wav.head | sed 's/.[wW][aA][vV]/.mp3/g' > stream.part3.mp3.head
print_debug "Creating Mp3 Attachment Header"
sed 's/x-[wW][aA][vV]/mpeg/g' stream.part3.wav.head | sed 's/.[wW][aA][vV]/.mp3/g' >stream.part3.mp3.head

# generate first part of mail body, converting it to LF only
print_debug "Generate 1st part of mail body"
mv stream.part stream.new
cat stream.part1 >> stream.new
cat stream.part2 >> stream.new
cat stream.part1 >>stream.new
cat stream.part2 >>stream.new

if [ "$VM_TRANSCRIBE" = "TRUE" ]
then
echo "--- Automated transcription result ---" >> stream.new
echo "$TRANSCRIPT" >> stream.new
if var_true "$ENABLE_VM_TRANSCRIBE"; then
print_debug "Showing Transcribe Details"
echo "--- Automated transcription result ---" >>stream.new
echo "$TRANSCRIPT" >>stream.new
fi

cat stream.part3.mp3.head >> stream.new
print_debug "Generate third part of mail body"
cat stream.part3.mp3.head >>stream.new
dos2unix -o stream.new

print_debug "Appending Base64 MP3 to mail body"
# append base64 mp3 to mail body, keeping CRLF
unix2dos -o stream.part3.mp3.base64
cat stream.part3.mp3.base64 >> stream.new
cat stream.part3.mp3.base64 >>stream.new

# append end of mail body, converting it to LF only
echo "" >> stream.tmp
echo "" >> stream.tmp
cat stream.part4 >> stream.tmp
print_debug "Append end of mail body"
echo "" >>stream.tmp
echo "" >>stream.tmp
cat stream.part4 >>stream.tmp
dos2unix -o stream.tmp
cat stream.tmp >> stream.new

cat stream.tmp >>stream.new
fi

# send the mail thru sendmail
print_debug "Sending stream via sendmail"
cat stream.new | sendmail -t

# remove all temporary files and temporary directory
print_debug "Cleaning up"
rm -rf "$TMPDIR"

0 comments on commit 28ac83d

Please sign in to comment.