-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
121 lines (110 loc) · 4.83 KB
/
index.html
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
<html>
<head>
<title>Web MIDI API</title>
<script src="https://cdn.jsdelivr.net/npm/webmidi"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="js/rdfstore.js"></script>
</head>
<body>
<h1>rdf pi</h1>
<div>
<textarea id='rdfin' rows="40" cols="80">@prefix midi: <http://purl.org/midi-ld/midi#> .
@prefix midi-note: <http://purl.org/midi-ld/notes/> .
@prefix midi-prog: <http://purl.org/midi-ld/programs/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
midi:myevent a midi:NoteOnEvent ;
midi:channel 4 ;
midi:pitch 57 ;
midi:velocity 89 .
</textarea>
</div>
<script>
var turtleString = "";
function getSel() // javascript
{
// obtain the object reference for the <textarea>
var txtarea = document.getElementById("rdfin");
// obtain the index of the first selected character
var start = txtarea.selectionStart;
// obtain the index of the last selected character
var finish = txtarea.selectionEnd;
// obtain the selected text
var sel = txtarea.value.substring(start, finish);
// do something with the selected content
turtleString = sel;
}
var map = {}; // You could also use an array
onkeydown = onkeyup = function(e){
e = e || event; // to deal with IE
map[e.keyCode] = e.type == 'keydown';
if (map[17] && map[13]) {
console.log('ctrl enter!!!');
// rdfstorejs for parsing turtle
rdfstore.create(function(err, store) {
getSel();
store.load("text/turtle", turtleString, function(err, results) {
store.setPrefix('midi', 'http://purl.org/midi-ld/midi#');
// simple query execution
store.execute("SELECT * { ?s ?p ?o }", function(err, results){
if(!err) {
// process results
for (var i=0; i<results.length; i++) {
if(results[i].s.token === 'uri') {
var s = results[i].s.value;
var p = results[i].p.value;
var o = results[i].o.value;
// console.log(s + ' ' + p + ' ' + o);
if (o == "http://purl.org/midi-ld/midi#NoteOnEvent") {
console.log("it's a note on!!");
var query = "PREFIX midi:<http://purl.org/midi-ld/midi#> SELECT * { <"+s+"> midi:channel ?channel}";
//console.log(query);
store.execute(query, function(err, results){
if(!err) {
var channel = results[0].channel.value;
console.log("channel: "+channel);
query = "PREFIX midi:<http://purl.org/midi-ld/midi#> SELECT * { <"+s+"> midi:pitch ?pitch}";
store.execute(query, function(err, results){
if (!err) {
var pitch = results[0].pitch.value;
console.log("pitch: "+pitch);
query = "PREFIX midi:<http://purl.org/midi-ld/midi#> SELECT * { <"+s+"> midi:velocity ?velocity}";
store.execute(query, function(err, results){
var velocity = results[0].velocity.value;
console.log('velocity :'+velocity);
// send the event to the web midi API
// Enable WebMidi.js
WebMidi.enable(function (err) {
if (err) {
console.log("WebMidi could not be enabled.", err);
}
// Retrieving an output port/device using its id, name or index
var output = WebMidi.outputs[1];
// Play a note on all channels of the selected output
output.playNote(pitch, channel, {duration: 2000, velocity: velocity});
WebMidi.disable();
});
});
}
});
} else{
console.log(err);
}
})
}
}
}
}
});
//
});
});
}
};
console.log('rdf pi ready!');
</script>
</body<
</html>