File: midi.html

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 63,220 kB
  • sloc: ansic: 192,643; cpp: 14,149; javascript: 9,654; objc: 9,181; python: 3,376; java: 3,337; sh: 1,840; yacc: 1,255; xml: 985; perl: 635; lisp: 411; tcl: 341; lex: 217; makefile: 128
file content (163 lines) | stat: -rw-r--r-- 3,860 bytes parent folder | download | duplicates (3)
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
<!DOCTYPE html>
<html>

<head>
  <title>WebAudio Csound</title>
  <style type="text/css">
  html, body {
    font-family: Monospace;
    color: #bf94f7;
    background-color: #000055;
  }
  #console {
      font-family:  Monospace;
      color: #b5b6ff;
      background-color: #000000;
      font-size: 16px;
      width: 805px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-style: solid;
      padding: 20px 0px;
    }


   H1 {
     font-size: 36px;
   }linenr

   button {
      margin: auto;
      font-family: Monospace;
      width: 155px;
      padding: 10px 10px;
      font-size: 24px;
      background-color: #b5b6ff;
   }
  </style>
  <script type="text/javascript" src="js/csound.js"></script>
  <script type="text/javascript">



  function WebMIDI_init(midi_handle) {
    var haveDevs = false;
    var devs = midi_handle.inputs.values();
    for ( var m = devs.next(); m && !m.done; m = devs.next()) {
      m.value.onmidimessage = onMIDIEvent;
      haveDevs = true;
    }
    if (!haveDevs)
      console.log("No MIDIDevs");
    else {
      console.log("WebMIDI support enabled");
    }
  }

  function WebMIDI_err(err) {
      console.log("Error starting WebMIDI");
  }

  function onMIDIEvent(event) {
      switch (event.data[0] & 0xf0) {
        case 0x90:
          if (event.data[2]!=0) {
            csound.NoteOn(1, event.data[1], event.data[2]);
            return;
          }
        case 0x80:
          csound.NoteOff(1, event.data[1], event.data[2]);
          return;
      }
    }


    // called by csound.js
    function moduleDidLoad() {
      csound.Play();
      csound.CompileOrc(
        `massign 1,1
  instr 1
  icps = cpsmidi()
  chnset icps, \"freq\"
  k1 linenr 0.5,0.01,0.1, 0.01
  a1 oscili k1, icps
  outs a1,a1
  endin`);

  if (navigator.requestMIDIAccess)
        navigator.requestMIDIAccess().then(WebMIDI_init,
          WebMIDI_err);
      else
        console.log("No WebMIDI support");

    }

    function attachListeners() {
      var notes = ['c', 'd', 'e', 'g', 'a'];
      for (i = 0; i < notes.length; i++) {
        document.getElementById(notes[i]).
        addEventListener("mousedown", Play);
        document.getElementById(notes[i]).
        addEventListener("mouseup", Stop);
        document.getElementById(notes[i]).
        addEventListener("mouseleave", Stop);
        document.getElementById(notes[i]).
        addEventListener("touchstart", Play);
        document.getElementById(notes[i]).
        addEventListener("touchend", Stop);
        document.getElementById(notes[i]).
        addEventListener("touchmove", Stop);
      }
    }
    var count = 0;

    function handleMessage(message) {
      var element = document.getElementById('console');
      element.value += message;
      element.scrollTop = 99999; // focus on bottom
      count += 1;
      if (count == 1000) {
        element.value = ' ';
        count = 0;
      }
    }
    var started = false;
    var playing = false;
    // click handler
    function Play(event) {
      event.preventDefault();
      if (started == false) {
        CsoundObj.CSOUND_AUDIO_CONTEXT.resume();
        started = true;
      }
      let note = event.target.value;
      csound.NoteOn(1, note, 60);
      playing = true;
    }

    function Stop(event) {
      event.preventDefault();
      if (playing) {
        let note = event.target.value;
        csound.NoteOff(1, note, 60);
        playing = false;
      }
    }
  </script>
</head>

<body>
  <H1> Virtual MIDI Keys </H1>
  <p>
    <button id="c" value=60>C</button>
    <button id="d" value=62>D</button>
    <button id="e" value=64>E</button>
    <button id="g" value=67>G</button>
    <button id="a" value=69>A</button>
  </p>
  <textarea class="console" rows="20" id="console">Csound: not loaded</textarea>
</body>

</html>