Skip to content

Commit 9768301

Browse files
committed
Added useMIDIConnectionManager and updated README.md
1 parent 7fc89a5 commit 9768301

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ const MIDINoteLog = ({ input }) => {
157157

158158
### `useMIDIOutput(output)`
159159

160-
`useMIDIOutput()` returns function that can be used to send data to the given output.
160+
`useMIDIOutput()` returns functions that can be used to send messages to the given output.
161161

162162
- `noteOn(note, velocity=127, channel=1)`: Sends a note on message to the output, defaulting to a velocity of 127 and channel 1.
163163
- `noteOff(note, velocity=127, channel=1)`: Sends a note of message to the output, defaulting to a velocity of 127 and channel 1.
@@ -179,3 +179,18 @@ const MIDIButton = ({ output }) => {
179179
return <div onClick={handleClick}>Click me!</div>;
180180
};
181181
```
182+
183+
### `useMIDIConnectionManager(connections)`
184+
185+
Given an array of connections (inputs or outputs), `useMIDIConnectionManager()` returns the first avaliable connection, as well as a function to change the connection by calling it with a connection id.
186+
187+
If a connection is no longer avaliable, or an unavailable id is given, the first avaliable connection will be returned.
188+
The functions returned can be used to create a selection UI, or used with `<MIDIConnectionManager />` from [@react-midi/components](https://github.com/nickroberts404/react-midi-components).
189+
190+
```js
191+
const App = () => {
192+
const [inputs, outputs] = useMIDI();
193+
const [input, setInputId] = useMIDIConnectionManager(inputs);
194+
const [output, setOutputId] = useMIDIConnectionManager(outputs);
195+
};
196+
```

src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const enrichInputs = (inputs) =>
7272
return input;
7373
});
7474

75-
// By using useConnectInput at the beggining of input hook, we prevent opening/maintaining connections with unused inputs.
75+
// By using useConnectInput at the beggining of an input hook, we prevent opening/maintaining connections with unused inputs.
7676
// This may have reprecusions when more than one hook is used for the same input, and one of them unregisters.
7777
const useConnectInput = (input) => {
7878
useEffect(() => {
@@ -82,6 +82,19 @@ const useConnectInput = (input) => {
8282
}, [input]);
8383
};
8484

85+
export const useMIDIConnectionManager = (connections) => {
86+
const connectionsAvaliable = connections.length > 0;
87+
const [id, setId] = useState(0);
88+
89+
useEffect(() => {
90+
const index = connections.findIndex((c) => c.id === id);
91+
// I believe setting the id to 0 here would result in an infinite loop if there actually aren't any connections
92+
if (index < 0) setId(connectionsAvaliable ? connections[0].id : 0);
93+
}, [connections, id]);
94+
const connection = connections.find((i) => i.id === id);
95+
return [connection, setId];
96+
};
97+
8598
export const useMIDIClock = (input, division = 1) => {
8699
useConnectInput(input);
87100
const [step, setStep] = useState(0);

0 commit comments

Comments
 (0)