source: roaraudio/roard/codecfilter_fishsound.c @ 993:ba20ae3efe93

Last change on this file since 993:ba20ae3efe93 was 993:ba20ae3efe93, checked in by phi, 15 years ago

use roar_buffer objects to buffer decoded audio data, added support to decode as 8, 16 and 32 bit audio

File size: 4.6 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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27#ifdef ROAR_HAVE_LIBFISHSOUND
28
29int cf_fishsound_decoded_float (FishSound * fsound, float ** pcm, long frames, void * user_data) {
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
42 if (!self->opened) {
43   fish_sound_command(fsound, FISH_SOUND_GET_INFO, &(self->fsinfo),
44                       sizeof(FishSoundInfo));
45 }
46
47 if ( roar_buffer_new(&buf, frames*stream->info.bits*stream->info.channels) == -1 )
48  return -1;
49
50 if ( roar_buffer_get_data(buf, &data.v) == -1 )
51  return -1;
52
53 frames *= self->fsinfo.channels;
54
55 switch (stream->info.bits) {
56  case  8:
57    for (i = 0; i < frames; i++) {
58     s  = ((float*)pcm)[i];
59     s *= 127;
60     data.c[i] = s;
61    }
62   break;
63  case 16:
64    for (i = 0; i < frames; i++) {
65     s  = ((float*)pcm)[i];
66     s *= 32767;
67     data.i16[i] = s;
68    }
69   break;
70  case 32:
71    for (i = 0; i < frames; i++) {
72     s  = ((float*)pcm)[i];
73     s *= 2147483647;
74     data.i32[i] = s;
75    }
76   break;
77  default:
78    return -1;
79 }
80
81 if ( self->buffer == NULL ) {
82  self->buffer = buf;
83 } else {
84  roar_buffer_add(self->buffer, buf);
85 }
86
87 return -1;
88}
89
90int cf_fishsound_read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data) {
91 FishSound * fsound = (FishSound *)user_data;
92
93 fish_sound_prepare_truncation(fsound, op->granulepos, op->e_o_s);
94 fish_sound_decode(fsound, op->packet, op->bytes);
95
96 return 0;
97}
98
99int cf_fishsound_open(CODECFILTER_USERDATA_T * inst, int codec,
100                                            struct roar_stream_server * info,
101                                            struct roar_codecfilter   * filter) {
102 struct codecfilter_fishsound_inst * self = malloc(sizeof(struct codecfilter_fishsound_inst));
103 struct roar_stream * s = ROAR_STREAM(info);
104
105 if ( !self )
106  return -1;
107
108 if ( s->dir != ROAR_DIR_PLAY ) {
109  free(self);
110  return -1;
111 }
112
113 self->stream               = info;
114 self->opened               = 0;
115 self->buffer               = NULL;
116 self->fsound               = fish_sound_new(FISH_SOUND_DECODE, &(self->fsinfo));
117
118 fish_sound_set_interleave(self->fsound, 1);
119 fish_sound_set_decoded_float_ilv(self->fsound, cf_fishsound_decoded_float, (void*)self);
120
121 self->oggz = oggz_new(OGGZ_READ);
122
123 oggz_set_read_callback(self->oggz, -1, cf_fishsound_read_packet, self->fsound);
124
125 *inst = (CODECFILTER_USERDATA_T) self;
126
127 return 0;
128}
129
130int cf_fishsound_close(CODECFILTER_USERDATA_T   inst) {
131 struct codecfilter_fishsound_inst * self = (struct codecfilter_fishsound_inst *) inst;
132
133 if ( inst == NULL )
134  return -1;
135
136 oggz_close(self->oggz);
137 fish_sound_delete(self->fsound);
138
139 if ( self->buffer != NULL )
140  roar_buffer_free(self->buffer);
141
142 free(inst);
143 return 0;
144}
145
146int cf_fishsound_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
147 struct codecfilter_fishsound_inst * self = (struct codecfilter_fishsound_inst *) inst;
148 struct roar_stream * s = ROAR_STREAM(self->stream);
149 long inlen;
150
151/*
152 if ( self->opened ) {
153//  return stream_vio_s_read(self->stream, buf, len);
154 } else {
155/-*
156  if (stream_vio_s_read(self->stream, tbuf, 44) != 44) {
157   return -1;
158  }
159*ä/
160
161  self->opened = 1;
162
163  errno = EAGAIN;
164  return -1;
165 }
166*/
167
168 if ( (inlen = stream_vio_s_read(self->stream, buf, len)) == -1 )
169  return -1;
170
171 oggz_read_input(self->oggz, (unsigned char *)buf, inlen);
172
173 if ( !self->opened ) {
174  s->info.channels = self->fsinfo.channels;
175  s->info.rate     = self->fsinfo.samplerate;
176  s->info.bits     = g_sa->bits;
177  s->info.codec    = ROAR_CODEC_NATIVE;
178  self->opened     = 1;
179  errno            = EAGAIN;
180  return -1;
181 }
182
183 return -1;
184}
185
186#endif
187
188//ll
Note: See TracBrowser for help on using the repository browser.