source: src/io/sndfileio.c @ 927f630

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

src/io: moved sndfileio from examples

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[96fb8ad]1/*
[6a2478c]2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[6a2478c]4  This file is part of aubio.
[96fb8ad]5
[6a2478c]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.
[96fb8ad]10
[6a2478c]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/>.
[96fb8ad]18
19*/
20
[6f67361]21#include "config.h"
22
[1081580]23#ifdef HAVE_SNDFILE
24
[96fb8ad]25#include <string.h>
26
27#include <sndfile.h>
28
29#include "aubio_priv.h"
[6c7d49b]30#include "fvec.h"
[96fb8ad]31#include "sndfileio.h"
32#include "mathutils.h"
33
34#define MAX_CHANNELS 6
35#define MAX_SIZE 4096
36
[5e9c68a]37struct _aubio_sndfile_t {
[96fb8ad]38        SNDFILE *handle;
39        int samplerate;
40        int channels;
41        int format;
42        float *tmpdata; /** scratch pad for interleaving/deinterleaving. */
43        int size;       /** store the size to check if realloc needed */
44};
45
[5e9c68a]46aubio_sndfile_t * new_aubio_sndfile_ro(const char* outputname) {
47        aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t);
[96fb8ad]48        SF_INFO sfinfo;
49        AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
50
[e83c895]51        f->handle = sf_open (outputname, SFM_READ, &sfinfo);
52
53        if (f->handle == NULL) {
[6c25201]54                AUBIO_ERR("Failed opening %s: %s\n", outputname,
55                        sf_strerror (NULL)); /* libsndfile err msg */
[f382ac6]56                return NULL;
[96fb8ad]57        }       
58
59        if (sfinfo.channels > MAX_CHANNELS) { 
60                AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
[f382ac6]61                return NULL;
[96fb8ad]62        }
63
64        f->size       = MAX_SIZE*sfinfo.channels;
65        f->tmpdata    = AUBIO_ARRAY(float,f->size);
66        /* get input specs */
67        f->samplerate = sfinfo.samplerate;
68        f->channels   = sfinfo.channels;
69        f->format     = sfinfo.format;
70
71        return f;
72}
73
[5e9c68a]74int aubio_sndfile_open_wo(aubio_sndfile_t * f, const char* inputname) {
[96fb8ad]75        SF_INFO sfinfo;
[e83c895]76        AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
[96fb8ad]77
78        /* define file output spec */
79        sfinfo.samplerate = f->samplerate;
80        sfinfo.channels   = f->channels;
81        sfinfo.format     = f->format;
82
83        if (! (f->handle = sf_open (inputname, SFM_WRITE, &sfinfo))) {
84                AUBIO_ERR("Not able to open output file %s.\n", inputname);
85                AUBIO_ERR("%s\n",sf_strerror (NULL)); /* libsndfile err msg */
86                AUBIO_QUIT(AUBIO_FAIL);
87        }       
88
89        if (sfinfo.channels > MAX_CHANNELS) { 
90                AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
91                AUBIO_QUIT(AUBIO_FAIL);
92        }
93        f->size       = MAX_SIZE*sfinfo.channels;
94        f->tmpdata    = AUBIO_ARRAY(float,f->size);
95        return AUBIO_OK;
96}
97
98/* setup file struct from existing one */
[5e9c68a]99aubio_sndfile_t * new_aubio_sndfile_wo(aubio_sndfile_t * fmodel, const char *outputname) {
100        aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t);
[96fb8ad]101        f->samplerate    = fmodel->samplerate;
[4621cd6]102        f->channels      = 1; //fmodel->channels;
[96fb8ad]103        f->format        = fmodel->format;
[5e9c68a]104        aubio_sndfile_open_wo(f, outputname);
[96fb8ad]105        return f;
106}
107
108
109/* return 0 if properly closed, 1 otherwise */
[5e9c68a]110int del_aubio_sndfile(aubio_sndfile_t * f) {
[96fb8ad]111        if (sf_close(f->handle)) {
112                AUBIO_ERR("Error closing file.");
113                puts (sf_strerror (NULL));
114                return 1;
115        }
116        AUBIO_FREE(f->tmpdata);
117        AUBIO_FREE(f);
118        //AUBIO_DBG("File closed.\n");
119        return 0;
120}
121
122/**************************************************************
123 *
124 * Read write methods
125 *
126 */
127
128
129/* read frames from file in data
130 *  return the number of frames actually read */
[4621cd6]131int aubio_sndfile_read(aubio_sndfile_t * f, int frames, fvec_t ** read) {
[96fb8ad]132        sf_count_t read_frames;
133        int i,j, channels = f->channels;
134        int nsamples = frames*channels;
135        int aread;
[d69e37d]136        smpl_t *pread; 
[96fb8ad]137
138        /* allocate data for de/interleaving reallocated when needed. */
139        if (nsamples >= f->size) {
[5e9c68a]140                AUBIO_ERR("Maximum aubio_sndfile_read buffer size exceeded.");
[96fb8ad]141                return -1;
142                /*
143                AUBIO_FREE(f->tmpdata);
144                f->tmpdata = AUBIO_ARRAY(float,nsamples);
145                */
146        }
147        //f->size = nsamples;
148
149        /* do actual reading */
150        read_frames = sf_read_float (f->handle, f->tmpdata, nsamples);
151
152        aread = (int)FLOOR(read_frames/(float)channels);
153
154        /* de-interleaving data  */
155        for (i=0; i<channels; i++) {
[4621cd6]156                pread = (smpl_t *)fvec_get_data(read[i]);
[96fb8ad]157                for (j=0; j<aread; j++) {
[d69e37d]158                        pread[j] = (smpl_t)f->tmpdata[channels*j+i];
[96fb8ad]159                }
160        }
161        return aread;
162}
163
[4621cd6]164int
165aubio_sndfile_read_mono (aubio_sndfile_t * f, int frames, fvec_t * read)
166{
167  sf_count_t read_frames;
168  int i, j, channels = f->channels;
169  int nsamples = frames * channels;
170  int aread;
171  smpl_t *pread;
172
173  /* allocate data for de/interleaving reallocated when needed. */
174  if (nsamples >= f->size) {
175    AUBIO_ERR ("Maximum aubio_sndfile_read buffer size exceeded.");
176    return -1;
177    /*
178    AUBIO_FREE(f->tmpdata);
179    f->tmpdata = AUBIO_ARRAY(float,nsamples);
180    */
181  }
182  //f->size = nsamples;
183
184  /* do actual reading */
185  read_frames = sf_read_float (f->handle, f->tmpdata, nsamples);
186
187  aread = (int) FLOOR (read_frames / (float) channels);
188
189  /* de-interleaving data  */
190  pread = (smpl_t *) fvec_get_data (read);
191  for (i = 0; i < channels; i++) {
192    for (j = 0; j < aread; j++) {
193      pread[j] += (smpl_t) f->tmpdata[channels * j + i];
194    }
195  }
196  for (j = 0; j < aread; j++) {
197    pread[j] /= (smpl_t)channels;
198  }
199
200  return aread;
201}
202
[96fb8ad]203/* write 'frames' samples to file from data
204 *   return the number of frames actually written
205 */
[4621cd6]206int aubio_sndfile_write(aubio_sndfile_t * f, int frames, fvec_t ** write) {
[96fb8ad]207        sf_count_t written_frames = 0;
208        int i, j,       channels = f->channels;
209        int nsamples = channels*frames;
[d69e37d]210        smpl_t *pwrite;
[96fb8ad]211
212        /* allocate data for de/interleaving reallocated when needed. */
213        if (nsamples >= f->size) {
[5e9c68a]214                AUBIO_ERR("Maximum aubio_sndfile_write buffer size exceeded.");
[96fb8ad]215                return -1;
216                /*
217                AUBIO_FREE(f->tmpdata);
218                f->tmpdata = AUBIO_ARRAY(float,nsamples);
219                */
220        }
221        //f->size = nsamples;
222
223        /* interleaving data  */
224        for (i=0; i<channels; i++) {
[4621cd6]225                pwrite = (smpl_t *)fvec_get_data(write[i]);
[96fb8ad]226                for (j=0; j<frames; j++) {
[d69e37d]227                        f->tmpdata[channels*j+i] = (float)pwrite[j];
[96fb8ad]228                }
229        }
230        written_frames = sf_write_float (f->handle, f->tmpdata, nsamples);
231        return written_frames/channels;
232}
233
234/*******************************************************************
235 *
236 * Get object info
237 *
238 */
239
[5e9c68a]240uint_t aubio_sndfile_channels(aubio_sndfile_t * f) {
[96fb8ad]241        return f->channels;
242}
243
[5e9c68a]244uint_t aubio_sndfile_samplerate(aubio_sndfile_t * f) {
[96fb8ad]245        return f->samplerate;
246}
247
[5e9c68a]248void aubio_sndfile_info(aubio_sndfile_t * f) {
[96fb8ad]249        AUBIO_DBG("srate    : %d\n", f->samplerate);
250        AUBIO_DBG("channels : %d\n", f->channels);
251        AUBIO_DBG("format   : %d\n", f->format);
252}
253
[1081580]254#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.