source: src/io/sink_sndfile.c @ 2c3d4ca

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 2c3d4ca was 2c3d4ca, checked in by Paul Brossier <piem@piem.org>, 8 years ago

added sink_sndfile.c draft

  • Property mode set to 100644
File size: 3.0 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 hop_size;
37  uint_t samplerate;
38  uint_t channels;
39  char_t *path;
40  SNDFILE *handle;
41  uint_t scratch_size;
42  smpl_t *scratch_data;
43};
44
45aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) {
46  aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
47
48  if (path == NULL) {
49    AUBIO_ERR("Aborted opening null path\n");
50    return NULL;
51  }
52
53  s->hop_size = hop_size;
54  s->samplerate = samplerate;
55  s->channels = 1;
56  s->path = path;
57
58  /* set output format */
59  SF_INFO sfinfo;
60  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
61  sfinfo.samplerate = s->samplerate;
62  sfinfo.channels   = s->channels;
63  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
64
65  /* try creating the file */
66  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
67
68  if (s->handle == NULL) {
69    /* show libsndfile err msg */
70    AUBIO_ERR("Failed opening %s. %s\n", s->path, sf_strerror (NULL));
71    return NULL;
72  }     
73
74  s->scratch_size = s->hop_size*s->channels;
75  /* allocate data for de/interleaving reallocated when needed. */
76  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
77    AUBIO_ERR("%d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
78        s->hop_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
79    return NULL;
80  }
81  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
82
83  return s;
84}
85
86void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write, uint_t * written){
87  sf_count_t written_frames = 0;
88  int i, j,     channels = s->channels;
89  int nsamples = channels*(*written);
90  smpl_t *pwrite;
91
92  /* interleaving data  */
93  for ( i = 0; i < channels; i++) {
94    pwrite = (smpl_t *)write->data;
95    for (j=0; j < s->hop_size; j++) {
96      s->scratch_data[channels*j+i] = pwrite[j];
97    }
98  }
99  written_frames = sf_write_float (s->handle, s->scratch_data, nsamples);
100  *written = written_frames/channels;
101  return;
102}
103
104void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
105  if (!s) return;
106  if (sf_close(s->handle)) {
107    AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
108  }
109  AUBIO_FREE(s->scratch_data);
110  AUBIO_FREE(s);
111}
112
113#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.