2#include "trkn/tracktion.hh"
16#define UDEBUG(...) Ase::debug ("undo", __VA_ARGS__)
23static Preference synth_latency_pref =
25 "project.default_license",
_(
"Default License"),
"",
26 "CC-BY-SA-4.0 - https://creativecommons.org/licenses/by-sa/4.0/legalcode",
29 String (
"descr=") +
_(
"Default LICENSE to apply in the project properties."), } });
38Project::last_project()
40 return g_projects.empty() ?
nullptr : g_projects.back();
46 tracktion::TransportControl &transport;
49 LoopID ppt = LoopID::INVALID;
55 int fps = 0, frame = 0;
56 int bar = 0, beat = 0;
57 int sxth = 0, tick = 0;
58 int snum = 0, sden = 0;
59 double bpm = 0, sec = 0;
63 transport (
tc), project_ (project),
65 edit_ (project.edit_.get()),
66 pos (*new (transport_block_.block_start)
Position{})
69 transport.addChangeListener (
this);
70 transport.addListener (
this);
79 transport.removeListener (
this);
80 transport.removeChangeListener (
this);
81 SERVER->telemem_release (transport_block_);
87 if (
id == tracktion_engine::IDs::name)
89 if (
id == tracktion_engine::IDs::bpm)
91 if (
id == tracktion_engine::IDs::numerator)
93 if (
id == tracktion_engine::IDs::denominator)
95 if (
id == tracktion_engine::IDs::volume) {
96 auto mvp = project_.edit_->getMasterVolumePlugin();
104 if (parent == edit_->
state && te::TrackList::isTrack (
child))
110 if (parent == edit_->
state && te::TrackList::isTrack (
child))
113 void valueTreeChildOrderChanged (
juce::ValueTree&,
int,
int)
override {}
118 if (source == &transport) {
119 transport_changed (
"change");
122 void autoSaveNow ()
override {}
124 void setVideoPosition (tracktion::TimePosition pos,
bool force_jump)
override {}
127 void recordingAboutToStart (tracktion::InputDeviceInstance &
device, tracktion::EditItemID target)
override {}
128 void recordingAboutToStop (tracktion::InputDeviceInstance &
device, tracktion::EditItemID target)
override {}
129 void recordingFinished (tracktion::InputDeviceInstance &
device, tracktion::EditItemID target,
132 playbackContextChanged ()
override
134 tracktion::EditPlaybackContext *context = transport.getCurrentPlaybackContext();
135 Ase::diag (
"PlaybackContextChanged: context=%p graph=%d playing=%d position=%.3fsecs\n", context,
136 context ? context->isPlaybackGraphAllocated() : 0,
137 context ? context->isPlaying() : 0,
138 context ? context->getPosition().inSeconds() : 0);
141 startVideo ()
override
145 if (ppt == LoopID::INVALID)
148 transport_changed (
"start-video");
151 stopVideo ()
override
154 main_loop->cancel (&ppt);
157 transport_changed (
"stop-video");
158 while (!stopped_callbacks_.empty()) {
159 const auto f = stopped_callbacks_.front();
160 stopped_callbacks_.pop_front();
167 if (transport.isPlaying())
168 stopped_callbacks_.push_back (f);
175 auto position = transport.getPosition();
176 Ase::printerr (
"Transport: playing=%d position=%.3fsecs (%s)\n",
177 transport.isPlaying(), position.inSeconds(), what.
c_str());
182 auto context = transport.getCurrentPlaybackContext();
185 auto &transport = project_.edit_->getTransport();
186 auto &
tempoSeq = project_.edit_->tempoSequence;
190 const tracktion::TimePosition
currentPos = transport.getPosition();
205 pos.beat =
bab.getWholeBeats();
222 pos.sden =
timesig.denominator;
226 pos.fps =
tdf.getFPS();
257ProjectImpl::ProjectImpl()
267 if (!edit_ || !transport_listener_)
268 fatal_error (
"failed to create tracktion::engine::edit");
284ProjectImpl::deactivate_edit()
288 if (transport.isPlaying() || transport.isRecording())
289 transport.
stop (
true,
true);
290 transport.freePlaybackContext();
295ProjectImpl::~ProjectImpl()
299 transport_listener_ =
nullptr;
305ProjectImpl::force_shutdown_all ()
308 for (
size_t i = 0; i < g_projects.size(); i++)
309 if (g_projects[i]->edit_) {
310 g_projects[i]->deactivate_edit();
316ProjectImpl::name()
const
319 return edit_ ? edit_->
state.
getProperty (tracktion_engine::IDs::name).toString().toStdString() :
"";
334 v.push_back (telemetry_field (
"current_tick", &transport_listener_->pos.tick));
335 v.push_back (telemetry_field (
"current_bar", &transport_listener_->pos.bar));
336 v.push_back (telemetry_field (
"current_beat", &transport_listener_->pos.beat));
337 v.push_back (telemetry_field (
"current_sixteenth", &transport_listener_->pos.sxth));
338 v.push_back (telemetry_field (
"current_bpm", &transport_listener_->pos.bpm));
339 v.push_back (telemetry_field (
"current_numerator", &transport_listener_->pos.snum));
340 v.push_back (telemetry_field (
"current_denominator", &transport_listener_->pos.sden));
341 v.push_back (telemetry_field (
"current_minutes", &transport_listener_->pos.min));
342 v.push_back (telemetry_field (
"current_seconds", &transport_listener_->pos.sec));
367 ProjectImplP project = ProjectImpl::make_shared();
368 g_projects.push_back (project);
370 project->edit_->getUndoManager().clearUndoHistory();
409is_anklang_dir (
const String &path)
411 return Path::check (Path::join (path,
".anklang.project"),
"r");
415find_anklang_parent_dir (
const String &path)
418 if (is_anklang_dir (p))
424make_anklang_dir (
const String &path)
426 String mime = Path::join (path,
".anklang.project");
427 return Path::stringwrite (
mime,
"# ANKLANG(1) project directory\n");
439 if (path.
back() ==
'/' ||
441 return Error::FILE_IS_DIR;
449 if (!is_anklang_dir (
dir))
450 return Error::NO_PROJECT_DIR;
467 return ase_error_from_errno (
errno);
469 if (!make_anklang_dir (path))
470 return ase_error_from_errno (
errno);
471 storage_->anklang_dir = path;
483 ASE_SERVER.user_note (
string_format (
"## Backup failed\n%s: \\\nFailed to create backup: \\\n%s",
489 strings_version_sort (&
backups,
true);
502 storage_->asset_hashes.
clear();
510 error = ws.store_file_data (
"project.json",
jsd,
true);
513 for (
const auto &[path,
dest] : storage_->writer_files) {
514 error = ws.store_file (
dest, path);
520 storage_->writer_files.
clear();
538 if (storage_->writer_cachedir.
empty() || !
Path::check (storage_->writer_cachedir,
"d"))
539 return Error::NO_PROJECT_DIR;
540 storage_->anklang_dir = storage_->writer_cachedir;
541 storage_->asset_hashes.
clear();
550ProjectImpl::writer_file_name (
const String &
fspath)
const
554 return Path::join (storage_->writer_cachedir,
fspath);
563 return Error::FILE_NOT_FOUND;
565 return Error::FILE_OPEN_FAILED;
576 return Error::FILE_NOT_FOUND;
580 return ase_error_from_errno (errno ? errno :
EIO);
582 for (
const auto &
hf : storage_->asset_hashes)
616 const StringPair
parts = Path::split_extension (
relpath,
true);
621 return ase_error_from_errno (errno);
625 return ase_error_from_errno (errno);
656 return ase_error_from_errno (
errno);
662 if (
rs.stringread (
"mimetype") !=
"application/x-anklang")
663 return Error::BAD_PROJECT;
667 return Error::FORMAT_INVALID;
668 storage_->loading_file =
fname;
669 storage_->anklang_dir = find_anklang_parent_dir (storage_->loading_file);
684 return Error::PARSE_ERROR;
685 saved_filename_ = storage_->loading_file;
694 return stream_reader_zip_member (storage_->loading_file,
fspath);
703 for (
const auto& [
hash,
relpath] : storage_->asset_hashes)
705 return Path::join (storage_->anklang_dir,
relpath);
721 warning (
"Project: failed to serialize project: %s\n",
ase_error_blurb (error));
736UndoScope::~UndoScope()
740 projectp_->edit_->getUndoManager().beginNewTransaction();
803 return te::volumeFaderPositionToDB (
volPlugin->volume.get());
812 const float sliderPos = te::decibelsToVolumeFaderPosition (
db);
814 volPlugin->volParam->updateFromAttachedValue();
832ProjectImpl::clear_undo ()
840ProjectImpl::bpm (
double newbpm)
845 auto *tempo =
tempoSeq.getTempo (0);
846 if (tempo && tempo->getBpm() !=
nbpm)
847 tempo->setBpm (
nbpm);
851ProjectImpl::bpm ()
const
855 return tempo ? tempo->
getBpm() : 120.0;
859ProjectImpl::numerator (
double num)
863 auto *timeSig =
tempoSeq.getTimeSig (0);
864 if (timeSig && timeSig->numerator !=
num)
865 timeSig->numerator =
num;
869ProjectImpl::numerator ()
const
873 return timeSig ? timeSig->numerator : 4.0;
877ProjectImpl::denominator (
double den)
881 auto *timeSig =
tempoSeq.getTimeSig (0);
882 if (timeSig && timeSig->denominator !=
den)
883 timeSig->denominator =
den;
887ProjectImpl::denominator ()
const
891 return timeSig ? timeSig->denominator : 4.0;
914 transport_listener_->run_when_stopped ([
this] {
916 edit_->
getTransport().setPosition (tracktion::TimePosition::fromSeconds (0.0));
917 transport_listener_->poll_position();
942 auto t = edit_->
insertNewAudioTrack (tracktion::TrackInsertPoint (
nullptr,
nullptr),
nullptr);
943 if (!t)
return nullptr;
945 emit_event (
"track",
"insert", { {
"track", track }, });
954 auto tf = [&] (
Track &track,
int depth)
964ProjectImpl::track_index (
const Track &
child)
const
968 auto tf = [&] (
Track &track,
int depth)
970 if (&track == &
child)
978 const_cast<ProjectImpl*
> (
this)->foreach_track (tf);
983ProjectImpl::bar_ticks ()
const
987 auto *timeSig =
tempoSeq.getTimeSig (0);
991 const int beats_per_bar = timeSig->numerator;
992 const int beat_unit = timeSig->denominator;
996 const int64 SEMIQUAVER_TICKS = 1209600;
1000 return beat_ticks * beats_per_bar;
bool is_active() override
Check whether this is the active synthesis engine project.
void _deactivate() override
Stop processing the corresponding AudioProcessor.
void _activate() override
Add AudioProcessor to the Engine and start processing.
void emit_notify(const String &detail) override
Emit notify:detail, multiple notifications maybe coalesced if a CoalesceNotifies instance exists.
void remove_self() override
Remove self from parent container.
void group_undo(const String &undoname) override
Merge upcoming undo steps.
void _deactivate() override
Stop processing the corresponding AudioProcessor.
void redo() override
Redo the last undo modification.
bool can_redo() override
Check if any redo steps have been recorded.
TrackP master_track() override
Retrieve the master track.
void undo() override
Undo the last project modification.
String match_serialized(const String ®ex, int group) override
Match regex against the serialized project state.
bool is_playing() const override
Check whether a project is currently playing (song sequencing).
void remove_self() override
Remove self from parent container.
bool can_undo() override
Check if any undo steps have been recorded.
void serialize(WritNode &xs) override
Serialize members and childern.
TelemetryFieldS telemetry() const override
Retrieve project telemetry locations.
TrackS all_tracks() override
List all tracks of the project.
void ungroup_undo() override
Stop merging undo steps.
String saved_filename() override
Retrieve UTF-8 filename for save or from load.
TrackP create_track() override
Create and append a new Track.
Error save_project(const String &utf8filename, bool collect) override
Store Project and collect external files.
void _activate() override
Add AudioProcessor to the Engine and start processing.
double master_volume() const override
Get master volume in dB.
String loader_resolve(const String &hexhash)
Find file from hash code, returns fspath.
void start_playback() override
Start playback of a project, requires active sound engine.
DeviceInfo device_info() override
Describe this Device type.
void discard() override
Discard project and associated resources.
Error load_project(const String &utf8filename) override
Load project from file filename.
double length() const override
Get the end time of the last clip in seconds.
void pause_playback() override
Pause playback at the current position.
void stop_playback() override
Stop project playback.
static String grep(const String ®ex, const String &input, int group=0, Flags=DEFAULT)
Find regex in input and return matching string.
Container for Clip objects and sequencing information.
One entry in a Writ serialization document.
void beginNewTransaction()
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
void addListener(Listener *listener)
const var & getProperty(const Identifier &name) const noexcept
void removeListener(Listener *listener)
VolumeAndPanPlugin::Ptr getMasterVolumePlugin() const
TransportControl & getTransport() const noexcept
TimeDuration getLength() const
void visitAllTopLevelTracks(std::function< bool(Track &)>) const
MasterTrack * getMasterTrack() const
TempoSequence tempoSequence
juce::ReferenceCountedObjectPtr< AudioTrack > insertNewAudioTrack(TrackInsertPoint, SelectionManager *)
juce::UndoManager & getUndoManager() noexcept
void cancelAnyPendingUpdates()
TimeSigSetting * getTimeSig(int index) const
TempoSetting * getTempo(int index) const
void ensureContextAllocated(bool alwaysReallocate=false)
bool isPlayContextActive() const
void play(bool justSendMMCIfEnabled)
void stop(bool discardRecordings, bool clearDevices, bool canSendMMCStop=true)
#define ASE_CLASS_NON_COPYABLE(ClassName)
Delete copy ctor and assignment operator.
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
#define CLAMP(v, mi, ma)
Yield v clamped to [mi … ma].
#define assert_warn(expr)
Issue an assertion warning if expr evaluates to false.
#define _(...)
Retrieve the translation of a C or C++ string.
size_t erase_first(C &container, const std::function< bool(typename C::value_type const &value)> &pred)
Erase first element for which pred() is true in vector or list.
void glob(const String &pathpattern, StringS &dirs, StringS &files)
Create list with directories and filenames matching pathpattern with shell wildcards.
String basename(const String &path)
Strips all directory components from path and returns the resulting file name.
String dirname(const String &path)
Retrieve the directory part of the filename path.
bool mkdirs(const String &dirpath, uint mode)
Create the directories in dirpath with mode, check errno on false returns.
bool check(const String &file, const String &mode)
String strip_slashes(const String &path)
Strip trailing directory terminators.
String abspath(const String &path, const String &incwd)
void rmrf(const String &dir)
Recursively delete directory tree.
size_t file_size(const String &path)
Retrieve the on-disk size in bytes of path.
bool copy_file(const String &src, const String &dest)
Copy a file to a new non-existing location, sets errno and returns false on error.
bool isroot(const String &path, bool dos_drives)
Return wether path is an absolute pathname which identifies the root directory.
bool dircontains(const String &dirpath, const String &descendant, String *relpath)
Check if descendant belongs to the directory hierarchy under dirpath.
String normalize(const String &path)
Convert path to normal form.
The Anklang C++ API namespace.
std::string string_format(const char *format, const Args &...args) __attribute__((__format__(__printf__
Format a string similar to sprintf(3) with support for std::string and std::ostringstream convertible...
String anklang_cachedir_create()
Create exclusive cache directory for this process' runtime.
String string_to_hex(const String &input)
Convert bytes in string input to hexadecimal numbers.
bool json_parse(const String &jsonstring, T &target)
Parse a well formed JSON string and assign contents to target.
int64_t int64
A 64-bit unsigned integer, use PRI*64 in format strings.
void register_ase_obj(VirtualBase *ase_impl, tracktion::Selectable &selectable)
Helper: register AseImpl with a tracktion Selectable via ase_obj_.
Error
Enum representing Error states.
const char * ase_error_blurb(Error error)
Describe Error condition.
std::string decodefs(const std::string &utf8str)
Decode UTF-8 string back into file system path representation, extracting surrogate code points as by...
void anklang_cachedir_clean_stale()
Clean stale cache directories from past runtimes, may be called from any thread.
String program_alias()
Retrieve the program name as used for logging or debug messages.
std::string String
Convenience alias for std::string.
constexpr const char STANDARD[]
STORAGE GUI READABLE WRITABLE.
void unregister_ase_obj(VirtualBase *ase_impl, tracktion::Selectable *selectable)
Helper: unregister AseImpl from a tracktion Selectable (selectable may be nullptr)
bool string_endswith(const String &string, const String &fragment)
Returns whether string ends with fragment.
void anklang_cachedir_cleanup(const String &cachedir)
Cleanup a cachedir previously created with anklang_cachedir_create().
std::string encodefs(const std::string &fschars)
Encode a file system path consisting of bytes into UTF-8, using surrogate code points to store non UT...
String json_stringify(const T &source, Writ::Flags flags=Writ::Flags(0))
Create JSON string from source.
bool string_startswith(const String &string, const String &fragment)
Returns whether string starts with fragment.
Reference for an allocated memory block.