index.html 22.6 KB
Newer Older
1
2
<!DOCTYPE html>
<html>
3
4
5
6
7
8
9
  <head>
    <meta charset="utf-8" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Purr Data</title>
    <meta name="description" content="Purr Data compiled for web with Emscripten" />
    <meta name="keywords" content="purr-data" />
    <meta name="generator" content="emsripten" />
10
11
	
	<link rel="shortcut icon" type="image/jpg" href="./purr.png"/>
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
    <link rel="stylesheet" type="text/css" href="./css/dejavu.css" />
    <link id="page_style" rel="stylesheet" type="text/css" href="./css/default.css" />
    <link rel="stylesheet" type="text/css" href="./css/webapp/webapp.css" />
    <link rel="stylesheet" href="libs/bootstrap/bootstrap.min.css" />
  </head>

  <body id="container">
    <!-- Modal -->
    <div class="modal fade" id="loading-modal" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
          <div class="modal-body text-center">
            <div class="spinner-border text-primary mt-2" style="width: 3rem; height: 3rem;" role="status">
              <span class="sr-only">Loading...</span>
            </div>
            <h3 class="mt-4">Loading Purr Data...</h3>
          </div>
        </div>
      </div>
    </div>

    <div id="container-app" class="container-fluid">
      <!-- Menu -->
      <nav id="menu"></nav>

      <!-- Content -->
      <div class="d-flex content-webapp" id="content">
        <!-- Sidebar -->

41
        <div class="card collapse show" id="sidebar">
42
43
44
          <div class="card-body text-center" id="sidebar-body">
            <!-- Pd logo -->
            <div id="pd-info">
45
              <img src="./purr.png" alt="purr-data-logo" />
46
47
48
49
50
51
52
53
54
              <h4>Purr Data</h4>
            </div>
            <hr />
            <div id="sidebar-body-dialog"></div>
            <hr />
            <!-- DSP section -->
            <div class="d-flex ml-3">
              <i class="fa fa fa-volume-up text-primary" aria-hidden="true"></i>
              <h5>DSP</h5>
55
              <label class="dsp_toggle align-self-center ml-5">
56
57
58
59
60
61
62
                <input type="checkbox" id="dsp_control" name="dsp_control" value="on" />
                <span></span>
              </label>
            </div>
            <hr />
            <!-- Files section -->
            <div>
Prakhar Agarwal's avatar
Prakhar Agarwal committed
63
64
65
66
67
68
              <div>
                <div class="d-flex ml-3 float-left">
                  <i class="fa fa-folder-open-o text-primary" aria-hidden="true"></i>
                  <h5>Files</h5>
                </div>
                <i class="fa fa-refresh ml-5 mt-1 text-primary inline-block" id="reload-i" aria-hidden="true" onclick="pdbundle.pdgui.update_file_ls()"></i>
69
              </div>
Prakhar Agarwal's avatar
Prakhar Agarwal committed
70
              
71
              <div class="text-left" id="sidebar-files">
Prakhar Agarwal's avatar
Prakhar Agarwal committed
72
                <ul class="ml-3 mt-2" id="file_ls"></ul>
73
                <div class="text-secondary text-center" id="file_ls_empty">
Gunjan Yadu's avatar
Gunjan Yadu committed
74
                  <p><i>No files found yet</i></p>
75
76
77
78
79
80
                </div>
              </div>
            </div>
          </div>
        </div>

81
		<a id="sidebar-collapse" class="m-2" data-toggle="collapse" data-target="#sidebar">
Prakhar Agarwal's avatar
Prakhar Agarwal committed
82
			<i class="fa fa-angle-double-left rotate-transition" onclick="rotate(this)"></i>
83
84
		</a>

85
86
87
88
89
90
91
92
        <!-- Canvas/Pd Windows -->
        <div class="d-flex flex-column flex-fill" id="windows">
          <!-- Canvas Container -->
          <div class="flex-grow-1 card" id="canvas-content">
            <div class="d-flex" id="canvas-container"></div>
          </div>

          <!-- Console window -->
93
          <div class="card d-flex justify-content-between" id="console-window">
94
            <div class="card-header d-flex justify-content-between">
Prakhar Agarwal's avatar
Prakhar Agarwal committed
95
              <h5 class="h-100 w-25" id= "console-heading" onclick="rotate(this)">
96
                <a data-toggle="collapse" data-parent="#windows" href="#console_bottom" class="d-flex align-items-center text-dark">Console
