source: roaraudio/libroarsndio/libroarsndio.c @ 4038:5e7b9cca7dd2

Last change on this file since 4038:5e7b9cca7dd2 was 4038:5e7b9cca7dd2, checked in by phi, 14 years ago

some cleanup

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