Skip to content

Commit f3da002

Browse files
committed
introduced typescript; reorganized project; default cc is undefined
1 parent c2cb35d commit f3da002

32 files changed

+102
-1330
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
build
1+
dist
22
node_modules

README.md

Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
# @react-midi/hooks
22

3-
This package provides hooks for working with midi in your React app.
3+
This package provides hooks for working with MIDI in your React app.
44

55
```
66
npm install --save @react-midi/hooks
7+
// or
8+
yarn add @react-midi/hooks
79
```
810

911
### `useMIDI()`
1012

1113
`useMidi()` requests midi access using the browser's web midi api.
12-
It returns and array containing MIDI inputs and outputs, respectively, and updates anytime a connection to one of these changes (for example, if an input or output is disconnected or a connection is opened).
14+
It returns an array of inputs, an array of outputs, and a boolean `hasMIDI` that is set to true if your browser supports midi. The inputs and outputs are updated anytime a change to their connection is made (for example, if an input or output is disconnected or a connection is opened).
1315

1416
```js
15-
const App = () => {
16-
const [inputs, outputs] = useMIDI(); // Initially returns [[], []]
17-
};
17+
const { inputs, outputs, hasMIDI } = useMIDI(); // Initially returns [[], []]
1818
```
1919

2020
### `useMIDINote(input, {note?, channel?}?)`
2121

2222
`useMIDINote()` subscribes to both note on and note off midi messages, optionally filtered by note and/or channel.
2323
It returns an object `{on, note, velocity, channel}` reflecting the received midi message, updating when a new relevant message is received.
2424

25-
- `on` will be `true` for note on, and `false` for note off
26-
- `note` will be a MIDI number (60 = C4)
27-
- `velocity` will be an integer between 0 - 127
25+
- `on` will be `true` for note on, and `false` for note off
26+
- `note` will be a MIDI number (60 = C4)
27+
- `velocity` will be an integer between 0 - 127
2828

2929
```js
3030
const App = () => {
31-
const [inputs, outputs] = useMIDI();
32-
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
33-
return <MIDINoteLog input={inputs[0]} />;
31+
const { inputs, outputs } = useMIDI();
32+
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
33+
return <MIDINoteLog input={inputs[0]} />;
3434
};
3535

3636
const MIDINoteLog = ({ input }) => {
37-
const event = useMIDINote(input, { channel: 1 }); // Intially returns {}
38-
const { on, note, velocity, channel } = event;
39-
return (
40-
<div>
41-
Note {note} {on ? 'on' : 'off'} ({velocity}) on channel {channel}
42-
</div>
43-
);
37+
const event = useMIDINote(input, { channel: 1 }); // Intially returns undefined
38+
if (!event) {
39+
return <div>Waiting for note...</div>;
40+
}
41+
const { on, note, velocity, channel } = event;
42+
return (
43+
<div>
44+
Note {note} {on ? 'on' : 'off'} ({velocity}) on channel {channel}
45+
</div>
46+
);
4447
};
4548
```
4649

@@ -51,14 +54,14 @@ It returns an object `{value, control, channel}` reflecting the received midi me
5154

5255
```js
5356
const App = () => {
54-
const [inputs, outputs] = useMIDI();
55-
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
56-
return <MIDIControlLog input={inputs[0]} />;
57+
const [inputs, outputs] = useMIDI();
58+
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
59+
return <MIDIControlLog input={inputs[0]} />;
5760
};
5861

5962
const MIDIControlLog = ({ input }) => {
60-
const control = useMIDIControl(input, { control: 15, channel: 1 }); // Initially returns {value: 0, control, channel}
61-
return <div>Value: {control.value}</div>;
63+
const control = useMIDIControl(input, { control: 15, channel: 1 }); // Initially returns {value: 0, control, channel}
64+
return <div>Value: {control.value}</div>;
6265
};
6366
```
6467

@@ -75,14 +78,14 @@ The steps are reset when the input receives a midi stop message.
7578

7679
```js
7780
const App = () => {
78-
const [inputs, outputs] = useMIDI();
79-
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
80-
return <MIDIClock input={inputs[0]} />;
81+
const [inputs, outputs] = useMIDI();
82+
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
83+
return <MIDIClock input={inputs[0]} />;
8184
};
8285

8386
const MIDIClock = ({ input }) => {
84-
const [step, isPlaying] = useMIDIClock(input, 12); // initially return [0, false]
85-
return <div>Eight notes since starting: {step}</div>;
87+
const [step, isPlaying] = useMIDIClock(input, 12); // initially return [0, false]
88+
return <div>Eight notes since starting: {step}</div>;
8689
};
8790
```
8891

@@ -93,23 +96,23 @@ It returns an array of integers representing the value of controls passed in wit
9396

9497
```js
9598
const App = () => {
96-
const [inputs, outputs] = useMIDI();
97-
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
98-
return <MIDIControlLog input={inputs[0]} />;
99+
const { inputs } = useMIDI();
100+
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
101+
return <MIDIControlLog input={inputs[0]} />;
99102
};
100103

101104
const MIDIControlLog = ({ input }) => {
102-
const controls = [13, 14, 15];
103-
const values = useMIDIControls(input, controls, { channel: 1 }); // Intially returns [0, 0, 0]
104-
return (
105-
<div>
106-
{controls.map((control) => (
107-
<div>
108-
Control {control}: {values[controls]}
109-
</div>
110-
))}
111-
</div>
112-
);
105+
const controls = [13, 14, 15];
106+
const values = useMIDIControls(input, controls, { channel: 1 }); // Intially returns [0, 0, 0]
107+
return (
108+
<div>
109+
{controls.map((control) => (
110+
<div>
111+
Control {control}: {values[control]}
112+
</div>
113+
))}
114+
</div>
115+
);
113116
};
114117
```
115118

