[462] | 1 | //libartsc.c: |
---|
| 2 | |
---|
[707] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[707] | 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 |
---|
[3517] | 24 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 25 | * Boston, MA 02110-1301, USA. |
---|
[707] | 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 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 33 | * or libpulse*: |
---|
| 34 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
| 35 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 36 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 37 | */ |
---|
| 38 | |
---|
[463] | 39 | #include <roaraudio.h> |
---|
| 40 | #include <kde/artsc/artsc.h> |
---|
| 41 | |
---|
[3833] | 42 | static struct roar_connection _libroarartsc_connection[1]; |
---|
[463] | 43 | |
---|
| 44 | struct _libroarartsc_stream { |
---|
[4891] | 45 | roar_vs_t * vss; |
---|
| 46 | struct roar_stream _stream; |
---|
| 47 | int _fh; |
---|
| 48 | int _blocking; |
---|
[466] | 49 | int block_size; |
---|
| 50 | int dir; |
---|
[463] | 51 | }; |
---|
| 52 | |
---|
| 53 | int arts_init(void) { |
---|
| 54 | return roar_simple_connect(_libroarartsc_connection, NULL, "libroarartsc client"); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | void arts_free(void) { |
---|
| 58 | roar_disconnect(_libroarartsc_connection); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * asks aRtsd to free the DSP device and return 1 if it was successful, |
---|
| 63 | * 0 if there were active non-suspendable modules |
---|
| 64 | */ |
---|
| 65 | int arts_suspend(void) { |
---|
| 66 | return roar_set_standby(_libroarartsc_connection, ROAR_STANDBY_ACTIVE) == 0 ? 1 : 0; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * asks aRtsd if the DSP device is free and return 1 if it is, |
---|
| 71 | * 0 if not |
---|
| 72 | */ |
---|
| 73 | int arts_suspended(void) { |
---|
| 74 | return roar_get_standby(_libroarartsc_connection) == ROAR_STANDBY_ACTIVE; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * converts an error code to a human readable error message |
---|
| 80 | * |
---|
| 81 | * @param errorcode the errorcode (from another arts function that failed) |
---|
| 82 | * @returns a text string with the error message |
---|
| 83 | */ |
---|
| 84 | const char *arts_error_text(int errorcode) { |
---|
[4891] | 85 | return roar_vs_strerr(errorcode); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | static arts_stream_t _arts_stream(int rate, int bits, int channels, const char *name, int dir) { |
---|
| 89 | struct _libroarartsc_stream * s = roar_mm_malloc(sizeof(struct _libroarartsc_stream)); |
---|
| 90 | struct roar_stream * stream; |
---|
| 91 | struct roar_audio_info auinfo; |
---|
| 92 | struct roar_stream_info info; |
---|
| 93 | struct roar_keyval kv[1]; |
---|
| 94 | int err = ROAR_ERROR_NONE; |
---|
| 95 | |
---|
| 96 | if ( s == NULL ) |
---|
| 97 | return NULL; |
---|
| 98 | |
---|
| 99 | if ( (s->vss = roar_vs_new_from_con(_libroarartsc_connection, &err)) == NULL ) { |
---|
| 100 | roar_mm_free(s); |
---|
| 101 | return NULL; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | memset(&auinfo, 0, sizeof(auinfo)); |
---|
| 105 | auinfo.rate = rate; |
---|
| 106 | auinfo.channels = channels; |
---|
| 107 | auinfo.bits = bits; |
---|
| 108 | auinfo.codec = ROAR_CODEC_DEFAULT; |
---|
| 109 | |
---|
| 110 | if ( roar_vs_stream(s->vss, &auinfo, dir, &err) == -1 ) { |
---|
| 111 | roar_vs_close(s->vss, ROAR_VS_TRUE, &err); |
---|
| 112 | roar_mm_free(s); |
---|
| 113 | return NULL; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | s->dir = dir; |
---|
| 117 | |
---|
| 118 | stream = roar_vs_stream_obj(s->vss, &err); |
---|
| 119 | |
---|
| 120 | if ( roar_stream_get_info(_libroarartsc_connection, stream, &info) != -1 ) { |
---|
| 121 | s->block_size = info.block_size; |
---|
| 122 | } else { |
---|
[4892] | 123 | ROAR_WARN("_arts_stream(*): Can not get stream block size from server, assuming 1024 Byte."); |
---|
| 124 | s->block_size = 1024; |
---|
[4891] | 125 | } |
---|
| 126 | |
---|
| 127 | if ( name != NULL && *name ) { |
---|
| 128 | kv[0].key = "description"; |
---|
| 129 | kv[0].value = (char*)name; |
---|
| 130 | |
---|
| 131 | roar_vs_meta(s->vss, kv, 1, &err); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | return (arts_stream_t) s; |
---|
[463] | 135 | } |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * open a stream for playing |
---|
| 139 | * |
---|
| 140 | * @param rate the sampling rate (something like 44100) |
---|
| 141 | * @param bits how many bits each sample has (8 or 16) |
---|
| 142 | * @param channels how many channels, 1 is mono, 2 is stereo |
---|
| 143 | * @param name the name of the stream (these will be used so that the user can |
---|
| 144 | * assign streams to effects/mixer channels and similar) |
---|
| 145 | * |
---|
| 146 | * @return a stream |
---|
| 147 | */ |
---|
| 148 | arts_stream_t arts_play_stream(int rate, int bits, int channels, const char *name) { |
---|
[4891] | 149 | return _arts_stream(rate, bits, channels, name, ROAR_DIR_PLAY); |
---|
[463] | 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * open a stream for recording |
---|
| 154 | * |
---|
| 155 | * @param rate the sampling rate (something like 44100) |
---|
| 156 | * @param bits how many bits each sample has (8 or 16) |
---|
| 157 | * @param channels how many channels, 1 is mono, 2 is stereo |
---|
| 158 | * @param name the name of the stream (these will be used so that the user can |
---|
| 159 | * assign streams to effects/mixer channels and similar) |
---|
| 160 | * |
---|
| 161 | * @return a stream |
---|
| 162 | */ |
---|
| 163 | arts_stream_t arts_record_stream(int rate, int bits, int channels, const char *name) { |
---|
[4891] | 164 | return _arts_stream(rate, bits, channels, name, ROAR_DIR_RECORD); |
---|
[463] | 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * close a stream |
---|
| 169 | */ |
---|
| 170 | void arts_close_stream(arts_stream_t stream) { |
---|
| 171 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
[4891] | 172 | if ( stream == NULL ) |
---|
[463] | 173 | return; |
---|
| 174 | |
---|
[4891] | 175 | roar_vs_close(s->vss, ROAR_VS_TRUE, NULL); |
---|
[463] | 176 | |
---|
[3833] | 177 | roar_mm_free(stream); |
---|
[463] | 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * read samples from stream |
---|
| 182 | * |
---|
| 183 | * @param stream a previously opened record stream |
---|
| 184 | * @param buffer a buffer with sample data |
---|
| 185 | * @param count the number of bytes contained in the buffer |
---|
| 186 | * |
---|
| 187 | * @returns number of read bytes on success or error code |
---|
| 188 | */ |
---|
| 189 | int arts_read(arts_stream_t stream, void *buffer, int count) { |
---|
| 190 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
[4891] | 191 | if ( stream == NULL ) |
---|
[463] | 192 | return -1; |
---|
| 193 | |
---|
[4891] | 194 | return roar_vs_read(s->vss, buffer, count, NULL); |
---|
[463] | 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * write samples to to stream |
---|
| 199 | * |
---|
| 200 | * @param stream a previously opened play stream |
---|
| 201 | * @param buffer a buffer with sample data |
---|
| 202 | * @param count the number of bytes contained in the buffer |
---|
| 203 | * |
---|
| 204 | * @returns number of written bytes on success or error code |
---|
| 205 | */ |
---|
| 206 | int arts_write(arts_stream_t stream, const void *buffer, int count) { |
---|
| 207 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
| 208 | if ( !stream ) |
---|
| 209 | return -1; |
---|
| 210 | |
---|
[4891] | 211 | return roar_vs_write(s->vss, buffer, count, NULL); |
---|
[463] | 212 | } |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * configure a parameter of a stream |
---|
| 216 | * |
---|
| 217 | * @param stream an opened record or play stream |
---|
| 218 | * @param parameter the parameter you want to modify |
---|
| 219 | * @param value the new value |
---|
| 220 | * |
---|
| 221 | * @returns the new value of the parameter (which may or may not be the value |
---|
| 222 | * you wanted to have), or an error code if something went wrong |
---|
| 223 | */ |
---|
| 224 | int arts_stream_set(arts_stream_t stream, arts_parameter_t param, int value) { |
---|
| 225 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
[4891] | 226 | if ( stream == NULL ) |
---|
[463] | 227 | return -1; |
---|
| 228 | |
---|
| 229 | if ( param == ARTS_P_BLOCKING ) { |
---|
[4891] | 230 | if ( roar_vs_blocking(s->vss, value ? ROAR_VS_TRUE : ROAR_VS_FALSE, NULL) == -1 ) |
---|
[463] | 231 | return -1; |
---|
| 232 | return arts_stream_get(stream, param); |
---|
| 233 | } |
---|
| 234 | |
---|
[466] | 235 | return arts_stream_get(stream, param); |
---|
[463] | 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * query a parameter of a stream |
---|
| 240 | * |
---|
| 241 | * @param stream an opened record or play stream |
---|
| 242 | * @param parameter the parameter you want to query |
---|
| 243 | * |
---|
| 244 | * @returns the value of the parameter, or an error code |
---|
| 245 | */ |
---|
| 246 | int arts_stream_get(arts_stream_t stream, arts_parameter_t param) { |
---|
| 247 | struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream; |
---|
[4891] | 248 | struct roar_vio_select vios; |
---|
| 249 | struct roar_vio_selecttv rtv; |
---|
| 250 | int events; |
---|
[465] | 251 | |
---|
[4891] | 252 | if ( stream == NULL ) |
---|
[463] | 253 | return -1; |
---|
| 254 | |
---|
| 255 | if ( param == ARTS_P_PACKET_SIZE ) { |
---|
[466] | 256 | return s->block_size; |
---|
| 257 | } else if ( param == ARTS_P_BUFFER_SPACE ) { |
---|
[4891] | 258 | rtv.sec = 0; |
---|
| 259 | rtv.nsec = 1000; |
---|
[466] | 260 | |
---|
[4891] | 261 | events = s->dir == ROAR_DIR_PLAY ? ROAR_VIO_SELECT_WRITE : ROAR_VIO_SELECT_READ; |
---|
[466] | 262 | |
---|
[4891] | 263 | ROAR_VIO_SELECT_SETVIO(&vios, roar_vs_vio_obj(s->vss, NULL), events); |
---|
| 264 | |
---|
| 265 | if ( roar_vio_select(&vios, 1, &rtv, NULL) == 1 ) |
---|
| 266 | return s->block_size; |
---|
[466] | 267 | |
---|
| 268 | return 0; |
---|
| 269 | } else if ( param == ARTS_P_BUFFER_SIZE ) { |
---|
| 270 | return s->block_size*2; |
---|
[463] | 271 | } else if ( param == ARTS_P_PACKET_COUNT ) { |
---|
| 272 | return 1; |
---|
| 273 | } else if ( param == ARTS_P_BLOCKING ) { |
---|
[4891] | 274 | return roar_vs_blocking(s->vss, ROAR_VS_ASK, NULL) == ROAR_VS_TRUE ? 1 : 0; |
---|
[463] | 275 | } |
---|
| 276 | |
---|
| 277 | return -1; |
---|
| 278 | } |
---|
| 279 | |
---|
[462] | 280 | //ll |
---|