source: roaraudio/roard/codecfilter_vorbis.c @ 362:b13b2abea6f0

Last change on this file since 362:b13b2abea6f0 was 362:b13b2abea6f0, checked in by phi, 16 years ago

only compile vorbis stuff if we have libvorbisfile

File size: 3.7 KB
Line 
1//codecfilter_vorbis.c:
2
3#include "roard.h"
4
5#ifdef ROAR_HAVE_LIBVORBISFILE
6
7int cf_vorbis_open(CODECFILTER_USERDATA_T * inst, int codec,
8                                            struct roar_stream_server * info,
9                                            struct roar_codecfilter   * filter) {
10 struct codecfilter_vorbis_inst * self = malloc(sizeof(struct codecfilter_vorbis_inst));
11
12 if ( !self )
13  return -1;
14
15 self->current_section      = -1;
16 self->last_section         = -1;
17 self->opened               = 0;
18 self->stream               = info;
19// self->outlen               = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
20
21 if ( (self->in = fdopen(((struct roar_stream*)info)->fh, "r")) == NULL ) {
22  free((void*)self);
23  return -1;
24 }
25
26 *inst = (CODECFILTER_USERDATA_T) self;
27
28 ((struct roar_stream*)info)->info.codec = ROAR_CODEC_DEFAULT;
29
30 return 0;
31}
32
33int cf_vorbis_close(CODECFILTER_USERDATA_T   inst) {
34 struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst;
35
36 if ( !inst )
37  return -1;
38
39 ov_clear(&(self->vf));
40
41 free(inst);
42 return 0;
43}
44
45int cf_vorbis_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
46 struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst;
47 long r;
48 long todo = len;
49 long done = 0;
50
51// printf("cf_vorbis_read(inst=%p, buf=%p, len=%i) = ?\n", inst, buf, len);
52
53 self->opened++;
54 if ( self->opened == 16 ) {
55  //printf("cf_vorbis_read(*): opening...\n");
56  if ( ov_open(self->in, &(self->vf), NULL, 0) < 0 ) {
57   free((void*)self);
58   return 0;
59  }
60 }
61
62 if ( self->opened < 16 ) {
63  return -1;
64 }
65
66 while (todo) {
67  r = ov_read(&(self->vf), buf+done, todo, 0, 2, 1, &(self->current_section));
68  if ( r < 1 ) {
69   break;
70  } else {
71   if ( self->last_section != self->current_section )
72    if ( cf_vorbis_update_stream(self) == -1 )
73     return 0;
74
75   self->last_section = self->current_section;
76   todo -= r;
77   done += r;
78  }
79 }
80
81 //printf("ov_read(*) = %i\n", done);
82
83 if ( done == 0 ) {
84  // do some EOF handling...
85  return 0;
86 } else {
87  return len;
88 }
89}
90
91int cf_vorbis_update_stream (struct codecfilter_vorbis_inst * self) {
92 vorbis_info *vi = ov_info(&(self->vf), -1);
93 char **ptr = ov_comment(&(self->vf), -1)->user_comments;
94 char key[80] = {0}, value[80] = {0};
95 struct roar_stream * s = (struct roar_stream *) self->stream;
96 int type;
97 int j, h = 0;
98 float rpg_track = 0, rpg_album = 0;
99
100 s->info.channels = vi->channels;
101 s->info.rate     = vi->rate;
102 s->info.bits     = 16;
103 s->info.codec    = ROAR_CODEC_DEFAULT;
104
105 stream_meta_clear(s->id);
106
107 while(*ptr){
108   for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
109    key[j] = (*ptr)[j];
110    key[j] = 0;
111
112   for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
113    value[h++] = (*ptr)[j];
114    value[h]   = 0;
115
116   type = roar_meta_inttype(key);
117   if ( type != -1 )
118    stream_meta_set(s->id, type, "", value);
119
120   ROAR_DBG("cf_vorbis_update_stream(*): Meta %-16s: %s", key, value);
121
122   if ( strcmp(key, "REPLAYGAIN_TRACK_PEAK") == 0 ) {
123    rpg_track = 1/atof(value);
124/*
125   } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) {
126    rpg_track = powf(10, atof(value)/20);
127*/
128   } else if ( strcmp(key, "REPLAYGAIN_ALBUM_PEAK") == 0 ) {
129    rpg_album = 1/atof(value);
130/*
131   } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) {
132    rpg_album = powf(10, atof(value)/20);
133*/
134   }
135
136   ++ptr;
137 }
138
139 if ( rpg_album ) {
140  self->stream->mixer.rpg_div = 2718;  // = int(exp(1)*1000)
141  self->stream->mixer.rpg_mul = (float)rpg_album*2718;
142 } else if ( rpg_track ) {
143  self->stream->mixer.rpg_div = 2718;
144  self->stream->mixer.rpg_mul = (float)rpg_track*2718;
145 }
146
147 //printf("RPG: mul=%i, div=%i\n", self->stream->mixer.rpg_mul, self->stream->mixer.rpg_div);
148 return 0;
149}
150
151#endif
152
153//ll
Note: See TracBrowser for help on using the repository browser.