Die Verwendung von swr_convert zum Wiederverhalten von Audio -Rahmen führte zu Audio mit erheblichen RauschenC++

Programme in C++. Entwicklerforum
Guest
 Die Verwendung von swr_convert zum Wiederverhalten von Audio -Rahmen führte zu Audio mit erheblichen Rauschen

Post by Guest »

Ich habe versucht, SWR_Convert zu verwenden, um Audio -Frames von 44100 Hz auf 16000 Hz wiederzuproben und die Frames einzeln für Testzwecke zu verarbeiten. Das resultierende Audio scheint jedoch mit Rauschen gemischt zu werden.

Code: Select all

AVFrame* output_frame = av_frame_alloc();
if (!output_frame) {
throw std::runtime_error("Failed to allocate output AVFrame");
}

AVRational output_time_base = {1, output_sample_rate};

SwrContext* swr_ctx = swr_alloc();
if (!swr_ctx) {
av_frame_free(&output_frame);
throw std::runtime_error("Failed to allocate SwrContext");
}

av_opt_set_int(swr_ctx, "in_sample_fmt", aframe->format, 0);
av_opt_set_int(swr_ctx, "out_sample_fmt", output_format, 0);
av_opt_set_int(swr_ctx, "in_channel_layout", aframe->channel_layout, 0);
av_opt_set_int(swr_ctx, "out_channel_layout", output_channel_layout, 0);
av_opt_set_int(swr_ctx, "in_sample_rate", aframe->sample_rate, 0);
av_opt_set_int(swr_ctx, "out_sample_rate", output_sample_rate, 0);

if (swr_init(swr_ctx) < 0) {
swr_free(&swr_ctx);
av_frame_free(&output_frame);
throw std::runtime_error("Failed to initialize SwrContext");
}

output_frame->format = output_format;
output_frame->channel_layout = output_channel_layout;
output_frame->sample_rate = output_sample_rate;

int64_t delay = swr_get_delay(swr_ctx, aframe->sample_rate);
output_frame->nb_samples = av_rescale_rnd(
delay + aframe->nb_samples, output_sample_rate, aframe->sample_rate, AV_ROUND_UP);

output_frame->nb_samples = av_rescale_rnd(
aframe->nb_samples, output_sample_rate, aframe->sample_rate, AV_ROUND_UP);

if (av_frame_get_buffer(output_frame, 0) < 0) {
swr_free(&swr_ctx);
av_frame_free(&output_frame);
throw std::runtime_error("Failed to allocate buffer for output AVFrame");
}

int ret = swr_convert(swr_ctx,
output_frame->data, output_frame->nb_samples,
(const uint8_t**)aframe->data, aframe->nb_samples);

if (ret < 0) {
swr_free(&swr_ctx);
av_frame_free(&output_frame);
throw std::runtime_error("Failed to resample audio data");
}

if (aframe->pts != AV_NOPTS_VALUE) {
output_frame->pts = av_rescale_q(aframe->pts, {1, aframe->sample_rate}, output_time_base);
} else {
output_frame->pts = AV_NOPTS_VALUE;
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post