Playing Audio files in Shiny R

Hey,
I am new to the R Studio community and I am trying to create an app using Shiny. The app lets you play the sound file as chosen by the user and the user can also alter the pitch. For now, I have created the code for the sound to be played and the code for altering the pitch is still to be written.

Well, I am facing a problem in the following code:

ui.R

library(shiny)
library(shinydashboard)
library(readr)
library(tuneR)
library(seewave)
library(sound)

ui <- fluidPage(
 titlePanel("Music Speed and Pitch Changer"),
 sidebarLayout(
   sidebarPanel(div(h4("Select a song"),style = "color: black;",
                    fileInput("file1","Choose File", multiple = F, accept = "audio/*")),
                div(h4("Play"),style = "color: black;",
                    actionButton("play","Play")),
                div(h4("Pitch"),style = "color: black;",
                    sliderInput("obs","Changes in semitones",min = -12.00, max = 12.00, value = 0.00),
                    div(p("A value smaller than zero means that the resulting key is lower than the original key (and vice-versa). A value of zero means that the key is not changed. One full step corresponds to one semitone. Decimals can be used for fine adjustments (for instance for non-western music styles). Example: a change of -12 semitones means that the song is played one octave lower."))),
                #div(h4("Speed"),style = "color: black;"),
                #sliderInput("obs","Change in tempo",min = 0.5, max = 2.0, value = 1),
                #div(p(" 1.0 is he original tempo, 0.5 is half tempo, 2.0 is double tempo"))
                ),
   mainPanel(div(h3("Output Song"),
                 actionButton("convert","convert")))
   )
)

server.R

library(shiny)
#library(shinydashboard)
library(readr)
library(tuneR)
library(seewave)
library(sound)

server <- function(input, output) {
  
  file <- reactive({file <- input$file1
  file <- readMP3(file)
  })
  
  setWavPlayer("C:/Program Files/Windows Media Player/wmplayer.exe")
  writeWave(file,"tmp.wav",extensible=FALSE)
  tmp <- readWave("tmp.wav")
    
    eventReactive(input$play, {
    insertUI(selector = "#play",
             where = "afterEnd",
             ui = tags$audio(src = "tmp.wav", type = "audio/wav", autoplay = NA, controls = NA, style="display:none;"))
  })
}

The error I am receiving on running the app:
Error in writeWave(file, "tmp.wav", extensible = FALSE) :
'object' needs to be of class 'Wave' or 'WaveMC'

Any help is much appreciated.
Thank You!

Hi,

Welcome to the RStudio community!

I don't have any of the files so I can't run your code, but here are some thoughts/comments:

  • The reactive function file looks a bit odd. You should not assign the read function to variable as it will have to be captured by the reactive output. So I suggest this:file <- reactive({readMP3(input$file1)})
  • You use a function called readMP3 to read a .wav file. That looks odd and might result in the error of incorrect format.
  • When calling a reactive variable like file, it needs to be in a reactive environment and the variable needs to be written like this: file(). Your setup now is not going to work as you have no reactive environment around the writeWave() function. So either you create a download button with reactive environment, or if you just want to save it locally and not have the user be able to access the saved file, use a regular button with an observeEvent()

Shiny has a bit of a learning curve, but once you get the hang of reactive environments it will all become clear :slight_smile:

PJ

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.