[992] | 1 | //codecfilter_wave.c: |
---|
| 2 | |
---|
| 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 |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include "roard.h" |
---|
| 26 | |
---|
| 27 | #ifdef ROAR_HAVE_LIBFISHSOUND |
---|
| 28 | |
---|
| 29 | int cf_fishsound_decoded_float (FishSound * fsound, float ** pcm, long frames, void * user_data) { |
---|
[993] | 30 | struct codecfilter_fishsound_inst * self = (struct codecfilter_fishsound_inst *) user_data; |
---|
| 31 | struct roar_stream * stream = ROAR_STREAM(self->stream); |
---|
| 32 | struct roar_buffer * buf; |
---|
| 33 | int i; |
---|
| 34 | double s; |
---|
| 35 | union { |
---|
| 36 | void * v; |
---|
| 37 | char * c; |
---|
| 38 | int16_t * i16; |
---|
| 39 | int32_t * i32; |
---|
| 40 | } data; |
---|
| 41 | |
---|
[997] | 42 | ROAR_DBG("cf_fishsound_decoded_float(fsound=%p, pcm=%p, frames=%li, user_data=%p) = ?", fsound, pcm, frames, user_data); |
---|
[994] | 43 | |
---|
[997] | 44 | ROAR_DBG("cf_fishsound_decoded_float(*): self->opened=%i", self->opened); |
---|
[994] | 45 | |
---|
[993] | 46 | if (!self->opened) { |
---|
| 47 | fish_sound_command(fsound, FISH_SOUND_GET_INFO, &(self->fsinfo), |
---|
| 48 | sizeof(FishSoundInfo)); |
---|
| 49 | } |
---|
| 50 | |
---|
[994] | 51 | if ( roar_buffer_new(&buf, frames*stream->info.bits*stream->info.channels/8) == -1 ) |
---|
[993] | 52 | return -1; |
---|
| 53 | |
---|
| 54 | if ( roar_buffer_get_data(buf, &data.v) == -1 ) |
---|
| 55 | return -1; |
---|
| 56 | |
---|
| 57 | frames *= self->fsinfo.channels; |
---|
| 58 | |
---|
| 59 | switch (stream->info.bits) { |
---|
| 60 | case 8: |
---|
| 61 | for (i = 0; i < frames; i++) { |
---|
| 62 | s = ((float*)pcm)[i]; |
---|
| 63 | s *= 127; |
---|
| 64 | data.c[i] = s; |
---|
| 65 | } |
---|
| 66 | break; |
---|
| 67 | case 16: |
---|
| 68 | for (i = 0; i < frames; i++) { |
---|
| 69 | s = ((float*)pcm)[i]; |
---|
| 70 | s *= 32767; |
---|
| 71 | data.i16[i] = s; |
---|
| 72 | } |
---|
| 73 | break; |
---|
| 74 | case 32: |
---|
| 75 | for (i = 0; i < frames; i++) { |
---|
| 76 | s = ((float*)pcm)[i]; |
---|
| 77 | s *= 2147483647; |
---|
| 78 | data.i32[i] = s; |
---|
| 79 | } |
---|
| 80 | break; |
---|
| 81 | default: |
---|
| 82 | return -1; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if ( self->buffer == NULL ) { |
---|
| 86 | self->buffer = buf; |
---|
| 87 | } else { |
---|
| 88 | roar_buffer_add(self->buffer, buf); |
---|
| 89 | } |
---|
| 90 | |
---|
[992] | 91 | return -1; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | int cf_fishsound_read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data) { |
---|
| 95 | FishSound * fsound = (FishSound *)user_data; |
---|
| 96 | |
---|
[993] | 97 | fish_sound_prepare_truncation(fsound, op->granulepos, op->e_o_s); |
---|
| 98 | fish_sound_decode(fsound, op->packet, op->bytes); |
---|
[992] | 99 | |
---|
| 100 | return 0; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | int cf_fishsound_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 104 | struct roar_stream_server * info, |
---|
| 105 | struct roar_codecfilter * filter) { |
---|
| 106 | struct codecfilter_fishsound_inst * self = malloc(sizeof(struct codecfilter_fishsound_inst)); |
---|
| 107 | struct roar_stream * s = ROAR_STREAM(info); |
---|
| 108 | |
---|
| 109 | if ( !self ) |
---|
| 110 | return -1; |
---|
| 111 | |
---|
| 112 | if ( s->dir != ROAR_DIR_PLAY ) { |
---|
| 113 | free(self); |
---|
| 114 | return -1; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | self->stream = info; |
---|
| 118 | self->opened = 0; |
---|
[993] | 119 | self->buffer = NULL; |
---|
[992] | 120 | self->fsound = fish_sound_new(FISH_SOUND_DECODE, &(self->fsinfo)); |
---|
| 121 | |
---|
| 122 | fish_sound_set_interleave(self->fsound, 1); |
---|
| 123 | fish_sound_set_decoded_float_ilv(self->fsound, cf_fishsound_decoded_float, (void*)self); |
---|
| 124 | |
---|
| 125 | self->oggz = oggz_new(OGGZ_READ); |
---|
| 126 | |
---|
| 127 | oggz_set_read_callback(self->oggz, -1, cf_fishsound_read_packet, self->fsound); |
---|
| 128 | |
---|
| 129 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
| 130 | |
---|
| 131 | return 0; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | int cf_fishsound_close(CODECFILTER_USERDATA_T inst) { |
---|
| 135 | struct codecfilter_fishsound_inst * self = (struct codecfilter_fishsound_inst *) inst; |
---|
| 136 | |
---|
| 137 | if ( inst == NULL ) |
---|
| 138 | return -1; |
---|
| 139 | |
---|
| 140 | oggz_close(self->oggz); |
---|
| 141 | fish_sound_delete(self->fsound); |
---|
| 142 | |
---|
[993] | 143 | if ( self->buffer != NULL ) |
---|
| 144 | roar_buffer_free(self->buffer); |
---|
| 145 | |
---|
[992] | 146 | free(inst); |
---|
| 147 | return 0; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | int cf_fishsound_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 151 | struct codecfilter_fishsound_inst * self = (struct codecfilter_fishsound_inst *) inst; |
---|
| 152 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
| 153 | long inlen; |
---|
[994] | 154 | int need_data = 0; |
---|
| 155 | struct roar_buffer_stats stats; |
---|
| 156 | size_t stlen; |
---|
| 157 | |
---|
[997] | 158 | ROAR_DBG("cf_fishsound_read(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
[992] | 159 | |
---|
| 160 | /* |
---|
| 161 | if ( self->opened ) { |
---|
| 162 | // return stream_vio_s_read(self->stream, buf, len); |
---|
| 163 | } else { |
---|
| 164 | /-* |
---|
| 165 | if (stream_vio_s_read(self->stream, tbuf, 44) != 44) { |
---|
| 166 | return -1; |
---|
| 167 | } |
---|
| 168 | *ä/ |
---|
| 169 | |
---|
| 170 | self->opened = 1; |
---|
| 171 | |
---|
| 172 | errno = EAGAIN; |
---|
| 173 | return -1; |
---|
| 174 | } |
---|
| 175 | */ |
---|
| 176 | |
---|
[994] | 177 | if ( self->buffer == NULL ) { |
---|
| 178 | need_data = 1; |
---|
| 179 | } else { |
---|
| 180 | if ( roar_buffer_ring_stats(self->buffer, &stats) == -1 ) |
---|
| 181 | return -1; |
---|
| 182 | |
---|
| 183 | if ( stats.bytes < len ) |
---|
| 184 | need_data = 1; |
---|
| 185 | } |
---|
| 186 | |
---|
[997] | 187 | ROAR_DBG("cf_fishsound_read(*): need_data=%i, self->opened=%i", need_data, self->opened); |
---|
[992] | 188 | |
---|
[997] | 189 | // while (need_data) { |
---|
| 190 | if ( (inlen = stream_vio_s_read(self->stream, buf, len)) == -1 ) { |
---|
| 191 | // if ( errno != EAGAIN ) { |
---|
| 192 | return -1; |
---|
| 193 | /* |
---|
| 194 | } else { |
---|
| 195 | return -1; |
---|
| 196 | } |
---|
| 197 | */ |
---|
| 198 | } else { |
---|
| 199 | if ( inlen == 0 ) |
---|
| 200 | return 0; |
---|
[994] | 201 | |
---|
[997] | 202 | oggz_read_input(self->oggz, (unsigned char *)buf, inlen); |
---|
[994] | 203 | |
---|
[997] | 204 | if( self->buffer != NULL ) { |
---|
| 205 | if ( roar_buffer_ring_stats(self->buffer, &stats) == -1 ) |
---|
| 206 | return -1; |
---|
[994] | 207 | |
---|
[997] | 208 | if ( stats.bytes < len ) { |
---|
| 209 | need_data = 1; |
---|
| 210 | } else { |
---|
| 211 | need_data = 0; |
---|
| 212 | } |
---|
| 213 | } |
---|
[994] | 214 | } |
---|
[997] | 215 | // } |
---|
| 216 | |
---|
| 217 | ROAR_DBG("cf_fishsound_read(*): need_data=%i, self->opened=%i", need_data, self->opened); |
---|
[994] | 218 | |
---|
[997] | 219 | if ( need_data ) { |
---|
| 220 | errno = EAGAIN; |
---|
| 221 | return -1; |
---|
| 222 | } |
---|
[992] | 223 | |
---|
| 224 | if ( !self->opened ) { |
---|
| 225 | s->info.channels = self->fsinfo.channels; |
---|
| 226 | s->info.rate = self->fsinfo.samplerate; |
---|
| 227 | s->info.bits = g_sa->bits; |
---|
| 228 | s->info.codec = ROAR_CODEC_NATIVE; |
---|
[993] | 229 | self->opened = 1; |
---|
| 230 | errno = EAGAIN; |
---|
[997] | 231 | ROAR_DBG("cf_fishsound_read(inst=%p, buf=%p, len=%i) = -1 // errno=EAGAIN", inst, buf, len); |
---|
[993] | 232 | return -1; |
---|
[992] | 233 | } |
---|
| 234 | |
---|
[994] | 235 | // ok, now we should have all the data we want... |
---|
| 236 | |
---|
| 237 | stlen = len; |
---|
| 238 | if ( roar_buffer_shift_out(&(self->buffer), buf, &stlen) == -1 ) { |
---|
[997] | 239 | ROAR_DBG("cf_fishsound_read(inst=%p, buf=%p, len=%i) = -1 // roar_buffer_shift_out() failed", inst, buf, len); |
---|
[994] | 240 | return -1; |
---|
| 241 | } |
---|
| 242 | |
---|
[997] | 243 | ROAR_DBG("cf_fishsound_read(inst=%p, buf=%p, len=%i) = %i", inst, buf, len, (int)stlen); |
---|
[994] | 244 | return stlen; |
---|
[992] | 245 | } |
---|
| 246 | |
---|
| 247 | #endif |
---|
| 248 | |
---|
| 249 | //ll |
---|