evalWavServer: Give feedback to participants on recording quality

View source: R/evalWavServer.R

evalWavServerR Documentation

Give feedback to participants on recording quality

Description

Give feedback to participants on recording quality

Usage

evalWavServer(
  wave,
  counter = 1,
  min_sf = 44100,
  snr_best = 30,
  snr_good = 20,
  max_clip = 0.01,
  tries = Inf,
  onFail = "stop"
)

Arguments

wave

Required. Either a tuneR::Wave object or a valid file path to an existing wav file.

counter

Required. A value that tells this function how many tries a participant has had so far.

min_sf

What is the minimum sample rate (in Hz) that you will allow for the recording? If a user's browser will not allow audio recording at this high of a sample rate, the user will get an error message. Set to 0 if you do not want to exclude participants who record at low sampling rates.

snr_best

Integer. If eval=TRUE, what is the minimum SNR (dB) required for the recording to be considered of the best quality? Defaults to 30.

snr_good

Integer. If eval=TRUE, what is the minimum SNR (dB) required for the recording to be considered acceptable? Must be less than snr_best. Defaults to 20.

max_clip

Numeric. What proportion of the frames can be clipped and the recording still be acceptable? Defaults to .01.

tries

Integer. How many tries should the user get to create a quality recording?

onFail

What kind of message should the user receive if they try to record the maximum number of tries and the quality is still poor? Must be either "stop" (the default, user gets an error message) or "continue" (user gets a success message and we ignore recording quality).

Value

One of three reactive values: "pass"=recording of sufficient quality; "warn"=recording of insufficient quality, but the participant has had fewer attempts than the number of tries; or "fail"=the recording is of insufficient quality and the participant has used all available attempts.

Examples

if (interactive()) {
  library(shiny)
  library(shinyjs)

  # Build UI
  ui <- fluidPage(

    ## Get background javascript ready
    useShinyjs(),
    tags$head(
      useRecorder()
    ),

    actionButton("next_trial", "NEXT"),

    ## Setup page layout
    hidden(div(id = "trialDiv",
               style = "text-align:center;",

               hidden(div(id = "textDiv",
                          h4("Please record yourself reading this sentence aloud:"),
                          h2(textOutput(outputId = "read_this")))),

               ## Create the buttons for controlling the recording
               actionButton(inputId = "start",
                            label = "start"),

               ### Hide the stop button until user clicks start
               hidden(actionButton(inputId = "stop",
                                   label = "stop"))
    ))
  )

  # The Server function
  server <- function(input, output, session) {
    ## Create an object that will count trials
    rvs <- reactiveValues(trial_n = 0)

    ## When the participant clicks "NEXT"...
    observeEvent(input$next_trial, {
      ### Increase the trial number
      rvs$trial_n <- rvs$trial_n + 1

      ### Show the recording interface
      showElement("trialDiv")

      ### Hide the next button
      hide("next_trial")
    })

    ## When the start button is clicked
    observeEvent(input$start, {

      ### Start the recording
      startRec()

      ### Disable the start button
      disable("start")

      ### Show the stop button
      delay(500, showElement("stop"))
    })

    ## When the user gives permission to record....
    observeEvent(input[["rec-ready"]], {

      ### Show the text they should read
      showElement("textDiv")
      output$read_this <- renderText({paste0("This is recording ",
                                             rvs$trial_n, ".")})
    })


    ## When the user clicks stop
    observeEvent(input$stop, {

      ### Stop recording
      stopRec(filename = paste0("rec", rvs$trial_n, ".wav"))

      ### Enable the start button
      enable("start")

      ### Hide the stop button
      hide("stop")
      hide("textDiv")
    })

    ## Once the wav file exists...
    observeEvent(input[["rec-done"]], {
      ### Evaluate the recording
      evalWavServer(wave = paste(input[["rec-done"]]))
    })

    ## Once the wav file has been evaluated...
    observeEvent(input[["evalWav-result"]], {

      ### If the recording is of sufficient quality...
      if (input[["evalWav-result"]] == "pass") {

        #### Hide the recording interface
        hide("trialDiv")

        #### And show the "next" button so that the participant can record the next file
        showElement("next_trial")
      }
    })
  }

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


abbey-thomas/speechcollectr documentation built on Nov. 19, 2024, 7:09 p.m.