/*1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2022 Goran Mekić4* Copyright (c) 2024 The FreeBSD Foundation5*6* Portions of this software were developed by Christos Margiolis7* <[email protected]> under sponsorship from the FreeBSD Foundation.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <err.h>32#include <fcntl.h>33#include <stdio.h>34#include <stdlib.h>35#include <unistd.h>3637#define CMD_MASK 0xF038#define CHANNEL_MASK 0x0F39#define NOTE_ON 0x9040#define NOTE_OFF 0x8041#define CTL_CHANGE 0xB04243int44main(int argc, char *argv[])45{46int fd;47unsigned char raw, type, channel, b1, b2;4849if ((fd = open("/dev/umidi0.0", O_RDWR)) < 0)50err(1, "Error opening MIDI device");5152for (;;) {53if (read(fd, &raw, sizeof(raw)) < sizeof(raw))54err(1, "Error reading command byte");55if (!(raw & 0x80))56continue;5758type = raw & CMD_MASK;59channel = raw & CHANNEL_MASK;6061if (read(fd, &b1, sizeof(b1)) < sizeof(b1))62err(1, "Error reading byte 1");63if (read(fd, &b2, sizeof(b2)) < sizeof(b2))64err(1, "Error reading byte 2");6566switch (type) {67case NOTE_ON:68printf("Channel %d, note on %d, velocity %d\n",69channel, b1, b2);70break;71case NOTE_OFF:72printf("Channel %d, note off %d, velocity %d\n",73channel, b1, b2);74break;75case CTL_CHANGE:76printf("Channel %d, controller change %d, value %d\n",77channel, b1, b2);78break;79default:80printf("Unknown event type %d\n", type);81break;82}83}8485close(fd);8687return (0);88}899091