recordServer: Server function for Recording User Audio

View source: R/recordServer.R

recordServerR Documentation

Server function for Recording User Audio

Description

The server function for recording user audio enables the 'stop' button after the user begins recording and checks to make sure the user has given the website permission to record audio in their browser. Requires the UI recordUI.

Usage

recordServer(
  id = "record",
  outFile,
  attempts = Inf,
  overwrite = FALSE,
  writtenStim = NULL,
  writtenDelay = 500
)

Arguments

id

The input ID associated with the record module. Must be the same as the id of recordUI().

outFile

Character or reactive expression. Where to store the audio file. Can indicate any subdirectory of the present working directory. If dynamic, wrap in reactive().

attempts

Numeric (Defaults to Inf). How many attempts to create this recording should the participant be allowed?

overwrite

Boolean. Defaults to FALSE so that a unique digit is appended to each filename so that all recordings will be saved even when the filename value is the same. If TRUE, will overwrite a file of the same name.

writtenStim

Either a character vector (for a single, static stimulus) or a reactive expression (created with reactive, for a stimulus that should be updated from trial to trial) representing a written stimulus that a participant will read while recording.

writtenDelay

Integer. How many milliseconds should elapse between the time the participant clicks record and the time the written stimulus appears? Defaults to 500. We recommend not using a value less than that.

Value

Returns a reactive expression containing: (1) n: the number of attempts at recording the current file and (2) file: the filename where only the most recent attempt is saved (previous attempts have been overwritten). Also returns a wav file in a filename comprising outPrefix and a unique four digit number.

Note

Must be placed at the top level of the application's server function. The "start" and "stop" buttons from this module can be accessed in the server code with the "id" of the module as follows: input[["id-start"]] or input[["id-stop"]]. The submit button can be accessed with input[["id-submit"]].

See Also

Must be used with recordUI. For a more flexible audio recording protocol, see startRec, stopRec, and useRecorder.

Other Audio recording module: recordUI()

Examples

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

  # Build UI
  ui <- fluidPage(

    ## Get background javascript ready
    useShinyjs(),

    ## Setup page layout
    div(id = "trialDiv",
        style = "text-align:center;",
        actionButton("next_trial", "NEXT"),

        ### Initialize the recording interface invisibly
        hidden(recordUI(id = "rec_module"))
    )
  )

  # 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("rec_module")

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

    ## Note that the call to recordServer() is at the top level of our app's server function
    ## And the returned filename and text to be read are wrapped in reactive()
    ## (since they need to be reactive).
    recordServer(id = "rec_module",
                 attempts = 3, overwrite = TRUE,
                 outFile = reactive(paste0("rec", rvs$trial_n, ".wav")),
                 writtenStim = reactive(paste0("This is recording ", rvs$trial_n, ".")))

    ## As with all speechcollectr modules, the submit button's Id
    ## can be accessed with the module id + `-submit`
    ## Here when the user clicks submit inside the recording module...
    observeEvent(input[["rec_module-submit"]], {

      ## Make the next button reappear
      showElement("next_trial")
    })

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


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