1 #include "vorbis.h"
2 #include "../pcmconv.h"
3 
4 /********************************************************
5  Audio Tools, a module and set of tools for manipulating audio data
6  Copyright (C) 2007-2014  Brian Langenberger
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 *******************************************************/
22 
23 #ifndef MIN
24 #define MIN(x, y) ((x) < (y) ? (x) : (y))
25 #endif
26 #ifndef MAX
27 #define MAX(x, y) ((x) > (y) ? (x) : (y))
28 #endif
29 
30 #define BITS_PER_SAMPLE 16
31 
32 PyObject*
VorbisDecoder_new(PyTypeObject * type,PyObject * args,PyObject * kwds)33 VorbisDecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
34     decoders_VorbisDecoder *self;
35 
36     self = (decoders_VorbisDecoder *)type->tp_alloc(type, 0);
37 
38     return (PyObject *)self;
39 }
40 
41 void
VorbisDecoder_dealloc(decoders_VorbisDecoder * self)42 VorbisDecoder_dealloc(decoders_VorbisDecoder *self) {
43     if (self->open_ok)
44         ov_clear(&(self->vorbisfile));
45 
46     self->channels->del(self->channels);
47     Py_XDECREF(self->audiotools_pcm);
48 
49     Py_TYPE(self)->tp_free((PyObject*)self);
50 }
51 
52 int
VorbisDecoder_init(decoders_VorbisDecoder * self,PyObject * args,PyObject * kwds)53 VorbisDecoder_init(decoders_VorbisDecoder *self, PyObject *args, PyObject *kwds) {
54     char* filename;
55     vorbis_info* info;
56 
57     self->open_ok = 0;
58     self->channel_count = 0;
59     self->rate = 0;
60     self->closed = 0;
61     self->channels = aa_int_new();
62     self->audiotools_pcm = NULL;
63 
64     if (!PyArg_ParseTuple(args, "s", &filename))
65         return -1;
66 
67     /*open file using reference Ogg Vorbis decoder*/
68     switch (ov_fopen(filename, &(self->vorbisfile))) {
69     case 0:
70     default:
71         self->open_ok = 1;
72         break;
73     case OV_EREAD:
74         PyErr_SetString(PyExc_ValueError, "I/O error");
75         return -1;
76     case OV_ENOTVORBIS:
77         PyErr_SetString(PyExc_ValueError, "not a Vorbis file");
78         return -1;
79     case OV_EVERSION:
80         PyErr_SetString(PyExc_ValueError, "Vorbis version mismatch");
81         return -1;
82     case OV_EBADHEADER:
83         PyErr_SetString(PyExc_ValueError, "invalid Vorbis bitstream header");
84         return -1;
85     case OV_EFAULT:
86         PyErr_SetString(PyExc_ValueError, "internal logic fault");
87         return -1;
88     }
89 
90     /*pull stream metadata from decoder*/
91     if ((info = ov_info(&(self->vorbisfile), -1)) != NULL) {
92         self->channel_count = info->channels;
93         self->rate = info->rate;
94     } else {
95         PyErr_SetString(PyExc_ValueError, "unable to get Vorbis info");
96         return -1;
97     }
98 
99     /*open FrameList creator*/
100     if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
101         return -1;
102 
103     return 0;
104 }
105 
106 static PyObject*
VorbisDecoder_sample_rate(decoders_VorbisDecoder * self,void * closure)107 VorbisDecoder_sample_rate(decoders_VorbisDecoder *self, void *closure) {
108     return Py_BuildValue("l", self->rate);
109 }
110 
111 static PyObject*
VorbisDecoder_bits_per_sample(decoders_VorbisDecoder * self,void * closure)112 VorbisDecoder_bits_per_sample(decoders_VorbisDecoder *self, void *closure) {
113     const int bits_per_sample = BITS_PER_SAMPLE;
114 
115     return Py_BuildValue("i", bits_per_sample);
116 }
117 
118 static PyObject*
VorbisDecoder_channels(decoders_VorbisDecoder * self,void * closure)119 VorbisDecoder_channels(decoders_VorbisDecoder *self, void *closure) {
120     return Py_BuildValue("i", self->channel_count);
121 }
122 
123 static PyObject*
VorbisDecoder_channel_mask(decoders_VorbisDecoder * self,void * closure)124 VorbisDecoder_channel_mask(decoders_VorbisDecoder *self, void *closure) {
125     int channel_mask;
126 
127     switch (self->channel_count) {
128     case 1:
129         /*fC*/
130         channel_mask = 0x4;
131         break;
132     case 2:
133         /*fL fR*/
134         channel_mask = 0x1 | 0x2;
135         break;
136     case 3:
137         /*fL fR fC*/
138         channel_mask = 0x1 | 0x2 | 0x4;
139         break;
140     case 4:
141         /*fL fR bL bR*/
142         channel_mask = 0x1 | 0x2 | 0x10 | 0x20;
143         break;
144     case 5:
145         /*fL fR fC bL bR*/
146         channel_mask = 0x1 | 0x2 | 0x4 | 0x10 | 0x20;
147         break;
148     case 6:
149         /*fL fR fC LFE bL bR*/
150         channel_mask = 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20;
151         break;
152     case 7:
153         /*fL fR fC LFE bC sL sR*/
154         channel_mask = 0x1 | 0x2 | 0x4 | 0x8 | 0x100 | 0x200 | 0x400;
155         break;
156     case 8:
157         /*fL fR fC LFE bL bR sL sR*/
158         channel_mask = 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x200 | 0x400;
159         break;
160     default:
161         /*undefined*/
162         channel_mask = 0x0;
163         break;
164     }
165 
166     return Py_BuildValue("i", channel_mask);
167 }
168 
169 static PyObject*
VorbisDecoder_read(decoders_VorbisDecoder * self,PyObject * args)170 VorbisDecoder_read(decoders_VorbisDecoder *self, PyObject *args) {
171     int current_bitstream;
172     long samples_read;
173     float **pcm_channels;
174     const int adjustment = 1 << (BITS_PER_SAMPLE - 1);
175     const int sample_min = -adjustment;
176     const int sample_max = adjustment - 1;
177 
178     if (self->closed) {
179         PyErr_SetString(PyExc_ValueError, "stream is closed");
180         return NULL;
181     }
182 
183     samples_read = ov_read_float(&(self->vorbisfile),
184                                  &pcm_channels,
185                                  4096,
186                                  &current_bitstream);
187 
188     if (samples_read >= 0) {
189         /*convert floating point samples to integer-based ones*/
190         aa_int* channels = self->channels;
191         int c;
192         void (*a_int_swap)(a_int* a, a_int* b) = channels->_[0]->swap;
193 
194         channels->reset(channels);
195         for (c = 0; c < self->channel_count; c++) {
196             a_int* channel = channels->append(channels);
197             long sample;
198 
199             channel->resize_for(channel, (unsigned)samples_read);
200 
201             for (sample = 0; sample < samples_read; sample++) {
202                 const int int_sample = (int)(pcm_channels[c][sample] *
203                                              adjustment);
204 
205                 a_append(channel,
206                          MAX(MIN(int_sample, sample_max), sample_min));
207             }
208         }
209 
210         /*reorder channels to .wav order if necessary*/
211         switch (self->channel_count) {
212         case 1:
213         case 2:
214         default:
215             /*no change*/
216             break;
217         case 3:
218             /*fL fC fR -> fL fR fC*/
219             a_int_swap(channels->_[1], channels->_[2]);
220             break;
221         case 4:
222             /*fL fR bL bR -> fL fR bL bR*/
223             /*no change*/
224             break;
225         case 5:
226             /*fL fC fR bL bR -> fL fR fC bL bR*/
227             a_int_swap(channels->_[1], channels->_[2]);
228             break;
229         case 6:
230             /*fL fC fR bL bR LFE -> fL fR fC bL bR LFE*/
231             a_int_swap(channels->_[1], channels->_[2]);
232 
233             /*fL fR fC bL bR LFE -> fL fR fC LFE bR bL*/
234             a_int_swap(channels->_[3], channels->_[5]);
235 
236             /*fL fR fC LFE bR bL -> fL fR fC LFE bL bR*/
237             a_int_swap(channels->_[4], channels->_[5]);
238             break;
239         case 7:
240             /*fL fC fR sL sR bC LFE -> fL fR fC sL sR bC LFE*/
241             a_int_swap(channels->_[1], channels->_[2]);
242 
243             /*fL fR fC sL sR bC LFE -> fL fR fC LFE sR bC sL*/
244             a_int_swap(channels->_[3], channels->_[6]);
245 
246             /*fL fR fC LFE sR bC sL -> fL fR fC LFE bC sR sL*/
247             a_int_swap(channels->_[4], channels->_[5]);
248 
249             /*fL fR fC LFE bC sR sL -> fL fR fC LFE bC sL sR*/
250             a_int_swap(channels->_[5], channels->_[6]);
251             break;
252         case 8:
253             /*fL fC fR sL sR bL bR LFE -> fL fR fC sL sR bL bR LFE*/
254             a_int_swap(channels->_[1], channels->_[2]);
255 
256             /*fL fR fC sL sR bL bR LFE -> fL fR fC LFE sR bL bR sL*/
257             a_int_swap(channels->_[3], channels->_[6]);
258 
259             /*fL fR fC LFE sR bL bR sL -> fL fR fC LFE bL sR bR sL*/
260             a_int_swap(channels->_[4], channels->_[5]);
261 
262             /*fL fR fC LFE bL sR bR sL -> fL fR fC LFE bL bR sR sL*/
263             a_int_swap(channels->_[5], channels->_[6]);
264 
265             /*fL fR fC LFE bL bR sR sL -> fL fR fC LFE bL bR sL sR*/
266             a_int_swap(channels->_[6], channels->_[7]);
267             break;
268         }
269 
270 
271         if ((samples_read == 0) && (self->vorbisfile.os.e_o_s == 0)) {
272             /*EOF encountered without EOF being marked in stream*/
273             PyErr_SetString(PyExc_IOError,
274                             "I/O error reading from Ogg stream");
275             return NULL;
276         } else {
277             /*return new FrameList object*/
278             return aa_int_to_FrameList(self->audiotools_pcm,
279                                        channels,
280                                        BITS_PER_SAMPLE);
281         }
282     } else {
283         switch (samples_read) {
284         case OV_HOLE:
285             PyErr_SetString(PyExc_ValueError, "data interruption detected");
286             return NULL;
287         case OV_EBADLINK:
288             PyErr_SetString(PyExc_ValueError, "invalid stream section");
289             return NULL;
290         case OV_EINVAL:
291             PyErr_SetString(PyExc_ValueError, "initial file headers corrupt");
292             return NULL;
293         default:
294             PyErr_SetString(PyExc_ValueError, "unspecified error");
295             return NULL;
296         }
297     }
298 }
299 
300 static PyObject*
VorbisDecoder_close(decoders_VorbisDecoder * self,PyObject * args)301 VorbisDecoder_close(decoders_VorbisDecoder *self, PyObject *args) {
302     self->closed = 1;
303 
304     Py_INCREF(Py_None);
305     return Py_None;
306 }
307 
308 static PyObject*
VorbisDecoder_enter(decoders_VorbisDecoder * self,PyObject * args)309 VorbisDecoder_enter(decoders_VorbisDecoder* self, PyObject *args)
310 {
311     Py_INCREF(self);
312     return (PyObject *)self;
313 }
314 
315 static PyObject*
VorbisDecoder_exit(decoders_VorbisDecoder * self,PyObject * args)316 VorbisDecoder_exit(decoders_VorbisDecoder* self, PyObject *args)
317 {
318     self->closed = 1;
319 
320     Py_INCREF(Py_None);
321     return Py_None;
322 }
323