library(httr)
library(dplyr)
api_key = 'Engr9t7gUHPERlnzpd7cDLqLl4zzaOoRDUg7m586'
client_id = 'vyxQp4X3sHS1jZU1JUzi'
code = 'ULZkz6wl4x8zDiUKM5zaH6TBUdlbBH'
search_query = 'kick'
#auth_flow_step_1 = httr::GET(glue::glue('https://freesound.org/apiv2/oauth2/authorize/?client_id={client_id}&response_type=code'))
#auth_flow_step_2 = httr::POST(glue::glue('https://freesound.org/apiv2/oauth2/access_token/?client_id={client_id}&client_secret={api_key}&grant_type=authorization_code&code={code}'))
#refresh_token = httr::content(auth_flow_step_2)$refresh_token
refresh_token = readRDS('inst/app/www/freesound_api_refresh_token.RDS')
auth_flow_step_3_access_token = httr::POST(glue::glue('https://freesound.org/apiv2/oauth2/access_token/?client_id={client_id}&client_secret={api_key}&grant_type=refresh_token&refresh_token={refresh_token}'))
access_token = httr::content(auth_flow_step_3_access_token)$access_token
get_results = function(search_query, api_key) {
#'https://freesound.org/apiv2/search/text/?query=music&filter=samplerate:44100%20type:wav%20channels:2'
result = httr::GET(glue::glue('https://freesound.org/apiv2/search/text/?query={search_query}&f=samplerate:"44100"%20duration:[0%20TO%2015]%20channels:"2"%20type:"wav"&page_size=150&token={api_key}'))
r_content = httr::content(result)
sample_id = lapply(r_content$results, '[[', 1) %>% unlist() %>% sample(1)
return(sample_id)
}
get_sound = function(sample_id, access_token) {
url = glue::glue("https://freesound.org/apiv2/sounds/{sample_id}/download/?access_token={access_token}")
local_path = glue::glue("inst/app/www/assets/temp_{sample_id}.wav")
download.file(url, local_path, quiet=TRUE)
temp_wav = tuneR::readWave(local_path)
unlink(local_path)
return(temp_wav)
}
search_qs = c('kick', 'snare', 'bass+synth', 'chords', 'hihat', 'vocal')
# SINGLE SAMPLES
sample_id = get_results(search_query, api_key)
my_sound = get_sound(sample_id, access_token)
# SAMPLE ARRAY
my_ids = purrr::map(search_qs, get_results, api_key)
my_wavs = purrr::map(my_ids, get_sound, access_token)
url = glue::glue("https://freesound.org/apiv2/sounds/{sample_id}/download/?access_token={access_token}")
download.file(url, "iris.wav", quiet=TRUE)
tuneR::readWave('iris.wav')
list.files('inst/app/www/assets/GCODY_midi_canon_01_backwards_G.mid')
tuneR::readMidi('inst/app/www/assets/GCODY_midi_canon_01_backwards_G.mid') %>% tuneR::getMidiNotes()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.