1 | //stream.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
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, 675 Mass Ave, Cambridge, MA 02139, 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 | |
---|
33 | #define ROAR_USE_OWN_SNDIO_HDL |
---|
34 | #include "libroarsndio.h" |
---|
35 | |
---|
36 | // this is void because we do not care about if this failed... |
---|
37 | static void send_vol_event (struct sio_hdl * hdl) { |
---|
38 | int channels; |
---|
39 | struct roar_mixer_settings mixer; |
---|
40 | int i; |
---|
41 | unsigned vol; |
---|
42 | |
---|
43 | // in case of no event handler we do not send the event at all ;) |
---|
44 | if ( hdl->on_vol == NULL ) |
---|
45 | return; |
---|
46 | |
---|
47 | if ( roar_get_vol(&(hdl->con), roar_stream_get_id(&(hdl->stream)), &mixer, &channels) == -1 ) |
---|
48 | return; |
---|
49 | |
---|
50 | ROAR_DBG("send_vol_event(*): channels=%i", channels); |
---|
51 | |
---|
52 | switch (channels) { |
---|
53 | case 1: |
---|
54 | ROAR_DBG("send_vol_event(*): mixer.scale=%i, mixer.mixer={%i,...}", mixer.scale, mixer.mixer[0]); |
---|
55 | vol = mixer.mixer[0] * SIO_MAXVOL; |
---|
56 | vol /= mixer.scale; |
---|
57 | ROAR_DBG("send_vol_event(*): vol=%u", vol); |
---|
58 | break; |
---|
59 | case 2: |
---|
60 | vol = (mixer.mixer[0] + mixer.mixer[1]) * SIO_MAXVOL / mixer.scale / 2; |
---|
61 | break; |
---|
62 | default: |
---|
63 | vol = 0; |
---|
64 | for (i = 0; i < channels; i++) |
---|
65 | vol += mixer.mixer[i]; |
---|
66 | |
---|
67 | vol /= channels; |
---|
68 | vol *= SIO_MAXVOL; |
---|
69 | vol /= mixer.scale; |
---|
70 | break; |
---|
71 | } |
---|
72 | |
---|
73 | ROAR_DBG("send_vol_event(*): vol=%u", vol); |
---|
74 | hdl->on_vol(hdl->on_vol_arg, vol); |
---|
75 | } |
---|
76 | |
---|
77 | #define _i(x) (hdl->info.x) |
---|
78 | int sio_start (struct sio_hdl * hdl) { |
---|
79 | |
---|
80 | // TODO: FIXME: use full VIO support here, not fh->vio! |
---|
81 | |
---|
82 | if ( hdl == NULL ) |
---|
83 | return 0; |
---|
84 | |
---|
85 | if ( hdl->stream_opened ) |
---|
86 | return 0; |
---|
87 | |
---|
88 | if ( roar_vio_simple_new_stream_obj(&(hdl->svio), &(hdl->con), &(hdl->stream), |
---|
89 | _i(rate), _i(channels), _i(bits), _i(codec), hdl->dir) == -1 ) |
---|
90 | return 0; |
---|
91 | |
---|
92 | ROAR_DBG("sio_start(hdl=%p): rate=%i, channels=%i, bits=%i, codec=%i", hdl, _i(rate), _i(channels), _i(bits), _i(codec)); |
---|
93 | |
---|
94 | send_vol_event(hdl); |
---|
95 | |
---|
96 | hdl->stream_opened = 1; |
---|
97 | |
---|
98 | return 1; |
---|
99 | } |
---|
100 | #undef _i |
---|
101 | |
---|
102 | int sio_stop (struct sio_hdl * hdl) { |
---|
103 | |
---|
104 | if ( hdl == NULL ) |
---|
105 | return 0; |
---|
106 | |
---|
107 | if ( !hdl->stream_opened ) |
---|
108 | return 0; |
---|
109 | |
---|
110 | roar_vio_close(&(hdl->svio)); |
---|
111 | |
---|
112 | hdl->stream_opened = -1; |
---|
113 | |
---|
114 | return 1; |
---|
115 | } |
---|
116 | |
---|
117 | size_t sio_read (struct sio_hdl * hdl, void * addr, size_t nbytes) { |
---|
118 | ssize_t ret; |
---|
119 | |
---|
120 | if ( hdl == NULL ) |
---|
121 | return 0; |
---|
122 | |
---|
123 | if ( !hdl->stream_opened ) |
---|
124 | return 0; |
---|
125 | |
---|
126 | if ( (ret = roar_vio_read(&(hdl->svio), addr, nbytes)) < 0 ) |
---|
127 | return 0; |
---|
128 | |
---|
129 | return ret; |
---|
130 | } |
---|
131 | size_t sio_write (struct sio_hdl * hdl, const void * addr, size_t nbytes) { |
---|
132 | ssize_t ret; |
---|
133 | |
---|
134 | if ( hdl == NULL ) |
---|
135 | return 0; |
---|
136 | |
---|
137 | if ( !hdl->stream_opened ) |
---|
138 | return 0; |
---|
139 | |
---|
140 | if ( (ret = roar_vio_write(&(hdl->svio), (void*) addr, nbytes)) < 0 ) |
---|
141 | return 0; |
---|
142 | |
---|
143 | if ( hdl->on_move != NULL ) { |
---|
144 | hdl->on_move(hdl->on_move_arg, 8*ret/(hdl->info.channels * hdl->info.bits)); |
---|
145 | } |
---|
146 | |
---|
147 | return ret; |
---|
148 | } |
---|
149 | |
---|
150 | //ll |
---|