R/app_server.R

Defines functions app_server

#' The application server-side
#'
#' @param input,output,session Internal parameters for {shiny}.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_server <- function( input, output, session ) {
  # List the first level callModules here

  observeEvent(input$yt_input_button, {

    current_file = "inst/app/www/preview_test.mp3"

    if(file.exists(current_file)) {

    unlink(current_file)

    }
    get_yt_audio(input$yt_url_input)
    serialise_file_input()
    session$sendCustomMessage('update_file', TRUE)
  })


  output$plotui = renderUI({
    #req(my_sample_prev$sample_path)
    plotOutput(session$ns("audio_amp_graph"), height = 300,
               brush = brushOpts(id = "plot_brush", direction = "x",
                                 fill = "red", opacity = 0.5)
    )
  })
  output$audio_amp_graph = renderPlot({

    audio = 'inst/app/www/preview_test.mp3'
    graph_input = tuneR::readMP3(audio)
    #graph_input = tuneR::readWave('www/preview_test.wav')
    snd = graph_input@left
    sound_wave_df = data.frame(wave = snd, index = seq(1:length(snd)))
    skip_rows = round(length(snd)/65)
    sound_wave_df_n = sound_wave_df[seq(1, nrow(sound_wave_df), skip_rows), ]
    posi <- sound_wave_df_n %>% dplyr::mutate(wave = abs(wave))
    negi <- sound_wave_df_n %>% dplyr::mutate(wave = abs(wave) * -1)
    new_df <- posi %>% dplyr::bind_rows(negi)
    ggplot2::ggplot(data=new_df, aes(x=index, y=wave)) +
      ggplot2::geom_col(fill = "white") + ggplot2::theme_void() + ggplot2::theme(panel.background = element_rect(fill = "#1e1e1e", colour = "#1e1e1e"))


  })
  selected_range = eventReactive(input$plot_brush,{
    list(xmin = round(input$plot_brush$xmin), xmax = round(input$plot_brush$xmax))
  })

  output$text_range = renderText({
    interactive_range = selected_range()
    from = interactive_range$xmin
    to = interactive_range$xmax

    paste0(from, to)

  })

  # interactive_audio = reactive({
  #   audio = my_sample_prev$sample_path
  #   interactive_range = selected_range()
  #
  #   extract_wave_simple <- function(x, from, to){
  #     x@left <- x@left[from:to]
  #     x@right <- x@right[from:to]
  #
  #     return(x)
  #   }
  #
  #   if (interactive_range$xmin <= 0 ){
  #     test_audio = tuneR::readWave(audio) %>% extract_wave_simple(., from = 1, to = interactive_range$xmax)
  #     #tuneR::writeWave(test_audio, 'www/interactive_preview.wav')
  #   } else {
  #     test_audio = tuneR::readWave(audio) %>% extract_wave_simple(., from = interactive_range$xmin, to = interactive_range$xmax)
  #     #tuneR::writeWave(test_audio, 'www/interactive_preview.wav')
  #   }
  # })
  #
  # observeEvent(input$interactive_player, {
  #
  #
  #   audio <- my_sample_prev$sample_path
  #
  #   #print(is.null(input$plot_brush))
  #
  #   if (is.null(input$plot_brush) == TRUE) {
  #     temp_audio <- tuneR::readWave(audio)
  #
  #   } else {
  #     temp_audio  <- interactive_audio()
  #
  #   }
  #
  #
  #   writeWave(temp_audio, "www/PREVIEW_SL_INTERACTIVE.wav")
  #
  #   audio <- "www/PREVIEW_SL_INTERACTIVE.wav"
  #
  #   audio_uri <- markdown:::.b64EncodeFile(audio)
  #   js$beep(audio_uri)
  #
  # })

}
myownelixir2/soundutilsdev documentation built on Dec. 5, 2020, 6:26 a.m.