[1563] | 1 | //stream.c: |
---|
[1559] | 2 | |
---|
| 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
[1559] | 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 |
---|
[3517] | 25 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 26 | * Boston, MA 02110-1301, USA. |
---|
[1559] | 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 | |
---|
[1571] | 34 | #define ROAR_USE_OWN_SNDIO_HDL |
---|
[1559] | 35 | #include "libroarsndio.h" |
---|
| 36 | |
---|
[2375] | 37 | // this is void because we do not care about if this failed... |
---|
| 38 | static 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 | |
---|
[1567] | 78 | #define _i(x) (hdl->info.x) |
---|
[1563] | 79 | int sio_start (struct sio_hdl * hdl) { |
---|
[2019] | 80 | |
---|
[2020] | 81 | // TODO: FIXME: use full VIO support here, not fh->vio! |
---|
| 82 | |
---|
[1567] | 83 | if ( hdl == NULL ) |
---|
| 84 | return 0; |
---|
| 85 | |
---|
[2019] | 86 | if ( hdl->stream_opened ) |
---|
| 87 | return 0; |
---|
| 88 | |
---|
[2843] | 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 ) |
---|
[1567] | 91 | return 0; |
---|
| 92 | |
---|
[2022] | 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 | |
---|
[3237] | 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 | |
---|
[2375] | 102 | send_vol_event(hdl); |
---|
[2019] | 103 | |
---|
| 104 | hdl->stream_opened = 1; |
---|
[3238] | 105 | hdl->ioerror = 0; |
---|
[1567] | 106 | |
---|
| 107 | return 1; |
---|
[1563] | 108 | } |
---|
[1567] | 109 | #undef _i |
---|
[1563] | 110 | |
---|
| 111 | int sio_stop (struct sio_hdl * hdl) { |
---|
| 112 | |
---|
| 113 | if ( hdl == NULL ) |
---|
| 114 | return 0; |
---|
| 115 | |
---|
[2019] | 116 | if ( !hdl->stream_opened ) |
---|
[1563] | 117 | return 0; |
---|
| 118 | |
---|
[2019] | 119 | roar_vio_close(&(hdl->svio)); |
---|
[1563] | 120 | |
---|
[2019] | 121 | hdl->stream_opened = -1; |
---|
[1563] | 122 | |
---|
| 123 | return 1; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | size_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 | |
---|
[2019] | 132 | if ( !hdl->stream_opened ) |
---|
[1563] | 133 | return 0; |
---|
| 134 | |
---|
[3238] | 135 | if ( (ret = roar_vio_read(&(hdl->svio), addr, nbytes)) < 0 ) { |
---|
| 136 | hdl->ioerror = 1; |
---|
[1563] | 137 | return 0; |
---|
[3238] | 138 | } |
---|
| 139 | |
---|
| 140 | if ( hdl->nonblock ) |
---|
| 141 | hdl->ioerror = 0; |
---|
[1563] | 142 | |
---|
| 143 | return ret; |
---|
| 144 | } |
---|
[2574] | 145 | size_t sio_write (struct sio_hdl * hdl, const void * addr, size_t nbytes) { |
---|
[1563] | 146 | ssize_t ret; |
---|
| 147 | |
---|
| 148 | if ( hdl == NULL ) |
---|
| 149 | return 0; |
---|
| 150 | |
---|
[2019] | 151 | if ( !hdl->stream_opened ) |
---|
[1563] | 152 | return 0; |
---|
| 153 | |
---|
[3238] | 154 | if ( (ret = roar_vio_write(&(hdl->svio), (void*) addr, nbytes)) < 0 ) { |
---|
| 155 | hdl->ioerror = 1; |
---|
[1563] | 156 | return 0; |
---|
[3238] | 157 | } |
---|
| 158 | |
---|
| 159 | if ( hdl->nonblock ) |
---|
| 160 | hdl->ioerror = 0; |
---|
[1563] | 161 | |
---|
[1577] | 162 | if ( hdl->on_move != NULL ) { |
---|
| 163 | hdl->on_move(hdl->on_move_arg, 8*ret/(hdl->info.channels * hdl->info.bits)); |
---|
| 164 | } |
---|
| 165 | |
---|
[1563] | 166 | return ret; |
---|
| 167 | } |
---|
| 168 | |
---|
[1559] | 169 | //ll |
---|