Prakhar Agarwal's avatar
Prakhar Agarwal committed
97
                  <i class="ml-2 fa fa-angle-double-down text-primary rotate-transition"S id="open-icon"></i>
98
99
                </a>
              
100
101
102
103
              </h5>

              <div class="console-find-webapp" id="console_find" style="display: none;">
                <div class="d-flex justify-content-end">
104
                  <label><input type="text" id="console_find_text" name="console_find_text" defaultValue="Search in Console" style="width: 15em;" placeholder="Search in Console" /> </label>
105
                  <label class="highlight-find align-self-center" style = "color: #6c757d">
106
107
108
109
110
                    Highlight All
                    <input type="checkbox" id="console_find_highlight" name="console_find_highlight" onchange="console_find_highlight_all(this);" />
                  </label>
                </div>
              </div>
111
                <i class='fa fa-trash ml-3 text-primary' style='cursor:pointer' id='clear' data-toggle="tooltip" title="Clear Console"></i>
112
113
114
115
116
            </div>

            <div class="collapse show" id="console_bottom">
              <div class="card-block">
                <div id="printout">
117
                  <pre id="p1" class='ml-2' style="white-space: pre-wrap; overflow: hidden;"></pre>
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <script>
      //--------------------- emscripten ----------------------------
      var Module = {
        preRun: [],
        postRun: [],
        print: function (e) {
          1 < arguments.length && (e = Array.prototype.slice.call(arguments).join(" "));
          console.log(e);
        },
        printErr: function (e) {
          1 < arguments.length && (e = Array.prototype.slice.call(arguments).join(" "));
          console.error(e);
        },
        pd: {}, // make pd object accessible from outside of the scope
        mainInit: function () {
          // called after Module is ready
          Module.pd = new Module.Pd(); // instantiate Pd object
          // Remove modal after everything loaded
          $(document).ready(function () {
            $("#loading-modal").modal("hide");
          });

          if (typeof Module.pd != "object") {
            alert("Pd: failed to instantiate pd object");
            console.error("Pd: failed to instantiate pd object");
            Module.mainExit();
            return;
          }
          var pd = Module.pd;
          pd.setNoGui(false); // set to true if you don't use the pd's gui
          // initializing webapp
          initialize_webapp();

          // create an AudioContext
          var isWebAudioSupported = false;
          var audioContextList = [];
          (function () {
            var AudioContext = self.AudioContext || self.webkitAudioContext || false;
            if (AudioContext) {
              isWebAudioSupported = true;
              self.AudioContext = new Proxy(AudioContext, {
                construct(target, args) {
                  var result = new target(...args);
                  audioContextList.push(result);
                  return result;
                },
              });
            }
          })();
          if (isWebAudioSupported) {
            console.log("Audio: successfully enabled");
          } else {
            alert("The Web Audio API is not supported in this browser.");
            console.error("Audio: failed to use the web audio");
            Module.mainExit();
            return;
          }

          // check if the web midi library exists and is supported
          var isWebMidiSupported = false;
          if (typeof WebMidi != "object") {
            alert("Midi: failed to find the 'WebMidi' object");
            console.error("Midi: failed to find the 'WebMidi' object");
            Module.mainExit();
            return;
          }

          // array of enabled midi device ids (without duplicates)
          var midiInIds = [];
          var midiOutIds = [];

          // 10 input, 10 output device numbers to send with "pd midi-dialog"
          // 0: no device, 1: first available device, 2: second available device...
          var midiarr = [];

          // enable midi
          WebMidi.enable(function (err) {
            if (err) {
              // if the browser doesn't support web midi, one can still use pd without it
              alert("The Web MIDI API is not supported in this browser.\nPlease check: https://github.com/djipco/webmidi#browser-support");
              console.error("Midi: failed to enable midi", err);
            } else {
              isWebMidiSupported = true;
              console.log("Midi: successfully enabled");

              // select all available input/output devices as default
              midiInIds = [];
              midiOutIds = [];
              for (var i = 0; i < WebMidi.inputs.length; i++) {
                midiInIds.push(WebMidi.inputs[i].id);
              }
              for (var i = 0; i < WebMidi.outputs.length; i++) {
                midiOutIds.push(WebMidi.outputs[i].id);
              }
              midiarr = [];
              for (var i = 0; i < 10; i++) {
                var devno = i < midiInIds.length ? i + 1 : 0;
                midiarr.push(devno);
              }
              for (var i = 0; i < 10; i++) {
                var devno = i < midiOutIds.length ? i + 1 : 0;
                midiarr.push(devno);
              }
              // called whenever input/output devices connection status changes
              function onConnectionChanged() {
                console.log("Midi: connection status changed");
                pdbundle.pdgui.pdsend("pd midi-dialog", midiarr.join(" ")); // send message to pd
              }
              // make sure we get only one callback at a time
              var timerId;
              WebMidi.addListener("connected", function (e) {
                clearTimeout(timerId);
                timerId = setTimeout(() => onConnectionChanged(), 100);
              });
              WebMidi.addListener("disconnected", function (e) {
                clearTimeout(timerId);
                timerId = setTimeout(() => onConnectionChanged(), 100);
              });
            }
          }, false); // not use sysex

          // reinit pd (called by "pd audio-dialog" message)
          Module.Pd.reinit = function (newinchan, newoutchan, newrate) {
            if (pd.init(newinchan, newoutchan, newrate, pd.getTicksPerBuffer())) {
              // print obtained settings
              console.log("Pd: successfully reinitialized");
              console.log("Pd: audio input channels: " + pd.getNumInChannels());
              console.log("Pd: audio output channels: " + pd.getNumOutChannels());
              console.log("Pd: audio sample rate: " + pd.getSampleRate());
              console.log("Pd: audio ticks per buffer: " + pd.getTicksPerBuffer());
            } else {
              // failed to reinit pd
              alert("Pd: failed to reinitialize pd");
              console.error("Pd: failed to reinitialize pd");
              Module.mainExit();
            }
          };

          // open midi (called by "pd midi-dialog" message)
          // receives input/output arrays of only selected devices
          // 0: first available device, 1: second available device...
          Module.Pd.openMidi = function (midiinarr, midioutarr) {
            if (!isWebMidiSupported) return;

            // if the selected device doesn't exist, use first available device instead
            midiinarr = midiinarr.map((item) => (item >= WebMidi.inputs.length || item < 0 ? 0 : item));
            midioutarr = midioutarr.map((item) => (item >= WebMidi.outputs.length || item < 0 ? 0 : item));

            // save this settings so we can check again later when connection status changes
            midiarr = [];
            for (var i = 0; i < 10; i++) {
              var devno = i < midiinarr.length ? midiinarr[i] + 1 : 0;
              midiarr.push(devno);
            }
            for (var i = 0; i < 10; i++) {
              var devno = i < midioutarr.length ? midioutarr[i] + 1 : 0;
              midiarr.push(devno);
            }
            // remove duplicates and convert device numbers to ids
            midiinarr = Array.from(new Set(midiinarr));
            midioutarr = Array.from(new Set(midioutarr));
            midiInIds = midiinarr.map((item) => WebMidi.inputs[item].id);
            midiOutIds = midioutarr.map((item) => WebMidi.outputs[item].id);

            // print all selected devices to the console
            for (var i = 0; i < midiInIds.length; i++) {
              var input = WebMidi.getInputById(midiInIds[i]);
              console.log("Midi: input" + (i + 1) + ": " + input.name);
            }
            for (var i = 0; i < midiOutIds.length; i++) {
              var output = WebMidi.getOutputById(midiOutIds[i]);
              console.log("Midi: output" + (i + 1) + ": " + output.name);
            }
            // receive midi messages from WebMidi and forward them to pd input
            function receiveNoteOn(e) {
              pd.sendNoteOn(e.channel, e.note.number, e.rawVelocity);
            }

            function receiveNoteOff(e) {
              pd.sendNoteOn(e.channel, e.note.number, 0);
            }

            function receiveControlChange(e) {
              pd.sendControlChange(e.channel, e.controller.number, e.value);
            }

            function receiveProgramChange(e) {
              pd.sendProgramChange(e.channel, e.value + 1);
            }

            function receivePitchBend(e) {
              // [bendin] takes 0 - 16383 while [bendout] returns -8192 - 8192
              pd.sendPitchBend(e.channel, e.value * 8192 + 8192);
            }

            function receiveAftertouch(e) {
              pd.sendAftertouch(e.channel, e.value * 127);
            }

            function receivePolyAftertouch(e) {
              pd.sendPolyAftertouch(e.channel, e.note.number, e.value * 127);
            }

            for (var i = 0; i < midiInIds.length; i++) {
              var input = WebMidi.getInputById(midiInIds[i]);
              if (input) {
                input.removeListener(); // remove all added listeners
                input.addListener("noteon", "all", receiveNoteOn);
                input.addListener("noteoff", "all", receiveNoteOff);
                input.addListener("controlchange", "all", receiveControlChange);
                input.addListener("programchange", "all", receiveProgramChange);
                input.addListener("pitchbend", "all", receivePitchBend);
                input.addListener("channelaftertouch", "all", receiveAftertouch);
                input.addListener("keyaftertouch", "all", receivePolyAftertouch);
              }
            }
          };

          // get midi in device name
          Module.Pd.getMidiInDeviceName = function (devno) {
            if (!isWebMidiSupported) return;
            if (devno >= WebMidi.inputs.length || devno < 0) {
              devno = 0;
            }
            var name = WebMidi.inputs[devno].name;
            var lengthBytes = lengthBytesUTF8(name) + 1;
            var stringOnWasmHeap = _malloc(lengthBytes);
            stringToUTF8(name, stringOnWasmHeap, lengthBytes);
            return stringOnWasmHeap;
          };

          // get midi out device name
          Module.Pd.getMidiOutDeviceName = function (devno) {
            if (!isWebMidiSupported) return;
            if (devno >= WebMidi.inputs.length || devno < 0) {
              devno = 0;
            }
            var name = WebMidi.outputs[devno].name;
            var lengthBytes = lengthBytesUTF8(name) + 1;
            var stringOnWasmHeap = _malloc(lengthBytes);
            stringToUTF8(name, stringOnWasmHeap, lengthBytes);
            return stringOnWasmHeap;
          };

          // receive gui commands (only called in gui mode)
          Module.Pd.receiveCommandBuffer = function (data) {
            var command_buffer = {
              next_command: "",
            };
            pdbundle.pdgui.perfect_parser(data, command_buffer);
          };

          // receive print messages (only called in no gui mode)
          Module.Pd.receivePrint = function (s) {
            console.log(s);
          };

          // receive from pd's subscribed sources
          Module.Pd.receiveBang = function (source) {};

          Module.Pd.receiveFloat = function (source, value) {};

          Module.Pd.receiveSymbol = function (source, symbol) {};

          Module.Pd.receiveList = function (source, list) {};

          Module.Pd.receiveMessage = function (source, symbol, list) {};

          // receive midi messages from pd and forward them to WebMidi output
          Module.Pd.receiveNoteOn = function (channel, pitch, velocity) {
            for (var i = 0; i < midiOutIds.length; i++) {
              var output = WebMidi.getOutputById(midiOutIds[i]);
              if (output) {
                output.playNote(pitch, channel, { rawVelocity: true, velocity: velocity });
              }
            }
          };

          Module.Pd.receiveControlChange = function (channel, controller, value) {
            for (var i = 0; i < midiOutIds.length; i++) {
              var output = WebMidi.getOutputById(midiOutIds[i]);
              if (output) {
                output.sendControlChange(controller, value, channel);
              }
            }
          };

          Module.Pd.receiveProgramChange = function (channel, value) {
            for (var i = 0; i < midiOutIds.length; i++) {
              var output = WebMidi.getOutputById(midiOutIds[i]);
              if (output) {
                output.sendProgramChange(value, channel);
              }
            }
          };

          Module.Pd.receivePitchBend = function (channel, value) {
            for (var i = 0; i < midiOutIds.length; i++) {
              var output = WebMidi.getOutputById(midiOutIds[i]);
              if (output) {
                // [bendin] takes 0 - 16383 while [bendout] returns -8192 - 8192
                output.sendPitchBend(value / 8192, channel);
              }
            }
          };

          Module.Pd.receiveAftertouch = function (channel, value) {
            for (var i = 0; i < midiOutIds.length; i++) {
              var output = WebMidi.getOutputById(midiOutIds[i]);
              if (output) {
                output.sendChannelAftertouch(value / 127, channel);
              }
            }
          };

          Module.Pd.receivePolyAftertouch = function (channel, pitch, value) {
            for (var i = 0; i < midiOutIds.length; i++) {
              var output = WebMidi.getOutputById(midiOutIds[i]);
              if (output) {
                output.sendKeyAftertouch(pitch, channel, value / 127);
              }
            }
          };

          Module.Pd.receiveMidiByte = function (port, byte) {};

          // default audio settings
          var numInChannels = 0; // supported values: 0, 1, 2
          var numOutChannels = 2; // supported values: 1, 2
          var sampleRate = 44100; // might change depending on browser/system
          var ticksPerBuffer = 32; // supported values: 4, 8, 16, 32, 64, 128, 256

          // open audio devices, init pd
          if (pd.init(numInChannels, numOutChannels, sampleRate, ticksPerBuffer)) {
            // print obtained settings
            console.log("Pd: successfully initialized");
            console.log("Pd: audio input channels:", pd.getNumInChannels());
            console.log("Pd: audio output channels:", pd.getNumOutChannels());
            console.log("Pd: audio sample rate:", pd.getSampleRate());
            console.log("Pd: audio ticks per buffer:", pd.getTicksPerBuffer());

            // add internals/externals help/search paths
            var helpPath = "purr-data/doc/5.reference";
            var extPath = "purr-data/extra";
            pd.addToHelpPath(helpPath);
            pd.addToSearchPath(extPath);
            pd.addToHelpPath(extPath);
            var dir = FS.readdir(extPath);
            for (var i = 0; i < dir.length; i++) {
              var item = dir[i];
              if (item.charAt(0) != ".") {
                var path = extPath + "/" + item;
                pd.addToSearchPath(path); // externals can be created without path prefix
                pd.addToHelpPath(path);
              }
            }
          } else {
            // failed to init pd
            alert("Pd: failed to initialize pd");
            console.error("Pd: failed to initialize pd");
            Module.mainExit();
          }
        },
        mainLoop: function () {
          // called every frame (use for whatever)
        },
        mainExit: function () {
          // this won't be called from emscripten
          console.error("quiting emscripten...");
          if (typeof Module.pd == "object") {
            Module.pd.clear(); // clear pd, close audio devices
            Module.pd.unsubscribeAll(); // unsubscribe all subscribed sources
            Module.pd.delete(); // quit SDL, emscripten
          }
          if (typeof WebMidi == "object") {
            WebMidi.disable(); // disable all midi devices
          }
        },
      };
