source: roaraudio/libroararts/libartsc.c @ 465:82fd66e50dc3

Last change on this file since 465:82fd66e50dc3 was 465:82fd66e50dc3, checked in by phi, 16 years ago

added support to get the size of optimal read() and write()s

File size: 5.7 KB
Line 
1//libartsc.c:
2
3#include <roaraudio.h>
4#include <kde/artsc/artsc.h>
5
6struct roar_connection _libroarartsc_connection[1];
7
8struct _libroarartsc_stream {
9 struct roar_stream stream;
10 int fh;
11 int blocking;
12};
13
14int arts_init(void) {
15 return roar_simple_connect(_libroarartsc_connection, NULL, "libroarartsc client");
16}
17
18void arts_free(void) {
19 roar_disconnect(_libroarartsc_connection);
20}
21
22/**
23 * asks aRtsd to free the DSP device and return 1 if it was successful,
24 * 0 if there were active non-suspendable modules
25 */
26int arts_suspend(void) {
27 return roar_set_standby(_libroarartsc_connection, ROAR_STANDBY_ACTIVE) == 0 ? 1 : 0;
28}
29
30/**
31 * asks aRtsd if the DSP device is free and return 1 if it is,
32 * 0 if not
33 */
34int arts_suspended(void) {
35 return roar_get_standby(_libroarartsc_connection) == ROAR_STANDBY_ACTIVE;
36}
37
38
39/**
40 * converts an error code to a human readable error message
41 *
42 * @param errorcode the errorcode (from another arts function that failed)
43 * @returns a text string with the error message
44 */
45const char *arts_error_text(int errorcode) {
46 return strerror(errorcode);
47}
48
49/**
50 * open a stream for playing
51 *
52 * @param rate the sampling rate (something like 44100)
53 * @param bits how many bits each sample has (8 or 16)
54 * @param channels how many channels, 1 is mono, 2 is stereo
55 * @param name the name of the stream (these will be used so that the user can
56 *          assign streams to effects/mixer channels and similar)
57 *
58 * @return a stream
59 */
60arts_stream_t arts_play_stream(int rate, int bits, int channels, const char *name) {
61 struct _libroarartsc_stream * s = malloc(sizeof(struct _libroarartsc_stream));
62 struct roar_meta meta;
63
64 if ( !s )
65  return NULL;
66
67 if ( (s->fh = roar_simple_new_stream_obj(_libroarartsc_connection, &(s->stream),
68                                 rate, channels, bits, ROAR_CODEC_DEFAULT, ROAR_DIR_PLAY)) == -1 ) {
69  free(s);
70  return NULL;
71 }
72
73 s->blocking = 1;
74
75 if ( name && *name ) {
76  meta.value  = (char*)name;
77  meta.key[0] = 0;
78  meta.type   = ROAR_META_TYPE_DESCRIPTION;
79
80  roar_stream_meta_set(_libroarartsc_connection, &(s->stream), ROAR_META_MODE_SET, &meta);
81 }
82
83 return (arts_stream_t) s;
84}
85
86/**
87 * open a stream for recording
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 */
97arts_stream_t arts_record_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;
100
101 if ( !s )
102  return NULL;
103
104 if ( (s->fh = roar_simple_new_stream_obj(_libroarartsc_connection, &(s->stream),
105                                 rate, channels, bits, ROAR_CODEC_DEFAULT, ROAR_DIR_RECORD)) == -1 ) {
106  free(s);
107  return NULL;
108 }
109
110 s->blocking = 1;
111
112 if ( name && *name ) {
113  meta.value  = (char*)name;
114  meta.key[0] = 0;
115  meta.type   = ROAR_META_TYPE_DESCRIPTION;
116
117  roar_stream_meta_set(_libroarartsc_connection, &(s->stream), ROAR_META_MODE_SET, &meta);
118 }
119
120 return (arts_stream_t) s;
121}
122
123/**
124 * close a stream
125 */
126void arts_close_stream(arts_stream_t stream) {
127 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
128 if ( !stream )
129  return;
130
131 close(s->fh);
132
133 free(stream);
134}
135
136/**
137 * read samples from stream
138 *
139 * @param stream a previously opened record stream
140 * @param buffer a buffer with sample data
141 * @param count the number of bytes contained in the buffer
142 *
143 * @returns number of read bytes on success or error code
144 */
145int arts_read(arts_stream_t stream, void *buffer, int count) {
146 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
147 if ( !stream )
148  return -1;
149
150 return read(s->fh, buffer, count);
151}
152
153/**
154 * write samples to to stream
155 *
156 * @param stream a previously opened play stream
157 * @param buffer a buffer with sample data
158 * @param count the number of bytes contained in the buffer
159 *
160 * @returns number of written bytes on success or error code
161 */
162int arts_write(arts_stream_t stream, const void *buffer, int count) {
163 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
164 if ( !stream )
165  return -1;
166
167 return write(s->fh, buffer, count);
168}
169
170/**
171 * configure a parameter of a stream
172 *
173 * @param stream an opened record or play stream
174 * @param parameter the parameter you want to modify
175 * @param value the new value
176 *
177 * @returns the new value of the parameter (which may or may not be the value
178 *          you wanted to have), or an error code if something went wrong
179 */
180int arts_stream_set(arts_stream_t stream, arts_parameter_t param, int value) {
181 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
182 if ( !stream )
183  return -1;
184
185 if ( param == ARTS_P_BLOCKING ) {
186  if ( roar_socket_nonblock(s->fh, value ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
187   return -1;
188  return arts_stream_get(stream, param);
189 }
190
191 return -1;
192}
193
194/**
195 * query a parameter of a stream
196 *
197 * @param stream an opened record or play stream
198 * @param parameter the parameter you want to query
199 *
200 * @returns the value of the parameter, or an error code
201 */
202int arts_stream_get(arts_stream_t stream, arts_parameter_t param) {
203 struct _libroarartsc_stream * s = (struct _libroarartsc_stream *) stream;
204 struct roar_stream_info info;
205
206 if ( !stream )
207  return -1;
208
209 if ( param == ARTS_P_PACKET_SIZE ) {
210  if ( roar_stream_get_info(_libroarartsc_connection, &(s->stream), &info) != -1 ) {
211   return info.block_size;
212  }
213  return -1;
214 } else if ( param == ARTS_P_PACKET_COUNT ) {
215  return 1;
216 } else if ( param == ARTS_P_BLOCKING ) {
217  return s->blocking;
218 }
219
220 return -1;
221}
222
223//ll
Note: See TracBrowser for help on using the repository browser.