export async function sendMidiMessage(midiChannelName, sampleNumber, midiAccess) {
  try {
    console.log(midiChannelName, sampleNumber);
    const availableMidiOutputs = midiAccess.outputs.values();
    let midiOut;

    if (availableMidiOutputs.length === 0) {
      console.error("No MIDI output devices found.");
      return;
    }

    for (const availableMidiOutput of availableMidiOutputs) {
      if (availableMidiOutput.name === midiChannelName) {
        midiOut = availableMidiOutput;
        break;
      }
    }

    // MIDI Note On (Play Note)
    midiOut.send([0x90, 35 + sampleNumber, 127]);

    // MIDI Note Off after 500ms
    setTimeout(() => {
      midiOut.send([0x80, 35 + sampleNumber, 0]); // Stop Note
    }, 500);
  } catch (error) {
    console.error("Failed to get MIDI access:", error);
  }
}
