-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek_2_track_features.R
178 lines (147 loc) · 6.5 KB
/
week_2_track_features.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
library(tidyverse)
library(remotes)
library(spotifyr)
library(ggplot2)
library(grid)
library(gridExtra)
spotifyr::get_spotify_access_token()
bach_prelude_playlist = "4yNYY3xmNhPTDrfFc0qG9b"
bach_goldberg_aria_playlist = "0wwqrAl2LgAKGw0zuJRuwe"
music_for_frogs_playlist = "6G6qnBVUORSeTf4kA7vC22"
music_for_cats_playlist = "7gfsHECGdEKU7dFQEQCh1p"
music_for_rabbits_playlist = "2JKtxntKKWXm5K9F1jEfC4"
music_for_dogs_playlist = "48DvsLs8VirGB4BnCYWl2x"
reggae_for_dogs_playlist = "4CHnPKSVHnze8p1FaVOZeB"
bach_prelude_playlist_features = get_playlist_audio_features("", bach_prelude_playlist)
bach_prelude_playlist_features = bach_prelude_playlist_features %>% mutate(track.duration_min = track.duration_ms/1000/60)
wrapped_tempo_label = "Unmodified"
bach_prelude_playlist_features = fix_tempo_wrap(bach_prelude_playlist_features)
wrapped_tempo_label = "Wrapped"
summarize_bach = bach_prelude_playlist_features %>% summarize(Median_Tempo_bpm=median(tempo), Median_Duration_min=median(track.duration_min))
# Inject additional features:
# playlist_features = playlist_features %>% mutate("Instrument"="Piano")
special_bach_track_notes = c('', 'Gould', '', '', '', 'Harp', '', '',
'Axel Gillison', 'Harpsichord', '', '', '', '', '',
'Lang Lang', 'Schiff w/ Fugue', 'Jazz Arrangement',
'', '', '', '', '', '', '', '', '', '', '',
'', '')
special_bach_track_notes_key = c('', '', '', '', '',
'Harp', '', '', 'Axel Gillison', 'Harpsichord',
'', '', '', '', '',
'', '', '', 'Schiff w/ Fugue', 'Jazz Arrangement',
'', '', '', '', '', '', '', '', '',
'', '')
# Best first plot, as a sanity check
# Duration Histogram
hist1 = ggplot(bach_prelude_playlist_features, aes(
x=track.duration_min,
)) + geom_histogram(bins=12) + xlab("Track Duration (min)") + scale_y_continuous(breaks=c(2,4,6,8))
loud_v_acoustic = ggplot(bach_prelude_playlist_features, aes(
x=loudness, y=acousticness,
)) + geom_point() + xlab("Loudness") + ylab("Acousticness") +
geom_text(
label=special_bach_track_notes_key
) + ggtitle(paste(wrapped_tempo_label, " Loudness vs Acousticness", sep=''))
loud_v_acoustic
bach_harpsichord_key = c('', '', '', '', '',
'', '', '', '', 'Harpsichord',
'', '', '', '', '',
'', '', '', '', '',
'', '', '', '', '', '', '', '', 'Harpsichord',
'', '')
acoustic_v_duration = ggplot(bach_prelude_playlist_features, aes(
x=track.duration_min, y=acousticness,
)) + geom_point() + xlab("Duration (min)") + ylab("Acousticness") +
geom_text(
label=bach_harpsichord_key, nudge_x=0, nudge_y=0.02
) + ggtitle("Acousticness vs Duration")
acoustic_v_duration
instrumental_v_acoustic = ggplot(bach_prelude_playlist_features, aes(
x=acousticness, y=instrumentalness,
)) + geom_point() + xlab("Acousticness") + ylab("Instrumentalness") +
geom_text(
label=special_bach_track_notes_key
) + ggtitle(paste(wrapped_tempo_label, " Insturmental vs Acousticness", sep=''))
instrumental_v_acoustic
dance_v_tempo = ggplot(bach_prelude_playlist_features, aes(
x=tempo, y=danceability,
)) + geom_point() + xlab("Tempo (bpm)") + ylab("Danceability") +
geom_text(
label=special_bach_track_notes_key
) + ggtitle(paste(wrapped_tempo_label, " Danceability vs Tempo", sep=''))
dance_v_tempo
# Best first plot, as a sanity check
# Tempo Histogram
hist2 = ggplot(bach_prelude_playlist_features, aes(
x=tempo,
)) + geom_histogram(bins=12) + xlab("Tempo (bpm)") + scale_y_continuous(breaks=c(2,4,6,8))
grid_plots = grid.arrange(hist1, hist2, ncol = 2, top=textGrob(paste(wrapped_tempo_label, " Histograms", sep=''),))
ggsave(
paste(wrapped_tempo_label, " Histograms.png", sep=''),
plot = grid_plots,
dpi = 300,
limitsize = TRUE,
bg = NULL,
)
# Tempo vs Duration
Energy_v_duration = ggplot(bach_prelude_playlist_features, aes(
x=track.duration_min, y=energy,
)) + geom_point() + xlab("Track Duration (min)") + ylab("Energy") +
geom_text(
label=special_bach_track_notes_key)
Energy_v_duration
# Valence vs Duration
valence_v_duration = ggplot(bach_prelude_playlist_features, aes(
x=track.duration_min, y=valence,
)) + geom_point() + xlab("Track Duration (min)") + ylab("valence") +
geom_text(
label=special_bach_track_notes_key)
valence_v_duration
# Tempo vs Duration
tempo_v_duration = ggplot(bach_prelude_playlist_features, aes(
x=track.duration_min, y=tempo,
)) + geom_point() + xlab("Track Duration (min)") + ylab("Tempo (bpm)") +
geom_text(
label=special_bach_track_notes_key,
nudge_x=.15, nudge_y=1.7
) + ggtitle(paste(wrapped_tempo_label, " Tempo vs Duration", sep=''))
ggsave(
paste(wrapped_tempo_label, " Tempo vs Duration.png", sep=''),
plot = tempo_v_duration,
dpi = 300,
limitsize = TRUE,
bg = NULL,
)
# Axel Gillison corrected... doesn't work. I don't get r
fix_axel = function(bach_playlist_features) {
minor_third_ratio = 19/16
minor_third_ratio_invert = 16/19
bach_prelude_playlist_features = bach_prelude_playlist_features %>% mutate(track.duration_ms = case_when(key_mode=="A major" ~ track.duration_ms*minor_third_ratio_invert, TRUE ~ track.duration_ms))
bach_prelude_playlist_features = bach_prelude_playlist_features %>% mutate(tempo = case_when(key_mode=="A major" ~ tempo.*minor_third_ratio, TRUE ~ tempo))
bach_prelude_playlist_features = bach_prelude_playlist_features %>% mutate(key_mode = case_when(key_mode=="A major" ~ "C Major Corrected", TRUE ~ tempo))
}
fix_tempo_wrap = function(bach_playlist_features) {
tempo_threshold = 95;
time_threshold = 1.5;
bach_prelude_playlist_features = bach_playlist_features %>%
mutate(tempo = case_when(tempo>tempo_threshold & track.duration_min > time_threshold ~ tempo * 0.5, TRUE ~ tempo))
}
bach_prelude_playlist_features = fix_axel(bach_prelude_playlist_features)
# Tempo vs Key
key_plot = ggplot(bach_prelude_playlist_features, aes(
x=tempo, y=key_mode
)) + geom_point() +
geom_text(
label=special_bach_track_notes_key,
nudge_y=0.25,
) + xlab("Tempo (bpm)") + ylab("Key")
ggsave("Key vs Tempo.png",
plot = key_plot, dpi = 250, limitsize = TRUE, width=10, height=3)
# Tempo Histogram
ggplot(bach_prelude_playlist_features, aes(
x=tempo,
)) + geom_histogram()
# Debug for Tempo:
ggplot(bach_prelude_playlist_features, aes(
x=tempo, y=key_mode
)) + geom_point()