├── .gitignore ├── images ├── forkme.png └── screenshot.jpg ├── test ├── resources │ ├── testharness.css │ ├── testharnessreport.js │ └── testharness.js └── index.html ├── index.html ├── js ├── voiceList.js ├── WebMIDIAPI.js ├── flatKeyboard.js ├── WebMIDIAPIWrapper.js └── ctrlWebMIDIAPIWrapper.js ├── README.md └── LICENSE.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /images/forkme.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryoyakawai/WebMIDIAPIWrapper/HEAD/images/forkme.png -------------------------------------------------------------------------------- /images/screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryoyakawai/WebMIDIAPIWrapper/HEAD/images/screenshot.jpg -------------------------------------------------------------------------------- /test/resources/testharness.css: -------------------------------------------------------------------------------- 1 | html { 2 | font-family:DejaVu Sans, Bitstream Vera Sans, Arial, Sans; 3 | } 4 | 5 | #log .warning, 6 | #log .warning a { 7 | color: black; 8 | background: yellow; 9 | } 10 | 11 | #log .error, 12 | #log .error a { 13 | color: white; 14 | background: red; 15 | } 16 | 17 | #log pre { 18 | border: 1px solid black; 19 | padding: 1em; 20 | } 21 | 22 | section#summary { 23 | margin-bottom:1em; 24 | } 25 | 26 | table#results { 27 | border-collapse:collapse; 28 | table-layout:fixed; 29 | width:100%; 30 | } 31 | 32 | table#results th:first-child, 33 | table#results td:first-child { 34 | width:4em; 35 | } 36 | 37 | table#results th:last-child, 38 | table#results td:last-child { 39 | width:50%; 40 | } 41 | 42 | table#results.assertions th:last-child, 43 | table#results.assertions td:last-child { 44 | width:35%; 45 | } 46 | 47 | table#results th { 48 | padding:0; 49 | padding-bottom:0.5em; 50 | border-bottom:medium solid black; 51 | } 52 | 53 | table#results td { 54 | padding:1em; 55 | padding-bottom:0.5em; 56 | border-bottom:thin solid black; 57 | } 58 | 59 | tr.pass > td:first-child { 60 | color:green; 61 | } 62 | 63 | tr.fail > td:first-child { 64 | color:red; 65 | } 66 | 67 | tr.timeout > td:first-child { 68 | color:red; 69 | } 70 | 71 | tr.notrun > td:first-child { 72 | color:blue; 73 | } 74 | 75 | .pass > td:first-child, .fail > td:first-child, .timeout > td:first-child, .notrun > td:first-child { 76 | font-variant:small-caps; 77 | } 78 | 79 | table#results span { 80 | display:block; 81 | } 82 | 83 | table#results span.expected { 84 | font-family:DejaVu Sans Mono, Bitstream Vera Sans Mono, Monospace; 85 | white-space:pre; 86 | } 87 | 88 | table#results span.actual { 89 | font-family:DejaVu Sans Mono, Bitstream Vera Sans Mono, Monospace; 90 | white-space:pre; 91 | } 92 | 93 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |Developed by Ryoya KAWAI.
62 | 63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/js/voiceList.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2013 Ryoya KAWAI
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | **/
16 |
17 | var VoiceList = function() {
18 | this.gmVoiceList = {
19 | 'instruments' : [
20 | 'Acoustic Grand Piano' ,'Bright Acoustic Piano'
21 | ,'Electric Grand Piano' ,'Honky-tonk Piano'
22 | ,'Rhodes Piano' ,'Chorused Piano'
23 | ,'Harpsichord' ,'Clavinet'
24 | ,'Celesta' ,'Glockenspiel'
25 | ,'Music Box' ,'Vibraphone'
26 | ,'Marimba' ,'Xylophone'
27 | ,'Tubular Bells' ,'Dulcimer'
28 | ,'Hammond Organ' ,'Percussive Organ'
29 | ,'Rock Organ' ,'Church Organ'
30 | ,'Reed Organ' ,'Accordion'
31 | ,'Harmonica' ,'Tango Accordion'
32 | ,'Acoustic Guitar (nylon)' ,'Acoustic Guitar (steel)'
33 | ,'Electric Guitar (jazz)' ,'Electric Guitar (clean)'
34 | ,'Electric Guitar (muted)' ,'Overdriven Guitar'
35 | ,'Distortion Guitar' ,'Guitar Harmonics'
36 | ,'Acoustic Bass' ,'Electric Bass (finger)'
37 | ,'Electric Bass (pick)' ,'Fretless Bass'
38 | ,'Slap Bass 1' ,'Slap Bass 2'
39 | ,'Synth Bass 1' ,'Synth Bass 2'
40 | ,'Violin' ,'Viola'
41 | ,'Cello' ,'Contrabass'
42 | ,'Tremelo Strings' ,'Pizzicato Strings'
43 | ,'Orchestral Harp' ,'Timpani'
44 | ,'String Ensemble 1' ,'String Ensemble 2'
45 | ,'SynthStrings 1' ,'SynthStrings 2'
46 | ,'Choir Aahs' ,'Voice Oohs'
47 | ,'Synth Voice' ,'Orchestra Hit'
48 | ,'Trumpet' ,'Trombone'
49 | ,'Tuba' ,'Muted Trumpet'
50 | ,'French Horn' ,'Brass Section'
51 | ,'Synth Brass 1' ,'Synth Brass 2'
52 | ,'Soprano Sax' ,'Alto Sax'
53 | ,'Tenor Sax' ,'Baritone Sax'
54 | ,'Oboe' ,'English Horn'
55 | ,'Bassoon' ,'Clarinet'
56 | ,'Piccolo' ,'Flute'
57 | ,'Recorder' ,'Pan Flute'
58 | ,'Bottle Blow' ,'Shakuhachi'
59 | ,'Whistle' ,'Whistle'
60 | ,'Ocarina' ,'Lead 2 (sawtooth)'
61 | ,'Lead 3 (calliope lead)' ,'Lead 4 (chiff lead)'
62 | ,'Lead 5 (charang)' ,'Lead 6 (voice)'
63 | ,'Lead 7 (fifths)' ,'Lead 8 (bass + lead)'
64 | ,'Pad 1 (new age)' ,'Pad 2 (warm)'
65 | ,'Pad 3 (polysynth)' ,'Pad 4 (choir)'
66 | ,'Pad 5 (bowed)' ,'Pad 6 (metallic)'
67 | ,'Pad 7 (halo)' ,'Pad 8 (sweep)'
68 | ,'FX 1 (rain)' ,'FX 2 (soundtrack)'
69 | ,'FX 3 (crystal)' ,'FX 4 (atmosphere)'
70 | ,'FX 5 (brightness)' ,'FX 6 (goblins)'
71 | ,'FX 7 (echoes)' ,'FX 8 (sci-fi)'
72 | ,'Sitar' ,'Banjo'
73 | ,'Shamisen' ,'Koto'
74 | ,'Kalimba' ,'Bagpipe'
75 | ,'Fiddle' ,'Shanai'
76 | ,'Tinkle Bell' ,'Agogo'
77 | ,'Steel Drums' ,'Woodblock'
78 | ,'Taiko Drum' ,'Melodic Tom'
79 | ,'Synth Drum' ,'Reverse Cymbal'
80 | ,'Guitar Fret Noise' ,'Breath Noise'
81 | ,'Seashore' ,'Bird Tweet'
82 | ,'Telephone Ring' ,'Helicopter'
83 | ,'Applause' ,'Gunshot'
84 | ],
85 | 'drums': [
86 | ]
87 | };
88 | };
89 |
90 | VoiceList.prototype.getGMVoiceName = function(type, num) {
91 | var out='';
92 | switch(type) {
93 | case 'instruments':
94 | default:
95 | out = this.gmVoiceList.instruments[num];
96 | break;
97 | case 'drums':
98 | out = this.gmVoiceList.drums[num];
99 | break;
100 | }
101 | return out;
102 | };
103 |
104 |
105 | var voiceList = new VoiceList();
106 |
107 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Web MIDI API Wrapper
2 |
3 | ## Live Demo
4 | [https://ryoyakawai.github.io/WebMIDIAPIWrapper/](https://ryoyakawai.github.io/WebMIDIAPIWrapper/)
5 |
6 | - Mac users: Select "Apple DLS Synth (0.Apple DLS Synth)" for output device.
7 | - Windows users: Select "Microsoft GS Wavetable Synth (0.Microsoft GS Wavetable Synth)" for output device.
8 |
9 | In the demo, js/ctrlctrlWebmidilib.js is the main JavaScript file.
10 |
11 | 
12 |
13 | ## What is this?
14 | This script is JavaScript wrapper for developers who wants to play with Musical Instruments and web browser!!
15 |
16 | - What is MIDI? -> [MIDI@wikipedia](http://en.wikipedia.org/wiki/MIDI)
17 | - What is Web MIDI API? -> [Web MIDI API@W3C](http://webaudio.github.io/web-midi-api/)
18 |
19 | ## Purpose of using this Wrapper
20 | MIDI is well defined protocol. But to use the protocol you must learn 7bit code, such as NoteOn: 9nH, NoteOff: 8nH.
21 | With using this wrapper you do NOT have to learn those 7bit code. And also, you do NOT have to know about method in Web MIDI API. A thing you must know is really basic JavaScript only!!
22 | Therefore, you can easily build MIDI application only with the wrapper and JavaScript !!
23 |
24 | ## Supported MIDI Message (as is 2013 Aug 19)
25 |
26 | - NoteOn
27 | - NoteOff
28 | - PitchBend
29 | - Sustain
30 | - Modulation
31 | - AllSoundOff
32 | - ResetAllController
33 | - AllNoteOff
34 |
35 | ## Requirements (as is 2013 Aug 19)
36 | Web MIDI API is NOT fully implemented in browser. Only [Chrome Canary](http://www.google.co.jp/intl/ja/chrome/browser/canary.html) accept MIDI input.
37 | So, please install [Jazz-Plugin](http://jazz-soft.net/) developed by Jazz-Soft.net.
38 |
39 | ## How to use
40 | - clone this repository
41 | - link to the wrapper and [Web MIDI API Shim](https://github.com/cwilso/WebMIDIAPIShim)
42 |
43 | ```
44 |
45 |
46 | ```
47 |
48 | - create constructor
49 |
50 | ```
51 | var wmaw = new WebMIDIAPIWrapper( false );
52 | ```
53 |
54 |
55 | - Set an EventHandler for **setMidiInputSelect** and **setMidiOutputSelect**
56 | - **setMidiInputSelect** : display MIDI *input* ports, and EventHandler of when MIDI *input* port is specified
57 | - **setMidiOutputSelect** : display MIDI *output* ports, and EventHandler of when MIDI *output* port is specified
58 | - Set an EventHandler named **onmidimessage** inside of setMidiInputSelect EventHandler.
59 |
60 | - Do initialization.
61 |
62 | ```
63 | wmaw.init();
64 | ```
65 |
66 | ## Method to send MIDI Messages
67 |
68 | **'sendNoteOn(portNo, ch, note, velocity, time)'**
69 | *description*: send noteOn message to ch of portNo.
70 | *values*:
71 |
72 | - portNo: port number to send message
73 | - ch: ch number to send message in the port
74 | - note: [noteNo](http://upload.wikimedia.org/wikipedia/commons/7/7a/NoteNamesFrequenciesAndMidiNumbers.svg) to noteOn
75 | - velocity : velocity of note
76 | - time: the time at which to begin sending the data to the port
77 |
78 | **'sendNoteOff(portNo, ch, note, velocity, time)'**
79 | *description*: send noteOff message to ch of portNo.
80 | *values*:
81 |
82 | - portNo: port number to send message
83 | - ch: ch number to send message in the port
84 | - note: [noteNo](http://upload.wikimedia.org/wikipedia/commons/7/7a/NoteNamesFrequenciesAndMidiNumbers.svg) to noteOff
85 | - velocity : velocity of note (some MIDI device accept the value as speed of noteOff)
86 | - time: the time at which to begin sending the data to the port
87 |
88 | **'sendProgramChange(portNo, ch, programNo, time)'**
89 | *description*: send programchange message to change voice
90 | *values*:
91 |
92 | - portNo: port number to send message
93 | - ch: ch number to send message in the port
94 | - programNo: voice number to change
95 | - time: the time at which to begin sending the data to the port
96 |
97 |
98 | **'setPitchBendValue(portNo, min, max, center)'**
99 | *description*: set the range of the pitch change value.(sometimes -8192 to 8191(center:0), 0 to 16383(center:8192). This value is depends on the MIDI device.)
100 | *values*:
101 |
102 | - portNo: port number to send message
103 | - min: minimum number of pitchbend value
104 | - max: maximum number of pitchbend value
105 | - center: center number of pitchbend value
106 |
107 | **'sendPitchBend(portNo, ch, value, time)'**
108 | *description*: send pitchbend message by value
109 | *values*:
110 |
111 | - portNo: port number to send message
112 | - ch: ch number to send message in the port
113 | - value: value to change
114 | - time: the time at which to begin sending the data to the port
115 |
116 | **'sendSustainStatus(portNo, ch, status, time)'**
117 | *description*: send sustain message by status
118 | *values*:
119 |
120 | - portNo: port number to send message
121 | - ch: ch number to send message in the port
122 | - status: "on" or "off"
123 | - time: the time at which to begin sending the data to the port
124 |
125 | **'sendModulationValue(portNo, ch, value, time)'**
126 | *description*: send modulation message by value
127 | *values*:
128 |
129 | - portNo: port number to send message
130 | - ch: ch number to send message in the port
131 | - value: value to change
132 | - time: the time at which to begin sending the data to the port
133 |
134 | **'sendAllSoundOff(portNo, ch, time)'**
135 | *description*: send AllSoundOff message to stop all sounds(even sustain and sound reflection) in channel.
136 | *values*:
137 |
138 | - portNo: port number to send message
139 | - ch: ch number to send message in the port
140 | - time: the time at which to begin sending the data to the port
141 |
142 | **'sendResetAllController(portNo, ch, time)'**
143 | *description*: send ResetAllController to reset all controller(pitchbend, after-touch and so on) to initial value in channel.
144 | *values*:
145 |
146 | - portNo: port number to send message
147 | - ch: ch number to send message in the port
148 | - time: the time at which to begin sending the data to the port
149 |
150 | **'sendAllNoteOff(portNo, ch, time)'**
151 | *description*: send AllNoteOff message to stop all sounds(sustain and sound reflection are NOTE affected) in channel.
152 | *values*:
153 |
154 | - portNo: port number to send message
155 | - ch: ch number to send message in the port
156 | - time: the time at which to begin sending the data to the port
157 |
158 | **'sendRaw(portNo, msg, time)'**
159 | *description*: send Raw MIDI message by array.
160 | *values*:
161 |
162 | - portNo: port number to send message
163 | - msg: set MIDI message as array. In the array, either hexadecimal or decimal (even mixture format) is allow to specify. e.g.) [0x80, 72, 120]
164 | - time: the time at which to begin sending the data to the port
165 |
166 | **'initializePerformanceNow()'**
167 | *description*: Initialize start time.
168 | *values*: no valuables
169 |
170 | ## License
171 |
172 | The Apache License Version 2.0
173 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction, and
10 | distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright
13 | owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all other entities
16 | that control, are controlled by, or are under common control with that entity.
17 | For the purposes of this definition, "control" means (i) the power, direct or
18 | indirect, to cause the direction or management of such entity, whether by
19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
20 | outstanding shares, or (iii) beneficial ownership of such entity.
21 |
22 | "You" (or "Your") shall mean an individual or Legal Entity exercising
23 | permissions granted by this License.
24 |
25 | "Source" form shall mean the preferred form for making modifications, including
26 | but not limited to software source code, documentation source, and configuration
27 | files.
28 |
29 | "Object" form shall mean any form resulting from mechanical transformation or
30 | translation of a Source form, including but not limited to compiled object code,
31 | generated documentation, and conversions to other media types.
32 |
33 | "Work" shall mean the work of authorship, whether in Source or Object form, made
34 | available under the License, as indicated by a copyright notice that is included
35 | in or attached to the work (an example is provided in the Appendix below).
36 |
37 | "Derivative Works" shall mean any work, whether in Source or Object form, that
38 | is based on (or derived from) the Work and for which the editorial revisions,
39 | annotations, elaborations, or other modifications represent, as a whole, an
40 | original work of authorship. For the purposes of this License, Derivative Works
41 | shall not include works that remain separable from, or merely link (or bind by
42 | name) to the interfaces of, the Work and Derivative Works thereof.
43 |
44 | "Contribution" shall mean any work of authorship, including the original version
45 | of the Work and any modifications or additions to that Work or Derivative Works
46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work
47 | by the copyright owner or by an individual or Legal Entity authorized to submit
48 | on behalf of the copyright owner. For the purposes of this definition,
49 | "submitted" means any form of electronic, verbal, or written communication sent
50 | to the Licensor or its representatives, including but not limited to
51 | communication on electronic mailing lists, source code control systems, and
52 | issue tracking systems that are managed by, or on behalf of, the Licensor for
53 | the purpose of discussing and improving the Work, but excluding communication
54 | that is conspicuously marked or otherwise designated in writing by the copyright
55 | owner as "Not a Contribution."
56 |
57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf
58 | of whom a Contribution has been received by Licensor and subsequently
59 | incorporated within the Work.
60 |
61 | 2. Grant of Copyright License.
62 |
63 | Subject to the terms and conditions of this License, each Contributor hereby
64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
65 | irrevocable copyright license to reproduce, prepare Derivative Works of,
66 | publicly display, publicly perform, sublicense, and distribute the Work and such
67 | Derivative Works in Source or Object form.
68 |
69 | 3. Grant of Patent License.
70 |
71 | Subject to the terms and conditions of this License, each Contributor hereby
72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
73 | irrevocable (except as stated in this section) patent license to make, have
74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where
75 | such license applies only to those patent claims licensable by such Contributor
76 | that are necessarily infringed by their Contribution(s) alone or by combination
77 | of their Contribution(s) with the Work to which such Contribution(s) was
78 | submitted. If You institute patent litigation against any entity (including a
79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a
80 | Contribution incorporated within the Work constitutes direct or contributory
81 | patent infringement, then any patent licenses granted to You under this License
82 | for that Work shall terminate as of the date such litigation is filed.
83 |
84 | 4. Redistribution.
85 |
86 | You may reproduce and distribute copies of the Work or Derivative Works thereof
87 | in any medium, with or without modifications, and in Source or Object form,
88 | provided that You meet the following conditions:
89 |
90 | You must give any other recipients of the Work or Derivative Works a copy of
91 | this License; and
92 | You must cause any modified files to carry prominent notices stating that You
93 | changed the files; and
94 | You must retain, in the Source form of any Derivative Works that You distribute,
95 | all copyright, patent, trademark, and attribution notices from the Source form
96 | of the Work, excluding those notices that do not pertain to any part of the
97 | Derivative Works; and
98 | If the Work includes a "NOTICE" text file as part of its distribution, then any
99 | Derivative Works that You distribute must include a readable copy of the
100 | attribution notices contained within such NOTICE file, excluding those notices
101 | that do not pertain to any part of the Derivative Works, in at least one of the
102 | following places: within a NOTICE text file distributed as part of the
103 | Derivative Works; within the Source form or documentation, if provided along
104 | with the Derivative Works; or, within a display generated by the Derivative
105 | Works, if and wherever such third-party notices normally appear. The contents of
106 | the NOTICE file are for informational purposes only and do not modify the
107 | License. You may add Your own attribution notices within Derivative Works that
108 | You distribute, alongside or as an addendum to the NOTICE text from the Work,
109 | provided that such additional attribution notices cannot be construed as
110 | modifying the License.
111 | You may add Your own copyright statement to Your modifications and may provide
112 | additional or different license terms and conditions for use, reproduction, or
113 | distribution of Your modifications, or for any such Derivative Works as a whole,
114 | provided Your use, reproduction, and distribution of the Work otherwise complies
115 | with the conditions stated in this License.
116 |
117 | 5. Submission of Contributions.
118 |
119 | Unless You explicitly state otherwise, any Contribution intentionally submitted
120 | for inclusion in the Work by You to the Licensor shall be under the terms and
121 | conditions of this License, without any additional terms or conditions.
122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of
123 | any separate license agreement you may have executed with Licensor regarding
124 | such Contributions.
125 |
126 | 6. Trademarks.
127 |
128 | This License does not grant permission to use the trade names, trademarks,
129 | service marks, or product names of the Licensor, except as required for
130 | reasonable and customary use in describing the origin of the Work and
131 | reproducing the content of the NOTICE file.
132 |
133 | 7. Disclaimer of Warranty.
134 |
135 | Unless required by applicable law or agreed to in writing, Licensor provides the
136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
138 | including, without limitation, any warranties or conditions of TITLE,
139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
140 | solely responsible for determining the appropriateness of using or
141 | redistributing the Work and assume any risks associated with Your exercise of
142 | permissions under this License.
143 |
144 | 8. Limitation of Liability.
145 |
146 | In no event and under no legal theory, whether in tort (including negligence),
147 | contract, or otherwise, unless required by applicable law (such as deliberate
148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be
149 | liable to You for damages, including any direct, indirect, special, incidental,
150 | or consequential damages of any character arising as a result of this License or
151 | out of the use or inability to use the Work (including but not limited to
152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or
153 | any and all other commercial damages or losses), even if such Contributor has
154 | been advised of the possibility of such damages.
155 |
156 | 9. Accepting Warranty or Additional Liability.
157 |
158 | While redistributing the Work or Derivative Works thereof, You may choose to
159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or
160 | other liability obligations and/or rights consistent with this License. However,
161 | in accepting such obligations, You may act only on Your own behalf and on Your
162 | sole responsibility, not on behalf of any other Contributor, and only if You
163 | agree to indemnify, defend, and hold each Contributor harmless for any liability
164 | incurred by, or claims asserted against, such Contributor by reason of your
165 | accepting any such warranty or additional liability.
166 |
167 |
--------------------------------------------------------------------------------
/js/WebMIDIAPI.js:
--------------------------------------------------------------------------------
1 | /* Copyright 2013 Chris Wilson
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
14 | */
15 |
16 | // Initialize the MIDI library.
17 | (function (global) {
18 | 'use strict';
19 | var midiIO, _requestMIDIAccess, _getMIDIAccess, MIDIAccess, _onReady, MIDIPort, MIDIInput, MIDIOutput, _midiProc;
20 |
21 | //init: create plugin
22 | if (!window.navigator.requestMIDIAccess) {
23 | window.navigator.requestMIDIAccess = _requestMIDIAccess;
24 | if (!window.navigator.getMIDIAccess)
25 | window.navigator.getMIDIAccess = _getMIDIAccess;
26 | }
27 |
28 | function Promise() {
29 |
30 | }
31 |
32 | Promise.prototype.then = function(accept, reject) {
33 | this.accept = accept;
34 | this.reject = reject;
35 | }
36 |
37 | Promise.prototype.succeed = function(access) {
38 | if (this.accept)
39 | this.accept(access);
40 | }
41 |
42 | Promise.prototype.fail = function(error) {
43 | if (this.reject)
44 | this.reject(error);
45 | }
46 |
47 | function _JazzInstance() {
48 | this.inputInUse = false;
49 | this.outputInUse = false;
50 |
51 | // load the Jazz plugin
52 | var o1 = document.createElement("object");
53 | o1.id = "_Jazz" + Math.random() + "ie";
54 | o1.classid = "CLSID:1ACE1618-1C7D-4561-AEE1-34842AA85E90";
55 |
56 | this.activeX = o1;
57 |
58 | var o2 = document.createElement("object");
59 | o2.id = "_Jazz" + Math.random();
60 | o2.type="audio/x-jazz";
61 | o1.appendChild(o2);
62 |
63 | this.objRef = o2;
64 |
65 | var e = document.createElement("p");
66 | e.appendChild(document.createTextNode("This page requires the "));
67 |
68 | var a = document.createElement("a");
69 | a.appendChild(document.createTextNode("Jazz plugin"));
70 | a.href = "http://jazz-soft.net/";
71 |
72 | e.appendChild(a);
73 | e.appendChild(document.createTextNode("."));
74 | o2.appendChild(e);
75 |
76 | var insertionPoint = document.getElementById("MIDIPlugin");
77 | if (!insertionPoint) {
78 | // Create hidden element
79 | var insertionPoint = document.createElement("div");
80 | insertionPoint.id = "MIDIPlugin";
81 | insertionPoint.style.position = "absolute";
82 | insertionPoint.style.visibility = "hidden";
83 | insertionPoint.style.left = "-9999px";
84 | insertionPoint.style.top = "-9999px";
85 | document.body.appendChild(insertionPoint);
86 | }
87 | insertionPoint.appendChild(o1);
88 |
89 | if (this.objRef.isJazz)
90 | this._Jazz = this.objRef;
91 | else if (this.activeX.isJazz)
92 | this._Jazz = this.activeX;
93 | else
94 | this._Jazz = null;
95 | if (this._Jazz) {
96 | this._Jazz._jazzTimeZero = this._Jazz.Time();
97 | this._Jazz._perfTimeZero = window.performance.now();
98 | }
99 | }
100 |
101 | function _requestMIDIAccess() {
102 | var access = new MIDIAccess();
103 | return access._promise;
104 | }
105 |
106 | // API Methods
107 |
108 | function MIDIAccess() {
109 | this._jazzInstances = new Array();
110 | this._jazzInstances.push( new _JazzInstance() );
111 | this._promise = new Promise;
112 |
113 | if (this._jazzInstances[0]._Jazz) {
114 | this._Jazz = this._jazzInstances[0]._Jazz;
115 | window.setTimeout( _onReady.bind(this), 3 );
116 | } else {
117 | window.setTimeout( _onNotReady.bind(this), 3 );
118 | }
119 | }
120 |
121 | function _onReady() {
122 | if (this._promise)
123 | this._promise.succeed(this);
124 | }
125 |
126 | function _onNotReady() {
127 | if (this._promise)
128 | this._promise.fail( { code: 1 } );
129 | }
130 |
131 | MIDIAccess.prototype.inputs = function( ) {
132 | if (!this._Jazz)
133 | return null;
134 | var list=this._Jazz.MidiInList();
135 | var inputs = new Array( list.length );
136 |
137 | for ( var i=0; i