source: src/io/sink_vorbis.c @ 0850e54

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

[io] ensure fwrite are successful in sink_vorbis

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*
2  Copyright (C) 2018 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/*
22  This file is largely inspired by `examples/encoder_example.c` in the
23  libvorbis source package (versions 1.3.5 and later) available online at
24  https://xiph.org/vorbis/
25*/
26
27#include "aubio_priv.h"
28
29#ifdef HAVE_VORBISENC
30
31#include "io/ioutils.h"
32#include "fmat.h"
33
34#include <vorbis/vorbisenc.h>
35#include <string.h> // strerror
36#include <errno.h> // errno
37#include <time.h> // time
38
39struct _aubio_sink_vorbis_t {
40  FILE *fid;            // file id
41  ogg_stream_state os;  // stream
42  ogg_page og;          // page
43  ogg_packet op;        // data packet
44  vorbis_info vi;       // vorbis bitstream settings
45  vorbis_comment vc;    // user comment
46  vorbis_dsp_state vd;  // working state
47  vorbis_block vb;      // working space
48
49  uint_t samplerate;
50  uint_t channels;
51  char_t *path;
52};
53
54typedef struct _aubio_sink_vorbis_t aubio_sink_vorbis_t;
55
56uint_t aubio_sink_vorbis_preset_channels(aubio_sink_vorbis_t *s,
57    uint_t channels);
58uint_t aubio_sink_vorbis_preset_samplerate(aubio_sink_vorbis_t *s,
59    uint_t samplerate);
60uint_t aubio_sink_vorbis_open(aubio_sink_vorbis_t *s);
61uint_t aubio_sink_vorbis_close (aubio_sink_vorbis_t *s);
62void del_aubio_sink_vorbis (aubio_sink_vorbis_t *s);
63
64aubio_sink_vorbis_t * new_aubio_sink_vorbis (const char_t *uri,
65    uint_t samplerate)
66{
67  aubio_sink_vorbis_t * s = AUBIO_NEW(aubio_sink_vorbis_t);
68
69  s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1);
70  strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1);
71  s->path[strnlen(uri, PATH_MAX)] = '\0';
72
73  s->channels = 0;
74
75  if ((sint_t)samplerate == 0)
76    return s;
77
78  aubio_sink_vorbis_preset_samplerate(s, samplerate);
79  s->channels = 1;
80
81  if (aubio_sink_vorbis_open(s) != AUBIO_OK)
82    goto failure;
83
84  return s;
85
86failure:
87  del_aubio_sink_vorbis(s);
88  return NULL;
89}
90
91void del_aubio_sink_vorbis (aubio_sink_vorbis_t *s)
92{
93  if (s->fid) aubio_sink_vorbis_close(s);
94  // clean up
95  ogg_stream_clear(&s->os);
96  vorbis_block_clear(&s->vb);
97  vorbis_dsp_clear(&s->vd);
98  vorbis_comment_clear(&s->vc);
99  vorbis_info_clear(&s->vi);
100
101  if (s->path) AUBIO_FREE(s->path);
102  AUBIO_FREE(s);
103}
104
105uint_t aubio_sink_vorbis_open(aubio_sink_vorbis_t *s)
106{
107  float quality_mode = .9;
108
109  if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
110
111  s->fid = fopen((const char *)s->path, "wb");
112  if (!s->fid) {
113    AUBIO_ERR("sink_vorbis: Error opening file %s (%s)\n",
114        s->path, strerror(errno));
115    return AUBIO_FAIL;
116  }
117
118  vorbis_info_init(&s->vi);
119  if (vorbis_encode_init_vbr(&s->vi, s->channels, s->samplerate, quality_mode))
120  {
121    AUBIO_ERR("sink_vorbis: vorbis_encode_init_vbr failed\n");
122    return AUBIO_FAIL;
123  }
124
125  // add comment
126  vorbis_comment_init(&s->vc);
127  vorbis_comment_add_tag(&s->vc, "ENCODER", "aubio");
128
129  // initalise analysis and block
130  vorbis_analysis_init(&s->vd, &s->vi);
131  vorbis_block_init(&s->vd, &s->vb);
132
133  // pick randome serial number
134  srand(time(NULL));
135  ogg_stream_init(&s->os, rand());
136
137  // write header
138  {
139    int ret = 0;
140    size_t wrote;
141    ogg_packet header;
142    ogg_packet header_comm;
143    ogg_packet header_code;
144
145    vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
146        &header_code);
147
148    ogg_stream_packetin(&s->os, &header);
149    ogg_stream_packetin(&s->os, &header_comm);
150    ogg_stream_packetin(&s->os, &header_code);
151
152    // make sure audio data will start on a new page
153    while (1)
154    {
155      ret = ogg_stream_flush(&s->os, &s->og);
156      if (ret==0) break;
157      wrote = fwrite(s->og.header, 1, s->og.header_len, s->fid);
158      ret = (wrote == (unsigned)s->og.header_len);
159      wrote = fwrite(s->og.body,   1, s->og.body_len,   s->fid);
160      ret &= (wrote == (unsigned)s->og.body_len);
161      if (ret == 0) {
162        AUBIO_ERR("sink_vorbis: failed writing \'%s\' to disk (%s)\n",
163            s->path, strerror(errno));
164        return AUBIO_FAIL;
165      }
166    }
167  }
168
169  return AUBIO_OK;
170}
171
172uint_t aubio_sink_vorbis_preset_samplerate(aubio_sink_vorbis_t *s,
173    uint_t samplerate)
174{
175  if (aubio_io_validate_samplerate("sink_vorbis", s->path, samplerate))
176    return AUBIO_FAIL;
177  s->samplerate = samplerate;
178  if (s->samplerate != 0 && s->channels != 0)
179    return aubio_sink_vorbis_open(s);
180  return AUBIO_OK;
181}
182
183uint_t aubio_sink_vorbis_preset_channels(aubio_sink_vorbis_t *s,
184    uint_t channels)
185{
186  if (aubio_io_validate_channels("sink_vorbis", s->path, channels)) {
187    return AUBIO_FAIL;
188  }
189  s->channels = channels;
190  // automatically open when both samplerate and channels have been set
191  if (s->samplerate != 0 && s->channels != 0) {
192    return aubio_sink_vorbis_open(s);
193  }
194  return AUBIO_OK;
195}
196
197uint_t aubio_sink_vorbis_get_samplerate(const aubio_sink_vorbis_t *s)
198{
199  return s->samplerate;
200}
201
202uint_t aubio_sink_vorbis_get_channels(const aubio_sink_vorbis_t *s)
203{
204  return s->channels;
205}
206
207void aubio_sink_vorbis_write(aubio_sink_vorbis_t *s)
208{
209  int result;
210  size_t wrote;
211  // pre-analysis
212  while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
213
214    vorbis_analysis(&s->vb, NULL);
215    vorbis_bitrate_addblock(&s->vb);
216
217    while (vorbis_bitrate_flushpacket(&s->vd, &s->op))
218    {
219      ogg_stream_packetin(&s->os, &s->op);
220
221      while (1) {
222        result = ogg_stream_pageout (&s->os, &s->og);
223        if (result == 0) break;
224        wrote = fwrite(s->og.header, 1, s->og.header_len, s->fid);
225        result = (wrote == (unsigned)s->og.header_len);
226        wrote = fwrite(s->og.body, 1, s->og.body_len,     s->fid);
227        result &= (wrote == (unsigned)s->og.body_len);
228        if (result == 0) {
229          AUBIO_WRN("sink_vorbis: failed writing \'%s\' to disk (%s)\n",
230              s->path, strerror(errno));
231        }
232        if (ogg_page_eos(&s->og)) break;
233      }
234    }
235  }
236}
237
238void aubio_sink_vorbis_do(aubio_sink_vorbis_t *s, fvec_t *write_data,
239    uint_t write)
240{
241  uint_t c, v;
242  float **buffer = vorbis_analysis_buffer(&s->vd, (long)write);
243  // fill buffer
244  if (!write) {
245    return;
246  } else if (!buffer) {
247    AUBIO_WRN("sink_vorbis: failed fetching buffer of size %d\n", write);
248    return;
249  } else {
250    for (c = 0; c < s->channels; c++) {
251      for (v = 0; v < write; v++) {
252        buffer[c][v] = write_data->data[v];
253      }
254    }
255    // tell vorbis how many frames were written
256    vorbis_analysis_wrote(&s->vd, (long)write);
257  }
258  // write to file
259  aubio_sink_vorbis_write(s);
260}
261
262void aubio_sink_vorbis_do_multi(aubio_sink_vorbis_t *s, fmat_t *write_data,
263    uint_t write)
264{
265  uint_t c, v;
266  uint_t channels = MIN(s->channels, write_data->height);
267  float **buffer = vorbis_analysis_buffer(&s->vd, (long)write);
268  // fill buffer
269  if (!write) {
270    return;
271  } else if (!buffer) {
272    AUBIO_WRN("sink_vorbis: failed fetching buffer of size %d\n", write);
273    return;
274  } else {
275    for (c = 0; c < channels; c++) {
276      for (v = 0; v < write; v++) {
277        buffer[c][v] = write_data->data[c][v];
278      }
279    }
280    // tell vorbis how many frames were written
281    vorbis_analysis_wrote(&s->vd, (long)write);
282  }
283
284  aubio_sink_vorbis_write(s);
285}
286
287uint_t aubio_sink_vorbis_close (aubio_sink_vorbis_t *s)
288{
289  //mark the end of stream
290  vorbis_analysis_wrote(&s->vd, 0);
291
292  aubio_sink_vorbis_write(s);
293
294  if (s->fid && fclose(s->fid)) {
295    AUBIO_ERR("sink_vorbis: Error closing file %s (%s)\n",
296        s->path, strerror(errno));
297    return AUBIO_FAIL;
298  }
299  s->fid = NULL;
300  return AUBIO_OK;
301}
302
303#endif /* HAVE_VORBISENC */
Note: See TracBrowser for help on using the repository browser.