[1555] | 1 | //libroarsndio.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[6052] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2015 |
---|
[1555] | 5 | * The code (may) include prototypes and comments (and maybe |
---|
[4038] | 6 | * other code fragements) from OpenBSD's sndio. |
---|
[4050] | 7 | * See 'Copyright for sndio' below for more information on |
---|
| 8 | * code fragments taken from OpenBSD's sndio. |
---|
| 9 | * |
---|
| 10 | * --- Copyright for sndio --- |
---|
| 11 | * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> |
---|
| 12 | * |
---|
| 13 | * Permission to use, copy, modify, and distribute this software for any |
---|
| 14 | * purpose with or without fee is hereby granted, provided that the above |
---|
| 15 | * copyright notice and this permission notice appear in all copies. |
---|
| 16 | * |
---|
| 17 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
| 18 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
| 19 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
| 20 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
| 21 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
| 22 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
| 23 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
| 24 | * --- End of Copyright for sndio --- |
---|
[1555] | 25 | * |
---|
| 26 | * This file is part of libroaresd a part of RoarAudio, |
---|
| 27 | * a cross-platform sound system for both, home and professional use. |
---|
| 28 | * See README for details. |
---|
| 29 | * |
---|
| 30 | * This file is free software; you can redistribute it and/or modify |
---|
| 31 | * it under the terms of the GNU General Public License version 3 |
---|
| 32 | * as published by the Free Software Foundation. |
---|
| 33 | * |
---|
| 34 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 37 | * GNU General Public License for more details. |
---|
| 38 | * |
---|
| 39 | * You should have received a copy of the GNU General Public License |
---|
| 40 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 41 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 42 | * Boston, MA 02110-1301, USA. |
---|
[1555] | 43 | * |
---|
| 44 | * NOTE for everyone want's to change something and send patches: |
---|
| 45 | * read README and HACKING! There a addition information on |
---|
| 46 | * the license of this document you need to read before you send |
---|
| 47 | * any patches. |
---|
| 48 | */ |
---|
| 49 | |
---|
[1571] | 50 | #define ROAR_USE_OWN_SNDIO_HDL |
---|
[1555] | 51 | #include "libroarsndio.h" |
---|
| 52 | |
---|
[2372] | 53 | static char * sndio_to_roar_names (char * name) { |
---|
[2373] | 54 | char * unitoffset = NULL; |
---|
| 55 | char * optsoffset = NULL; |
---|
| 56 | int unit; |
---|
[2372] | 57 | |
---|
| 58 | if ( name == NULL ) |
---|
| 59 | return NULL; |
---|
| 60 | |
---|
[6082] | 61 | if ( !strcmp(name, SIO_DEVANY) || !strcmp(name, MIO_PORTANY) ) { |
---|
| 62 | return NULL; // use default. |
---|
| 63 | } else if ( !strncmp(name, "sun:", 4) ) { |
---|
[2373] | 64 | unitoffset = name + 4; |
---|
| 65 | } else if ( !strncmp(name, "aucat:", 6) ) { |
---|
| 66 | unitoffset = name + 6; |
---|
| 67 | } else if ( !strncmp(name, "rmidi:", 6) ) { |
---|
| 68 | unitoffset = name + 6; |
---|
| 69 | } else if ( !strncmp(name, "midithru:", 9) ) { |
---|
| 70 | unitoffset = name + 9; |
---|
| 71 | } else { |
---|
| 72 | return name; |
---|
| 73 | } |
---|
| 74 | |
---|
[4969] | 75 | if ( (optsoffset = strstr(unitoffset, "/")) != NULL ) { |
---|
| 76 | *optsoffset = 0; |
---|
| 77 | return unitoffset; |
---|
| 78 | } else if ( (optsoffset = strstr(unitoffset, ".")) != NULL ) { |
---|
[2373] | 79 | // TODO: add some code to strip the options of the end |
---|
| 80 | return name; |
---|
| 81 | } else { |
---|
| 82 | unit = atoi(unitoffset); |
---|
| 83 | switch (unit) { |
---|
| 84 | // use defaulst |
---|
| 85 | case 0: return NULL; break; |
---|
| 86 | // use UNIX user sock, TODO: need some code here... |
---|
| 87 | case 1: return NULL; break; |
---|
| 88 | // use UNIX global sock: |
---|
| 89 | case 2: return ROAR_DEFAULT_SOCK_GLOBAL; break; |
---|
| 90 | // use DECnet localnode: |
---|
| 91 | case 3: return "::"; break; |
---|
| 92 | // use IPv4 localhost: |
---|
[2513] | 93 | case 4: return ROAR_DEFAULT_INET4_HOST; break; |
---|
[2515] | 94 | // reserved for DECnet Phase V: |
---|
| 95 | case 5: return name; break; |
---|
[2373] | 96 | // use IPv6 localhost: |
---|
[2513] | 97 | case 6: return ROAR_DEFAULT_INET6_HOST; break; |
---|
[2373] | 98 | // default: |
---|
| 99 | default: |
---|
| 100 | return name; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
[2372] | 104 | return name; |
---|
| 105 | } |
---|
| 106 | |
---|
[2574] | 107 | struct sio_hdl * sio_open(const char * name, unsigned mode, int nbio_flag) { |
---|
[1560] | 108 | struct sio_hdl * hdl = NULL; |
---|
[2359] | 109 | int is_midi = 0; |
---|
[4969] | 110 | char * tmp; |
---|
[1560] | 111 | |
---|
[3833] | 112 | if ( (hdl = roar_mm_malloc(sizeof(struct sio_hdl))) == NULL ) |
---|
[1560] | 113 | return NULL; |
---|
| 114 | |
---|
| 115 | memset(hdl, 0, sizeof(struct sio_hdl)); |
---|
[4969] | 116 | hdl->device = NULL; |
---|
[1560] | 117 | |
---|
[2357] | 118 | switch (mode) { |
---|
| 119 | case SIO_PLAY: |
---|
| 120 | hdl->dir = ROAR_DIR_PLAY; |
---|
| 121 | break; |
---|
[4969] | 122 | case SIO_REC: |
---|
| 123 | hdl->dir = ROAR_DIR_PLAY; |
---|
| 124 | break; |
---|
[2357] | 125 | case MIO_OUT: |
---|
[2359] | 126 | is_midi = 1; |
---|
| 127 | hdl->dir = ROAR_DIR_MIDI_IN; |
---|
| 128 | break; |
---|
[2357] | 129 | case MIO_IN: |
---|
[2359] | 130 | is_midi = 1; |
---|
| 131 | hdl->dir = ROAR_DIR_MIDI_OUT; |
---|
| 132 | break; |
---|
[2360] | 133 | |
---|
| 134 | // unsupported: |
---|
[2357] | 135 | case SIO_PLAY|SIO_REC: |
---|
| 136 | case MIO_OUT|MIO_IN: |
---|
[2360] | 137 | |
---|
| 138 | // illigal: |
---|
[2357] | 139 | default: |
---|
[3833] | 140 | roar_mm_free(hdl); |
---|
[2357] | 141 | return NULL; |
---|
| 142 | } |
---|
| 143 | |
---|
[2371] | 144 | if ( name == NULL ) { |
---|
| 145 | if ( is_midi ) { |
---|
[5754] | 146 | name = roar_env_get("MIDIDEVICE"); |
---|
[2371] | 147 | } else { |
---|
[5754] | 148 | name = roar_env_get("AUDIODEVICE"); |
---|
[2371] | 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
[4969] | 152 | if ( name != NULL ) { |
---|
| 153 | tmp = roar_mm_strdup(name); |
---|
| 154 | name = sndio_to_roar_names(tmp); |
---|
[2372] | 155 | |
---|
[4969] | 156 | if ( name != NULL ) |
---|
| 157 | hdl->device = roar_mm_strdup(name); |
---|
| 158 | |
---|
| 159 | roar_mm_free(tmp); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | if ( roar_simple_connect(&(hdl->con), hdl->device, "libroarsndio") == -1 ) { |
---|
| 163 | roar_mm_free(hdl->device); |
---|
[3833] | 164 | roar_mm_free(hdl); |
---|
[1573] | 165 | return NULL; |
---|
| 166 | } |
---|
| 167 | |
---|
[1560] | 168 | sio_initpar(&(hdl->para)); |
---|
| 169 | |
---|
[2019] | 170 | hdl->stream_opened = 0; |
---|
[1560] | 171 | |
---|
[2359] | 172 | if ( is_midi ) { |
---|
| 173 | hdl->info.codec = ROAR_CODEC_MIDI; |
---|
[2575] | 174 | hdl->info.bits = ROAR_MIDI_BITS; |
---|
| 175 | hdl->info.channels = ROAR_MIDI_CHANNELS_DEFAULT; |
---|
| 176 | hdl->info.rate = ROAR_MIDI_TICKS_PER_BEAT; |
---|
[2359] | 177 | if ( !sio_start(hdl) ) { |
---|
| 178 | sio_close(hdl); |
---|
| 179 | return NULL; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
[3237] | 183 | hdl->nonblock = nbio_flag; |
---|
| 184 | |
---|
[1560] | 185 | return hdl; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void sio_close (struct sio_hdl * hdl) { |
---|
| 189 | if ( hdl == NULL ) |
---|
| 190 | return; |
---|
| 191 | |
---|
[2019] | 192 | sio_stop(hdl); |
---|
[1560] | 193 | |
---|
[1573] | 194 | roar_disconnect(&(hdl->con)); |
---|
| 195 | |
---|
[4969] | 196 | if ( hdl->device != NULL ) |
---|
| 197 | roar_mm_free(hdl->device); |
---|
[3833] | 198 | roar_mm_free(hdl); |
---|
[1560] | 199 | } |
---|
| 200 | |
---|
| 201 | int sio_eof (struct sio_hdl * hdl) { |
---|
[3238] | 202 | return hdl->ioerror; |
---|
[1560] | 203 | } |
---|
| 204 | |
---|
| 205 | void sio_onmove (struct sio_hdl * hdl, void (*cb)(void * arg, int delta), void * arg) { |
---|
| 206 | if ( hdl == NULL ) |
---|
| 207 | return; |
---|
| 208 | |
---|
| 209 | hdl->on_move = cb; |
---|
| 210 | hdl->on_move_arg = arg; |
---|
| 211 | } |
---|
| 212 | |
---|
[1555] | 213 | //ll |
---|