source: src/io/source_sndfile.c @ 9b1e101

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

src/io/source_sndfile.{c,h}: added sndfile source

  • Property mode set to 100644
File size: 3.4 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 "source_sndfile.h"
30#include "fvec.h"
31#include "mathutils.h"
32
33#define MAX_CHANNELS 6
34#define MAX_SIZE 4096
35
36struct _aubio_source_sndfile_t {
37  uint_t hop_size;
38  uint_t samplerate;
39  uint_t channels;
40  int input_samplerate;
41  int input_channels;
42  int input_format;
43  char_t *path;
44  SNDFILE *handle;
45  uint_t scratch_size;
46  smpl_t *scratch_data;
47};
48
49aubio_source_sndfile_t * new_aubio_source_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) {
50  aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t);
51
52  if (path == NULL) {
53    AUBIO_ERR("Aborted opening null path\n");
54    return NULL;
55  }
56
57  s->hop_size = hop_size;
58  s->samplerate = samplerate;
59  s->channels = 1;
60  s->path = path;
61
62  // try opening the file, geting the info in sfinfo
63  SF_INFO sfinfo;
64  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
65  s->handle = sf_open (s->path, SFM_READ, &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  if (sfinfo.channels > MAX_CHANNELS) { 
74    AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
75    return NULL;
76  }
77
78  /* get input specs */
79  s->input_samplerate = sfinfo.samplerate;
80  s->input_channels   = sfinfo.channels;
81  s->input_format     = sfinfo.format;
82
83  if (s->samplerate != s->input_samplerate) {
84    AUBIO_ERR("resampling not implemented yet\n");
85    return NULL;
86  }
87 
88  s->scratch_size = s->hop_size*s->input_channels;
89  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
90
91  /* allocate data for de/interleaving reallocated when needed. */
92  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
93    AUBIO_ERR("%d exceeds maximum aubio_source_sndfile buffer size %d\n", s->scratch_size, MAX_CHANNELS * MAX_CHANNELS);
94    return NULL;
95  }
96
97  return s;
98}
99
100void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
101  sf_count_t read_frames;
102  int i,j, input_channels = s->input_channels;
103  int aread;
104  smpl_t *pread;       
105
106  /* do actual reading */
107  read_frames = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
108
109  aread = (int)FLOOR(read_frames/(float)input_channels);
110
111  /* de-interleaving and down-mixing data  */
112  for (j = 0; j < aread; j++) {
113    read_data->data[j] = 0;
114    for (i = 0; i < input_channels; i++) {
115      read_data->data[j] += (smpl_t)s->scratch_data[input_channels*j+i];
116    }
117    read_data->data[j] /= (smpl_t)input_channels;
118  }
119  *read = aread;
120}
121
122void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
123  if (sf_close(s->handle)) {
124    AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
125  }
126  AUBIO_FREE(s->scratch_data);
127  AUBIO_FREE(s);
128}
129
130#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.