[1563] | 1 | //stream.c: |
---|
[1559] | 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 | |
---|
[1571] | 33 | #define ROAR_USE_OWN_SNDIO_HDL |
---|
[1559] | 34 | #include "libroarsndio.h" |
---|
| 35 | |
---|
[2375] | 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 | |
---|
[1567] | 77 | #define _i(x) (hdl->info.x) |
---|
[1563] | 78 | int sio_start (struct sio_hdl * hdl) { |
---|
[2051] | 79 | #ifndef ROAR_TARGET_WIN32 |
---|
[2019] | 80 | int fh; |
---|
[2051] | 81 | #endif |
---|
[2019] | 82 | |
---|
[2020] | 83 | // TODO: FIXME: use full VIO support here, not fh->vio! |
---|
| 84 | |
---|
[1567] | 85 | if ( hdl == NULL ) |
---|
| 86 | return 0; |
---|
| 87 | |
---|
[2019] | 88 | if ( hdl->stream_opened ) |
---|
| 89 | return 0; |
---|
| 90 | |
---|
[2051] | 91 | #ifndef ROAR_TARGET_WIN32 |
---|
[2358] | 92 | if ( (fh = roar_simple_new_stream_obj(&(hdl->con), &(hdl->stream), _i(rate), _i(channels), _i(bits), _i(codec), hdl->dir)) == -1 ) |
---|
[1567] | 93 | return 0; |
---|
| 94 | |
---|
[2022] | 95 | ROAR_DBG("sio_start(hdl=%p): rate=%i, channels=%i, bits=%i, codec=%i", hdl, _i(rate), _i(channels), _i(bits), _i(codec)); |
---|
| 96 | |
---|
[2019] | 97 | if ( roar_vio_open_fh_socket(&(hdl->svio), fh) == -1 ) { |
---|
| 98 | close(fh); |
---|
[1567] | 99 | return 0; |
---|
[2019] | 100 | } |
---|
[2375] | 101 | |
---|
| 102 | send_vol_event(hdl); |
---|
[2051] | 103 | #else |
---|
| 104 | if ( roar_stream_new_by_id(&(hdl->stream), -1) == -1 ) |
---|
| 105 | return 0; |
---|
| 106 | |
---|
[2358] | 107 | if (roar_vio_simple_stream(&(hdl->svio), _i(rate), _i(channels), _i(bits), _i(codec), NULL, hdl->dir, "libroarsndio(win32)") == -1 ) { |
---|
[2051] | 108 | return 0; |
---|
| 109 | } |
---|
[2375] | 110 | |
---|
| 111 | // FIXME: this does not work in this case |
---|
| 112 | //send_vol_event(hdl); |
---|
[2051] | 113 | #endif |
---|
[2019] | 114 | |
---|
| 115 | hdl->stream_opened = 1; |
---|
[1567] | 116 | |
---|
| 117 | return 1; |
---|
[1563] | 118 | } |
---|
[1567] | 119 | #undef _i |
---|
[1563] | 120 | |
---|
| 121 | int sio_stop (struct sio_hdl * hdl) { |
---|
| 122 | |
---|
| 123 | if ( hdl == NULL ) |
---|
| 124 | return 0; |
---|
| 125 | |
---|
[2019] | 126 | if ( !hdl->stream_opened ) |
---|
[1563] | 127 | return 0; |
---|
| 128 | |
---|
[2019] | 129 | roar_vio_close(&(hdl->svio)); |
---|
[1563] | 130 | |
---|
[2019] | 131 | hdl->stream_opened = -1; |
---|
[1563] | 132 | |
---|
| 133 | return 1; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | size_t sio_read (struct sio_hdl * hdl, void * addr, size_t nbytes) { |
---|
| 137 | ssize_t ret; |
---|
| 138 | |
---|
| 139 | if ( hdl == NULL ) |
---|
| 140 | return 0; |
---|
| 141 | |
---|
[2019] | 142 | if ( !hdl->stream_opened ) |
---|
[1563] | 143 | return 0; |
---|
| 144 | |
---|
[2019] | 145 | if ( (ret = roar_vio_read(&(hdl->svio), addr, nbytes)) < 0 ) |
---|
[1563] | 146 | return 0; |
---|
| 147 | |
---|
| 148 | return ret; |
---|
| 149 | } |
---|
| 150 | size_t sio_write (struct sio_hdl * hdl, void * addr, size_t nbytes) { |
---|
| 151 | ssize_t ret; |
---|
| 152 | |
---|
| 153 | if ( hdl == NULL ) |
---|
| 154 | return 0; |
---|
| 155 | |
---|
[2019] | 156 | if ( !hdl->stream_opened ) |
---|
[1563] | 157 | return 0; |
---|
| 158 | |
---|
[2019] | 159 | if ( (ret = roar_vio_write(&(hdl->svio), addr, nbytes)) < 0 ) |
---|
[1563] | 160 | return 0; |
---|
| 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 |
---|