From 674961bfe5b6625b158eda669c64165277458ff6 Mon Sep 17 00:00:00 2001
From: Colin Fletcher <colin.m.fletcher@googlemail.com>
Date: Mon, 18 Apr 2022 16:55:54 +0100
Subject: [PATCH 1/2] libs/ardour: allow creating region source markers while
 recording

Add a list of marker locations to the session, for the the UI to add the
current location to when "add-region-cue-marker" happens whilst recording.
On record-stop, create source markers at the locations in that list in all
newly-recorded audio regions.
---
 libs/ardour/ardour/session.h     | 1 +
 libs/ardour/ardour/source.h      | 1 +
 libs/ardour/disk_writer.cc       | 2 +-
 libs/ardour/session_transport.cc | 2 ++
 libs/ardour/source.cc            | 9 +++++++++
 5 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/libs/ardour/ardour/session.h b/libs/ardour/ardour/session.h
index e62440ca17..8d0faca10f 100644
--- a/libs/ardour/ardour/session.h
+++ b/libs/ardour/ardour/session.h
@@ -1994,6 +1994,7 @@ public:
 	}
 
 	bool playlist_is_active( boost::shared_ptr<Playlist>);
+	ARDOUR::CueMarkers pending_source_markers; // source markers created while recording
 
 private:
 	void reset_write_sources (bool mark_write_complete, bool force = false);
diff --git a/libs/ardour/ardour/source.h b/libs/ardour/ardour/source.h
index f7dac6932d..c559e2d134 100644
--- a/libs/ardour/ardour/source.h
+++ b/libs/ardour/ardour/source.h
@@ -114,6 +114,7 @@ public:
 	size_t n_captured_xruns () const { return _xruns.size (); }
 	XrunPositions const& captured_xruns () const { return _xruns; }
 	void set_captured_xruns (XrunPositions const& xruns) { _xruns = xruns; }
+	void set_captured_marks (CueMarkers const& marks);
 
 	CueMarkers const & cue_markers() const { return _cue_markers; }
 	bool add_cue_marker (CueMarker const &);
diff --git a/libs/ardour/disk_writer.cc b/libs/ardour/disk_writer.cc
index 03d00d122a..46e42d6109 100644
--- a/libs/ardour/disk_writer.cc
+++ b/libs/ardour/disk_writer.cc
@@ -1244,9 +1244,9 @@ DiskWriter::transport_stopped_wallclock (struct tm& when, time_t twhen, bool abo
 
 		(*chan)->write_source->stamp (twhen);
 		(*chan)->write_source->set_captured_xruns (capture_info.front()->xruns);
+		(*chan)->write_source->set_captured_marks (_session.pending_source_markers);
 	}
 
-
 	/* MIDI */
 
 	if (_midi_write_source) {
diff --git a/libs/ardour/session_transport.cc b/libs/ardour/session_transport.cc
index 15b4ec1462..3119483141 100644
--- a/libs/ardour/session_transport.cc
+++ b/libs/ardour/session_transport.cc
@@ -1384,6 +1384,8 @@ Session::non_realtime_stop (bool abort, int on_entry, bool& finished, bool will_
 		}
 	}
 
+	pending_source_markers.clear();
+
 	if (abort && did_record) {
 		_state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
 	}
diff --git a/libs/ardour/source.cc b/libs/ardour/source.cc
index 7bb822363c..3f0e6991df 100644
--- a/libs/ardour/source.cc
+++ b/libs/ardour/source.cc
@@ -468,6 +468,15 @@ Source::writable () const
         return (_flags & Writable) && _session.writable();
 }
 
+void
+Source::set_captured_marks (CueMarkers const &marks)
+{
+	for (auto mark : marks) {
+		std::cerr << "adding " << mark.text() << " at " << mark.position() << "\n";
+		add_cue_marker(mark);
+	}
+}
+
 bool
 Source::add_cue_marker (CueMarker const & cm)
 {
-- 
2.30.2

