File: libSndFileWrapper.H

package info (click to toggle)
mffm-libsndfilew 1.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 128 kB
  • ctags: 13
  • sloc: cpp: 139; makefile: 57
file content (205 lines) | stat: -rw-r--r-- 5,853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Copyright 2001 Matt Flax <flatmax@ieee.org>
   This file is part of MFFM Time Scale Modification for Audio.

   MFFM Time Scale Modification for Audio is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   MFFM Time Scale Modification for Audio is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   A copy of the GPL license is available here :
   http://www.gnu.org/licenses/licenses.html#GPL
   http://www.gnu.org/licenses/gpl.html
 */

/*
Version 1.2
*/

#ifndef LIBSNDFILEWRAPPER_H_
#define LIBSNDFILEWRAPPER_H_
/*
  C++ Wrapper for the libsndfile library
  http://www.zip.com.au/~erikd/libsndfile/
  Author: Matt Flax <flatmax@ieee.org>
*/

#include <sndfile.h>
#include <string.h>

//#define DEBUG_LSFW
#ifdef DEBUG_LSFW
#include <iostream>
#endif


/** Opens a file for reading, on failure returns NULL */
SNDFILE* openAudioRead(const char *name, SF_INFO *sfinfo){
  sfinfo->format=0;
  SNDFILE *file=sf_open(name, SFM_READ,sfinfo);
  if (file==NULL){
    perror("couldn't open input file\n");
    sf_perror(file);
    return NULL;
  }
  return file;
}

/** Opens a WAV file for writing, on failure returns NULL */
SNDFILE* openWavWrite(SF_INFO *sfInfo, const char *name, int freq, int channelCnt, int bitCnt){
  printf("libSndFileWrapper::openWavWrite : opening %s\n %dHz\t%d channel(s)\t%d bits per word\n", name, freq, channelCnt, bitCnt);
    sfInfo->samplerate=freq;
    sfInfo->channels=channelCnt;
    /*    sfInfo->pcmbitwidth=bitCnt; from version 0*/
    switch (bitCnt){
    case 8:
      sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_S8);
    case 16:
      sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_16);
    case 24:
      sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_24);
    case 32:
      sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_32);
    default:
      sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_16);     
    }

    return sf_open(name, SFM_WRITE, sfInfo);
}

/** Opens an AIFF sound file for writing, on failure returns NULL */
SNDFILE* openAiffWrite(SF_INFO *sfInfo, const char *name, int freq, int channelCnt, int bitCnt){
  printf("libSndFileWrapper::openAiffWrite : opening %s\n %dHz\t%d channel(s)\t%d bits per word\n", name, freq, channelCnt, bitCnt);
  sfInfo->samplerate=freq;
    sfInfo->channels=channelCnt;
    switch (bitCnt){
    case 8:
      sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_S8);
    case 16:
      sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_16);
    case 24:
      sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_24);
    case 32:
      sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_32);
    default:
      sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_16);     
    }
    return sf_open(name, SFM_WRITE, sfInfo);
}

/**Closes a sound file*/
void closeSndFile(SNDFILE* file){
  sf_close(file);
}

/** writes N samples to file
   returns number written */
int writeNSndFile(SNDFILE *file, int N, short int *output){
  int written;
#ifdef DEBUG_LSFW
  printf("about to write : %d\n",N);
#endif
  /*sfInfo->samples+=N;*/
  written=sf_write_short(file, output, N);
#ifdef DEBUG_LSFW
  printf("written : %d\n",written);
#endif
  return written;
}

/** writes an entire wav file */
void writeWav(const char *name, int cnt, short int *output, int freq, int channelCnt, int bitCnt){
  /*Write out to file ...*/
  int written;
  SF_INFO sfInfo;
  memset(&sfInfo, 0, sizeof (sfInfo)) ;
  SNDFILE *file=openWavWrite(&sfInfo, name, freq, channelCnt, bitCnt);
  if (file==NULL){
    perror("couldn't open output file\n");
    sf_perror(file);
    exit(-1);
  }
  sfInfo.frames=cnt;
#ifdef DEBUG_LSFW
  printf("about to write : %d\n",cnt);
#endif
  written=sf_write_short(file, output, channelCnt*cnt);
  /*  printf("written : %d\n",written);*/
#ifdef DEBUG_LSFW
  printf("written : %d\n",written);
#endif
  closeSndFile(file);
}

/** Read N (cnt) units from the WAV file to a supplied array, returns the number read.
*/
int readNWav(int start, int cnt, int channels, SNDFILE *file, short int* input){
  /*Read from file ...*/
  int readn;
  
  if (file==NULL){
    perror("supplied input file handle is NULL\n");
    sf_perror(file);
    exit(-1);
  }

  //locate to the correct file location
  if (sf_seek  (file, start, SEEK_SET)<0){
    perror("seek error. Tried to seek to loction ");
    sf_perror(file);
    return -1;
  }

#ifdef DEBUG_LSFW
  printf("about to read N= %d\n",cnt);
#endif
  readn=sf_read_short(file, input, cnt*channels) ;
#ifdef DEBUG_LSFW
  printf("read : %d\n",readn);
#endif
  return readn/channels;
}


/** Read an entire WAV file, allocating the memory array returned.
    You must de-allocate yourself.
*/
short int* readWav(const char *name, int *cnt, int *freq, int *channels){
  /*Read from file ...*/
  short int *input;
  int readn;
  SF_INFO sfinfo;
  sfinfo.format=0;
  SNDFILE *file=sf_open(name, SFM_READ,&sfinfo);
  if (file==NULL){
    perror("couldn't open input file\n");
    sf_perror(file);
    exit(-1);
  }

  *cnt=sfinfo.frames;
  *freq=sfinfo.samplerate;
  *channels=sfinfo.channels;

  input=(short int *)malloc(sizeof(short int)*(*cnt)*(*channels));
  if (!input){
    perror("readWav:: input malloc error - out of memory ?");
    printf("\t requested %d bytes",sizeof(short int)*(*cnt)*(*channels));
    exit(-1);
  }

#ifdef DEBUG_LSFW
  printf("about to read : %d\n",*cnt);
#endif
  readn=sf_read_short(file, input, (*cnt)*(*channels)) ;
#ifdef DEBUG_LSFW
  printf("read : %d\n",readn);
#endif
  closeSndFile(file);
  return input;
}
#endif /*LIBSNDFILEWRAPPER_H_*/