source: roaraudio/roard/codecfilter_fishsound.c @ 5331:dc6687b73157

Last change on this file since 5331:dc6687b73157 was 5331:dc6687b73157, checked in by phi, 12 years ago

coding style

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