src/io/source.*: add _get_duration
[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 "config.h"
22 #include "aubio_priv.h"
23 #include "fvec.h"
24 #include "fmat.h"
25 #include "io/source.h"
26 #ifdef HAVE_LIBAV
27 #include "io/source_avcodec.h"
28 #endif /* HAVE_LIBAV */
29 #ifdef HAVE_SOURCE_APPLE_AUDIO
30 #include "io/source_apple_audio.h"
31 #endif /* HAVE_SOURCE_APPLE_AUDIO */
32 #ifdef HAVE_SNDFILE
33 #include "io/source_sndfile.h"
34 #endif /* HAVE_SNDFILE */
35 #ifdef HAVE_WAVREAD
36 #include "io/source_wavread.h"
37 #endif /* HAVE_WAVREAD */
38
39 typedef void (*aubio_source_do_t)(aubio_source_t * s, fvec_t * data, uint_t * read);
40 typedef void (*aubio_source_do_multi_t)(aubio_source_t * s, fmat_t * data, uint_t * read);
41 typedef uint_t (*aubio_source_get_samplerate_t)(aubio_source_t * s);
42 typedef uint_t (*aubio_source_get_channels_t)(aubio_source_t * s);
43 typedef uint_t (*aubio_source_get_duration_t)(aubio_source_t * s);
44 typedef uint_t (*aubio_source_seek_t)(aubio_source_t * s, uint_t seek);
45 typedef uint_t (*aubio_source_close_t)(aubio_source_t * s);
46 typedef void (*del_aubio_source_t)(aubio_source_t * s);
47
48 struct _aubio_source_t { 
49   void *source;
50   aubio_source_do_t s_do;
51   aubio_source_do_multi_t s_do_multi;
52   aubio_source_get_samplerate_t s_get_samplerate;
53   aubio_source_get_channels_t s_get_channels;
54   aubio_source_get_duration_t s_get_duration;
55   aubio_source_seek_t s_seek;
56   aubio_source_close_t s_close;
57   del_aubio_source_t s_del;
58 };
59
60 aubio_source_t * new_aubio_source(const char_t * uri, uint_t samplerate, uint_t hop_size) {
61   aubio_source_t * s = AUBIO_NEW(aubio_source_t);
62 #ifdef HAVE_LIBAV
63   s->source = (void *)new_aubio_source_avcodec(uri, samplerate, hop_size);
64   if (s->source) {
65     s->s_do = (aubio_source_do_t)(aubio_source_avcodec_do);
66     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_avcodec_do_multi);
67     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_avcodec_get_channels);
68     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_avcodec_get_samplerate);
69     s->s_get_duration = (aubio_source_get_duration_t)(aubio_source_avcodec_get_duration);
70     s->s_seek = (aubio_source_seek_t)(aubio_source_avcodec_seek);
71     s->s_close = (aubio_source_close_t)(aubio_source_avcodec_close);
72     s->s_del = (del_aubio_source_t)(del_aubio_source_avcodec);
73     return s;
74   }
75 #endif /* HAVE_LIBAV */
76 #ifdef HAVE_SOURCE_APPLE_AUDIO
77   s->source = (void *)new_aubio_source_apple_audio(uri, samplerate, hop_size);
78   if (s->source) {
79     s->s_do = (aubio_source_do_t)(aubio_source_apple_audio_do);
80     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_apple_audio_do_multi);
81     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_apple_audio_get_channels);
82     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_apple_audio_get_samplerate);
83     s->s_get_duration = (aubio_source_get_duration_t)(aubio_source_apple_audio_get_duration);
84     s->s_seek = (aubio_source_seek_t)(aubio_source_apple_audio_seek);
85     s->s_close = (aubio_source_close_t)(aubio_source_apple_audio_close);
86     s->s_del = (del_aubio_source_t)(del_aubio_source_apple_audio);
87     return s;
88   }
89 #endif /* HAVE_SOURCE_APPLE_AUDIO */
90 #ifdef HAVE_SNDFILE
91   s->source = (void *)new_aubio_source_sndfile(uri, samplerate, hop_size);
92   if (s->source) {
93     s->s_do = (aubio_source_do_t)(aubio_source_sndfile_do);
94     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_sndfile_do_multi);
95     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_sndfile_get_channels);
96     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_sndfile_get_samplerate);
97     s->s_get_duration = (aubio_source_get_duration_t)(aubio_source_sndfile_get_duration);
98     s->s_seek = (aubio_source_seek_t)(aubio_source_sndfile_seek);
99     s->s_close = (aubio_source_close_t)(aubio_source_sndfile_close);
100     s->s_del = (del_aubio_source_t)(del_aubio_source_sndfile);
101     return s;
102   }
103 #endif /* HAVE_SNDFILE */
104 #ifdef HAVE_WAVREAD
105   s->source = (void *)new_aubio_source_wavread(uri, samplerate, hop_size);
106   if (s->source) {
107     s->s_do = (aubio_source_do_t)(aubio_source_wavread_do);
108     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_wavread_do_multi);
109     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_wavread_get_channels);
110     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_wavread_get_samplerate);
111     s->s_get_duration = (aubio_source_get_duration_t)(aubio_source_wavread_get_duration);
112     s->s_seek = (aubio_source_seek_t)(aubio_source_wavread_seek);
113     s->s_close = (aubio_source_close_t)(aubio_source_wavread_close);
114     s->s_del = (del_aubio_source_t)(del_aubio_source_wavread);
115     return s;
116   }
117 #endif /* HAVE_WAVREAD */
118   AUBIO_ERROR("source: failed creating aubio source with %s"
119      " at samplerate %d with hop_size %d\n", uri, samplerate, hop_size);
120   AUBIO_FREE(s);
121   return NULL;
122 }
123
124 void aubio_source_do(aubio_source_t * s, fvec_t * data, uint_t * read) {
125   s->s_do((void *)s->source, data, read);
126 }
127
128 void aubio_source_do_multi(aubio_source_t * s, fmat_t * data, uint_t * read) {
129   s->s_do_multi((void *)s->source, data, read);
130 }
131
132 uint_t aubio_source_close(aubio_source_t * s) {
133   return s->s_close((void *)s->source);
134 }
135
136 void del_aubio_source(aubio_source_t * s) {
137   if (!s) return;
138   s->s_del((void *)s->source);
139   AUBIO_FREE(s);
140 }
141
142 uint_t aubio_source_get_samplerate(aubio_source_t * s) {
143   return s->s_get_samplerate((void *)s->source);
144 }
145
146 uint_t aubio_source_get_channels(aubio_source_t * s) {
147   return s->s_get_channels((void *)s->source);
148 }
149
150 uint_t aubio_source_get_duration(aubio_source_t *s) {
151   return s->s_get_duration((void *)s->source);
152 }
153
154 uint_t aubio_source_seek (aubio_source_t * s, uint_t seek ) {
155   return s->s_seek((void *)s->source, seek);
156 }