File: libSndFileWrapper_8H-source.html

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 (176 lines) | stat: -rw-r--r-- 11,794 bytes parent folder | download | duplicates (3)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>libSndFileWrapper.H Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.18 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; </center>
<hr><h1>libSndFileWrapper.H</h1><div class="fragment"><pre>00001 <span class="comment">/* Copyright 2001 Matt Flax &lt;flatmax@ieee.org&gt;</span>
00002 <span class="comment">   This file is part of MFFM Time Scale Modification for Audio.</span>
00003 <span class="comment"></span>
00004 <span class="comment">   MFFM Time Scale Modification for Audio is free software; you can redistribute it and/or modify</span>
00005 <span class="comment">   it under the terms of the GNU General Public License as published by</span>
00006 <span class="comment">   the Free Software Foundation; either version 2 of the License, or</span>
00007 <span class="comment">   (at your option) any later version.</span>
00008 <span class="comment">   </span>
00009 <span class="comment">   MFFM Time Scale Modification for Audio is distributed in the hope that it will be useful,</span>
00010 <span class="comment">   but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
00011 <span class="comment">   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
00012 <span class="comment">   GNU General Public License for more details.</span>
00013 <span class="comment">   </span>
00014 <span class="comment">   A copy of the GPL license is available here :</span>
00015 <span class="comment">   http://www.gnu.org/licenses/licenses.html#GPL</span>
00016 <span class="comment">   http://www.gnu.org/licenses/gpl.html</span>
00017 <span class="comment"> */</span>
00018 
00019 <span class="comment">/*</span>
00020 <span class="comment">Version 1.1</span>
00021 <span class="comment">*/</span>
00022 
00023 <span class="preprocessor">#ifndef LIBSNDFILEWRAPPER_H_</span>
00024 <span class="preprocessor"></span><span class="preprocessor">#define LIBSNDFILEWRAPPER_H_</span>
00025 <span class="preprocessor"></span><span class="comment">/*</span>
00026 <span class="comment">  C++ Wrapper for the libsndfile library</span>
00027 <span class="comment">  http://www.zip.com.au/~erikd/libsndfile/</span>
00028 <span class="comment">  Author: Matt Flax &lt;flatmax@ieee.org&gt;</span>
00029 <span class="comment">*/</span>
00030 
00031 <span class="preprocessor">#include &lt;sndfile.h&gt;</span>
00032 <span class="preprocessor">#include &lt;string.h&gt;</span>
00033 
00034 <span class="comment">/*#define DEBUG_LSFW*/</span>
00035 <span class="preprocessor">#ifdef DEBUG_LSFW</span>
00036 <span class="preprocessor"></span><span class="preprocessor">#include &lt;iostream&gt;</span>
00037 <span class="preprocessor">#endif</span>
00038 <span class="preprocessor"></span><span class="comment"></span>
00039 <span class="comment">/** Opens a WAV file for writing, on failure returns NULL */</span>
00040 SNDFILE* openWavWrite(SF_INFO *sfInfo, <span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keywordtype">int</span> freq, <span class="keywordtype">int</span> channelCnt, <span class="keywordtype">int</span> bitCnt){
00041   printf(<span class="stringliteral">"libSndFileWrapper::openWavWrite : opening %s\n %dHz\t%d channel(s)\t%d bits per word\n"</span>, name, freq, channelCnt, bitCnt);
00042     sfInfo-&gt;samplerate=freq;
00043     sfInfo-&gt;channels=channelCnt;
00044     <span class="comment">/*    sfInfo-&gt;pcmbitwidth=bitCnt; from version 0*/</span>
00045     <span class="keywordflow">switch</span> (bitCnt){
00046     <span class="keywordflow">case</span> 8:
00047       sfInfo-&gt;format=(SF_FORMAT_WAV | SF_FORMAT_PCM_S8);
00048     <span class="keywordflow">case</span> 16:
00049       sfInfo-&gt;format=(SF_FORMAT_WAV | SF_FORMAT_PCM_16);
00050     <span class="keywordflow">case</span> 24:
00051       sfInfo-&gt;format=(SF_FORMAT_WAV | SF_FORMAT_PCM_24);
00052     <span class="keywordflow">case</span> 32:
00053       sfInfo-&gt;format=(SF_FORMAT_WAV | SF_FORMAT_PCM_32);
00054     <span class="keywordflow">default</span>:
00055       sfInfo-&gt;format=(SF_FORMAT_WAV | SF_FORMAT_PCM_16);     
00056     }
00057 
00058     <span class="keywordflow">return</span> sf_open(name, SFM_WRITE, sfInfo);
00059 }
00060 <span class="comment"></span>
00061 <span class="comment">/** Opens an AIFF sound file for writing, on failure returns NULL */</span>
00062 SNDFILE* openAiffWrite(SF_INFO *sfInfo, <span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keywordtype">int</span> freq, <span class="keywordtype">int</span> channelCnt, <span class="keywordtype">int</span> bitCnt){
00063   printf(<span class="stringliteral">"libSndFileWrapper::openAiffWrite : opening %s\n %dHz\t%d channel(s)\t%d bits per word\n"</span>, name, freq, channelCnt, bitCnt);
00064   sfInfo-&gt;samplerate=freq;
00065     sfInfo-&gt;channels=channelCnt;
00066     <span class="keywordflow">switch</span> (bitCnt){
00067     <span class="keywordflow">case</span> 8:
00068       sfInfo-&gt;format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_S8);
00069     <span class="keywordflow">case</span> 16:
00070       sfInfo-&gt;format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_16);
00071     <span class="keywordflow">case</span> 24:
00072       sfInfo-&gt;format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_24);
00073     <span class="keywordflow">case</span> 32:
00074       sfInfo-&gt;format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_32);
00075     <span class="keywordflow">default</span>:
00076       sfInfo-&gt;format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_16);     
00077     }
00078     <span class="keywordflow">return</span> sf_open(name, SFM_WRITE, sfInfo);
00079 }
00080 <span class="comment"></span>
00081 <span class="comment">/**Closes a sound file*/</span>
00082 <span class="keywordtype">void</span> closeSndFile(SNDFILE* file){
00083   sf_close(file);
00084 }
00085 <span class="comment"></span>
00086 <span class="comment">/** writes N samples to file</span>
00087 <span class="comment">   returns number written */</span>
00088 <span class="keywordtype">int</span> writeNSndFile(SNDFILE *file, <span class="keywordtype">int</span> N, <span class="keywordtype">short</span> <span class="keywordtype">int</span> *output){
00089   <span class="keywordtype">int</span> written;
00090 <span class="preprocessor">#ifdef DEBUG_LSFW</span>
00091 <span class="preprocessor"></span>  printf(<span class="stringliteral">"about to write : %d\n"</span>,N);
00092 <span class="preprocessor">#endif</span>
00093 <span class="preprocessor"></span>  <span class="comment">/*sfInfo-&gt;samples+=N;*/</span>
00094   written=sf_write_short(file, output, N);
00095 <span class="preprocessor">#ifdef DEBUG_LSFW</span>
00096 <span class="preprocessor"></span>  printf(<span class="stringliteral">"written : %d\n"</span>,written);
00097 <span class="preprocessor">#endif</span>
00098 <span class="preprocessor"></span>  <span class="keywordflow">return</span> written;
00099 }
00100 <span class="comment"></span>
00101 <span class="comment">/** writes an entire wav file */</span>
00102 <span class="keywordtype">void</span> writeWav(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keywordtype">int</span> cnt, <span class="keywordtype">short</span> <span class="keywordtype">int</span> *output, <span class="keywordtype">int</span> freq, <span class="keywordtype">int</span> channelCnt, <span class="keywordtype">int</span> bitCnt){
00103   <span class="comment">/*Write out to file ...*/</span>
00104   <span class="keywordtype">int</span> written;
00105   SF_INFO sfInfo;
00106   memset(&amp;sfInfo, 0, <span class="keyword">sizeof</span> (sfInfo)) ;
00107   SNDFILE *file=openWavWrite(&amp;sfInfo, name, freq, channelCnt, bitCnt);
00108   <span class="keywordflow">if</span> (file==NULL){
00109     perror(<span class="stringliteral">"couldn't open output file\n"</span>);
00110     sf_perror(file);
00111     exit(-1);
00112   }
00113   sfInfo.frames=cnt;
00114 <span class="preprocessor">#ifdef DEBUG_LSFW</span>
00115 <span class="preprocessor"></span>  printf(<span class="stringliteral">"about to write : %d\n"</span>,cnt);
00116 <span class="preprocessor">#endif</span>
00117 <span class="preprocessor"></span>  written=sf_write_short(file, output, channelCnt*cnt);
00118   <span class="comment">/*  printf("written : %d\n",written);*/</span>
00119 <span class="preprocessor">#ifdef DEBUG_LSFW</span>
00120 <span class="preprocessor"></span>  printf(<span class="stringliteral">"written : %d\n"</span>,written);
00121 <span class="preprocessor">#endif</span>
00122 <span class="preprocessor"></span>  closeSndFile(file);
00123 }
00124 <span class="comment"></span>
00125 <span class="comment">/** Read an entire WAV file, allocating the memory array returned.</span>
00126 <span class="comment">    You must de-allocate yourself.</span>
00127 <span class="comment">*/</span>
00128 <span class="keywordtype">short</span> <span class="keywordtype">int</span>* readWav(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keywordtype">int</span> *cnt, <span class="keywordtype">int</span> *freq, <span class="keywordtype">int</span> *channels){
00129   <span class="comment">/*Read from file ...*/</span>
00130   <span class="keywordtype">short</span> <span class="keywordtype">int</span> *input;
00131   <span class="keywordtype">int</span> readn;
00132   SF_INFO sfinfo;
00133   sfinfo.format=0;
00134   SNDFILE *file=sf_open(name, SFM_READ,&amp;sfinfo);
00135   <span class="keywordflow">if</span> (file==NULL){
00136     perror(<span class="stringliteral">"couldn't open input file\n"</span>);
00137     sf_perror(file);
00138     exit(-1);
00139   }
00140 
00141   *cnt=sfinfo.frames;
00142   *freq=sfinfo.samplerate;
00143   *channels=sfinfo.channels;
00144 
00145   input=(<span class="keywordtype">short</span> <span class="keywordtype">int</span> *)malloc(<span class="keyword">sizeof</span>(<span class="keywordtype">short</span> <span class="keywordtype">int</span>)*(*cnt)*(*channels));
00146   <span class="keywordflow">if</span> (!input){
00147     perror(<span class="stringliteral">"readWav:: input malloc error - out of memory ?"</span>);
00148     printf(<span class="stringliteral">"\t requested %d bytes"</span>,<span class="keyword">sizeof</span>(<span class="keywordtype">short</span> <span class="keywordtype">int</span>)*(*cnt)*(*channels));
00149     exit(-1);
00150   }
00151 
00152 <span class="preprocessor">#ifdef DEBUG_LSFW</span>
00153 <span class="preprocessor"></span>  printf(<span class="stringliteral">"about to read : %d\n"</span>,*cnt);
00154 <span class="preprocessor">#endif</span>
00155 <span class="preprocessor"></span>  readn=sf_read_short(file, input, (*cnt)*(*channels)) ;
00156 <span class="preprocessor">#ifdef DEBUG_LSFW</span>
00157 <span class="preprocessor"></span>  printf(<span class="stringliteral">"read : %d\n"</span>,readn);
00158 <span class="preprocessor">#endif</span>
00159 <span class="preprocessor"></span>  closeSndFile(file);
00160   <span class="keywordflow">return</span> input;
00161 }
00162 <span class="preprocessor">#endif </span><span class="comment">/*LIBSNDFILEWRAPPER_H_*/</span>
</pre></div><hr><address style="align: right;"><small>Generated on Fri Aug 22 10:28:37 2003 for MFFM libsndfile wrapper by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.18 </small></address>
</body>
</html>