3
library(shiny)
library(cognizer)


#' the required api keys 
username_TTS <-""
password_TTS <- ""
TEXT_TO_SPEECH_USERNAME_PASSWORD = paste(username_TTS,":",password_TTS,sep="")

ui <- fluidPage(

   # Application title
   titlePanel("Just some text to speech example"),

   fluidRow(textInput("caption","Enter the caption"),actionButton("gobutton","submit")),
   fluidRow(verbatimTextOutput("answer"), width = 4),

  # plays from www/  
  fluidRow(tags$audio(src = "1.wav", type = "audio/wav", controls = NA), helpText("Key in any sentences, wait and press F5. Then you can play your audio"))


  )



server <- function(input, output) {

textfunction <- eventReactive(input$gobutton, {
    thetext <- input$caption
    text_audio(thetext, TEXT_TO_SPEECH_USERNAME_PASSWORD, directory = 'www', accept = "audio/wav")
    "Done!"

        })


output$answer <- renderText({textfunction()})


}

# Run the application 
shinyApp(ui = ui, server = server)

I created an app using Watson to convert text to speech. The user input a text sentence. Click submit and watson will convert the to audio file. HOWEVER, the audio file played is is always the previous text, unless you refresh the app via F5. How can i make the audio player take the latest audio file?

example: text1: hello submit and play will be "hello" text2: bye submit and play, still "hello" until i F5, then when i click play, its "bye"

Germ
  • 65
  • 6

1 Answers1

0

The problem is related to Shiny's caching of the audio file. A workaround, as described here, can help you:

Rename the wav file after it is queried and pass it into a reactive UI component using renderUI() on the server side and uiOutput() in the UI.

Please see a working example below:

library(shiny)
library(cognizer)

# the required api keys
username_TTS <-""
password_TTS <- ""
TEXT_TO_SPEECH_USERNAME_PASSWORD = paste(username_TTS,":",password_TTS,sep="")

ui <- fluidPage(

  # Application title
  titlePanel("Just some text to speech example"),

  fluidRow(textInput("caption","Enter the caption"),actionButton("gobutton","submit")),
  fluidRow(verbatimTextOutput("answer"), width = 4),

  # plays from www/
  fluidRow(
    uiOutput("play")
  )
)

server <- function(input, output) {
  observeEvent(input$gobutton, {
    unlink("www/*.wav")
    thetext <- input$caption
    text_audio(thetext, TEXT_TO_SPEECH_USERNAME_PASSWORD, directory = 'www', accept = "audio/wav")
    file.rename("www/1.wav", paste0("www/number",input$gobutton,".wav"))
    "Done!"
    output$play <- renderUI(
      tags$audio(src = paste0("temp.wav"), type = "audio/wav", controls = NA)
    )
    output$play <- renderUI(
      tags$audio(src = paste0("number", input$gobutton,".wav"), type = "audio/wav", controls = NA)
    )
    output$answer <- renderText(paste("Done!", input$gobutton))
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Community
  • 1
  • 1
Robert Hawlik
  • 442
  • 3
  • 15
  • Hi robert, I am currently reading the post. It is the same problem. However, i am stuck at renaming my wav file since the watson function output a wav file as 1.wav – Germ Mar 16 '17 at 16:16
  • tried many methods. I modify the function to do a file.rename. render in UI and output. All the audio files are created but not played. Issue2: How can i delete the audio files after playing. Each time a text is submitted, the audio file is permanently in the directory – Germ Mar 16 '17 at 16:54
  • I added a working example. You can delete the unnecessary files by using `file.remove()`. I am worried that this is not a particularly stable version if you have multiple clients using your app. – Robert Hawlik Mar 16 '17 at 17:15
  • thanks @Robert I manage to make a working version but i could not make it read more than one words. Your version works perfectly! Now, to figure out how this works. I am experimenting. So, not particularly worried on multiple users – Germ Mar 16 '17 at 17:35
  • how do i file.remove? is there anyway i can specify file,remove after playing the audio file? – Germ Mar 16 '17 at 18:18
  • I fixed the code by adding `unlink("www/*.wav")`. Now all wav file are deleted before downloading the next one. – Robert Hawlik Mar 16 '17 at 18:47