Skip to content

Commit

Permalink
working shiny speech app
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEdmondson1234 committed Nov 24, 2017
1 parent be2f8ff commit 2d101aa
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 19 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Expand Up @@ -38,5 +38,7 @@ Suggests:
rmarkdown,
rvest,
stringdist,
shinyjs,
tidyr,
tuneR,
xml2
6 changes: 6 additions & 0 deletions inst/shiny/capture_speech/DESCRIPTION
@@ -0,0 +1,6 @@
Title: Cloud Speech in Shiny
Author: Mark Edmondson
AuthorUrl: http://code.markedmondson.me
License: MIT
DisplayMode: Showcase
Type: Shiny
5 changes: 5 additions & 0 deletions inst/shiny/capture_speech/README.md
@@ -0,0 +1,5 @@
# Google Cloud Speech API Shiny app

This is a demo on using the [Cloud Speech API](https://cloud.google.com/speech/) with Shiny.

It uses `library(tuneR)` to process the audio file, and a JavaScript audio library from [Web Audio Demos](https://webaudiodemos.appspot.com/AudioRecorder/index.html) to capture the audio in your browser.
29 changes: 21 additions & 8 deletions inst/shiny/capture_speech/server.R
@@ -1,15 +1,14 @@
library(shiny)
library(tuneR)
library(googleLanguageR)
library(shinyjs)

# Define server logic required to draw a histogram
function(input, output, session){

input_audio <- reactive({
req(input$audio)
a <- input$audio

saveRDS(a, "raw_audio.rds")
if(length(a) > 0){
return(a)
} else {
Expand All @@ -28,16 +27,15 @@ function(input, output, session){
a1 <- a[1:audio_split]
a2 <- a[(audio_split+1):length(a)]

# construct wav object that the API likes
Wobj <- Wave(a1, a2, samp.rate = 44100, bit = 16)
Wobj <- normalize(Wobj, unit = "16", pcm = TRUE)
Wobj <- mono(Wobj)

wav_name <- paste0("audio",gsub("[^0-9]","",Sys.time()),".wav")

writeWave(Wobj, wav_name, extensible = FALSE)
message("saved wav to ", wav_name)

# "audio20171123132816.wav"
wav_name


Expand All @@ -46,21 +44,36 @@ function(input, output, session){

output$result_text <- renderText({
req(wav_name())
req(input$language)

if(input$language == ""){
stop("Must enter a languageCode - default en-US")
}

wav_name <- wav_name()

if(!file.exists(wav_name)){
return(NULL)
}

message("Calling API")
me <- googleLanguageR::gl_speech(wav_name, sampleRateHertz = 44100L)
str(me)
shinyjs::show(id = "api", anim = TRUE, animType = "fade", time = 1)

message("API returned: ", me$transcript$transcript)
# make API call
me <- gl_speech(wav_name,
sampleRateHertz = 44100L,
languageCode = input$language)

## remove old file
unlink(wav_name)

message("API returned: ", me$transcript$transcript)
shinyjs::hide(id = "api", anim = TRUE, animType = "fade", time = 1)

as.character(me$transcript$transcript)

})

output$the_time <- renderText({as.character(Sys.time())})


}
47 changes: 36 additions & 11 deletions inst/shiny/capture_speech/ui.R
@@ -1,31 +1,56 @@
library(shiny)
library(shinyjs)

shinyUI(
fluidPage(
useShinyjs(),
includeCSS("www/style.css"),
includeScript("www/main.js"),
includeScript("www/speech.js"),
includeScript("www/audiodisplay.js"),

titlePanel("Capture Speech"),

a("Adapted from Web Audio Demos",
href="https://webaudiodemos.appspot.com/AudioRecorder/index.html"),
titlePanel("Capture audio and send to Google Speech API"),

sidebarLayout(
sidebarPanel(
helpText("Click on the microphone to record, click again to send to Cloud Speech API"),
img(id = "record", src = "mic128.png", onclick = "toggleRecording(this);"),
helpText("Click on the microphone to record, click again to send to Cloud Speech API and wait for results."),
img(id = "record",
src = "mic128.png",
onclick = "toggleRecording(this);",
style = "display:block; margin:1px auto;"),
hr(),
div(id = "viz",
tags$canvas(id = "analyser"),
tags$canvas(id = "wavedisplay")
)
),
br(),
hr(),
selectInput("language", "Add a languageCode", choices = c("English (UK)" = "en-GB",
"English (Americans)" = "en-US",
"Danish" = "da-DK",
"French (France)" = "fr-FR",
"German" = "de-DE",
"Spanish (Spain)" = "es-ES",
"Spanish (Chile)" = "es-CL",
"Dutch" = "nl-NL",
"Romainian" = "ro-RO",
"Italian" = "it-IT",
"Norwegian" = "nb-NO",
"Swedish" = "sv-SE")),
helpText("Many more languages are supported in the API but I couldn't be bothered to put them all in - see here:",
a(href="https://cloud.google.com/speech/docs/languages", "Supported languages"))
),

mainPanel(
h3("Transcription"),
textOutput("result_text"),
textOutput("the_time")
helpText("Transcription will appear here when ready. (Can take 30 seconds +). Streaming support not implemented yet."),
shinyjs::hidden(
div(id = "api",
p("Calling API - please wait", icon("circle-o-notch fa-spin fa-fw"))
)),
h3(textOutput("result_text"))
)
)
),
helpText(
a("Adapted from Web Audio Demos",
href="https://webaudiodemos.appspot.com/AudioRecorder/index.html"))
))

0 comments on commit 2d101aa

Please sign in to comment.