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