Generate a synthetic audio signal, and Use libswresample API to perform audio resampling. The output is written to a raw audio file to be played with ffplay.
{
struct sample_fmt_entry {
} sample_fmt_entries[] = {
};
struct sample_fmt_entry *
entry = &sample_fmt_entries[
i];
if (sample_fmt ==
entry->sample_fmt) {
return 0;
}
}
fprintf(stderr,
"Sample format %s not supported as output format\n",
}
static void fill_samples(
double *
dst,
int nb_samples,
int nb_channels,
int sample_rate,
double *t)
{
double tincr = 1.0 / sample_rate, *dstp =
dst;
const double c = 2 *
M_PI * 440.0;
for (
i = 0;
i < nb_samples;
i++) {
for (j = 1; j < nb_channels; j++)
dstp[j] = dstp[0];
dstp += nb_channels;
*t += tincr;
}
}
int main(
int argc,
char **argv)
{
int src_rate = 48000, dst_rate = 44100;
uint8_t **src_data =
NULL, **dst_data =
NULL;
int src_nb_channels = 0, dst_nb_channels = 0;
int src_linesize, dst_linesize;
int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
const char *dst_filename =
NULL;
FILE *dst_file;
int dst_bufsize;
const char *fmt;
char buf[64];
double t;
if (argc != 2) {
fprintf(stderr, "Usage: %s output_file\n"
"API example program to show how to resample an audio stream with libswresample.\n"
"This program generates a series of audio frames, resamples them to a specified "
"output format and rate and saves them to an output file named output_file.\n",
argv[0]);
exit(1);
}
dst_filename = argv[1];
dst_file = fopen(dst_filename, "wb");
if (!dst_file) {
fprintf(stderr, "Could not open destination file %s\n", dst_filename);
exit(1);
}
if (!swr_ctx) {
fprintf(stderr, "Could not allocate resampler context\n");
goto end;
}
fprintf(stderr, "Failed to initialize the resampling context\n");
goto end;
}
src_nb_samples, src_sample_fmt, 0);
fprintf(stderr, "Could not allocate source samples\n");
goto end;
}
max_dst_nb_samples = dst_nb_samples =
dst_nb_channels = dst_ch_layout.nb_channels;
dst_nb_samples, dst_sample_fmt, 0);
fprintf(stderr, "Could not allocate destination samples\n");
goto end;
}
t = 0;
do {
fill_samples((
double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);
if (dst_nb_samples > max_dst_nb_samples) {
dst_nb_samples, dst_sample_fmt, 1);
break;
max_dst_nb_samples = dst_nb_samples;
}
ret =
swr_convert(swr_ctx, dst_data, dst_nb_samples, (
const uint8_t **)src_data, src_nb_samples);
fprintf(stderr, "Error while converting\n");
goto end;
}
if (dst_bufsize < 0) {
fprintf(stderr, "Could not get sample buffer size\n");
goto end;
}
printf(
"t:%f in:%d out:%d\n", t, src_nb_samples,
ret);
fwrite(dst_data[0], 1, dst_bufsize, dst_file);
} while (t < 10);
goto end;
fprintf(stderr, "Resampling succeeded. Play the output file with the command:\n"
"ffplay -f %s -channel_layout %s -channels %d -ar %d %s\n",
fmt, buf, dst_nb_channels, dst_rate, dst_filename);
end:
fclose(dst_file);
if (src_data)
if (dst_data)
}