source: roaraudio/libroarsndio/stream.c @ 3811:49db840fb4f4

Last change on this file since 3811:49db840fb4f4 was 3811:49db840fb4f4, checked in by phi, 14 years ago

fixed some copyright statements

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