source: roaraudio/libroararts/libartsc.c @ 3833:fc0909593cea

Last change on this file since 3833:fc0909593cea was 3833:fc0909593cea, checked in by phi, 14 years ago

use roar_mm_*()

File size: 8.1 KB
Line 
1//libartsc.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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
42static struct roar_connection _libroarartsc_connection[1];
43
44struct _libroarartsc_stream {
45 struct roar_stream stream;
46 int fh;
47 int blocking;
48 int block_size;
49 int dir;
50};
51
52int arts_init(void) {
53 return roar_simple_connect(_libroarartsc_connection, NULL, "libroarartsc client");
54}
55
56void arts_free(void) {
57 roar_disconnect(_libroarartsc_connection);
58}
59
60/**
61 * asks aRtsd to free the DSP device and return 1 if it was successful,
62 * 0 if there were active non-suspendable modules
63 */
64int arts_suspend(void) {
65 return roar_set_standby(_libroarartsc_connection, ROAR_STANDBY_ACTIVE) == 0 ? 1 : 0;
66}
67
68/**
69 * asks aRtsd if the DSP device is free and return 1 if it is,
70 * 0 if not
71 */
72int arts_suspended(void) {
73 return roar_get_standby(_libroarartsc_connection) == ROAR_STANDBY_ACTIVE;
74}
75
76
77/**
78 * converts an error code to a human readable error message
79 *
80 * @param errorcode the errorcode (from another arts function that failed)
81 * @returns a text string with the error message
82 */
83const char *arts_error_text(int errorcode) {
84 return strerror(errorcode);
85}
86
87/**
88 * open a stream for playing
89 *
90 * @param rate the sampling rate (something like 44100)
91 * @param bits how many bits each sample has (8 or 16)
92 * @param channels how many channels, 1 is mono, 2 is stereo
93 * @param name the name of the stream (these will be used so that the user can
94 *          assign streams to effects/mixer channels and similar)
95 *
96 * @return a stream
97 */
98arts_stream_t arts_play_stream(int rate, int bits, int channels, const char *name) {
99 struct _libroarartsc_stream * s = roar_mm_malloc(sizeof(struct _libroarartsc_stream));
100 struct roar_meta meta;
101 struct roar_stream_info info;
102
103 if ( !s )
104  return NULL;
105
106 if ( (s->fh = roar_simple_new_stream_obj(_libroarartsc_connection, &(s->stream),
107                                 rate, channels, bits, ROAR_CODEC_DEFAULT, ROAR_DIR_PLAY)) == -1 ) {
108  roar_mm_free(s);
109  return NULL;
110 }
111
112 s->dir = ROAR_DIR_PLAY;
113
114 s->blocking = 1;
115
116 if ( roar_stream_get_info(_libroarartsc_connection, &(s->stream), &info) != -1 ) {
117  s->block_size = info.block_size;
118 } else {
119  close(s->fh);
120  roar_mm_free(s);
121  return NULL;
122 }
123
124 if ( name && *name ) {
125  meta.value  = (char*)name;
126  meta.key[0] = 0;
127  meta.type   = ROAR_META_TYPE_DESCRIPTION;
128
129  roar_stream_meta_set(_libroarartsc_connection, &(s->stream), ROAR_META_MODE_SET, &meta);
130 }
131
132 return (arts_stream_t) s;
133}
134
135/**
136 * open a stream for recording
137 *
138 * @param rate the sampling rate (something like 44100)
139 * @param bits how many bits each sample has (8 or 16)
140 * @param channels how many channels, 1 is mono, 2 is stereo
141 * @param name the name of the stream (these will be used so that the user can
142 *          assign streams to effects/mixer channels and similar)
143 *
144 * @return a stream
145 */
146arts_stream_t arts_record_stream(int rate, int bits, int channels, const char *name) {
147 struct _libroarartsc_stream * s = roar_mm_malloc(sizeof(struct _libroarartsc_stream));
148 struct roar_meta meta;
149 struct roar_stream_info info;
150
151 if ( !s )
152  return NULL;
153
154 if ( (s->fh = roar_simple_new_stream_obj(_libroarartsc_connection, &(s->stream),
155                                 rate, channels, bits, ROAR_CODEC_DEFAULT, ROAR_DIR_RECORD)) == -1 ) {
156  roar_mm_free(s);
157  return NULL;
158 }
159
160 s->dir = ROAR_DIR_RECORD;
161
162 s->blocking = 1;
163
164 if ( roar_stream_get_info(_libroarartsc_connection, &(s->stream), &info) != -1 ) {
165  s->block_size = info.block_size;
166 } else {
167  close(s->fh);
168  roar_mm_free(s);
169  return NULL;
170 }
171
172 if ( name && *name ) {
173  meta.value  = (char*)name;
174  meta.key[0] = 0;
175  meta.type   = ROAR_META_TYPE_DESCRIPTION;
176
177  roar_stream_meta_set(_libroarartsc_connection, &(s->stream), ROAR_META_MODE_SET, &meta);
178 }
179
180 return (arts_stream_t) s;
181}
182
183/**
184 * close a stream
185 */
186void arts_close_stream(arts_stream_t stream) {
187 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
188 if ( !stream )
189  return;
190
191 close(s->fh);
192
193 roar_mm_free(stream);
194}
195
196/**
197 * read samples from stream
198 *
199 * @param stream a previously opened record stream
200 * @param buffer a buffer with sample data
201 * @param count the number of bytes contained in the buffer
202 *
203 * @returns number of read bytes on success or error code
204 */
205int arts_read(arts_stream_t stream, void *buffer, int count) {
206 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
207 if ( !stream )
208  return -1;
209
210 return read(s->fh, buffer, count);
211}
212
213/**
214 * write samples to to stream
215 *
216 * @param stream a previously opened play stream
217 * @param buffer a buffer with sample data
218 * @param count the number of bytes contained in the buffer
219 *
220 * @returns number of written bytes on success or error code
221 */
222int arts_write(arts_stream_t stream, const void *buffer, int count) {
223 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
224 if ( !stream )
225  return -1;
226
227 return write(s->fh, buffer, count);
228}
229
230/**
231 * configure a parameter of a stream
232 *
233 * @param stream an opened record or play stream
234 * @param parameter the parameter you want to modify
235 * @param value the new value
236 *
237 * @returns the new value of the parameter (which may or may not be the value
238 *          you wanted to have), or an error code if something went wrong
239 */
240int arts_stream_set(arts_stream_t stream, arts_parameter_t param, int value) {
241 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
242 if ( !stream )
243  return -1;
244
245 if ( param == ARTS_P_BLOCKING ) {
246  if ( roar_socket_nonblock(s->fh, value ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
247   return -1;
248  return arts_stream_get(stream, param);
249 }
250
251 return arts_stream_get(stream, param);
252}
253
254/**
255 * query a parameter of a stream
256 *
257 * @param stream an opened record or play stream
258 * @param parameter the parameter you want to query
259 *
260 * @returns the value of the parameter, or an error code
261 */
262int arts_stream_get(arts_stream_t stream, arts_parameter_t param) {
263 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
264 fd_set sl;
265 struct timeval tv;
266
267 if ( !stream )
268  return -1;
269
270 if ( param == ARTS_P_PACKET_SIZE ) {
271   return s->block_size;
272 } else if ( param == ARTS_P_BUFFER_SPACE ) {
273  FD_ZERO(&sl);
274  FD_SET(s->fh, &sl);
275
276  tv.tv_sec  = 0;
277  tv.tv_usec = 1;
278
279  if ( s->dir == ROAR_DIR_PLAY ) {
280   if (select(s->fh + 1, NULL, &sl, NULL, &tv) > 0)
281    return s->block_size;
282  } else {
283   if (select(s->fh + 1, &sl, NULL, NULL, &tv) > 0)
284    return s->block_size;
285  }
286
287  return 0;
288 } else if ( param == ARTS_P_BUFFER_SIZE ) {
289   return s->block_size*2;
290 } else if ( param == ARTS_P_PACKET_COUNT ) {
291  return 1;
292 } else if ( param == ARTS_P_BLOCKING ) {
293  return s->blocking;
294 }
295
296 return -1;
297}
298
299//ll
Note: See TracBrowser for help on using the repository browser.