source: roaraudio/libroararts/libartsc.c @ 4891:7d1d30510aa6

Last change on this file since 4891:7d1d30510aa6 was 4891:7d1d30510aa6, checked in by phi, 13 years ago

clear some more ckport warnings

File size: 8.0 KB
Line 
1//libartsc.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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 roar_vs_t * vss;
46 struct roar_stream _stream;
47 int _fh;
48 int _blocking;
49 int block_size;
50 int dir;
51};
52
53int arts_init(void) {
54 return roar_simple_connect(_libroarartsc_connection, NULL, "libroarartsc client");
55}
56
57void 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 */
65int 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 */
73int 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 */
84const char *arts_error_text(int errorcode) {
85 return roar_vs_strerr(errorcode);
86}
87
88static 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 {
123  roar_vs_close(s->vss, ROAR_VS_TRUE, &err);
124  roar_mm_free(s);
125  return NULL;
126 }
127
128 if ( name != NULL && *name ) {
129  kv[0].key   = "description";
130  kv[0].value = (char*)name;
131
132  roar_vs_meta(s->vss, kv, 1, &err);
133 }
134
135 return (arts_stream_t) s;
136}
137
138/**
139 * open a stream for playing
140 *
141 * @param rate the sampling rate (something like 44100)
142 * @param bits how many bits each sample has (8 or 16)
143 * @param channels how many channels, 1 is mono, 2 is stereo
144 * @param name the name of the stream (these will be used so that the user can
145 *          assign streams to effects/mixer channels and similar)
146 *
147 * @return a stream
148 */
149arts_stream_t arts_play_stream(int rate, int bits, int channels, const char *name) {
150 return _arts_stream(rate, bits, channels, name, ROAR_DIR_PLAY);
151}
152
153/**
154 * open a stream for recording
155 *
156 * @param rate the sampling rate (something like 44100)
157 * @param bits how many bits each sample has (8 or 16)
158 * @param channels how many channels, 1 is mono, 2 is stereo
159 * @param name the name of the stream (these will be used so that the user can
160 *          assign streams to effects/mixer channels and similar)
161 *
162 * @return a stream
163 */
164arts_stream_t arts_record_stream(int rate, int bits, int channels, const char *name) {
165 return _arts_stream(rate, bits, channels, name, ROAR_DIR_RECORD);
166}
167
168/**
169 * close a stream
170 */
171void arts_close_stream(arts_stream_t stream) {
172 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
173 if ( stream == NULL )
174  return;
175
176 roar_vs_close(s->vss, ROAR_VS_TRUE, NULL);
177
178 roar_mm_free(stream);
179}
180
181/**
182 * read samples from stream
183 *
184 * @param stream a previously opened record stream
185 * @param buffer a buffer with sample data
186 * @param count the number of bytes contained in the buffer
187 *
188 * @returns number of read bytes on success or error code
189 */
190int arts_read(arts_stream_t stream, void *buffer, int count) {
191 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
192 if ( stream == NULL )
193  return -1;
194
195 return roar_vs_read(s->vss, buffer, count, NULL);
196}
197
198/**
199 * write samples to to stream
200 *
201 * @param stream a previously opened play stream
202 * @param buffer a buffer with sample data
203 * @param count the number of bytes contained in the buffer
204 *
205 * @returns number of written bytes on success or error code
206 */
207int arts_write(arts_stream_t stream, const void *buffer, int count) {
208 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
209 if ( !stream )
210  return -1;
211
212 return roar_vs_write(s->vss, buffer, count, NULL);
213}
214
215/**
216 * configure a parameter of a stream
217 *
218 * @param stream an opened record or play stream
219 * @param parameter the parameter you want to modify
220 * @param value the new value
221 *
222 * @returns the new value of the parameter (which may or may not be the value
223 *          you wanted to have), or an error code if something went wrong
224 */
225int arts_stream_set(arts_stream_t stream, arts_parameter_t param, int value) {
226 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
227 if ( stream == NULL )
228  return -1;
229
230 if ( param == ARTS_P_BLOCKING ) {
231  if ( roar_vs_blocking(s->vss, value ? ROAR_VS_TRUE : ROAR_VS_FALSE, NULL) == -1 )
232   return -1;
233  return arts_stream_get(stream, param);
234 }
235
236 return arts_stream_get(stream, param);
237}
238
239/**
240 * query a parameter of a stream
241 *
242 * @param stream an opened record or play stream
243 * @param parameter the parameter you want to query
244 *
245 * @returns the value of the parameter, or an error code
246 */
247int arts_stream_get(arts_stream_t stream, arts_parameter_t param) {
248 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
249 struct roar_vio_select vios;
250 struct roar_vio_selecttv rtv;
251 int events;
252
253 if ( stream == NULL )
254  return -1;
255
256 if ( param == ARTS_P_PACKET_SIZE ) {
257   return s->block_size;
258 } else if ( param == ARTS_P_BUFFER_SPACE ) {
259  rtv.sec    = 0;
260  rtv.nsec   = 1000;
261
262  events     = s->dir == ROAR_DIR_PLAY ? ROAR_VIO_SELECT_WRITE : ROAR_VIO_SELECT_READ;
263
264  ROAR_VIO_SELECT_SETVIO(&vios, roar_vs_vio_obj(s->vss, NULL), events);
265
266  if ( roar_vio_select(&vios, 1, &rtv, NULL) == 1 )
267   return s->block_size;
268
269  return 0;
270 } else if ( param == ARTS_P_BUFFER_SIZE ) {
271   return s->block_size*2;
272 } else if ( param == ARTS_P_PACKET_COUNT ) {
273  return 1;
274 } else if ( param == ARTS_P_BLOCKING ) {
275  return roar_vs_blocking(s->vss, ROAR_VS_ASK, NULL) == ROAR_VS_TRUE ? 1 : 0;
276 }
277
278 return -1;
279}
280
281//ll
Note: See TracBrowser for help on using the repository browser.