Skip to content

Commit 050cd4c

Browse files
committed
Add constant-power crossfade to mix transition
1 parent 2c5323b commit 050cd4c

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎src/modules/core/transition_mix.c‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* transition_mix.c -- mix two audio streams
3-
* Copyright (C) 2003-2020 Meltytech, LLC
3+
* Copyright (C) 2003-2026 Meltytech, LLC
44
*
55
* This library is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU Lesser General Public
@@ -49,7 +49,8 @@ static void mix_audio(double weight_start,
4949
int channels_a,
5050
int channels_b,
5151
int channels_out,
52-
int samples)
52+
int samples,
53+
int power)
5354
{
5455
int i, j;
5556
double a, b, v;
@@ -59,10 +60,12 @@ static void mix_audio(double weight_start,
5960
double mix_step = (weight_end - weight_start) / samples;
6061

6162
for (i = 0; i < samples; i++) {
63+
double gain_b = power ? sin(mix * M_PI_2) : mix;
64+
double gain_a = power ? cos(mix * M_PI_2) : 1.0 - mix;
6265
for (j = 0; j < channels_out; j++) {
6366
a = (double) buffer_a[i * channels_a + j];
6467
b = (double) buffer_b[i * channels_b + j];
65-
v = mix * b + (1.0 - mix) * a;
68+
v = gain_b * b + gain_a * a;
6669
buffer_a[i * channels_a + j] = v;
6770
}
6871
mix += mix_step;
@@ -280,7 +283,17 @@ static int transition_get_audio(mlt_frame frame_a,
280283
mix_start = 1.0 - mix_start;
281284
mix_end = 1.0 - mix_end;
282285
}
283-
mix_audio(mix_start, mix_end, buffer_a, buffer_b, channels_a, channels_b, *channels, *samples);
286+
int power = mlt_properties_get_double(MLT_TRANSITION_PROPERTIES(transition), "start")
287+
< -1.0;
288+
mix_audio(mix_start,
289+
mix_end,
290+
buffer_a,
291+
buffer_b,
292+
channels_a,
293+
channels_b,
294+
*channels,
295+
*samples,
296+
power);
284297
}
285298

286299
// Copy the audio from the dest buffer into the frame.

‎src/modules/core/transition_mix.yml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
schema_version: 0.2
1+
schema_version: 7.0
22
type: transition
33
identifier: mix
44
title: Mix
5-
version: 1
5+
version: 2
66
copyright: Meltytech, LLC
77
creator: Dan Dennedy
88
license: LGPLv2.1
@@ -19,8 +19,9 @@ parameters:
1919
type: float
2020
mutable: yes
2121
description: >
22-
The mix level to apply to the second frame. Any negative value causes an
23-
automatic crossfade from 0 to 1.
22+
The mix level to apply to the second frame. -1 causes an automatic
23+
linear crossfade from 0 to 1. -2 causes an automatic constant-power
24+
(equal-power) crossfade from 0 to 1.
2425
2526
- identifier: end
2627
title: End

0 commit comments

Comments
 (0)