Prakhar Agarwal's avatar
Prakhar Agarwal committed
505

Akash Negi's avatar
Akash Negi committed
506
    //Rotate Icon
Prakhar Agarwal's avatar
Prakhar Agarwal committed
507
508
509
510
511
512
513
    function rotate(elmnt) {
      var cons_head = elmnt.id === "console-heading";
      if (cons_head) {
        document.getElementById("open-icon").classList.toggle("rotate");
      } else {
        elmnt.classList.toggle("rotate");
      }
Akash Negi's avatar
Akash Negi committed
514
515
    }

516
517

      // Add pd module to pd
518
519
520

      //Clear Console
      document.getElementById("clear").addEventListener("click",()=>{
521
        document.getElementById('p1').innerHTML='';
522
523
  
      })
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
    </script>

    <!-- Pd Bundle -->
    <script src="dist/bundle.js"></script>

    <script src="index.js"></script>

    <script src="/utils/console_search.js"></script>

    <!-- Emscripten backend -->
    <script src="main.js"></script>

    <!-- Utils -->
    <script src="utils/actions.js"></script>
    <!-- <script src="utils/audio_ctx.js"></script>  -->
    <script src="utils/console_find.js"></script>

    <!-- Components -->
    <script src="components/menu/menu.js"></script>
    <script src="components/canvas/canvas.js"></script>

    <!-- Libs -->
    <!-- Webmidi -->
    <script src="libs/webmidi/webmidi.min.js"></script>

    <!-- jQuery -->
    <script src="libs/jquery/jquery-3.6.0.min.js"></script>
    <!-- jQuery UI -->
    <script src="libs/jquery/jquery-ui.js"></script>

    <!-- Fontawesome -->
    <script src="libs/fa/fontawesome_1b8a796d74.js"></script>

    <!-- Bootstrap -->
    <script src="libs/bootstrap/popper.min.js"></script>
    <script src="libs/bootstrap/bootstrap.min.js"></script>

    <!-- Show modal on load -->
    <script>
      $(document).ready(function () {
        $("#loading-modal").modal("show", { backdrop: "static", keyboard: false });
      });
Prakhar Agarwal's avatar
Prakhar Agarwal committed
566
567
568
569
570
571
572

      var counter = 0;
      $('#reload-i').click(function () {

      counter += 360;
      $('#reload-i').css('transform', 'rotate(' + counter + 'deg)')
      });
573
574
575
    </script>

  </body>
Akash Negi's avatar
Akash Negi committed
576
</html>