source: src/io/sink_sndfile.c @ 8aed26d

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 8aed26d was 8aed26d, checked in by Paul Brossier <piem@piem.org>, 12 years ago

src/io/sink*: read write size instead of returning it

  • Property mode set to 100644
File size: 3.2 KB
Line 
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
22#include "config.h"
23
24#ifdef HAVE_SNDFILE
25
26#include <sndfile.h>
27
28#include "aubio_priv.h"
29#include "sink_sndfile.h"
30#include "fvec.h"
31
32#define MAX_CHANNELS 6
33#define MAX_SIZE 4096
34
35struct _aubio_sink_sndfile_t {
36  uint_t samplerate;
37  uint_t channels;
38  char_t *path;
39  SNDFILE *handle;
40  uint_t scratch_size;
41  smpl_t *scratch_data;
42};
43
44aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate) {
45  aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
46
47  if (path == NULL) {
48    AUBIO_ERR("Aborted opening null path\n");
49    return NULL;
50  }
51
52  s->samplerate = samplerate;
53  s->max_size = MAX_SIZE;
54  s->channels = 1;
55  s->path = path;
56
57  /* set output format */
58  SF_INFO sfinfo;
59  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
60  sfinfo.samplerate = s->samplerate;
61  sfinfo.channels   = s->channels;
62  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
63
64  /* try creating the file */
65  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
66
67  if (s->handle == NULL) {
68    /* show libsndfile err msg */
69    AUBIO_ERR("Failed opening %s. %s\n", s->path, sf_strerror (NULL));
70    return NULL;
71  }     
72
73  s->scratch_size = s->max-size*s->channels;
74  /* allocate data for de/interleaving reallocated when needed. */
75  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
76    AUBIO_ERR("%d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
77        s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
78    return NULL;
79  }
80  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
81
82  return s;
83}
84
85void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
86  sf_count_t written_frames = 0;
87  int i, j,     channels = s->channels;
88  int nsamples = channels*write;
89  smpl_t *pwrite;
90
91  if (write > s->max_size) {
92    write = s->max_size;
93    AUBIO_WRN("trying to write %d frames, but only %d can be written at a time",
94      write, s->max_frames);
95  }
96
97  /* interleaving data  */
98  for ( i = 0; i < channels; i++) {
99    pwrite = (smpl_t *)write_data->data;
100    for (j=0; j < write; j++) {
101      s->scratch_data[channels*j+i] = pwrite[j];
102    }
103  }
104
105  uint_t written = sf_write_float (s->handle, s->scratch_data, nsamples);
106  if (written/channels != write) {
107    AUBIO_WRN("trying to write %d frames to %s, but only %d could be written",
108      write, s->path, written);
109  }
110  return;
111}
112
113void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
114  if (!s) return;
115  if (sf_close(s->handle)) {
116    AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
117  }
118  AUBIO_FREE(s->scratch_data);
119  AUBIO_FREE(s);
120}
121
122#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.