source: roaraudio/libroarsndio/stream.c @ 4038:5e7b9cca7dd2

Last change on this file since 4038:5e7b9cca7dd2 was 4038:5e7b9cca7dd2, checked in by phi, 14 years ago

some cleanup

File size: 4.0 KB
Line 
1//stream.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from OpenBSD's sndio.
7 *
8 *  This file is part of libroaresd a part of RoarAudio,
9 *  a cross-platform sound system for both, home and professional use.
10 *  See README for details.
11 *
12 *  This file is free software; you can redistribute it and/or modify
13 *  it under the terms of the GNU General Public License version 3
14 *  as published by the Free Software Foundation.
15 *
16 *  RoarAudio is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License
22 *  along with this software; see the file COPYING.  If not, write to
23 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24 *  Boston, MA 02110-1301, USA.
25 *
26 *  NOTE for everyone want's to change something and send patches:
27 *  read README and HACKING! There a addition information on
28 *  the license of this document you need to read before you send
29 *  any patches.
30 */
31
32#define ROAR_USE_OWN_SNDIO_HDL
33#include "libroarsndio.h"
34
35// this is void because we do not care about if this failed...
36static void send_vol_event (struct sio_hdl * hdl) {
37 int channels;
38 struct roar_mixer_settings mixer;
39 int i;
40 unsigned vol;
41
42 // in case of no event handler we do not send the event at all ;)
43 if ( hdl->on_vol == NULL )
44  return;
45
46 if ( roar_get_vol(&(hdl->con), roar_stream_get_id(&(hdl->stream)), &mixer, &channels) == -1 )
47  return;
48
49 ROAR_DBG("send_vol_event(*): channels=%i", channels);
50
51 switch (channels) {
52  case 1:
53    ROAR_DBG("send_vol_event(*): mixer.scale=%i, mixer.mixer={%i,...}", mixer.scale, mixer.mixer[0]);
54    vol  = mixer.mixer[0] * SIO_MAXVOL;
55    vol /= mixer.scale;
56    ROAR_DBG("send_vol_event(*): vol=%u", vol);
57   break;
58  case 2:
59    vol = (mixer.mixer[0] + mixer.mixer[1]) * SIO_MAXVOL / mixer.scale / 2;
60   break;
61  default:
62    vol = 0;
63    for (i = 0; i < channels; i++)
64     vol += mixer.mixer[i];
65
66    vol /= channels;
67    vol *= SIO_MAXVOL;
68    vol /= mixer.scale;
69   break;
70 }
71
72 ROAR_DBG("send_vol_event(*): vol=%u", vol);
73 hdl->on_vol(hdl->on_vol_arg, vol);
74}
75
76#define _i(x) (hdl->info.x)
77int    sio_start  (struct sio_hdl * hdl) {
78
79 // TODO: FIXME: use full VIO support here, not fh->vio!
80
81 if ( hdl == NULL )
82  return 0;
83
84 if ( hdl->stream_opened )
85  return 0;
86
87 if ( roar_vio_simple_new_stream_obj(&(hdl->svio), &(hdl->con), &(hdl->stream),
88                                     _i(rate), _i(channels), _i(bits), _i(codec), hdl->dir) == -1 )
89  return 0;
90
91 ROAR_DBG("sio_start(hdl=%p): rate=%i, channels=%i, bits=%i, codec=%i", hdl, _i(rate), _i(channels), _i(bits), _i(codec));
92
93 if ( hdl->nonblock ) {
94  if ( roar_vio_nonblock(&(hdl->svio), ROAR_SOCKET_NONBLOCK) == -1 ) {
95   roar_vio_close(&(hdl->svio));
96   return 0;
97  }
98 }
99
100 send_vol_event(hdl);
101
102 hdl->stream_opened = 1;
103 hdl->ioerror       = 0;
104
105 return 1;
106}
107#undef _i
108
109int    sio_stop   (struct sio_hdl * hdl) {
110
111 if ( hdl == NULL )
112  return 0;
113
114 if ( !hdl->stream_opened )
115  return 0;
116
117 roar_vio_close(&(hdl->svio));
118
119 hdl->stream_opened = -1;
120
121 return 1;
122}
123
124size_t sio_read   (struct sio_hdl * hdl, void * addr, size_t nbytes) {
125 ssize_t ret;
126
127 if ( hdl == NULL )
128  return 0;
129
130 if ( !hdl->stream_opened )
131  return 0;
132
133 if ( (ret = roar_vio_read(&(hdl->svio), addr, nbytes)) < 0 ) {
134  hdl->ioerror = 1;
135  return 0;
136 }
137
138 if ( hdl->nonblock )
139  hdl->ioerror = 0;
140
141 return ret;
142}
143size_t sio_write  (struct sio_hdl * hdl, const void * addr, size_t nbytes) {
144 ssize_t ret;
145
146 if ( hdl == NULL )
147  return 0;
148
149 if ( !hdl->stream_opened )
150  return 0;
151
152 if ( (ret = roar_vio_write(&(hdl->svio), (void*) addr, nbytes)) < 0 ) {
153  hdl->ioerror = 1;
154  return 0;
155 }
156
157 if ( hdl->nonblock )
158  hdl->ioerror = 0;
159
160 if ( hdl->on_move != NULL ) {
161  hdl->on_move(hdl->on_move_arg, 8*ret/(hdl->info.channels * hdl->info.bits));
162 }
163
164 return ret;
165}
166
167//ll
Note: See TracBrowser for help on using the repository browser.