src/io/source.c: add source_wavread
[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 __APPLE__
30 #include "io/source_apple_audio.h"
31 #endif /* __APPLE__ */
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_seek_t)(aubio_source_t * s, uint_t seek);
44 typedef uint_t (*del_aubio_source_t)(aubio_source_t * s);
45
46 struct _aubio_source_t { 
47   void *source;
48   aubio_source_do_t s_do;
49   aubio_source_do_multi_t s_do_multi;
50   aubio_source_get_samplerate_t s_get_samplerate;
51   aubio_source_get_channels_t s_get_channels;
52   aubio_source_seek_t s_seek;
53   del_aubio_source_t s_del;
54 };
55
56 aubio_source_t * new_aubio_source(char_t * uri, uint_t samplerate, uint_t hop_size) {
57   aubio_source_t * s = AUBIO_NEW(aubio_source_t);
58 #if HAVE_LIBAV
59   s->source = (void *)new_aubio_source_avcodec(uri, samplerate, hop_size);
60   if (s->source) {
61     s->s_do = (aubio_source_do_t)(aubio_source_avcodec_do);
62     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_avcodec_do_multi);
63     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_avcodec_get_channels);
64     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_avcodec_get_samplerate);
65     s->s_seek = (aubio_source_seek_t)(aubio_source_avcodec_seek);
66     s->s_del = (del_aubio_source_t)(del_aubio_source_avcodec);
67     return s;
68   }
69 #endif /* HAVE_LIBAV */
70 #ifdef __APPLE__
71   s->source = (void *)new_aubio_source_apple_audio(uri, samplerate, hop_size);
72   if (s->source) {
73     s->s_do = (aubio_source_do_t)(aubio_source_apple_audio_do);
74     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_apple_audio_do_multi);
75     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_apple_audio_get_channels);
76     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_apple_audio_get_samplerate);
77     s->s_seek = (aubio_source_seek_t)(aubio_source_apple_audio_seek);
78     s->s_del = (del_aubio_source_t)(del_aubio_source_apple_audio);
79     return s;
80   }
81 #endif /* __APPLE__ */
82 #if HAVE_SNDFILE
83   s->source = (void *)new_aubio_source_sndfile(uri, samplerate, hop_size);
84   if (s->source) {
85     s->s_do = (aubio_source_do_t)(aubio_source_sndfile_do);
86     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_sndfile_do_multi);
87     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_sndfile_get_channels);
88     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_sndfile_get_samplerate);
89     s->s_seek = (aubio_source_seek_t)(aubio_source_sndfile_seek);
90     s->s_del = (del_aubio_source_t)(del_aubio_source_sndfile);
91     return s;
92   }
93 #endif /* HAVE_SNDFILE */
94 #if HAVE_WAVREAD
95   s->source = (void *)new_aubio_source_wavread(uri, samplerate, hop_size);
96   if (s->source) {
97     s->s_do = (aubio_source_do_t)(aubio_source_wavread_do);
98     s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_wavread_do_multi);
99     s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_wavread_get_channels);
100     s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_wavread_get_samplerate);
101     s->s_seek = (aubio_source_seek_t)(aubio_source_wavread_seek);
102     s->s_del = (del_aubio_source_t)(del_aubio_source_wavread);
103     return s;
104   }
105 #endif /* HAVE_WAVREAD */
106   AUBIO_ERROR("failed creating aubio source with %s\n", uri);
107   AUBIO_FREE(s);
108   return NULL;
109 }
110
111 void aubio_source_do(aubio_source_t * s, fvec_t * data, uint_t * read) {
112   s->s_do((void *)s->source, data, read);
113 }
114
115 void aubio_source_do_multi(aubio_source_t * s, fmat_t * data, uint_t * read) {
116   s->s_do_multi((void *)s->source, data, read);
117 }
118
119 void del_aubio_source(aubio_source_t * s) {
120   if (!s) return;
121   s->s_del((void *)s->source);
122   AUBIO_FREE(s);
123 }
124
125 uint_t aubio_source_get_samplerate(aubio_source_t * s) {
126   return s->s_get_samplerate((void *)s->source);
127 }
128
129 uint_t aubio_source_get_channels(aubio_source_t * s) {
130   return s->s_get_channels((void *)s->source);
131 }
132
133 uint_t aubio_source_seek (aubio_source_t * s, uint_t seek ) {
134   return s->s_seek((void *)s->source, seek);
135 }