[559] | 1 | //codecfilter_wave.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of roard a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[668] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[559] | 26 | #include "roard.h" |
---|
| 27 | |
---|
[2505] | 28 | #ifndef ROAR_WITHOUT_CF_WAVE |
---|
| 29 | |
---|
[559] | 30 | int cf_wave_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 31 | struct roar_stream_server * info, |
---|
| 32 | struct roar_codecfilter * filter) { |
---|
| 33 | struct codecfilter_wave_inst * self = malloc(sizeof(struct codecfilter_wave_inst)); |
---|
[609] | 34 | struct roar_stream * s = ROAR_STREAM(info); |
---|
[559] | 35 | |
---|
| 36 | if ( !self ) |
---|
| 37 | return -1; |
---|
| 38 | |
---|
| 39 | self->stream = info; |
---|
[2595] | 40 | self->vstream = NULL; |
---|
[559] | 41 | self->opened = 0; |
---|
| 42 | |
---|
| 43 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
| 44 | |
---|
| 45 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 46 | return 0; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | int cf_wave_close(CODECFILTER_USERDATA_T inst) { |
---|
[562] | 50 | // struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
[559] | 51 | |
---|
| 52 | if ( !inst ) |
---|
| 53 | return -1; |
---|
| 54 | |
---|
| 55 | free(inst); |
---|
| 56 | return 0; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | int cf_wave_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 60 | struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
| 61 | int r = -1; |
---|
[562] | 62 | char tbuf[44]; |
---|
[2599] | 63 | struct roar_stream * ps = ROAR_STREAM(self->stream); |
---|
| 64 | struct roar_stream * s; |
---|
| 65 | struct roar_audio_info info; |
---|
[2590] | 66 | uint16_t tmp16; |
---|
| 67 | uint32_t tmp32; |
---|
| 68 | int codec = -1; |
---|
[2599] | 69 | int vid, fh; |
---|
[559] | 70 | |
---|
| 71 | if ( self->opened ) { |
---|
[606] | 72 | return stream_vio_s_read(self->stream, buf, len); |
---|
[559] | 73 | } else { |
---|
[606] | 74 | if (stream_vio_s_read(self->stream, tbuf, 44) != 44) { |
---|
[559] | 75 | return -1; |
---|
| 76 | } |
---|
[565] | 77 | |
---|
| 78 | // TODO: write better code here! |
---|
| 79 | |
---|
[2599] | 80 | if ( (fh = streams_get_fh(ps->id)) == -1 ) { |
---|
| 81 | return -1; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if ( (vid = streams_new_virtual(ps->id, &(self->vstream))) == -1 ) { |
---|
| 85 | return -1; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | ROAR_DBG("cf_wave_read(*): self->vstream=%p", self->vstream); |
---|
| 89 | |
---|
| 90 | s = ROAR_STREAM(self->vstream); |
---|
| 91 | |
---|
[2590] | 92 | memcpy(&tmp32, tbuf+24, 4); |
---|
| 93 | s->info.rate = ROAR_LE2HOST32(tmp32); |
---|
| 94 | |
---|
| 95 | memcpy(&tmp16, tbuf+22, 2); |
---|
| 96 | s->info.channels = ROAR_LE2HOST16(tmp16); |
---|
| 97 | |
---|
| 98 | memcpy(&tmp16, tbuf+34, 2); |
---|
| 99 | s->info.bits = ROAR_LE2HOST16(tmp16); |
---|
| 100 | |
---|
| 101 | memcpy(&tmp16, tbuf+20, 2); |
---|
| 102 | |
---|
| 103 | switch (ROAR_LE2HOST16(tmp16)) { |
---|
| 104 | case ROAR_RIFF_WAVE_CID_PCM: |
---|
| 105 | if ( s->info.bits == 8 ) { |
---|
| 106 | codec = ROAR_CODEC_PCM_U_LE; |
---|
| 107 | } else { |
---|
| 108 | codec = ROAR_CODEC_PCM_S_LE; |
---|
| 109 | } |
---|
| 110 | break; |
---|
| 111 | case ROAR_RIFF_WAVE_CID_ALAW: |
---|
| 112 | codec = ROAR_CODEC_ALAW; |
---|
| 113 | break; |
---|
| 114 | case ROAR_RIFF_WAVE_CID_MULAW: |
---|
| 115 | codec = ROAR_CODEC_MULAW; |
---|
| 116 | break; |
---|
| 117 | case ROAR_RIFF_WAVE_CID_IEEE_FLOAT: |
---|
| 118 | default: |
---|
| 119 | return -1; |
---|
| 120 | } |
---|
| 121 | |
---|
[2599] | 122 | s->info.codec = codec; |
---|
| 123 | self->vstream->codec_orgi = codec; |
---|
| 124 | |
---|
| 125 | memcpy(&info, &(s->info), sizeof(struct roar_audio_info)); |
---|
| 126 | |
---|
| 127 | if ( streams_set_fh(vid, fh) == -1 ) { |
---|
| 128 | return -1; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /* |
---|
| 132 | if ( roar_vio_open_pass(&(self->vstream->vio), &(self->stream->vio)) == -1 ) { |
---|
| 133 | return -1; |
---|
| 134 | } |
---|
| 135 | */ |
---|
| 136 | |
---|
| 137 | memcpy(&(self->vstream->vio), &(self->stream->vio), sizeof(struct roar_vio_calls)); |
---|
| 138 | |
---|
[2607] | 139 | if ( streams_set_null_io(ps->id) == -1 ) { |
---|
[2599] | 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | memcpy(&(ps->info), &info, sizeof(struct roar_audio_info)); |
---|
[559] | 144 | |
---|
[564] | 145 | self->opened = 1; |
---|
| 146 | |
---|
[559] | 147 | errno = EAGAIN; |
---|
| 148 | return -1; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return r; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | int cf_wave_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 155 | struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
[1012] | 156 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
[2816] | 157 | void * header; |
---|
[1012] | 158 | int32_t tmp32; |
---|
| 159 | int16_t tmp16; |
---|
| 160 | int16_t bits; |
---|
| 161 | int16_t codec; |
---|
[2816] | 162 | int sid; |
---|
[559] | 163 | |
---|
[1013] | 164 | ROAR_DBG("cf_wave_write(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
| 165 | ROAR_DBG("cf_wave_write(inst=%p, buf=%p, len=%i): self->opened=%i", inst, buf, len, self->opened); |
---|
[1012] | 166 | |
---|
| 167 | if ( self->opened ) { |
---|
| 168 | return stream_vio_s_write(self->stream, buf, len); |
---|
| 169 | } else { |
---|
| 170 | |
---|
| 171 | if ( s->fh == -1 ) { |
---|
| 172 | errno = EAGAIN; |
---|
| 173 | return -1; |
---|
| 174 | } |
---|
| 175 | |
---|
[2816] | 176 | sid = ROAR_STREAM(self->stream)->id; |
---|
| 177 | |
---|
| 178 | if ( stream_prethru_destroy(sid) == -1 ) { |
---|
| 179 | return -1; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if ( stream_prethru_add_data(sid, &header, 44) == -1 ) { |
---|
| 183 | return -1; |
---|
| 184 | } |
---|
| 185 | |
---|
[1012] | 186 | memcpy(header , "RIFF\367\377\377\177WAVEfmt \020", 17); |
---|
| 187 | memcpy(header+36, "data\313\377\377\177", 8); |
---|
| 188 | |
---|
| 189 | switch (s->info.codec) { |
---|
| 190 | case ROAR_CODEC_PCM_S_LE: |
---|
| 191 | codec = 0x0001; |
---|
| 192 | break; |
---|
| 193 | default: |
---|
| 194 | ROAR_ERR("cf_wave_write(*) Codec not supported!"); |
---|
| 195 | return -1; |
---|
| 196 | break; |
---|
| 197 | } |
---|
| 198 | |
---|
[1013] | 199 | ROAR_DBG("cf_wave_write(*) Codec supported!"); |
---|
[1012] | 200 | |
---|
| 201 | bits = s->info.bits; |
---|
| 202 | memcpy(header+24, &(s->info.rate ), 4); |
---|
| 203 | memcpy(header+22, &(s->info.channels), 2); |
---|
| 204 | memcpy(header+34, &bits, 2); |
---|
| 205 | |
---|
| 206 | tmp16 = s->info.channels * bits / 8; |
---|
| 207 | memcpy(header+32, &tmp16, 2); |
---|
| 208 | tmp32 = tmp16 * s->info.rate; |
---|
| 209 | memcpy(header+28, &tmp32, 4); |
---|
| 210 | memcpy(header+20, &codec, 2); |
---|
| 211 | |
---|
| 212 | if ( stream_vio_s_write(self->stream, header, 44) != 44 ) |
---|
| 213 | return -1; |
---|
| 214 | |
---|
| 215 | self->opened = 1; |
---|
| 216 | |
---|
| 217 | errno = EAGAIN; |
---|
| 218 | // return -1; |
---|
| 219 | |
---|
| 220 | len = stream_vio_s_write(self->stream, buf, len); |
---|
| 221 | |
---|
| 222 | cf_wave_close(inst); |
---|
| 223 | ROAR_STREAM_SERVER(s)->codecfilter = -1; |
---|
| 224 | |
---|
| 225 | return len; |
---|
| 226 | |
---|
| 227 | // return stream_vio_s_write(self->stream, buf, len); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | return -1; |
---|
[559] | 231 | } |
---|
| 232 | |
---|
[2604] | 233 | int cf_wave_ctl(CODECFILTER_USERDATA_T inst, int cmd, void * data) { |
---|
[2607] | 234 | struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
| 235 | int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK; |
---|
| 236 | |
---|
| 237 | cmd -= type; |
---|
| 238 | |
---|
| 239 | ROAR_DBG("cf_wave_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 240 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
| 241 | |
---|
| 242 | switch (cmd) { |
---|
| 243 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE): |
---|
| 244 | streams_delete(ROAR_STREAM(self->stream)->id); |
---|
| 245 | return 0; |
---|
| 246 | break; |
---|
| 247 | default: |
---|
| 248 | ROAR_DBG("cf_wave_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 249 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
| 250 | return -1; |
---|
| 251 | } |
---|
| 252 | |
---|
[2604] | 253 | return -1; |
---|
| 254 | } |
---|
| 255 | |
---|
[2505] | 256 | #endif |
---|
| 257 | |
---|
[559] | 258 | //ll |
---|