1 /**********************************************************************
5 Written by Dominic Mazzoni
8 http://www-ccrma.stanford.edu/~jos/resample/
10 License: LGPL - see the file LICENSE.txt for more information
12 **********************************************************************/
14 #include "../include/libresample.h"
25 #define MIN(A, B) (A) < (B)? (A) : (B)
27 void usage(char *progname
)
29 fprintf(stderr
, "Usage: %s -by <ratio> <input> <output>\n", progname
);
30 fprintf(stderr
, " %s -to <rate> <input> <output>\n", progname
);
31 fprintf(stderr
, "\n");
35 int main(int argc
, char **argv
)
37 SNDFILE
*srcfile
, *dstfile
;
38 SF_INFO srcinfo
, dstinfo
;
39 SF_FORMAT_INFO formatinfo
;
49 struct timeval tv0
, tv1
;
52 int pos
, bufferpos
, outcount
;
58 if (!strcmp(argv
[1], "-by")) {
59 ratio
= atof(argv
[2]);
61 fprintf(stderr
, "Ratio of %f is illegal\n", ratio
);
65 else if (!strcmp(argv
[1], "-to")) {
66 dstrate
= atof(argv
[2]);
67 if (dstrate
< 10.0 || dstrate
> 100000.0) {
68 fprintf(stderr
, "Sample rate of %f is illegal\n", dstrate
);
75 memset(&srcinfo
, 0, sizeof(srcinfo
));
76 memset(&dstinfo
, 0, sizeof(dstinfo
));
77 srcfile
= sf_open(argv
[3], SFM_READ
, &srcinfo
);
79 fprintf(stderr
, "%s", sf_strerror(NULL
));
83 srcrate
= srcinfo
.samplerate
;
85 dstrate
= srcrate
* ratio
;
87 ratio
= dstrate
/ srcrate
;
89 channels
= srcinfo
.channels
;
91 /* figure out format of destination file */
93 extension
= strstr(argv
[4], ".");
96 sf_command(NULL
, SFC_GET_FORMAT_MAJOR_COUNT
,
97 &numformats
, sizeof(numformats
));
98 for(i
=0; i
<numformats
; i
++) {
99 memset(&formatinfo
, 0, sizeof(formatinfo
));
100 formatinfo
.format
= i
;
101 sf_command(NULL
, SFC_GET_FORMAT_MAJOR
,
102 &formatinfo
, sizeof(formatinfo
));
103 if (!strcmp(formatinfo
.extension
, extension
)) {
104 printf("Using %s for output format.\n", formatinfo
.name
);
105 dstinfo
.format
= formatinfo
.format
|
106 (srcinfo
.format
& SF_FORMAT_SUBMASK
);
112 if (!dstinfo
.format
) {
114 printf("Warning: output format (%s) not recognized, "
115 "using same as input format.\n",
117 dstinfo
.format
= srcinfo
.format
;
120 dstinfo
.samplerate
= (int)(dstrate
+ 0.5);
121 dstinfo
.channels
= channels
;
123 dstfile
= sf_open(argv
[4], SFM_WRITE
, &dstinfo
);
125 fprintf(stderr
, "%s", sf_strerror(NULL
));
129 printf("Source: %s (%d frames, %.2f Hz)\n",
130 argv
[3], (int)srcinfo
.frames
, srcrate
);
131 printf("Destination: %s (%.2f Hz, ratio=%.5f)\n", argv
[4],
135 dstlen
= (srclen
* ratio
+ 1000);
136 srci
= (float *)malloc(srclen
* channels
* sizeof(float));
137 dsti
= (float *)malloc(dstlen
* channels
* sizeof(float));
138 src
= (float *)malloc(srclen
* sizeof(float));
139 dst
= (float *)malloc(dstlen
* sizeof(float));
141 handle
= (void **)malloc(channels
* sizeof(void *));
142 for(c
=0; c
<channels
; c
++)
143 handle
[c
] = resample_open(1, ratio
, ratio
);
145 gettimeofday(&tv0
, NULL
);
150 while(pos
< srcinfo
.frames
) {
151 int block
= MIN(srclen
-bufferpos
, srcinfo
.frames
-pos
);
152 int lastFlag
= (pos
+block
== srcinfo
.frames
);
153 int inUsed
, inUsed2
=0, out
=0, out2
=0;
155 sf_readf_float(srcfile
, &srci
[bufferpos
*channels
], block
);
158 for(c
=0; c
<channels
; c
++) {
159 for(i
=0; i
<block
; i
++)
160 src
[i
] = srci
[i
*channels
+c
];
163 out
= resample_process(handle
[c
], ratio
, src
, block
, lastFlag
,
164 &inUsed
, dst
, dstlen
);
170 if (inUsed2
!= inUsed
|| out2
!= out
) {
171 fprintf(stderr
, "Fatal error: channels out of sync!\n");
179 dsti
[i
*channels
+c
] = -1;
181 dsti
[i
*channels
+c
] = 1;
183 dsti
[i
*channels
+c
] = dst
[i
];
187 sf_writef_float(dstfile
, dsti
, out
);
189 bufferpos
= block
- inUsed
;
190 for(i
=0; i
<bufferpos
*channels
; i
++)
191 srci
[i
] = srci
[i
+(inUsed
*channels
)];
199 gettimeofday(&tv1
, NULL
);
201 (tv1
.tv_sec
+ tv1
.tv_usec
* 0.000001) -
202 (tv0
.tv_sec
+ tv0
.tv_usec
* 0.000001);
204 printf("Elapsed time: %.3f seconds\n", deltat
);
205 printf("%d frames written to output file\n", outcount
);