[462] | 1 | //libartsc.c: |
---|
| 2 | |
---|
[707] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * The code (may) include prototypes and comments (and maybe |
---|
| 6 | * other code fragements) from libarts*. They are mostly copyrighted by: |
---|
| 7 | * Stefan Westerfeld <stefan@space.twc.de> |
---|
| 8 | * |
---|
| 9 | * This file is part of libroararts a part of RoarAudio, |
---|
| 10 | * a cross-platform sound system for both, home and professional use. |
---|
| 11 | * See README for details. |
---|
| 12 | * |
---|
| 13 | * This file is free software; you can redistribute it and/or modify |
---|
| 14 | * it under the terms of the GNU General Public License version 3 |
---|
| 15 | * as published by the Free Software Foundation. |
---|
| 16 | * |
---|
| 17 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 20 | * GNU General Public License for more details. |
---|
| 21 | * |
---|
| 22 | * You should have received a copy of the GNU General Public License |
---|
| 23 | * along with this software; see the file COPYING. If not, write to |
---|
| 24 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 32 | * or libpulse*: |
---|
| 33 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
| 34 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 35 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 36 | */ |
---|
| 37 | |
---|
[463] | 38 | #include <roaraudio.h> |
---|
| 39 | #include <kde/artsc/artsc.h> |
---|
| 40 | |
---|
| 41 | struct roar_connection _libroarartsc_connection[1]; |
---|
| 42 | |
---|
| 43 | struct _libroarartsc_stream { |
---|
| 44 | struct roar_stream stream; |
---|
| 45 | int fh; |
---|
| 46 | int blocking; |
---|
[466] | 47 | int block_size; |
---|
| 48 | int dir; |
---|
[463] | 49 | }; |
---|
| 50 | |
---|
| 51 | int arts_init(void) { |
---|
| 52 | return roar_simple_connect(_libroarartsc_connection, NULL, "libroarartsc client"); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void arts_free(void) { |
---|
| 56 | roar_disconnect(_libroarartsc_connection); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * asks aRtsd to free the DSP device and return 1 if it was successful, |
---|
| 61 | * 0 if there were active non-suspendable modules |
---|
| 62 | */ |
---|
| 63 | int arts_suspend(void) { |
---|
| 64 | return roar_set_standby(_libroarartsc_connection, ROAR_STANDBY_ACTIVE) == 0 ? 1 : 0; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * asks aRtsd if the DSP device is free and return 1 if it is, |
---|
| 69 | * 0 if not |
---|
| 70 | */ |
---|
| 71 | int arts_suspended(void) { |
---|
| 72 | return roar_get_standby(_libroarartsc_connection) == ROAR_STANDBY_ACTIVE; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * converts an error code to a human readable error message |
---|
| 78 | * |
---|
| 79 | * @param errorcode the errorcode (from another arts function that failed) |
---|
| 80 | * @returns a text string with the error message |
---|
| 81 | */ |
---|
| 82 | const char *arts_error_text(int errorcode) { |
---|
| 83 | return strerror(errorcode); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * open a stream for playing |
---|
| 88 | * |
---|
| 89 | * @param rate the sampling rate (something like 44100) |
---|
| 90 | * @param bits how many bits each sample has (8 or 16) |
---|
| 91 | * @param channels how many channels, 1 is mono, 2 is stereo |
---|
| 92 | * @param name the name of the stream (these will be used so that the user can |
---|
| 93 | * assign streams to effects/mixer channels and similar) |
---|
| 94 | * |
---|
| 95 | * @return a stream |
---|
| 96 | */ |
---|
| 97 | arts_stream_t arts_play_stream(int rate, int bits, int channels, const char *name) { |
---|
| 98 | struct _libroarartsc_stream * s = malloc(sizeof(struct _libroarartsc_stream)); |
---|
| 99 | struct roar_meta meta; |
---|
[466] | 100 | struct roar_stream_info info; |
---|
[463] | 101 | |
---|
| 102 | if ( !s ) |
---|
| 103 | return NULL; |
---|
| 104 | |
---|
| 105 | if ( (s->fh = roar_simple_new_stream_obj(_libroarartsc_connection, &(s->stream), |
---|
| 106 | rate, channels, bits, ROAR_CODEC_DEFAULT, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 107 | free(s); |
---|
| 108 | return NULL; |
---|
| 109 | } |
---|
| 110 | |
---|
[466] | 111 | s->dir = ROAR_DIR_PLAY; |
---|
| 112 | |
---|
[463] | 113 | s->blocking = 1; |
---|
| 114 | |
---|
[466] | 115 | if ( roar_stream_get_info(_libroarartsc_connection, &(s->stream), &info) != -1 ) { |
---|
| 116 | s->block_size = info.block_size; |
---|
| 117 | } else { |
---|
| 118 | close(s->fh); |
---|
| 119 | free(s); |
---|
| 120 | return NULL; |
---|
| 121 | } |
---|
| 122 | |
---|
[463] | 123 | if ( name && *name ) { |
---|
| 124 | meta.value = (char*)name; |
---|
| 125 | meta.key[0] = 0; |
---|
| 126 | meta.type = ROAR_META_TYPE_DESCRIPTION; |
---|
| 127 | |
---|
| 128 | roar_stream_meta_set(_libroarartsc_connection, &(s->stream), ROAR_META_MODE_SET, &meta); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | return (arts_stream_t) s; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | * open a stream for recording |
---|
| 136 | * |
---|
| 137 | * @param rate the sampling rate (something like 44100) |
---|
| 138 | * @param bits how many bits each sample has (8 or 16) |
---|
| 139 | * @param channels how many channels, 1 is mono, 2 is stereo |
---|
| 140 | * @param name the name of the stream (these will be used so that the user can |
---|
| 141 | * assign streams to effects/mixer channels and similar) |
---|
| 142 | * |
---|
| 143 | * @return a stream |
---|
| 144 | */ |
---|
| 145 | arts_stream_t arts_record_stream(int rate, int bits, int channels, const char *name) { |
---|
| 146 | struct _libroarartsc_stream * s = malloc(sizeof(struct _libroarartsc_stream)); |
---|
| 147 | struct roar_meta meta; |
---|
[466] | 148 | struct roar_stream_info info; |
---|
[463] | 149 | |
---|
| 150 | if ( !s ) |
---|
| 151 | return NULL; |
---|
| 152 | |
---|
| 153 | if ( (s->fh = roar_simple_new_stream_obj(_libroarartsc_connection, &(s->stream), |
---|
| 154 | rate, channels, bits, ROAR_CODEC_DEFAULT, ROAR_DIR_RECORD)) == -1 ) { |
---|
| 155 | free(s); |
---|
| 156 | return NULL; |
---|
| 157 | } |
---|
| 158 | |
---|
[466] | 159 | s->dir = ROAR_DIR_RECORD; |
---|
| 160 | |
---|
[463] | 161 | s->blocking = 1; |
---|
| 162 | |
---|
[466] | 163 | if ( roar_stream_get_info(_libroarartsc_connection, &(s->stream), &info) != -1 ) { |
---|
| 164 | s->block_size = info.block_size; |
---|
| 165 | } else { |
---|
| 166 | close(s->fh); |
---|
| 167 | free(s); |
---|
| 168 | return NULL; |
---|
| 169 | } |
---|
| 170 | |
---|
[463] | 171 | if ( name && *name ) { |
---|
| 172 | meta.value = (char*)name; |
---|
| 173 | meta.key[0] = 0; |
---|
| 174 | meta.type = ROAR_META_TYPE_DESCRIPTION; |
---|
| 175 | |
---|
| 176 | roar_stream_meta_set(_libroarartsc_connection, &(s->stream), ROAR_META_MODE_SET, &meta); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return (arts_stream_t) s; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * close a stream |
---|
| 184 | */ |
---|
| 185 | void arts_close_stream(arts_stream_t stream) { |
---|
| 186 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
| 187 | if ( !stream ) |
---|
| 188 | return; |
---|
| 189 | |
---|
| 190 | close(s->fh); |
---|
| 191 | |
---|
| 192 | free(stream); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * read samples from stream |
---|
| 197 | * |
---|
| 198 | * @param stream a previously opened record stream |
---|
| 199 | * @param buffer a buffer with sample data |
---|
| 200 | * @param count the number of bytes contained in the buffer |
---|
| 201 | * |
---|
| 202 | * @returns number of read bytes on success or error code |
---|
| 203 | */ |
---|
| 204 | int arts_read(arts_stream_t stream, void *buffer, int count) { |
---|
| 205 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
| 206 | if ( !stream ) |
---|
| 207 | return -1; |
---|
| 208 | |
---|
| 209 | return read(s->fh, buffer, count); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * write samples to to stream |
---|
| 214 | * |
---|
| 215 | * @param stream a previously opened play stream |
---|
| 216 | * @param buffer a buffer with sample data |
---|
| 217 | * @param count the number of bytes contained in the buffer |
---|
| 218 | * |
---|
| 219 | * @returns number of written bytes on success or error code |
---|
| 220 | */ |
---|
| 221 | int arts_write(arts_stream_t stream, const void *buffer, int count) { |
---|
| 222 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
| 223 | if ( !stream ) |
---|
| 224 | return -1; |
---|
| 225 | |
---|
| 226 | return write(s->fh, buffer, count); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * configure a parameter of a stream |
---|
| 231 | * |
---|
| 232 | * @param stream an opened record or play stream |
---|
| 233 | * @param parameter the parameter you want to modify |
---|
| 234 | * @param value the new value |
---|
| 235 | * |
---|
| 236 | * @returns the new value of the parameter (which may or may not be the value |
---|
| 237 | * you wanted to have), or an error code if something went wrong |
---|
| 238 | */ |
---|
| 239 | int arts_stream_set(arts_stream_t stream, arts_parameter_t param, int value) { |
---|
| 240 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
| 241 | if ( !stream ) |
---|
| 242 | return -1; |
---|
| 243 | |
---|
| 244 | if ( param == ARTS_P_BLOCKING ) { |
---|
| 245 | if ( roar_socket_nonblock(s->fh, value ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 246 | return -1; |
---|
| 247 | return arts_stream_get(stream, param); |
---|
| 248 | } |
---|
| 249 | |
---|
[466] | 250 | return arts_stream_get(stream, param); |
---|
[463] | 251 | } |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | * query a parameter of a stream |
---|
| 255 | * |
---|
| 256 | * @param stream an opened record or play stream |
---|
| 257 | * @param parameter the parameter you want to query |
---|
| 258 | * |
---|
| 259 | * @returns the value of the parameter, or an error code |
---|
| 260 | */ |
---|
| 261 | int arts_stream_get(arts_stream_t stream, arts_parameter_t param) { |
---|
| 262 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
[466] | 263 | fd_set sl; |
---|
| 264 | struct timeval tv; |
---|
[465] | 265 | |
---|
[463] | 266 | if ( !stream ) |
---|
| 267 | return -1; |
---|
| 268 | |
---|
| 269 | if ( param == ARTS_P_PACKET_SIZE ) { |
---|
[466] | 270 | return s->block_size; |
---|
| 271 | } else if ( param == ARTS_P_BUFFER_SPACE ) { |
---|
| 272 | FD_ZERO(&sl); |
---|
| 273 | FD_SET(s->fh, &sl); |
---|
| 274 | |
---|
| 275 | tv.tv_sec = 0; |
---|
| 276 | tv.tv_usec = 1; |
---|
| 277 | |
---|
| 278 | if ( s->dir == ROAR_DIR_PLAY ) { |
---|
| 279 | if (select(s->fh + 1, NULL, &sl, NULL, &tv) > 0) |
---|
| 280 | return s->block_size; |
---|
| 281 | } else { |
---|
| 282 | if (select(s->fh + 1, &sl, NULL, NULL, &tv) > 0) |
---|
| 283 | return s->block_size; |
---|
[465] | 284 | } |
---|
[466] | 285 | |
---|
| 286 | return 0; |
---|
| 287 | } else if ( param == ARTS_P_BUFFER_SIZE ) { |
---|
| 288 | return s->block_size*2; |
---|
[463] | 289 | } else if ( param == ARTS_P_PACKET_COUNT ) { |
---|
| 290 | return 1; |
---|
| 291 | } else if ( param == ARTS_P_BLOCKING ) { |
---|
| 292 | return s->blocking; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | return -1; |
---|
| 296 | } |
---|
| 297 | |
---|
[462] | 298 | //ll |
---|