2#include "trkn/tracktion.hh"
18#define UDEBUG(...) Ase::debug ("undo", __VA_ARGS__)
25static Preference synth_latency_pref =
27 "project.default_license",
_(
"Default License"),
"",
28 "CC-BY-SA-4.0 - https://creativecommons.org/licenses/by-sa/4.0/legalcode",
31 String (
"descr=") +
_(
"Default LICENSE to apply in the project properties."), } });
36Project::last_project()
38 return g_projects.empty() ?
nullptr : g_projects.back();
44 tracktion::TransportControl &transport;
47 LoopID ppt = LoopID::INVALID;
53 int fps = 0, frame = 0;
54 int bar = 0, beat = 0;
55 int sxth = 0, tick = 0;
56 int snum = 0, sden = 0;
57 double bpm = 0, sec = 0;
61 transport (
tc), project_ (project),
63 edit_ (project.edit_.get()),
64 pos (*new (transport_block_.block_start)
Position{})
67 transport.addChangeListener (
this);
68 transport.addListener (
this);
77 transport.removeListener (
this);
78 transport.removeChangeListener (
this);
79 SERVER->telemem_release (transport_block_);
85 if (
id == tracktion_engine::IDs::name)
87 if (
id == tracktion_engine::IDs::bpm)
89 if (
id == tracktion_engine::IDs::numerator)
91 if (
id == tracktion_engine::IDs::denominator)
93 if (
id == tracktion_engine::IDs::volume) {
94 auto mvp = project_.edit_->getMasterVolumePlugin();
102 if (parent == edit_->
state && te::TrackList::isTrack (
child))
108 if (parent == edit_->
state && te::TrackList::isTrack (
child))
111 void valueTreeChildOrderChanged (
juce::ValueTree&,
int,
int)
override {}
116 if (source == &transport) {
117 transport_changed (
"change");
120 void autoSaveNow ()
override {}
122 void setVideoPosition (tracktion::TimePosition pos,
bool force_jump)
override {}
125 void recordingAboutToStart (tracktion::InputDeviceInstance &
device, tracktion::EditItemID target)
override {}
126 void recordingAboutToStop (tracktion::InputDeviceInstance &
device, tracktion::EditItemID target)
override {}
127 void recordingFinished (tracktion::InputDeviceInstance &
device, tracktion::EditItemID target,
130 playbackContextChanged ()
override
132 tracktion::EditPlaybackContext *context = transport.getCurrentPlaybackContext();
133 Ase::diag (
"PlaybackContextChanged: context=%p graph=%d playing=%d position=%.3fsecs\n", context,
134 context ? context->isPlaybackGraphAllocated() : 0,
135 context ? context->isPlaying() : 0,
136 context ? context->getPosition().inSeconds() : 0);
139 startVideo ()
override
143 if (ppt == LoopID::INVALID)
146 transport_changed (
"start-video");
149 stopVideo ()
override
152 main_loop->cancel (&ppt);
155 transport_changed (
"stop-video");
156 while (!stopped_callbacks_.empty()) {
157 const auto f = stopped_callbacks_.front();
158 stopped_callbacks_.pop_front();
165 if (transport.isPlaying())
166 stopped_callbacks_.push_back (f);
173 auto position = transport.getPosition();
174 Ase::printerr (
"Transport: playing=%d position=%.3fsecs (%s)\n",
175 transport.isPlaying(), position.inSeconds(), what.
c_str());
180 auto context = transport.getCurrentPlaybackContext();
183 auto &transport = project_.edit_->getTransport();
184 auto &
tempoSeq = project_.edit_->tempoSequence;
188 const tracktion::TimePosition
currentPos = transport.getPosition();
203 pos.beat =
bab.getWholeBeats();
220 pos.sden =
timesig.denominator;
224 pos.fps =
tdf.getFPS();
260 track->name (
"LiquidSFZTest");
267 ClipP clip =
trackimpl->create_midi_clip (
"NotesClip", start, duration);
271 auto add_note = [&] (
int start,
int key,
int duration)
279 note.velocity = 0.8f;
285 clip->change_batch (
batch,
"Add Note");
290 auto plugin =
trackimpl->create_plugin (LiquidSFZPlugin::xmlTypeName);
298 transport.
setLoopRange({ tracktion::TimePosition::fromSeconds (start), tracktion::TimeDuration::fromSeconds (duration) });
299 transport.looping =
true;
300 transport.setPosition (tracktion::TimePosition::fromSeconds (start));
303ProjectImpl::ProjectImpl()
313 if (!edit_ || !transport_listener_)
314 fatal_error (
"failed to create tracktion::engine::edit");
316 if (
auto filename = getenv (
"SFZ"))
317 test_sfz (
this, edit_.get(), filename);
333ProjectImpl::deactivate_edit()
337 if (transport.isPlaying() || transport.isRecording())
338 transport.
stop (
true,
true);
339 transport.freePlaybackContext();
344ProjectImpl::~ProjectImpl()
348 transport_listener_ =
nullptr;
354ProjectImpl::force_shutdown_all ()
357 for (
size_t i = 0; i < g_projects.size(); i++)
358 if (g_projects[i]->edit_) {
359 g_projects[i]->deactivate_edit();
365ProjectImpl::name()
const
368 return edit_ ? edit_->
state.
getProperty (tracktion_engine::IDs::name).toString().toStdString() :
"";
383 v.push_back (telemetry_field (
"current_tick", &transport_listener_->pos.tick));
384 v.push_back (telemetry_field (
"current_bar", &transport_listener_->pos.bar));
385 v.push_back (telemetry_field (
"current_beat", &transport_listener_->pos.beat));
386 v.push_back (telemetry_field (
"current_sixteenth", &transport_listener_->pos.sxth));
387 v.push_back (telemetry_field (
"current_bpm", &transport_listener_->pos.bpm));
388 v.push_back (telemetry_field (
"current_numerator", &transport_listener_->pos.snum));
389 v.push_back (telemetry_field (
"current_denominator", &transport_listener_->pos.sden));
390 v.push_back (telemetry_field (
"current_minutes", &transport_listener_->pos.min));
391 v.push_back (telemetry_field (
"current_seconds", &transport_listener_->pos.sec));
416 ProjectImplP project = ProjectImpl::make_shared();
417 g_projects.push_back (project);
419 project->edit_->getUndoManager().clearUndoHistory();
442is_anklang_dir (
const String &path)
444 return Path::check (Path::join (path,
".anklang.project"),
"r");
448find_anklang_parent_dir (
const String &path)
451 if (is_anklang_dir (p))
457make_anklang_dir (
const String &path)
459 String mime = Path::join (path,
".anklang.project");
460 return Path::stringwrite (
mime,
"# ANKLANG(1) project directory\n");
472 if (path.
back() ==
'/' ||
474 return Error::FILE_IS_DIR;
482 if (!is_anklang_dir (
dir))
483 return Error::NO_PROJECT_DIR;
500 return ase_error_from_errno (
errno);
502 if (!make_anklang_dir (path))
503 return ase_error_from_errno (
errno);
504 storage_->anklang_dir = path;
516 ASE_SERVER.user_note (
string_format (
"## Backup failed\n%s: \\\nFailed to create backup: \\\n%s",
522 strings_version_sort (&
backups,
true);
535 storage_->asset_hashes.
clear();
541 error = ws.store_file_data (
"project.json",
"{}\n",
true);
544 for (
const auto &[path,
dest] : storage_->writer_files) {
545 error = ws.store_file (
dest, path);
551 storage_->writer_files.
clear();
569 if (storage_->writer_cachedir.
empty() || !
Path::check (storage_->writer_cachedir,
"d"))
570 return Error::NO_PROJECT_DIR;
571 storage_->anklang_dir = storage_->writer_cachedir;
572 storage_->asset_hashes.
clear();
581ProjectImpl::writer_file_name (
const String &
fspath)
const
585 return Path::join (storage_->writer_cachedir,
fspath);
594 return Error::FILE_NOT_FOUND;
596 return Error::FILE_OPEN_FAILED;
607 return Error::FILE_NOT_FOUND;
611 return ase_error_from_errno (errno ? errno :
EIO);
613 for (
const auto &
hf : storage_->asset_hashes)
647 const StringPair
parts = Path::split_extension (
relpath,
true);
652 return ase_error_from_errno (errno);
656 return ase_error_from_errno (errno);
687 return ase_error_from_errno (
errno);
693 if (
rs.stringread (
"mimetype") !=
"application/x-anklang")
694 return Error::BAD_PROJECT;
698 return Error::FORMAT_INVALID;
699 storage_->loading_file =
fname;
700 storage_->anklang_dir = find_anklang_parent_dir (storage_->loading_file);
715 saved_filename_ = storage_->loading_file;
724 return stream_reader_zip_member (storage_->loading_file,
fspath);
733 for (
const auto& [
hash,
relpath] : storage_->asset_hashes)
735 return Path::join (storage_->anklang_dir,
relpath);
745 warning (
"Project: failed to serialize project: %s\n",
ase_error_blurb (error));
760UndoScope::~UndoScope()
764 projectp_->edit_->getUndoManager().beginNewTransaction();
827 return te::volumeFaderPositionToDB (
volPlugin->volume.get());
836 const float sliderPos = te::decibelsToVolumeFaderPosition (
db);
838 volPlugin->volParam->updateFromAttachedValue();
856ProjectImpl::clear_undo ()
864ProjectImpl::bpm (
double newbpm)
869 auto *tempo =
tempoSeq.getTempo (0);
870 if (tempo && tempo->getBpm() !=
nbpm)
871 tempo->setBpm (
nbpm);
875ProjectImpl::bpm ()
const
879 return tempo ? tempo->
getBpm() : 120.0;
883ProjectImpl::numerator (
double num)
887 auto *timeSig =
tempoSeq.getTimeSig (0);
888 if (timeSig && timeSig->numerator !=
num)
889 timeSig->numerator =
num;
893ProjectImpl::numerator ()
const
897 return timeSig ? timeSig->numerator : 4.0;
901ProjectImpl::denominator (
double den)
905 auto *timeSig =
tempoSeq.getTimeSig (0);
906 if (timeSig && timeSig->denominator !=
den)
907 timeSig->denominator =
den;
911ProjectImpl::denominator ()
const
915 return timeSig ? timeSig->denominator : 4.0;
938 transport_listener_->run_when_stopped ([
this] {
940 edit_->
getTransport().setPosition (tracktion::TimePosition::fromSeconds (0.0));
941 transport_listener_->poll_position();
966 auto t = edit_->
insertNewAudioTrack (tracktion::TrackInsertPoint (
nullptr,
nullptr),
nullptr);
967 if (!t)
return nullptr;
969 emit_event (
"track",
"insert", { {
"track", track }, });
978 auto tf = [&] (
Track &track,
int depth)
988ProjectImpl::track_index (
const Track &
child)
const
992 auto tf = [&] (
Track &track,
int depth)
994 if (&track == &
child)
1002 const_cast<ProjectImpl*
> (
this)->foreach_track (tf);
1007ProjectImpl::bar_ticks ()
const
1011 auto *timeSig =
tempoSeq.getTimeSig (0);
1015 const int beats_per_bar = timeSig->numerator;
1016 const int beat_unit = timeSig->denominator;
1020 const int64 SEMIQUAVER_TICKS = 1209600;
1024 return beat_ticks * beats_per_bar;
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 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.
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.
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.
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 setLoopRange(TimeRange)
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.
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.
constexpr const int64 TRANSPORT_PPQN
Maximum number of sample frames to calculate in Processor::render().
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...
bool string_startswith(const String &string, const String &fragment)
Returns whether string starts with fragment.
Part specific note event representation.
Reference for an allocated memory block.