xref: /freebsd/share/examples/sound/midi.c (revision 0789c3bd)
1f092c21bSGoran Mekic /*
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3f092c21bSGoran Mekic  *
4f092c21bSGoran Mekic  * Copyright (c) 2022 Goran Mekić
5f092c21bSGoran Mekic  *
6f092c21bSGoran Mekic  * Redistribution and use in source and binary forms, with or without
7f092c21bSGoran Mekic  * modification, are permitted provided that the following conditions
8f092c21bSGoran Mekic  * are met:
9f092c21bSGoran Mekic  * 1. Redistributions of source code must retain the above copyright
10f092c21bSGoran Mekic  *    notice, this list of conditions and the following disclaimer.
11f092c21bSGoran Mekic  * 2. Redistributions in binary form must reproduce the above copyright
12f092c21bSGoran Mekic  *    notice, this list of conditions and the following disclaimer in the
13f092c21bSGoran Mekic  *    documentation and/or other materials provided with the distribution.
14f092c21bSGoran Mekic  *
15f092c21bSGoran Mekic  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16f092c21bSGoran Mekic  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f092c21bSGoran Mekic  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f092c21bSGoran Mekic  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f092c21bSGoran Mekic  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f092c21bSGoran Mekic  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f092c21bSGoran Mekic  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f092c21bSGoran Mekic  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f092c21bSGoran Mekic  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f092c21bSGoran Mekic  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f092c21bSGoran Mekic  * SUCH DAMAGE.
26f092c21bSGoran Mekic  */
27f092c21bSGoran Mekic 
28f092c21bSGoran Mekic #include <unistd.h>
29f092c21bSGoran Mekic 
30f092c21bSGoran Mekic #include "ossmidi.h"
31f092c21bSGoran Mekic 
32f092c21bSGoran Mekic int
main()33f092c21bSGoran Mekic main()
34f092c21bSGoran Mekic {
35f092c21bSGoran Mekic 	midi_event_t event;
36f092c21bSGoran Mekic 	midi_config_t midi_config;
37f092c21bSGoran Mekic 	int l = -1;
38f092c21bSGoran Mekic 	unsigned char raw;
39f092c21bSGoran Mekic 
40f092c21bSGoran Mekic 	midi_config.device = "/dev/umidi1.0";
41f092c21bSGoran Mekic 	oss_midi_init(&midi_config);
42f092c21bSGoran Mekic 
43f092c21bSGoran Mekic 	while ((l = read(midi_config.fd, &raw, sizeof(raw))) != -1) {
44f092c21bSGoran Mekic 		if (!(raw & 0x80)) {
45f092c21bSGoran Mekic 			continue;
46f092c21bSGoran Mekic 		}
47f092c21bSGoran Mekic 		event.type = raw & CMD_MASK;
48f092c21bSGoran Mekic 		event.channel = raw & CHANNEL_MASK;
49f092c21bSGoran Mekic 		switch (event.type) {
50f092c21bSGoran Mekic 		case NOTE_ON:
51f092c21bSGoran Mekic 		case NOTE_OFF:
520789c3bdSAssume-Zhan 		case CONTROLLER_ON:
53f092c21bSGoran Mekic 			if ((l = read(midi_config.fd, &(event.note), sizeof(event.note))) == -1) {
54f092c21bSGoran Mekic 				perror("Error reading MIDI note");
55f092c21bSGoran Mekic 				exit(1);
56f092c21bSGoran Mekic 			}
57f092c21bSGoran Mekic 			if ((l = read(midi_config.fd, &(event.velocity), sizeof(event.velocity))) == -1) {
58f092c21bSGoran Mekic 				perror("Error reading MIDI velocity");
59f092c21bSGoran Mekic 				exit(1);
60f092c21bSGoran Mekic 			}
61f092c21bSGoran Mekic 			break;
62f092c21bSGoran Mekic 		}
63f092c21bSGoran Mekic 		switch (event.type) {
64f092c21bSGoran Mekic 		case NOTE_ON:
65f092c21bSGoran Mekic 		case NOTE_OFF:
66f092c21bSGoran Mekic 			printf("Channel %d, note %d, velocity %d\n", event.channel, event.note, event.velocity);
67f092c21bSGoran Mekic 			break;
680789c3bdSAssume-Zhan 		case CONTROLLER_ON:
69f092c21bSGoran Mekic 			printf("Channel %d, controller %d, value %d\n", event.channel, event.controller, event.value);
70f092c21bSGoran Mekic 			break;
71f092c21bSGoran Mekic 		default:
72f092c21bSGoran Mekic 			printf("Unknown event type %d\n", event.type);
73f092c21bSGoran Mekic 		}
74f092c21bSGoran Mekic 	}
75f092c21bSGoran Mekic 	return 0;
76f092c21bSGoran Mekic }
77