source: src/io/sink.c @ d013a93

feature/autosinkfeature/cnnfeature/crepefix/ffmpeg5
Last change on this file since d013a93 was d013a93, checked in by Paul Brossier <piem@piem.org>, 5 years ago

Merge branch 'feature/sink_flac' into feature/autosink

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2  Copyright (C) 2012-2014 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/sink.h"
25#ifdef HAVE_SINK_APPLE_AUDIO
26#include "io/sink_apple_audio.h"
27#endif /* HAVE_SINK_APPLE_AUDIO */
28#ifdef HAVE_SNDFILE
29#include "io/sink_sndfile.h"
30#endif
31#ifdef HAVE_WAVWRITE
32#include "io/sink_wavwrite.h"
33#endif
34
35typedef void (*aubio_sink_do_t)(aubio_sink_t * s, fvec_t * data, uint_t write);
36typedef void (*aubio_sink_do_multi_t)(aubio_sink_t * s, fmat_t * data, uint_t write);
37typedef uint_t (*aubio_sink_preset_samplerate_t)(aubio_sink_t * s, uint_t samplerate);
38typedef uint_t (*aubio_sink_preset_channels_t)(aubio_sink_t * s, uint_t channels);
39typedef uint_t (*aubio_sink_get_samplerate_t)(aubio_sink_t * s);
40typedef uint_t (*aubio_sink_get_channels_t)(aubio_sink_t * s);
41typedef uint_t (*aubio_sink_close_t)(aubio_sink_t * s);
42typedef void (*del_aubio_sink_t)(aubio_sink_t * s);
43
44struct _aubio_sink_t { 
45  void *sink;
46  aubio_sink_do_t s_do;
47  aubio_sink_do_multi_t s_do_multi;
48  aubio_sink_preset_samplerate_t s_preset_samplerate;
49  aubio_sink_preset_channels_t s_preset_channels;
50  aubio_sink_get_samplerate_t s_get_samplerate;
51  aubio_sink_get_channels_t s_get_channels;
52  aubio_sink_close_t s_close;
53  del_aubio_sink_t s_del;
54};
55
56#ifdef HAVE_VORBISENC
57typedef struct _aubio_sink_vorbis_t aubio_sink_vorbis_t;
58extern aubio_sink_vorbis_t * new_aubio_sink_vorbis(const char_t *uri,
59    uint_t samplerate);
60extern void del_aubio_sink_vorbis (aubio_sink_vorbis_t *s);
61extern uint_t aubio_sink_vorbis_open(aubio_sink_vorbis_t *s);
62extern uint_t aubio_sink_vorbis_close(aubio_sink_vorbis_t *s);
63extern uint_t aubio_sink_vorbis_preset_channels(aubio_sink_vorbis_t *s,
64    uint_t channels);
65extern uint_t aubio_sink_vorbis_preset_samplerate(aubio_sink_vorbis_t *s,
66    uint_t samplerate);
67extern uint_t aubio_sink_vorbis_get_channels(aubio_sink_vorbis_t *s);
68extern uint_t aubio_sink_vorbis_get_samplerate(aubio_sink_vorbis_t *s);
69extern void aubio_sink_vorbis_do(aubio_sink_vorbis_t *s, fvec_t*
70    write_data, uint_t write);
71extern void aubio_sink_vorbis_do_multi(aubio_sink_vorbis_t *s, fmat_t*
72    write_data, uint_t write);
73#endif /* HAVE_VORBISENC */
74
75#ifdef HAVE_FLAC
76typedef struct _aubio_sink_flac_t aubio_sink_flac_t;
77extern aubio_sink_flac_t * new_aubio_sink_flac(const char_t *uri,
78    uint_t samplerate);
79extern void del_aubio_sink_flac (aubio_sink_flac_t *s);
80extern uint_t aubio_sink_flac_open(aubio_sink_flac_t *s);
81extern uint_t aubio_sink_flac_close(aubio_sink_flac_t *s);
82extern uint_t aubio_sink_flac_preset_channels(aubio_sink_flac_t *s,
83    uint_t channels);
84extern uint_t aubio_sink_flac_preset_samplerate(aubio_sink_flac_t *s,
85    uint_t samplerate);
86extern uint_t aubio_sink_flac_get_channels(aubio_sink_flac_t *s);
87extern uint_t aubio_sink_flac_get_samplerate(aubio_sink_flac_t *s);
88extern void aubio_sink_flac_do(aubio_sink_flac_t *s, fvec_t*
89    write_data, uint_t write);
90extern void aubio_sink_flac_do_multi(aubio_sink_flac_t *s, fmat_t*
91    write_data, uint_t write);
92#endif /* HAVE_FLAC */
93
94static const char_t *aubio_get_extension(const char_t *filename)
95{
96  // find last occurence of dot character
97  const char_t *ext = strrchr(filename, '.');
98  if (!ext || ext == filename) return "";
99  else return ext + 1;
100}
101
102aubio_sink_t * new_aubio_sink(const char_t * uri, uint_t samplerate) {
103  aubio_sink_t * s = AUBIO_NEW(aubio_sink_t);
104#if defined(HAVE_VORBISENC) || defined(HAVE_FLAC)
105  const char_t * uri_ext = aubio_get_extension(uri);
106#endif /* defined(HAVE_VORBISENC) || defined(HAVE_FLAC) */
107
108#ifdef HAVE_VORBISENC
109  // check if this uri could be for us
110  uint_t match_oggstream = 0;
111  if (strcmp (uri_ext, "ogg") == 0) match_oggstream = 1;
112  if (match_oggstream) {
113    s->sink = (void *)new_aubio_sink_vorbis(uri, samplerate);
114    if (s->sink) {
115      s->s_do = (aubio_sink_do_t)(aubio_sink_vorbis_do);
116      s->s_do_multi = (aubio_sink_do_multi_t)(aubio_sink_vorbis_do_multi);
117      s->s_preset_samplerate = (aubio_sink_preset_samplerate_t)(aubio_sink_vorbis_preset_samplerate);
118      s->s_preset_channels = (aubio_sink_preset_channels_t)(aubio_sink_vorbis_preset_channels);
119      s->s_get_samplerate = (aubio_sink_get_samplerate_t)(aubio_sink_vorbis_get_samplerate);
120      s->s_get_channels = (aubio_sink_get_channels_t)(aubio_sink_vorbis_get_channels);
121      s->s_close = (aubio_sink_close_t)(aubio_sink_vorbis_close);
122      s->s_del = (del_aubio_sink_t)(del_aubio_sink_vorbis);
123      return s;
124    }
125  }
126#endif /* HAVE_VORBISENC */
127
128#ifdef HAVE_FLAC
129  // check if this uri could be for us
130  uint_t match_flacstream = 0;
131  if (strcmp (uri_ext, "flac") == 0) match_flacstream = 1;
132  if (match_flacstream) {
133    s->sink = (void *)new_aubio_sink_flac(uri, samplerate);
134    if (s->sink) {
135      s->s_do = (aubio_sink_do_t)(aubio_sink_flac_do);
136      s->s_do_multi = (aubio_sink_do_multi_t)(aubio_sink_flac_do_multi);
137      s->s_preset_samplerate = (aubio_sink_preset_samplerate_t)(aubio_sink_flac_preset_samplerate);
138      s->s_preset_channels = (aubio_sink_preset_channels_t)(aubio_sink_flac_preset_channels);
139      s->s_get_samplerate = (aubio_sink_get_samplerate_t)(aubio_sink_flac_get_samplerate);
140      s->s_get_channels = (aubio_sink_get_channels_t)(aubio_sink_flac_get_channels);
141      s->s_close = (aubio_sink_close_t)(aubio_sink_flac_close);
142      s->s_del = (del_aubio_sink_t)(del_aubio_sink_flac);
143      return s;
144    }
145  }
146#endif /* HAVE_FLAC */
147
148#ifdef HAVE_SINK_APPLE_AUDIO
149  s->sink = (void *)new_aubio_sink_apple_audio(uri, samplerate);
150  if (s->sink) {
151    s->s_do = (aubio_sink_do_t)(aubio_sink_apple_audio_do);
152    s->s_do_multi = (aubio_sink_do_multi_t)(aubio_sink_apple_audio_do_multi);
153    s->s_preset_samplerate = (aubio_sink_preset_samplerate_t)(aubio_sink_apple_audio_preset_samplerate);
154    s->s_preset_channels = (aubio_sink_preset_channels_t)(aubio_sink_apple_audio_preset_channels);
155    s->s_get_samplerate = (aubio_sink_get_samplerate_t)(aubio_sink_apple_audio_get_samplerate);
156    s->s_get_channels = (aubio_sink_get_channels_t)(aubio_sink_apple_audio_get_channels);
157    s->s_close = (aubio_sink_close_t)(aubio_sink_apple_audio_close);
158    s->s_del = (del_aubio_sink_t)(del_aubio_sink_apple_audio);
159    return s;
160  }
161#endif /* HAVE_SINK_APPLE_AUDIO */
162#ifdef HAVE_SNDFILE
163  s->sink = (void *)new_aubio_sink_sndfile(uri, samplerate);
164  if (s->sink) {
165    s->s_do = (aubio_sink_do_t)(aubio_sink_sndfile_do);
166    s->s_do_multi = (aubio_sink_do_multi_t)(aubio_sink_sndfile_do_multi);
167    s->s_preset_samplerate = (aubio_sink_preset_samplerate_t)(aubio_sink_sndfile_preset_samplerate);
168    s->s_preset_channels = (aubio_sink_preset_channels_t)(aubio_sink_sndfile_preset_channels);
169    s->s_get_samplerate = (aubio_sink_get_samplerate_t)(aubio_sink_sndfile_get_samplerate);
170    s->s_get_channels = (aubio_sink_get_channels_t)(aubio_sink_sndfile_get_channels);
171    s->s_close = (aubio_sink_close_t)(aubio_sink_sndfile_close);
172    s->s_del = (del_aubio_sink_t)(del_aubio_sink_sndfile);
173    return s;
174  }
175#endif /* HAVE_SNDFILE */
176#ifdef HAVE_WAVWRITE
177  s->sink = (void *)new_aubio_sink_wavwrite(uri, samplerate);
178  if (s->sink) {
179    s->s_do = (aubio_sink_do_t)(aubio_sink_wavwrite_do);
180    s->s_do_multi = (aubio_sink_do_multi_t)(aubio_sink_wavwrite_do_multi);
181    s->s_preset_samplerate = (aubio_sink_preset_samplerate_t)(aubio_sink_wavwrite_preset_samplerate);
182    s->s_preset_channels = (aubio_sink_preset_channels_t)(aubio_sink_wavwrite_preset_channels);
183    s->s_get_samplerate = (aubio_sink_get_samplerate_t)(aubio_sink_wavwrite_get_samplerate);
184    s->s_get_channels = (aubio_sink_get_channels_t)(aubio_sink_wavwrite_get_channels);
185    s->s_close = (aubio_sink_close_t)(aubio_sink_wavwrite_close);
186    s->s_del = (del_aubio_sink_t)(del_aubio_sink_wavwrite);
187    return s;
188  }
189#endif /* HAVE_WAVWRITE */
190#if !defined(HAVE_WAVWRITE) && \
191  !defined(HAVE_SNDFILE) && \
192  !defined(HAVE_SINK_APPLE_AUDIO) && \
193  !defined(HAVE_VORBISENC) && \
194  !defined(HAVE_FLAC)
195  AUBIO_ERROR("sink: failed creating '%s' at %dHz (no sink built-in)\n", uri, samplerate);
196#endif
197  del_aubio_sink(s);
198  return NULL;
199}
200
201void aubio_sink_do(aubio_sink_t * s, fvec_t * write_data, uint_t write) {
202  s->s_do((void *)s->sink, write_data, write);
203}
204
205void aubio_sink_do_multi(aubio_sink_t * s, fmat_t * write_data, uint_t write) {
206  s->s_do_multi((void *)s->sink, write_data, write);
207}
208
209uint_t aubio_sink_preset_samplerate(aubio_sink_t * s, uint_t samplerate) {
210  return s->s_preset_samplerate((void *)s->sink, samplerate);
211}
212
213uint_t aubio_sink_preset_channels(aubio_sink_t * s, uint_t channels) {
214  return s->s_preset_channels((void *)s->sink, channels);
215}
216
217uint_t aubio_sink_get_samplerate(const aubio_sink_t * s) {
218  return s->s_get_samplerate((void *)s->sink);
219}
220
221uint_t aubio_sink_get_channels(const aubio_sink_t * s) {
222  return s->s_get_channels((void *)s->sink);
223}
224
225uint_t aubio_sink_close(aubio_sink_t *s) {
226  return s->s_close((void *)s->sink);
227}
228
229void del_aubio_sink(aubio_sink_t * s) {
230  AUBIO_ASSERT(s);
231  if (s->s_del && s->sink)
232    s->s_del((void *)s->sink);
233  AUBIO_FREE(s);
234}
Note: See TracBrowser for help on using the repository browser.