Skip to content

Commit

Permalink
fix right channel having lower volume after recording audio in mono (…
Browse files Browse the repository at this point in the history
…~alextee/zrythm-bug#225)
  • Loading branch information
alex-tee committed Jan 8, 2021
1 parent 4bf0e40 commit f48fe62
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 37 deletions.
13 changes: 12 additions & 1 deletion inc/audio/track.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down Expand Up @@ -978,6 +978,17 @@ void
track_update_frames (
Track * track);

/**
* Returns the Fader (if applicable).
*
* @param post_fader True to get post fader,
* false to get pre fader.
*/
Fader *
track_get_fader (
Track * track,
bool post_fader);

/**
* Returns the FaderType corresponding to the given
* Track.
Expand Down
2 changes: 1 addition & 1 deletion inc/gui/widgets/ruler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down
22 changes: 21 additions & 1 deletion inc/utils/dsp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2020-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down Expand Up @@ -117,4 +117,24 @@ dsp_mix_add2 (
float k2,
size_t size);

/**
* Makes the two signals mono.
*
* @param equal_power True for equal power, false
* for equal amplitude.
*
* @note Equal amplitude is more suitable for mono
* compatibility checking. For reference:
* equal power sum =
* (L+R) * 0.7079 (-3dB)
* equal amplitude sum =
* (L+R) /2 (-6.02dB)
*/
void
dsp_make_mono (
float * l,
float * r,
size_t size,
bool equal_power);

#endif
2 changes: 1 addition & 1 deletion src/actions/arranger_selections.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down
13 changes: 4 additions & 9 deletions src/audio/fader.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down Expand Up @@ -839,17 +839,12 @@ fader_process (
* (L+R) /2 (-6.02dB) */
if (self->mono_compat_enabled)
{
dsp_mix2 (
dsp_make_mono (
&self->stereo_out->l->buf[
start_frame],
&self->stereo_out->r->buf[
start_frame],
0.5f, 0.5f, nframes);
dsp_copy (
&self->stereo_out->r->buf[
start_frame],
&self->stereo_out->r->buf[
start_frame], nframes);
start_frame], nframes,
false);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/audio/recording_manager.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down Expand Up @@ -490,16 +490,16 @@ recording_manager_handle_recording (
re->g_start_frames = g_start_frames;
re->local_offset = local_offset;
re->nframes = nframes;
memcpy (
dsp_copy (
&re->lbuf[local_offset],
&track_processor->stereo_in->l->buf[
&track_processor->stereo_out->l->buf[
local_offset],
sizeof (float) * (size_t) nframes);
memcpy (
nframes);
dsp_copy (
&re->rbuf[local_offset],
&track_processor->stereo_in->r->buf[
&track_processor->stereo_out->r->buf[
local_offset],
sizeof (float) * (size_t) nframes);
nframes);
strcpy (re->track_name, tr->name);
recording_event_queue_push_back_event (
self->event_queue, re);
Expand Down
29 changes: 28 additions & 1 deletion src/audio/track.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down Expand Up @@ -1862,6 +1862,33 @@ track_stringize_type (
return _(track_type_strings[type].str);
}

/**
* Returns the Fader (if applicable).
*
* @param post_fader True to get post fader,
* false to get pre fader.
*/
Fader *
track_get_fader (
Track * self,
bool post_fader)
{
Channel * ch = track_get_channel (self);
if (ch)
{
if (post_fader)
{
return ch->fader;
}
else
{
return ch->prefader;
}
}

return NULL;
}

/**
* Returns the FaderType corresponding to the given
* Track.
Expand Down
22 changes: 11 additions & 11 deletions src/audio/track_processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,17 +832,6 @@ track_processor_process (
nframes, false);
}

if (track_type_can_record (tr->type) ||
tr->automation_tracklist.num_ats > 0)
{
/* handle recording. this will only create
* events in regions. it will not copy the
* input content to the output ports */
handle_recording (
self, g_start_frames, local_offset,
nframes);
}

/* add inputs to outputs */
switch (tr->in_signal_type)
{
Expand Down Expand Up @@ -891,6 +880,17 @@ track_processor_process (
default:
break;
}

if (track_type_can_record (tr->type) ||
tr->automation_tracklist.num_ats > 0)
{
/* handle recording. this will only create
* events in regions. it will not copy the
* input content to the output ports */
handle_recording (
self, g_start_frames, local_offset,
nframes);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/gui/backend/arranger_selections.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/editor_ruler.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/ruler.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down
27 changes: 26 additions & 1 deletion src/utils/dsp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2020-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down Expand Up @@ -326,3 +326,28 @@ dsp_mix_add2 (
}
#endif
}

/**
* Makes the two signals mono.
*
* @param equal_power True for equal power, false
* for equal amplitude.
*
* @note Equal amplitude is more suitable for mono
* compatibility checking. For reference:
* equal power sum =
* (L+R) * 0.7079 (-3dB)
* equal amplitude sum =
* (L+R) /2 (-6.02dB)
*/
void
dsp_make_mono (
float * l,
float * r,
size_t size,
bool equal_power)
{
float multiple = equal_power ? 0.7079f : 0.5f;
dsp_mix2 (l, r, multiple, multiple, size);
dsp_copy (r, l, size);
}
108 changes: 107 additions & 1 deletion tests/integration/recording.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2020-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down Expand Up @@ -774,6 +774,109 @@ test_recording ()
}
#endif

static void
test_mono_recording (void)
{
test_helper_zrythm_init ();

/* stop dummy audio engine processing so we can
* process manually */
AUDIO_ENGINE->stop_dummy_audio_thread = true;
g_usleep (1000000);

/* create an audio track */
UndoableAction * ua =
create_tracks_action_new (
TRACK_TYPE_AUDIO, NULL, NULL,
TRACKLIST->num_tracks, NULL, 1);
undo_manager_perform (UNDO_MANAGER, ua);
Track * audio_track =
TRACKLIST->tracks[TRACKLIST->num_tracks - 1];

prepare ();
TRANSPORT->recording = true;
transport_request_roll (TRANSPORT);

/* disable loop & punch */
transport_set_loop (TRANSPORT, false);
transport_set_punch_mode_enabled (
TRANSPORT, false);

/* move playhead to 2.1.1.0 */
Position pos;
position_set_to_bar (&pos, PLAYHEAD_START_BAR);
transport_set_playhead_pos (TRANSPORT, &pos);

/* enable recording for audio track */
track_set_recording (audio_track, true, false);

/* set mono */
TrackProcessor * audio_track_processor =
audio_track->processor;
port_set_control_value (
audio_track_processor->mono, 1.f, true,
F_NO_PUBLISH_EVENTS);

for (nframes_t i = 0; i < CYCLE_SIZE; i++)
{
AUDIO_ENGINE->dummy_input->l->buf[i] =
AUDIO_VAL;
AUDIO_ENGINE->dummy_input->r->buf[i] = 0.f;
}

/* run the engine for 1 cycle */
engine_process (AUDIO_ENGINE, CYCLE_SIZE);
recording_manager_process_events (
RECORDING_MANAGER);

ZRegion * audio_r;
ArrangerObject * audio_r_obj;

/* assert that audio events are created */
g_assert_cmpint (
audio_track->lanes[0]->num_regions, ==, 1);
audio_r = audio_track->lanes[0]->regions[0];
audio_r_obj = (ArrangerObject *) audio_r;
position_set_to_pos (
&pos, &TRANSPORT->playhead_pos);
g_assert_cmppos (&pos, &audio_r_obj->end_pos);
position_add_frames (&pos, - CYCLE_SIZE);
g_assert_cmppos (&pos, &audio_r_obj->pos);
position_from_frames (&pos, CYCLE_SIZE);
g_assert_cmppos (
&pos, &audio_r_obj->loop_end_pos);

/* assert that audio is correct */
AudioClip * clip =
audio_region_get_clip (audio_r);
g_assert_cmpint (
clip->num_frames, ==, CYCLE_SIZE);
for (nframes_t i = 0; i < CYCLE_SIZE; i++)
{
g_assert_cmpfloat_with_epsilon (
clip->ch_frames[0][i], AUDIO_VAL,
0.000001f);
g_assert_cmpfloat_with_epsilon (
clip->ch_frames[1][i], AUDIO_VAL,
0.000001f);
}

/* stop recording */
track_set_recording (audio_track, false, false);

/* run engine 1 more cycle to finalize recording */
engine_process (AUDIO_ENGINE, CYCLE_SIZE);
recording_manager_process_events (
RECORDING_MANAGER);

/* save and undo/redo */
test_project_save_and_reload ();
undo_manager_undo (UNDO_MANAGER);
undo_manager_redo (UNDO_MANAGER);

test_helper_zrythm_cleanup ();
}

int
main (int argc, char *argv[])
{
Expand All @@ -786,6 +889,9 @@ main (int argc, char *argv[])
TEST_PREFIX "test_recording",
(GTestFunc) test_recording);
#endif
g_test_add_func (
TEST_PREFIX "test mono recording",
(GTestFunc) test_mono_recording);

return g_test_run ();
}

0 comments on commit f48fe62

Please sign in to comment.