-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
374 lines (275 loc) · 11 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinyWidgets)
library(tuneR)
library(tidyverse)
library(ggiraph)
# clear out www folder in case it still has mp3s in it
if(!dir.exists("www")){
dir.create("www")
}
file.remove(list.files(path = "www",pattern = "note[0-9].mp3",full.names = TRUE))
file.remove(list.files(path = "www",pattern = "chord[0-9].mp3",full.names = TRUE))
file.remove(list.files(path = "www",pattern = "song.mp3",full.names = TRUE))
load("data/noteFreqTable.RData")
load("data/pianoKeyPlotData.RData")
load("data/pianoKeyPolygonData.RData")
source("code/helperFunctions.R")
noteFreqTable <- noteFreqTable %>%
# mutate(Note = str_extract(Note,"[A-Z][#]?[0-9]")) %>%
mutate(noteLabel = ifelse(str_detect(Note,"/"),
Note %>%
str_split("/") %>%
map_chr(~ paste0(.[2],"/",.[1])) %>%
str_remove("[0-9]"),
Note))
# save this to global environment so that it can be reset after finishing a chord
pianoKeyPlot <-
pianoKeyPolygonData %>%
ggplot() +
geom_polygon_interactive(aes(x=x,y=y,
group = noteOctave,data_id = noteOctave,fill = key),
colour = "black") +
geom_text(data = pianoKeyPlotData %>%
group_by(noteOctave,key) %>%
summarise(x = mean(c(xmin,xmax)),
y = ifelse(str_detect(noteOctave,"b"),.35,.05)),
aes(x = x,y = y,label = noteOctave,colour = key),
size = 2) +
scale_fill_manual(values = c("black","white")) +
scale_colour_manual(values = c("white","black")) +
# coord_fixed(ratio = 50,expand = FALSE) +
theme_void() +
theme(legend.position = "none")
# Define UI for application that draws a histogram
ui <- fluidPage(
tags$style(type='text/css', ".irs-grid-text { font-size: 7pt; }"),
shinyjs::useShinyjs(),
fluidRow(column(width = 12,ggiraph::ggiraphOutput(outputId = "pianoKeys",width = "100%",height = "500px"))),
splitLayout(selectInput(inputId = "waveType",label = "Wave Type",choices = c("sine","pulse")),
numericInput(inputId = "chordDuration",label = "Duration (sec)",value = 1,min = 0),
actionButton(inputId = "nextChord",label = "Next Chord"),
cellWidths = c("10%","10%","10%")),
br(),
fluidRow(column(width = 4,
uiOutput(outputId = "chordPlay"),
uiOutput(outputId = "noteSelection")),
column(width = 8,
plotOutput(outputId = "chordPlot")
# ,plotOutput(outputId = "chordSpect")
)),
br(),
fluidRow(
div(id = "songUI",
uiOutput(outputId = "songPlay"),
downloadButton(outputId = "songExport",label = "Export Song"),
# plotOutput(outputId = "songWaveform")
plotOutput(outputId = "songSpect")
)
)
)
# Define server logic required to draw a histogram
server <- function(session,input, output){
output$pianoKeys <- renderGirafe({
return(ggiraph::girafe(ggobj = pianoKeyPlot,
options = list(opts_selection(type = "multiple",
css = "fill:red;stroke:black")),
width_svg = 10,
height_svg = 2))
})
output$noteSelection <- renderUI({
req(length(input$pianoKeys_selected) > 0)
selectedNotes <- input$pianoKeys_selected
ret <- map2(selectedNotes,
1:length(selectedNotes),
function(note,ind){
return(tags$div(
br(),
id = paste0("note",ind,"ui"),
h5(paste0("Listen to ",note,":")),
uiOutput(outputId = paste0("note",ind,"Player"))
))
})
return(tagList(ret))
})
numChords <- 1
observeEvent(list(input$pianoKeys_selected,input$waveType,input$chordDuration),{
req(length(input$pianoKeys_selected) > 0)
#clear out the www folder each time we add a new note -- this ensures that
#the user can remove a note from a chord and it will be removed from the UI
file.remove(list.files(path = "www",pattern = "note[0-9].mp3",full.names = TRUE))
file.remove(list.files(path = "www",pattern = paste0("chord[0-9].mp3"),full.names = TRUE))
# save individual note mp3s
map2(input$pianoKeys_selected,
1:length(input$pianoKeys_selected),
function(note,ind){
# pull the frequency information from the selected note
selectedNoteFreq <- noteFreqTable %>%
filter(noteLabel == note) %>%
pull(freq_Hz)
# create a signal of a specific wave type
noteWave <- periodicSignal(waveType = input$waveType,
frequency = selectedNoteFreq,
duration = round(input$chordDuration*44100))
filename <- paste0("www/note",ind,".mp3")
# save it to the www file
tuneR::writeWave(noteWave,filename = filename)
# render an audio player below the slider input
output[[paste0("note",ind,"Player")]] <- renderUI({
return(tags$audio(controls = TRUE,
tags$source(src = paste0("note",ind,".mp3")),
title = paste0("Note ",ind)))
})
})
audioFiles <- list.files("www/",pattern = "note[0-9].mp3",full.names = TRUE)
req(length(audioFiles) > 0)
audioList <- map(audioFiles,
function(audioFile){
wave <- tuneR::readWave(audioFile)
})
combinedAudio <- Reduce(`+`,audioList)
output$chordPlay <- renderUI({
tuneR::writeWave(normalize(combinedAudio),filename = paste0("www/chord",numChords,".mp3"))
return(tags$div(
tags$h5("Listen to full chord:"),
tags$audio(controls = TRUE,
tags$source(src = paste0("chord",numChords,".mp3")),
title = "Listen to chord")))
})
output$chordPlot <- renderPlot({
individualWaves <- audioList %>%
map_dfr(.id = "note",
~ {
data.frame(val = .@left,
sec = seq(1/combinedAudio@samp.rate,input$chordDuration,by = 1/combinedAudio@samp.rate))
})
data.frame(val = combinedAudio@left,
sec = seq(1/combinedAudio@samp.rate,input$chordDuration,by = 1/combinedAudio@samp.rate)) %>%
ggplot(aes(x = sec,y = val)) +
geom_line(size = 1.5) +
geom_line(data = individualWaves,
aes(colour = note),alpha = .7) +
xlim(c(NA,.05)) +
labs(x = "Wavelength (sec)",
y = "Amplitude",
title = "Chord Waveform (50 ms)") +
theme_minimal()
})
# output$chordSpect <- renderPlot({
#
# spec <- signal::specgram(combinedAudio@left)
#
# expand_grid(tim = spec$t,f = spec$f) %>%
# mutate(S = abs(c(spec$S))) %>%
# mutate(S = 10*log10(S/max(S)),
# tim = 2*tim/combinedAudio@samp.rate) %>%
# ggplot(aes(x=tim,y=f,fill = S)) +
# geom_raster() +
# scale_fill_gradient(low = "purple",high = "orange") +
# theme_minimal() +
# theme(panel.grid = element_blank()) +
# labs(x = "Time (sec)",
# y = "Frequency",
# title = "Chord Spectrogram") +
# coord_cartesian(expand = FALSE)
#
# })
})
# the user can only move on to the next chord if they have at least one note
observe({
if(length(input$pianoKeys_selected) == 0){
shinyjs::hideElement(id = "nextChord")
}
else{
shinyjs::showElement(id = "nextChord")
}
})
# we won't show the song UI until the user starts a second chord
observe({
if(numChords < 2){
shinyjs::hideElement(id = "songUI")
}
})
observeEvent(input$nextChord,{
# concatenate the current chord to the song file in the data folder
chord <- tuneR::readWave(paste0("www/chord",numChords,".mp3"))
if(file.exists("www/song.mp3")){
song <- tuneR::readWave(paste0("www/song.mp3"))
tuneR::writeWave(object = tuneR::bind(song,chord),filename = "www/song.mp3")
# also save the song to the data folder since there's a lot of exhanging of files going on in www
tuneR::writeWave(object = tuneR::bind(song,chord),filename = "data/song.mp3")
}
else{
tuneR::writeWave(object = chord,filename = "www/song.mp3")
tuneR::writeWave(object = chord,filename = "data/song.mp3")
song <- chord
}
numChords <<- numChords + 1
shinyjs::showElement("songUI")
# visualize the song UI at the bottom of the app
output$songPlay <- renderUI({
tags$audio(controls = TRUE,
tags$source(src = paste0("song.mp3")),
title = "Listen to song")
})
output$songExport <- downloadHandler(
filename = function(){
paste0("song",Sys.Date(),".mp3",sep = "")
},
content = function(file){
ret <- tuneR::readWave("data/song.mp3")
tuneR::writeWave(ret,file)
}
)
output$songSpect <- renderPlot({
spec <- signal::specgram(song@left)
expand_grid(tim = spec$t,f = spec$f) %>%
mutate(S = abs(c(spec$S))) %>%
mutate(S = 10*log10(S/max(S)),
tim = 2*tim/song@samp.rate) %>%
ggplot(aes(x=tim,y=f,fill = S)) +
geom_raster() +
scale_fill_gradient(low = "purple",high = "orange") +
theme_minimal() +
theme(panel.grid = element_blank()) +
labs(x = "Time (sec)",
y = "Frequency",
title = "Song Spectrogram") +
coord_cartesian(expand = FALSE)
})
# reset the note and chord UI to start a new chord
# output$noteSelection <- NULL
shinyjs::reset(id = "noteSelection")
output$chordPlay <- NULL
output$chordPlot <- NULL
output$chordSpect <- NULL
output$pianoKeys <- NULL
output$pianoKeys <- renderGirafe({
return(ggiraph::girafe(ggobj = pianoKeyPlot,
options = list(opts_selection(type = "multiple",
css = "fill:red;stroke:black")),
width_svg = 10,
height_svg = 2))
})
})
# delete the temporary mp3 files and list of selected notes created while
# running the app
session$onSessionEnded(function() {
if(exists("noteSelections")){
rm(noteSelections,envir = .GlobalEnv)
}
file.remove(list.files(path = "www",pattern = "note[0-9].mp3",full.names = TRUE))
file.remove(list.files(path = "www",pattern = "chord[0-9].mp3",full.names = TRUE))
file.remove(list.files(path = "www",pattern = "song.mp3",full.names = TRUE))
file.remove(list.files(path = "data",pattern = "song.mp3",full.names = TRUE))
})
}
# Run the application
shinyApp(ui = ui, server = server)