[source] simplify and avoid unrequired checks
[aubio.git] / src / io / source.c
1 /*
2   Copyright (C) 2012 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "aubio_priv.h"
22 #include "fvec.h"
23 #include "fmat.h"
24 #include "io/source.h"
25 #ifdef HAVE_LIBAV
26 #include "io/source_avcodec.h"
27 #endif /* HAVE_LIBAV */
28 #ifdef HAVE_SOURCE_APPLE_AUDIO
29 #include "io/source_apple_audio.h"
30 #endif /* HAVE_SOURCE_APPLE_AUDIO */
31 #ifdef HAVE_SNDFILE
32 #include "io/source_sndfile.h"
33 #endif /* HAVE_SNDFILE */
34 #ifdef HAVE_WAVREAD
35 #include "io/source_wavread.h"
36 #endif /* HAVE_WAVREAD */
37
38 typedef void (*aubio_source_do_t)(aubio_source_t * s, fvec_t * data, uint_t * read);
39 typedef void (*aubio_source_do_multi_t)(aubio_source_t * s, fmat_t * data, uint_t * read);
40 typedef uint_t (*aubio_source_get_samplerate_t)(aubio_source_t * s);
41 typedef uint_t (*aubio_source_get_channels_t)(aubio_source_t * s);
42 typedef uint_t (*aubio_source_get_duration_t)(aubio_source_t * s);
43 typedef uint_t (*aubio_source_seek_t)(aubio_source_t * s, uint_t seek);
44 typedef uint_t (*aubio_source_close_t)(aubio_source_t * s);
45 typedef void (*del_aubio_source_t)(aubio_source_t * s);
46
47 struct _aubio_source_t { 
48   void *source;
49   aubio_source_do_t s_do;
50   aubio_source_do_multi_t s_do_multi;
51   aubio_source_get_samplerate_t s_get_samplerate;
52   aubio_source_get_channels_t s_get_channels;
53   aubio_source_get_duration_t s_get_duration;
54   aubio_source_seek_t s_seek;
55   aubio_source_close_t s_close;
56   del_aubio_source_t s_del;
57 };
58
59 aubio_source_t * new_aubio_source(const char_t * uri, uint_t samplerate, uint_t hop_size) {
60   aubio_source_t * s = AUBIO_NEW(aubio_source_t);
61 #ifdef HAVE_LIBAV
62   s->source = (void *)new_aubio_source_avcodec(uri, samplerate, hop_size);
63   if (s->source) {
64     s->s_do = (aubio_source_do_t)(aubio_source_avcodec_do);
65     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_avcodec_do_multi);
66     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_avcodec_get_channels);
67     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_avcodec_get_samplerate);
68     s->s_get_duration = (aubio_source_get_duration_t)(aubio_source_avcodec_get_duration);
69     s->s_seek = (aubio_source_seek_t)(aubio_source_avcodec_seek);
70     s->s_close = (aubio_source_close_t)(aubio_source_avcodec_close);
71     s->s_del = (del_aubio_source_t)(del_aubio_source_avcodec);
72     return s;
73   }
74 #endif /* HAVE_LIBAV */
75 #ifdef HAVE_SOURCE_APPLE_AUDIO
76   s->source = (void *)new_aubio_source_apple_audio(uri, samplerate, hop_size);
77   if (s->source) {
78     s->s_do = (aubio_source_do_t)(aubio_source_apple_audio_do);
79     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_apple_audio_do_multi);
80     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_apple_audio_get_channels);
81     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_apple_audio_get_samplerate);
82     s->s_get_duration = (aubio_source_get_duration_t)(aubio_source_apple_audio_get_duration);
83     s->s_seek = (aubio_source_seek_t)(aubio_source_apple_audio_seek);
84     s->s_close = (aubio_source_close_t)(aubio_source_apple_audio_close);
85     s->s_del = (del_aubio_source_t)(del_aubio_source_apple_audio);
86     return s;
87   }
88 #endif /* HAVE_SOURCE_APPLE_AUDIO */
89 #ifdef HAVE_SNDFILE
90   s->source = (void *)new_aubio_source_sndfile(uri, samplerate, hop_size);
91   if (s->source) {
92     s->s_do = (aubio_source_do_t)(aubio_source_sndfile_do);
93     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_sndfile_do_multi);
94     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_sndfile_get_channels);
95     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_sndfile_get_samplerate);
96     s->s_get_duration = (aubio_source_get_duration_t)(aubio_source_sndfile_get_duration);
97     s->s_seek = (aubio_source_seek_t)(aubio_source_sndfile_seek);
98     s->s_close = (aubio_source_close_t)(aubio_source_sndfile_close);
99     s->s_del = (del_aubio_source_t)(del_aubio_source_sndfile);
100     return s;
101   }
102 #endif /* HAVE_SNDFILE */
103 #ifdef HAVE_WAVREAD
104   s->source = (void *)new_aubio_source_wavread(uri, samplerate, hop_size);
105   if (s->source) {
106     s->s_do = (aubio_source_do_t)(aubio_source_wavread_do);
107     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_wavread_do_multi);
108     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_wavread_get_channels);
109     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_wavread_get_samplerate);
110     s->s_get_duration = (aubio_source_get_duration_t)(aubio_source_wavread_get_duration);
111     s->s_seek = (aubio_source_seek_t)(aubio_source_wavread_seek);
112     s->s_close = (aubio_source_close_t)(aubio_source_wavread_close);
113     s->s_del = (del_aubio_source_t)(del_aubio_source_wavread);
114     return s;
115   }
116 #endif /* HAVE_WAVREAD */
117 #if !defined(HAVE_WAVREAD) && \
118   !defined(HAVE_LIBAV) && \
119   !defined(HAVE_SOURCE_APPLE_AUDIO) && \
120   !defined(HAVE_SNDFILE)
121   AUBIO_ERROR("source: failed creating with %s at %dHz with hop size %d"
122      " (no source built-in)\n", uri, samplerate, hop_size);
123 #endif
124   del_aubio_source(s);
125   return NULL;
126 }
127
128 void aubio_source_do(aubio_source_t * s, fvec_t * data, uint_t * read) {
129   s->s_do((void *)s->source, data, read);
130 }
131
132 void aubio_source_do_multi(aubio_source_t * s, fmat_t * data, uint_t * read) {
133   s->s_do_multi((void *)s->source, data, read);
134 }
135
136 uint_t aubio_source_close(aubio_source_t * s) {
137   return s->s_close((void *)s->source);
138 }
139
140 void del_aubio_source(aubio_source_t * s) {
141   AUBIO_ASSERT(s);
142   if (s->s_del && s->source)
143     s->s_del((void *)s->source);
144   AUBIO_FREE(s);
145 }
146
147 uint_t aubio_source_get_samplerate(aubio_source_t * s) {
148   return s->s_get_samplerate((void *)s->source);
149 }
150
151 uint_t aubio_source_get_channels(aubio_source_t * s) {
152   return s->s_get_channels((void *)s->source);
153 }
154
155 uint_t aubio_source_get_duration(aubio_source_t *s) {
156   return s->s_get_duration((void *)s->source);
157 }
158
159 uint_t aubio_source_seek (aubio_source_t * s, uint_t seek ) {
160   return s->s_seek((void *)s->source, seek);
161 }