From 4b7e62ddb7b850cf3fce0301f5de3b8dd1cd317a Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Fri, 5 Oct 2018 11:02:04 +1000 Subject: [PATCH] added speed changes while maintaining audio pitch --- dialogs/speeddialog.cpp | 9 +++++++-- dialogs/speeddialog.h | 2 ++ playback/cacher.cpp | 21 ++++++++++++++++++--- project/clip.cpp | 3 ++- project/clip.h | 4 ++-- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/dialogs/speeddialog.cpp b/dialogs/speeddialog.cpp index b10aa2a7a..c4090e382 100644 --- a/dialogs/speeddialog.cpp +++ b/dialogs/speeddialog.cpp @@ -43,9 +43,12 @@ SpeedDialog::SpeedDialog(QWidget *parent) : QDialog(parent) { main_layout->addLayout(grid); reverse = new QCheckBox("Reverse"); + maintain_pitch = new QCheckBox("Maintain Audio Pitch"); + ripple = new QCheckBox("Ripple Changes"); + main_layout->addWidget(reverse); - main_layout->addWidget(new QCheckBox("Maintain Audio Pitch")); - main_layout->addWidget(new QCheckBox("Ripple")); + main_layout->addWidget(maintain_pitch); + main_layout->addWidget(ripple); QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); buttonBox->setCenterButtons(true); @@ -179,6 +182,8 @@ void SpeedDialog::accept() { c->timeline_out = proposed_out; c->recalculateMaxLength(); + + c->maintain_audio_pitch = maintain_pitch->isChecked(); } panel_timeline->redraw_all_clips(true); QDialog::accept(); diff --git a/dialogs/speeddialog.h b/dialogs/speeddialog.h index e466cfc70..54f903ba3 100644 --- a/dialogs/speeddialog.h +++ b/dialogs/speeddialog.h @@ -26,6 +26,8 @@ private: LabelSlider* frame_rate; QCheckBox* reverse; + QCheckBox* maintain_pitch; + QCheckBox* ripple; double default_frame_rate; double current_frame_rate; diff --git a/playback/cacher.cpp b/playback/cacher.cpp index 440a71953..469637af0 100644 --- a/playback/cacher.cpp +++ b/playback/cacher.cpp @@ -490,13 +490,28 @@ void open_clip_worker(Clip* clip) { qDebug() << "[ERROR] Could not set output sample format"; } - int sample_rates[] = { qRound(sequence->audio_frequency / clip->speed), 0 }; + int target_sample_rate = sequence->audio_frequency; + + if (qFuzzyCompare(clip->speed, 1.0)) { + avfilter_link(clip->buffersrc_ctx, 0, clip->buffersink_ctx, 0); + } else if (clip->maintain_audio_pitch) { + char speed_param[10]; + snprintf(speed_param, sizeof(speed_param), "%f", clip->speed); + + AVFilterContext* tempo_filter; + avfilter_graph_create_filter(&tempo_filter, avfilter_get_by_name("atempo"), "atempo", speed_param, NULL, clip->filter_graph); + avfilter_link(clip->buffersrc_ctx, 0, tempo_filter, 0); + avfilter_link(tempo_filter, 0, clip->buffersink_ctx, 0); + } else { + target_sample_rate = qRound(sequence->audio_frequency / clip->speed); + avfilter_link(clip->buffersrc_ctx, 0, clip->buffersink_ctx, 0); + } + + int sample_rates[] = { target_sample_rate, 0 }; if (av_opt_set_int_list(clip->buffersink_ctx, "sample_rates", sample_rates, 0, AV_OPT_SEARCH_CHILDREN) < 0) { qDebug() << "[ERROR] Could not set output sample rates"; } - avfilter_link(clip->buffersrc_ctx, 0, clip->buffersink_ctx, 0); - avfilter_graph_config(clip->filter_graph, NULL); clip->audio_reset = true; diff --git a/project/clip.cpp b/project/clip.cpp index da1ef56aa..fd47c9c21 100644 --- a/project/clip.cpp +++ b/project/clip.cpp @@ -33,7 +33,8 @@ Clip::Clip(Sequence* s) : filter_graph(NULL), texture(NULL), fbo(NULL), - autoscale(config.autoscale_by_default) + autoscale(config.autoscale_by_default), + maintain_audio_pitch(false) { reset(); } diff --git a/project/clip.h b/project/clip.h index 19f5b8df8..ddcbbb7d5 100644 --- a/project/clip.h +++ b/project/clip.h @@ -116,8 +116,8 @@ struct Clip long texture_frame; bool autoscale; - // audio playback variables -// SwrContext* swr_ctx; + // audio playback variables + bool maintain_audio_pitch; int frame_sample_index; int audio_buffer_write; bool audio_reset;