source: roaraudio/roard/codecfilter_fishsound.c @ 3764:2a2b1eb426c4

Last change on this file since 3764:2a2b1eb426c4 was 3764:2a2b1eb426c4, checked in by phi, 14 years ago

added new buffer function roar_buffer_new_data() to make common alloc case more easy

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_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  roar_buffer_add(self->buffer, buf);
87 }
88
89 return -1;
90}
91
92int cf_fishsound_read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data) {
93 FishSound * fsound = (FishSound *)user_data;
94
95 fish_sound_prepare_truncation(fsound, op->granulepos, op->e_o_s);
96 fish_sound_decode(fsound, op->packet, op->bytes);
97
98 return 0;
99}
100
101int cf_fishsound_open(CODECFILTER_USERDATA_T * inst, int codec,
102                                            struct roar_stream_server * info,
103                                            struct roar_codecfilter   * filter) {
104 struct codecfilter_fishsound_inst * self = malloc(sizeof(struct codecfilter_fishsound_inst));
105 struct roar_stream * s = ROAR_STREAM(info);
106
107 if ( !self )
108  return -1;
109
110 if ( s->dir != ROAR_DIR_PLAY ) {
111  free(self);
112  return -1;
113 }
114
115 self->stream               = info;
116 self->opened               = 0;
117 self->buffer               = NULL;
118 self->fsound               = fish_sound_new(FISH_SOUND_DECODE, &(self->fsinfo));
119
120 fish_sound_set_interleave(self->fsound, 1);
121 fish_sound_set_decoded_float_ilv(self->fsound, cf_fishsound_decoded_float, (void*)self);
122
123 self->oggz = oggz_new(OGGZ_READ);
124
125 oggz_set_read_callback(self->oggz, -1, cf_fishsound_read_packet, self->fsound);
126
127 *inst = (CODECFILTER_USERDATA_T) self;
128
129 return 0;
130}
131
132int cf_fishsound_close(CODECFILTER_USERDATA_T   inst) {
133 struct codecfilter_fishsound_inst * self = (struct codecfilter_fishsound_inst *) inst;
134
135 if ( inst == NULL )
136  return -1;
137
138 oggz_close(self->oggz);
139 fish_sound_delete(self->fsound);
140
141 if ( self->buffer != NULL )
142  roar_buffer_free(self->buffer);
143
144 free(inst);
145 return 0;
146}
147
148int cf_fishsound_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
149 struct codecfilter_fishsound_inst * self = (struct codecfilter_fishsound_inst *) inst;
150 struct roar_stream * s = ROAR_STREAM(self->stream);
151 long inlen;
152 int need_data = 0;
153 struct roar_buffer_stats stats;
154 size_t stlen;
155
156 ROAR_DBG("cf_fishsound_read(inst=%p, buf=%p, len=%i) = ?", inst, buf, len);
157
158/*
159 if ( self->opened ) {
160//  return stream_vio_s_read(self->stream, buf, len);
161 } else {
162/-*
163  if (stream_vio_s_read(self->stream, tbuf, 44) != 44) {
164   return -1;
165  }
166*ä/
167
168  self->opened = 1;
169
170  errno = EAGAIN;
171  return -1;
172 }
173*/
174
175 if ( self->buffer == NULL ) {
176  need_data = 1;
177 } else {
178  if ( roar_buffer_ring_stats(self->buffer, &stats) == -1 )
179   return -1;
180
181  if ( stats.bytes < len )
182   need_data = 1;
183 }
184
185 ROAR_DBG("cf_fishsound_read(*): need_data=%i, self->opened=%i", need_data, self->opened);
186
187// while (need_data) {
188  if ( (inlen = stream_vio_s_read(self->stream, buf, len)) == -1 ) {
189//   if ( errno != EAGAIN ) {
190    return -1;
191/*
192   } else {
193    return -1;
194   }
195*/
196  } else {
197   if ( inlen == 0 )
198    return 0;
199
200   oggz_read_input(self->oggz, (unsigned char *)buf, inlen);
201
202   if( self->buffer != NULL ) {
203    if ( roar_buffer_ring_stats(self->buffer, &stats) == -1 )
204     return -1;
205
206    if ( stats.bytes < len ) {
207     need_data = 1;
208    } else {
209     need_data = 0;
210    }
211   }
212  }
213// }
214
215 ROAR_DBG("cf_fishsound_read(*): need_data=%i, self->opened=%i", need_data, self->opened);
216
217 if ( need_data ) {
218  errno = EAGAIN;
219  return -1;
220 }
221
222 if ( !self->opened ) {
223  s->info.channels = self->fsinfo.channels;
224  s->info.rate     = self->fsinfo.samplerate;
225  s->info.bits     = g_sa->bits;
226  s->info.codec    = ROAR_CODEC_NATIVE;
227  self->opened     = 1;
228  errno            = EAGAIN;
229  ROAR_DBG("cf_fishsound_read(inst=%p, buf=%p, len=%i) = -1 // errno=EAGAIN", inst, buf, len);
230  return -1;
231 }
232
233 // ok, now we should have all the data we want...
234
235 stlen = len;
236 if ( roar_buffer_shift_out(&(self->buffer), buf, &stlen) == -1 ) {
237  ROAR_DBG("cf_fishsound_read(inst=%p, buf=%p, len=%i) = -1 // roar_buffer_shift_out() failed", inst, buf, len);
238  return -1;
239 }
240
241 ROAR_DBG("cf_fishsound_read(inst=%p, buf=%p, len=%i) = %i", inst, buf, len, (int)stlen);
242 return stlen;
243}
244
245#endif
246
247//ll
Note: See TracBrowser for help on using the repository browser.