@@ -120,16 +123,19 @@ It returns an array of active note objects (see above), updating when notes are
120123

121124
```js
122125
const App = () => {
123-
const [inputs, outputs] = useMIDI();
124-
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
125-
return <MIDINoteLog input={inputs[0]} />;
126+
const { inputs } = useMIDI();
127+
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
128+
return <MIDINoteLog input={inputs[0]} />;
126129
};
127130

128131
const MIDINoteLog = ({ input }) => {
129-
const notes = useMIDINotes(input, { channel: 1 }); // Intially returns []
130-
return (
131-
<div>Playing notes: {notes.map((n) => n.note).join(', ')} // Playing notes: 60, 72, 80</div>
132-
);
132+
const notes = useMIDINotes(input, { channel: 1 }); // Intially returns []
133+
return (
134+
<div>
135+
Playing notes: {notes.map((n) => n.note).join(', ')} // Playing notes: 60,
136+
72, 80
137+
</div>
138+
);
133139
};
134140
```
135141

@@ -139,44 +145,44 @@ const MIDINoteLog = ({ input }) => {
139145

140146
```js
141147
const App = () => {
142-
const [inputs, outputs] = useMIDI();
143-
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
144-
return <MIDILog input={inputs[0]} />;
148+
const { inputs } = useMIDI();
149+
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
150+
return <MIDILog input={inputs[0]} />;
145151
};
146152

147153
const MIDINoteLog = ({ input }) => {
148-
const message = useMIDIMessage(input); // initially return {}
149-
return (
150-
<div>
151-
Message Data: {message.data ? message.data.join(', ') : ''} // Message Data: 144, 60,
152-
100 (Note On, Middle C, Velocity 100)
153-
</div>
154-
);
154+
const message = useMIDIMessage(input); // initially return {}
155+
return (
156+
<div>
157+
Message Data: {message.data ? message.data.join(', ') : ''} // Message
158+
Data: 144, 60, 100 (Note On, Middle C, Velocity 100)
159+
</div>
160+
);
155161
};
156162
```
157163

158164
### `useMIDIOutput(output)`
159165

160166
`useMIDIOutput()` returns functions that can be used to send messages to the given output.
161167

162-
- `noteOn(note, velocity=127, channel=1)`: Sends a note on message to the output, defaulting to a velocity of 127 and channel 1.
163-
- `noteOff(note, velocity=127, channel=1)`: Sends a note of message to the output, defaulting to a velocity of 127 and channel 1.
164-
- `cc(value, control, channel=1)`: Sends a midi cc (control change) message to the output, defaulting to channel 1.
168+
- `noteOn(note, velocity=127, channel=1)`: Sends a note on message to the output, defaulting to a velocity of 127 and channel 1.
169+
- `noteOff(note, velocity=127, channel=1)`: Sends a note of message to the output, defaulting to a velocity of 127 and channel 1.
170+
- `cc(value, control, channel=1)`: Sends a midi cc (control change) message to the output, defaulting to channel 1.
165171

166172
```js
167173
const App = () => {
168-
const [inputs, outputs] = useMIDI();
169-
if (inputs.length < 1) return <div>No MIDI Inputs</div>;
170-
return <MIDIButton output={inputs[0]} />;
174+
const { outputs } = useMIDI();
175+
if (outputs.length < 1) return <div>No MIDI Outputs</div>;
176+
return <MIDIButton output={outputs[0]} />;
171177
};
172178

173179
const MIDIButton = ({ output }) => {
174-
const { noteOn, noteOff } = useMIDIOutput(output);
175-
const handleClick = () => {
176-
noteOn(60); // Play middle C, using velocity and channel defaults
177-
setTimeout(() => noteOff(60), 200); // Wait 200ms and then trigger note off.
178-
};
179-
return <div onClick={handleClick}>Click me!</div>;
180+
const { noteOn, noteOff } = useMIDIOutput(output);
181+
const handleClick = () => {
182+
noteOn(60); // Play middle C, using velocity and channel defaults
183+
setTimeout(() => noteOff(60), 200); // Wait 200ms and then trigger note off.
184+
};
185+
return <button onClick={handleClick}>Play C3</button>;
180186
};
181187
```
182188

@@ -189,8 +195,8 @@ The functions returned can be used to create a selection UI, or used with `<MIDI
189195

190196
```js
191197
const App = () => {
192-
const [inputs, outputs] = useMIDI();
193-
const [input, setInputId] = useMIDIConnectionManager(inputs);
194-
const [output, setOutputId] = useMIDIConnectionManager(outputs);
198+
const { inputs, outputs } = useMIDI();
199+
const [input, setInputId] = useMIDIConnectionManager(inputs);
200+
const [output, setOutputId] = useMIDIConnectionManager(outputs);
195201
};
196202
```

0 commit comments

Comments
 (0)