-
Notifications
You must be signed in to change notification settings - Fork 1
/
calma_chord.R
182 lines (161 loc) · 6.57 KB
/
calma_chord.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
library("RCurl")
library("SPARQL")
library("rrdf")
library("ggplot2")
library("magrittr")
library("dplyr")
setwd(tempdir())
#following line: see http://www.bramschoenmakers.nl/en/node/726
#options( java.parameters = "-Xmx3g" )
endpoint = "http://etree.linkedmusic.org/sparql"
etreeTrackQuery <- "
PREFIX etree:<http://etree.linkedmusic.org/vocab/>
PREFIX mo:<http://purl.org/ontology/mo/>
PREFIX event:<http://purl.org/NET/c4dm/event.owl#>
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
PREFIX timeline:<http://purl.org/NET/c4dm/timeline.owl#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX calma:<http://calma.linkedmusic.org/vocab/>
SELECT ?track ?calma
{
BIND(<http://etree.linkedmusic.org/artist/422feb50-4aac-012f-19e9-00254bd44c28> as ?artist) .
?artist mo:performed ?performance .
?performance event:hasSubEvent ?track .
?track calma:data ?calma ;
skos:prefLabel \"The Captain\" .
}
ORDER BY ?calma
LIMIT 102
"
whatFeaturesQuery <- "
prefix prov: <http://www.w3.org/ns/prov#>
select distinct ?feature where {
?file prov:wasAssociatedWith ?feature
}
ORDER BY ?feature"
calmaFeatureQuery <- "
PREFIX af: <http://purl.org/ontology/af/>
PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX event: <http://purl.org/NET/c4dm/event.owl#>
PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?event ?chord ?onsetTime where {
?event a af:ChordSegment ;
event:time ?time ;
rdfs:label ?chord .
?time tl:at ?onsetTime .
}
ORDER BY ?event"
graphify <- function(files, remote=TRUE) {
if(remote) {
files <- gsub('<|>', '', files) # get rid of uri decoration if any
data <- gsub('\\"', '"', getURL(files)) # un-escape quote marks
files = gsub("http://calma.linkedmusic.org/data/", "", files)
files = gsub("/", "--", files)
}
else { # got sent a local directory
files <- paste0(files, "/", list.files(files))
}
g = new.rdf()
for (f in 1:length(files)) {
if(remote){ # need to write to our local tmp folder
track <- files[f]
track <- regmatches(track, gregexpr(pattern = "track_[^/]*", track))[[1]]
cat(x=data[f], file=files[f], append=FALSE)
}
thisg = load.rdf(filename = files[f], format="TURTLE")
g = combine.rdf(g, load.rdf(filename = files[f], format="TURTLE"))
}
return(g)
}
untarBlobs <- function(blobURIs) {
tmp = tempfile(tmpdir = tempdir())
blobFiles <- basename(blobURIs)
for (f in 1:length(blobFiles)) {
download.file(blobURIs[f], blobFiles[f])
untar(blobFiles[f], compressed = 'gzip', exdir = tmp)
file = gsub(".tar.gz", "", blobFiles[f])
file = gsub(".tar.bz2", "", file)
file <- paste0(tmp, "/", file)
}
files <- paste0(tmp, "/", list.files(tmp))
print("---------------------")
for (f in 1:length(files)) {
contextualized <- gsub("<#>", paste0("<", files[f], "#>"), readLines(files[f]))
cat(x = contextualized, file=files[f], append = FALSE)
}
return(tmp)
}
queryFeatureFiles <- function(g, feature) {
calmaFileQuery <- paste0("
prefix prov: <http://www.w3.org/ns/prov#>
select distinct ?file where {
?file prov:wasAssociatedWith <", feature, ">
}
ORDER BY ?file")
files <- sparql.rdf(g, calmaFileQuery)
return (files)
}
calculateEventDurations <- function(featureData) {
calcPerTrack <- function(trackData) {
for(i in 1:nrow(trackData)) {
trackData[i, "duration"] <- trackData[i+1, "onsetTime"] - trackData[i, "onsetTime"]
# FIXME for last event, calculate based on track duration
}
return(trackData)
}
result <- data.frame()
tracks <- unique(featureData$track)
for (t in 1:length(tracks)){
result <- rbind(result, calcPerTrack(featureData[featureData$track == tracks[t], ]))
}
return (result)
}
result <- SPARQL(endpoint, etreeTrackQuery)$results
# chop off the <'s and >'s
calma <- substr(result$calma, 2, nchar(result$calma))
calma <- substr(calma, 1, nchar(calma)-1)
# add trailing /
calma <- paste0(calma, "/")
files <- paste0(calma, "analyses.ttl")
etreeCalma <- cbind(result, files)
g <- graphify(files)
# What features are available for these files?
features <- sparql.rdf(g, whatFeaturesQuery)
#featurefiles <- queryFeatureFiles(g, features[18]) # tempo
#featurefiles <- queryFeatureFiles(g, features[9]) # key
featurefiles <- queryFeatureFiles(g, features[5]) # simple chord
g <- graphify(featurefiles)
etreeCalma <- cbind(etreeCalma, featurefiles)
names(etreeCalma) <- c("etree", "calma", "analysis.ttl", "feature")
etreeCalma$etree <- factor(etreeCalma$etree)
etreeCalma$calma<- factor(etreeCalma$calma)
query = "select distinct ?blob where {
?s <http://calma.linkedmusic.org/vocab/feature_blob> ?blob
}
ORDER BY ?blob"
blobFileURIs <- sparql.rdf(g, query)
blobFileDir <- untarBlobs(blobFileURIs)
featureGraph <- graphify(blobFileDir, remote=FALSE)
summarize.rdf(featureGraph)
featureData <- as.data.frame(sparql.rdf(featureGraph, calmaFeatureQuery))
#featureData$track <- factor((gsub(".*/([^/#]+)#.*", "\\1", as.character(featureData$event),fixed=FALSE, perl=TRUE)))
featureData$track <- factor((gsub(".*/([^/#]+)\\.wav.*$", "\\1", as.character(featureData$event),fixed=FALSE, perl=TRUE)))
featureData$eventNum <- as.numeric(gsub(".*event_(\\d+)", "\\1", as.character(featureData$event),fixed=FALSE, perl=TRUE))
featureData$onsetTime <- as.numeric(gsub("[PTS]", "", as.character(featureData$onsetTime), fixed=FALSE, perl=TRUE))
featureData <- unique(featureData[order(featureData$track, featureData$eventNum),])
featureData <- calculateEventDurations(featureData)
featureData <- select(featureData, chord, track, duration) %>%
group_by(track, chord) %>%
filter(chord != "N") %>% # remove NA's
summarise(chord_duration = sum(duration, na.rm=TRUE))
featureData <- inner_join(featureData, select(featureData, track, chord_duration) %>%
group_by(track) %>%
summarise(chord_track_duration = sum(chord_duration, na.rm=TRUE)))
ggplot(featureData, aes(chord, chord_duration)) + geom_bar(stat="identity") + facet_wrap(~track, ncol=5) + theme_bw() +
#geom_text(data=chordmappings, aes(feature, 0, label=chord, angle=90), color="#aaaaaa", hjust=0, size=2) +
geom_text(aes(chord, chord_duration + 20, label = chord, size = 4*log(chord_duration/chord_track_duration)), color="#aaaaaa") +
labs(x="Feature (chord)", y = "Total duration (seconds)") + scale_y_continuous(breaks=seq(0,230,25)) +
theme(text = element_text(size = 10), axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
guides(size = FALSE)
#----scratch-----#