1 /*
2 * Copyright (c) 2012 Stefano Sabatini
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 /**
24 * @file audio resampling API usage example
25 * @example resample_audio.c
26 *
27 * Generate a synthetic audio signal, and Use libswresample API to perform audio
28 * resampling. The output is written to a raw audio file to be played with
29 * ffplay.
30 */
31
36
39 {
41 struct sample_fmt_entry {
43 } sample_fmt_entries[] = {
49 };
51
53 struct sample_fmt_entry *
entry = &sample_fmt_entries[
i];
54 if (sample_fmt ==
entry->sample_fmt) {
56 return 0;
57 }
58 }
59
60 fprintf(stderr,
61 "Sample format %s not supported as output format\n",
64 }
65
66 /**
67 * Fill dst buffer with nb_samples, generated starting from t.
68 */
70 {
73 const double c = 2 *
M_PI * 440.0;
74
75 /* generate sin tone with 440Hz frequency and duplicated channels */
76 for (
i = 0;
i < nb_samples;
i++) {
78 for (j = 1; j < nb_channels; j++)
79 dstp[j] = dstp[0];
80 dstp += nb_channels;
81 *t += tincr;
82 }
83 }
84
85 int main(
int argc,
char **argv)
86 {
88 int src_rate = 48000, dst_rate = 44100;
89 uint8_t **src_data =
NULL, **dst_data =
NULL;
90 int src_nb_channels = 0, dst_nb_channels = 0;
91 int src_linesize, dst_linesize;
92 int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
94 const char *dst_filename =
NULL;
95 FILE *dst_file;
96 int dst_bufsize;
97 const char *fmt;
99 char buf[64];
100 double t;
102
103 if (argc != 2) {
104 fprintf(stderr, "Usage: %s output_file\n"
105 "API example program to show how to resample an audio stream with libswresample.\n"
106 "This program generates a series of audio frames, resamples them to a specified "
107 "output format and rate and saves them to an output file named output_file.\n",
108 argv[0]);
109 exit(1);
110 }
111 dst_filename = argv[1];
112
113 dst_file = fopen(dst_filename, "wb");
114 if (!dst_file) {
115 fprintf(stderr, "Could not open destination file %s\n", dst_filename);
116 exit(1);
117 }
118
119 /* create resampler context */
121 if (!swr_ctx) {
122 fprintf(stderr, "Could not allocate resampler context\n");
124 goto end;
125 }
126
127 /* set options */
131
135
136 /* initialize the resampling context */
138 fprintf(stderr, "Failed to initialize the resampling context\n");
139 goto end;
140 }
141
142 /* allocate source and destination samples buffers */
143
146 src_nb_samples, src_sample_fmt, 0);
148 fprintf(stderr, "Could not allocate source samples\n");
149 goto end;
150 }
151
152 /* compute the number of converted samples: buffering is avoided
153 * ensuring that the output buffer will contain at least all the
154 * converted input samples */
155 max_dst_nb_samples = dst_nb_samples =
157
158 /* buffer is going to be directly written to a rawaudio file, no alignment */
159 dst_nb_channels = dst_ch_layout.nb_channels;
161 dst_nb_samples, dst_sample_fmt, 0);
163 fprintf(stderr, "Could not allocate destination samples\n");
164 goto end;
165 }
166
167 t = 0;
168 do {
169 /* generate synthetic audio */
170 fill_samples((
double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
171
172 /* compute destination number of samples */
175 if (dst_nb_samples > max_dst_nb_samples) {
178 dst_nb_samples, dst_sample_fmt, 1);
180 break;
181 max_dst_nb_samples = dst_nb_samples;
182 }
183
184 /* convert to destination format */
185 ret =
swr_convert(swr_ctx, dst_data, dst_nb_samples, (
const uint8_t **)src_data, src_nb_samples);
187 fprintf(stderr, "Error while converting\n");
188 goto end;
189 }
191 ret, dst_sample_fmt, 1);
192 if (dst_bufsize < 0) {
193 fprintf(stderr, "Could not get sample buffer size\n");
194 goto end;
195 }
196 printf(
"t:%f in:%d out:%d\n", t, src_nb_samples,
ret);
197 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
198 } while (t < 10);
199
201 goto end;
203 fprintf(stderr, "Resampling succeeded. Play the output file with the command:\n"
204 "ffplay -f %s -channel_layout %s -channels %d -ar %d %s\n",
205 fmt, buf, dst_nb_channels, dst_rate, dst_filename);
206
207 end:
208 fclose(dst_file);
209
210 if (src_data)
213
214 if (dst_data)
217
220 }