source: roaraudio/roard/codecfilter_fishsound.c @ 3517:1a3218a3fc5b

Last change on this file since 3517:1a3218a3fc5b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

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