1 | //libroarsndio.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
5 | * The code (may) include prototypes and comments (and maybe |
---|
6 | * other code fragements) from EsounD. |
---|
7 | * They are mostly copyrighted by Eric B. Mitchell (aka 'Ricdude) |
---|
8 | * <ericmit@ix.netcom.com>. For more information see AUTHORS.esd. |
---|
9 | * |
---|
10 | * This file is part of libroaresd a part of RoarAudio, |
---|
11 | * a cross-platform sound system for both, home and professional use. |
---|
12 | * See README for details. |
---|
13 | * |
---|
14 | * This file is free software; you can redistribute it and/or modify |
---|
15 | * it under the terms of the GNU General Public License version 3 |
---|
16 | * as published by the Free Software Foundation. |
---|
17 | * |
---|
18 | * RoarAudio is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with this software; see the file COPYING. If not, write to |
---|
25 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
26 | * |
---|
27 | * NOTE for everyone want's to change something and send patches: |
---|
28 | * read README and HACKING! There a addition information on |
---|
29 | * the license of this document you need to read before you send |
---|
30 | * any patches. |
---|
31 | */ |
---|
32 | |
---|
33 | #define ROAR_USE_OWN_SNDIO_HDL |
---|
34 | #include "libroarsndio.h" |
---|
35 | |
---|
36 | static char * sndio_to_roar_names (char * name) { |
---|
37 | char * unitoffset = NULL; |
---|
38 | char * optsoffset = NULL; |
---|
39 | int unit; |
---|
40 | |
---|
41 | if ( name == NULL ) |
---|
42 | return NULL; |
---|
43 | |
---|
44 | if ( !strncmp(name, "sun:", 4) ) { |
---|
45 | unitoffset = name + 4; |
---|
46 | } else if ( !strncmp(name, "aucat:", 6) ) { |
---|
47 | unitoffset = name + 6; |
---|
48 | } else if ( !strncmp(name, "rmidi:", 6) ) { |
---|
49 | unitoffset = name + 6; |
---|
50 | } else if ( !strncmp(name, "midithru:", 9) ) { |
---|
51 | unitoffset = name + 9; |
---|
52 | } else { |
---|
53 | return name; |
---|
54 | } |
---|
55 | |
---|
56 | if ( (optsoffset = strstr(unitoffset, ".")) != NULL ) { |
---|
57 | // TODO: add some code to strip the options of the end |
---|
58 | return name; |
---|
59 | } else { |
---|
60 | unit = atoi(unitoffset); |
---|
61 | switch (unit) { |
---|
62 | // use defaulst |
---|
63 | case 0: return NULL; break; |
---|
64 | // use UNIX user sock, TODO: need some code here... |
---|
65 | case 1: return NULL; break; |
---|
66 | // use UNIX global sock: |
---|
67 | case 2: return ROAR_DEFAULT_SOCK_GLOBAL; break; |
---|
68 | // use DECnet localnode: |
---|
69 | case 3: return "::"; break; |
---|
70 | // use IPv4 localhost: |
---|
71 | case 4: return ROAR_DEFAULT_INET4_HOST; break; |
---|
72 | // reserved for DECnet Phase V: |
---|
73 | case 5: return name; break; |
---|
74 | // use IPv6 localhost: |
---|
75 | case 6: return ROAR_DEFAULT_INET6_HOST; break; |
---|
76 | // default: |
---|
77 | default: |
---|
78 | return name; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | return name; |
---|
83 | } |
---|
84 | |
---|
85 | struct sio_hdl * sio_open(const char * name, unsigned mode, int nbio_flag) { |
---|
86 | struct sio_hdl * hdl = NULL; |
---|
87 | int is_midi = 0; |
---|
88 | |
---|
89 | if ( (hdl = malloc(sizeof(struct sio_hdl))) == NULL ) |
---|
90 | return NULL; |
---|
91 | |
---|
92 | memset(hdl, 0, sizeof(struct sio_hdl)); |
---|
93 | |
---|
94 | switch (mode) { |
---|
95 | case SIO_PLAY: |
---|
96 | hdl->dir = ROAR_DIR_PLAY; |
---|
97 | break; |
---|
98 | case MIO_OUT: |
---|
99 | is_midi = 1; |
---|
100 | hdl->dir = ROAR_DIR_MIDI_IN; |
---|
101 | break; |
---|
102 | case MIO_IN: |
---|
103 | is_midi = 1; |
---|
104 | hdl->dir = ROAR_DIR_MIDI_OUT; |
---|
105 | break; |
---|
106 | |
---|
107 | // unsupported: |
---|
108 | case SIO_REC: |
---|
109 | case SIO_PLAY|SIO_REC: |
---|
110 | case MIO_OUT|MIO_IN: |
---|
111 | |
---|
112 | // illigal: |
---|
113 | default: |
---|
114 | free(hdl); |
---|
115 | return NULL; |
---|
116 | } |
---|
117 | |
---|
118 | if ( name == NULL ) { |
---|
119 | if ( is_midi ) { |
---|
120 | name = getenv("MIDIDEVICE"); |
---|
121 | } else { |
---|
122 | name = getenv("AUDIODEVICE"); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | name = sndio_to_roar_names((char*) name); |
---|
127 | |
---|
128 | if ( roar_simple_connect(&(hdl->con), (char*) name, "libroarsndio") == -1 ) { |
---|
129 | free(hdl); |
---|
130 | return NULL; |
---|
131 | } |
---|
132 | |
---|
133 | sio_initpar(&(hdl->para)); |
---|
134 | |
---|
135 | hdl->stream_opened = 0; |
---|
136 | |
---|
137 | if ( name != NULL ) |
---|
138 | hdl->device = strdup(name); |
---|
139 | |
---|
140 | if ( is_midi ) { |
---|
141 | hdl->info.codec = ROAR_CODEC_MIDI; |
---|
142 | hdl->info.bits = ROAR_MIDI_BITS; |
---|
143 | hdl->info.channels = ROAR_MIDI_CHANNELS_DEFAULT; |
---|
144 | hdl->info.rate = ROAR_MIDI_TICKS_PER_BEAT; |
---|
145 | if ( !sio_start(hdl) ) { |
---|
146 | sio_close(hdl); |
---|
147 | return NULL; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | return hdl; |
---|
152 | } |
---|
153 | |
---|
154 | void sio_close (struct sio_hdl * hdl) { |
---|
155 | if ( hdl == NULL ) |
---|
156 | return; |
---|
157 | |
---|
158 | sio_stop(hdl); |
---|
159 | |
---|
160 | roar_disconnect(&(hdl->con)); |
---|
161 | |
---|
162 | free(hdl); |
---|
163 | } |
---|
164 | |
---|
165 | int sio_eof (struct sio_hdl * hdl) { |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | void sio_onmove (struct sio_hdl * hdl, void (*cb)(void * arg, int delta), void * arg) { |
---|
170 | if ( hdl == NULL ) |
---|
171 | return; |
---|
172 | |
---|
173 | hdl->on_move = cb; |
---|
174 | hdl->on_move_arg = arg; |
---|
175 | } |
---|
176 | |
---|
177 | //ll |
---|