source: roaraudio/roard/driver_artsc.c @ 5056:b31e60545552

Last change on this file since 5056:b31e60545552 was 5056:b31e60545552, checked in by phi, 11 years ago

support auto reconf bits=32->16 in case not supported (See: #48)

File size: 4.1 KB
Line 
1//driver_artsc.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifdef ROAR_HAVE_LIBARTSC
29
30#define INIT     +1
31#define SHUTDOWN -1
32
33static int driver_artsc_init_shutdown (int action) {
34 static int state = 0;
35
36 if ( action != INIT && action != SHUTDOWN )
37  return -1;
38
39 if ( state == 0 && action == INIT ) {
40  if ( arts_init() != 0 )
41   return -1;
42 } else if ( state == 1 && action == SHUTDOWN ) {
43  arts_free();
44 }
45
46 state += action;
47
48 return 0;
49}
50
51int     driver_artsc_open_vio(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) {
52 struct roar_artsc * self = NULL;
53 int autoconfig           = 0;
54
55 (void)sstream;
56
57 if ( fh != -1 || device != NULL )
58  return -1;
59
60 if ( sstream != NULL ) {
61  autoconfig = streams_get_flag(ROAR_STREAM(sstream)->id, ROAR_FLAG_AUTOCONF);
62 }
63
64 self = roar_mm_malloc(sizeof(struct roar_artsc));
65
66 if ( self == NULL )
67  return -1;
68
69 memset(self, 0, sizeof(struct roar_artsc));
70
71 if ( driver_artsc_init_shutdown(INIT) == -1 ) {
72  roar_mm_free(self);
73  return -1;
74 }
75
76 info->codec = ROAR_CODEC_DEFAULT;
77
78 self->info   = info;
79 self->stream = arts_play_stream(info->rate, info->bits, info->channels, "roard");
80
81 if ( self->stream == NULL && autoconfig ) {
82  info->bits   = 16;
83  self->stream = arts_play_stream(info->rate, info->bits, info->channels, "roard");
84 }
85
86 if ( self->stream == NULL ) {
87  roar_mm_free(self);
88  driver_artsc_init_shutdown(SHUTDOWN);
89  return -1;
90 }
91
92 memset(inst, 0, sizeof(struct roar_vio_calls));
93
94 inst->inst     = (void*) self;
95 inst->write    = driver_artsc_write;
96 inst->close    = driver_artsc_close;
97 inst->ctl      = driver_artsc_ctl;
98 inst->nonblock = driver_artsc_nonblock;
99
100 return 0;
101}
102
103ssize_t driver_artsc_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
104 struct roar_artsc * self = vio->inst;
105 int ret = arts_write(self->stream, buf, count);
106
107 if ( ret >= 0 )
108  return ret;
109
110 return -1;
111}
112
113int     driver_artsc_close   (struct roar_vio_calls * vio) {
114 struct roar_artsc * self = vio->inst;
115
116 arts_close_stream(self->stream);
117
118 roar_mm_free(self);
119
120 return driver_artsc_init_shutdown(SHUTDOWN);
121}
122
123int     driver_artsc_nonblock(struct roar_vio_calls * vio, int state) {
124 struct roar_artsc * self = vio->inst;
125 int ret;
126
127 switch (state) {
128  case ROAR_SOCKET_NONBLOCK:
129    ret = arts_stream_set(self->stream, ARTS_P_BLOCKING, 0);
130    if ( ret == 0 )
131     return 0;
132   break;
133  case ROAR_SOCKET_BLOCK:
134    ret = arts_stream_set(self->stream, ARTS_P_BLOCKING, 1);
135    if ( ret == 1 )
136     return 0;
137   break;
138 }
139
140 return -1;
141}
142
143int     driver_artsc_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
144 struct roar_artsc * self = vio->inst;
145 ssize_t bps;
146 int ret;
147 uint_least32_t tmp;
148
149 switch (cmd) {
150  case ROAR_VIO_CTL_GET_DBLKSIZE:
151    ret = arts_stream_get(self->stream, ARTS_P_PACKET_SIZE);
152    if ( ret > 0 ) {
153     *(uint_least32_t *)data = ret;
154     return 0;
155    }
156   break;
157  case ROAR_VIO_CTL_GET_DELAY:
158    ret = arts_stream_get(self->stream, ARTS_P_TOTAL_LATENCY);
159    if ( ret < 0 )
160     return -1;
161    bps = roar_info2bitspersec(self->info);
162    if ( bps < 0 )
163     return -1;
164
165    tmp  = bps / (ssize_t)8;
166    tmp *= ret;
167    tmp /= (uint_least32_t)1000;
168
169    *(uint_least32_t *)data = tmp;
170
171    return 0;
172  default:
173    return -1;
174 }
175
176 return -1;
177}
178
179#endif
180
181//ll
Note: See TracBrowser for help on using the repository browser.