source: roaraudio/roard/codecfilter_vorbis.c @ 568:697e904f07c7

Last change on this file since 568:697e904f07c7 was 541:4bb3a9f715a6, checked in by phi, 16 years ago

fixed a cf_vorbis about errno

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