diff --git a/share/examples/Makefile b/share/examples/Makefile --- a/share/examples/Makefile +++ b/share/examples/Makefile @@ -324,6 +324,7 @@ ossinit.h \ ossmidi.h \ midi.c \ + polling.c \ README SE_DIRS+= sunrpc diff --git a/share/examples/sound/polling.c b/share/examples/sound/polling.c new file mode 100644 --- /dev/null +++ b/share/examples/sound/polling.c @@ -0,0 +1,186 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2022 Goran Mekić + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if !defined(SELECT) && !defined(POLL) +#error "Compile with -DSELECT or -DPOLL" +#endif + +#include +#include + +#include "ossinit.h" +#include "ossmidi.h" + +int8_t *ibuf = NULL; +int8_t *obuf = NULL; +sample_t *channels = NULL; + +void +handle_midi(midi_config_t *midi_config) +{ + midi_event_t event; + uint8_t raw; + int l = -1; + + if ((l = read(midi_config->fd, &raw, sizeof(raw))) != -1) { + if (!(raw & 0x80)) { + return; + } + event.type = raw & CMD_MASK; + event.channel = raw & CHANNEL_MASK; + switch (event.type) { + case NOTE_ON: + case NOTE_OFF: + case CONTROLER_ON: + if ((l = read(midi_config->fd, &(event.note), + sizeof(event.note))) == -1) { + perror("Error reading MIDI note"); + exit(1); + } + if ((l = read(midi_config->fd, &(event.velocity), + sizeof(event.velocity))) == -1) { + perror("Error reading MIDI velocity"); + exit(1); + } + break; + } + switch (event.type) { + case NOTE_ON: + case NOTE_OFF: + printf("Channel %d, note %d, velocity %d\n", + event.channel, event.note, event.velocity); + break; + case CONTROLER_ON: + printf("Channel %d, controller %d, value %d\n", + event.channel, event.controller, event.value); + break; + default: + printf("Unknown event type %d\n", event.type); + } + } +} + +void +handle_audio(config_t *config) +{ + int ret; + int bytes = config->buffer_info.bytes; + + ret = read(config->fd, ibuf, bytes); + if (ret < bytes) { + fprintf(stderr, "Requested %d bytes, but read %d!\n", bytes, + ret); + return; + } + oss_split(config, (sample_t *)ibuf, channels); + /* All processing will happen here */ + printf("Audio processing\n"); + oss_merge(config, channels, (sample_t *)obuf); + ret = write(config->fd, obuf, bytes); + if (ret < bytes) { + fprintf(stderr, "Requested %d bytes, but wrote %d!\n", bytes, + ret); + } +} + +int +main() +{ + int ret; + int bytes; + int maxfd; + config_t config = { + .device = "/dev/dsp", + .channels = -1, + .format = format, + .frag = -1, + .sample_rate = 48000, + .sample_size = sizeof(sample_t), + .buffer_info.fragments = -1, + .mmap = 0, + }; + midi_config_t midi_config = { + .device = "/dev/umidi0.0", + }; + + oss_init(&config); + oss_midi_init(&midi_config); + + /* + * Allocate input and output buffers so that their size match + * frag_size + */ + bytes = config.buffer_info.bytes; + ibuf = malloc(bytes); + obuf = malloc(bytes); + channels = malloc(bytes); + maxfd = config.fd > midi_config.fd ? midi_config.fd : midi_config.fd; + +#ifdef SELECT + fd_set fds; +#endif + +#ifdef POLL + // For simplicity of this example just use POLLIN. + // For more complex examples one can split input and output processing. + struct pollfd pfds[2]; + pfds[0].fd = config.fd; + pfds[0].events = POLLIN; + pfds[1].fd = midi_config.fd; + pfds[1].events = POLLIN; +#endif + + for (;;) { +#ifdef SELECT + FD_ZERO(&fds); + FD_SET(config.fd, &fds); + FD_SET(midi_config.fd, &fds); + ret = select(maxfd + 1, &fds, NULL, NULL, NULL); + if (FD_ISSET(config.fd, &fds)) { + handle_audio(&config); + } else if (FD_ISSET(midi_config.fd, &fds)) { + handle_midi(&midi_config); + } +#endif + +#ifdef POLL + ret = poll(pfds, sizeof(pfds) / sizeof(struct pollfd), -1); + if (pfds[0].revents != 0) { + handle_audio(&config); + } else if (pfds[1].revents != 0) { + handle_midi(&midi_config); + } +#endif + } + + /* Cleanup */ + free(channels); + free(obuf); + free(ibuf); + close(config.fd); + return (0); +}