Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
project.cc
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#include "trkn/tracktion.hh" // PCH include must come first
3
4#include "project.hh"
5#include "plugin.hh"
6#include "jsonipc/jsonipc.hh"
7#include "main.hh"
8#include "compress.hh"
9#include "path.hh"
10#include "strings.hh"
11#include "unicode.hh"
12#include "storage.hh"
13#include "server.hh"
14#include "internal.hh"
16#include <list>
17
18#define UDEBUG(...) Ase::debug ("undo", __VA_ARGS__)
19
20using namespace std::literals;
21namespace te = tracktion::engine;
22
23namespace Ase {
24
25static Preference synth_latency_pref =
26 Preference ({
27 "project.default_license", _("Default License"), "",
28 "CC-BY-SA-4.0 - https://creativecommons.org/licenses/by-sa/4.0/legalcode",
29 "",
30 {}, STANDARD, {
31 String ("descr=") + _("Default LICENSE to apply in the project properties."), } });
32
34
36Project::last_project()
37{
38 return g_projects.empty() ? nullptr : g_projects.back();
39}
40
41// == TransportListener ==
43{
44 tracktion::TransportControl &transport;
45 ProjectImpl &project_;
47 LoopID ppt = LoopID::INVALID;
48 FastMemory::Block transport_block_;
49 std::list<std::function<void()>> stopped_callbacks_;
50 te::Edit *edit_ = nullptr;
51public:
52 struct Position {
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;
58 int min = 0;
59 } &pos;
60 TransportListener (tracktion::TransportControl &tc, ProjectImpl &project) :
61 transport (tc), project_ (project),
62 transport_block_ (SERVER->telemem_allocate (sizeof (Position))),
63 edit_ (project.edit_.get()),
64 pos (*new (transport_block_.block_start) Position{})
65 {
66 assert_return (this_thread_is_ase());
67 transport.addChangeListener (this); // for ChangeListener
68 transport.addListener (this); // for TransportControl::Listener
69 if (edit_)
70 edit_->state.addListener (this);
71 }
72 ~TransportListener() override
73 {
74 assert_return (this_thread_is_ase());
75 if (edit_)
76 edit_->state.removeListener (this);
77 transport.removeListener (this);
78 transport.removeChangeListener (this);
79 SERVER->telemem_release (transport_block_);
80 }
81 void
82 valueTreePropertyChanged (juce::ValueTree &vtree, const juce::Identifier &id) override
83 {
84 return_unless (project_.edit_);
85 if (id == tracktion_engine::IDs::name) // vtree == edit_->state
86 project_.emit_notify ("name");
87 if (id == tracktion_engine::IDs::bpm) // vtree == edit_->tempoSequence.getTempo (0)->state
88 project_.emit_notify ("bpm");
89 if (id == tracktion_engine::IDs::numerator)
90 project_.emit_notify ("numerator");
91 if (id == tracktion_engine::IDs::denominator)
92 project_.emit_notify ("denominator");
93 if (id == tracktion_engine::IDs::volume) {
94 auto mvp = project_.edit_->getMasterVolumePlugin();
95 if (mvp && vtree == mvp->state)
96 project_.emit_notify ("master_volume");
97 }
98 }
99 void
100 valueTreeChildAdded (juce::ValueTree &parent, juce::ValueTree &child) override
101 {
102 if (parent == edit_->state && te::TrackList::isTrack (child))
103 project_.emit_notify ("all_tracks");
104 }
105 void
106 valueTreeChildRemoved (juce::ValueTree &parent, juce::ValueTree &child, int) override
107 {
108 if (parent == edit_->state && te::TrackList::isTrack (child))
109 project_.emit_notify ("all_tracks");
110 }
111 void valueTreeChildOrderChanged (juce::ValueTree&, int, int) override {}
112 void valueTreeParentChanged (juce::ValueTree&) override {}
113 void
114 changeListenerCallback (juce::ChangeBroadcaster *source) override
115 {
116 if (source == &transport) {
117 transport_changed ("change");
118 }
119 }
120 void autoSaveNow () override {}
121 void setAllLevelMetersActive (bool become_inactive) override {}
122 void setVideoPosition (tracktion::TimePosition pos, bool force_jump) override {}
123 void recordingStarted (tracktion::SyncPoint start, std::optional<tracktion::TimeRange> punch_range) override {}
124 void recordingStopped (tracktion::SyncPoint sync_point, bool discard_recordings) 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,
129 void
130 playbackContextChanged () override
131 {
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);
137 }
138 void
139 startVideo () override
140 {
141 assert_return (this_thread_is_ase());
142 poll_position();
143 if (ppt == LoopID::INVALID) // TODO: can we optimize telemetry form trkn?
144 ppt = main_loop->add ([this] { this->poll_position(); return true; }, std::chrono::milliseconds (16));
145 project_.emit_notify ("is_playing");
146 transport_changed ("start-video");
147 }
148 void
149 stopVideo () override
150 {
151 assert_return (this_thread_is_ase());
152 main_loop->cancel (&ppt);
153 poll_position();
154 project_.emit_notify ("is_playing");
155 transport_changed ("stop-video");
156 while (!stopped_callbacks_.empty()) {
157 const auto f = stopped_callbacks_.front();
158 stopped_callbacks_.pop_front();
159 f();
160 }
161 }
162 void
163 run_when_stopped (const std::function<void()> &f)
164 {
165 if (transport.isPlaying())
166 stopped_callbacks_.push_back (f);
167 else
168 f();
169 }
170 void
171 transport_changed (const std::string &what)
172 {
173 auto position = transport.getPosition();
174 Ase::printerr ("Transport: playing=%d position=%.3fsecs (%s)\n",
175 transport.isPlaying(), position.inSeconds(), what.c_str());
176 }
177 void
178 poll_position()
179 {
180 auto context = transport.getCurrentPlaybackContext();
181 return_unless (!!context);
182
183 auto &transport = project_.edit_->getTransport();
184 auto &tempoSeq = project_.edit_->tempoSequence;
185
186 // Get Current Time - getPosition() is the cursor position.
187 // Use edit->getCurrentPlaybackContext()->getAudibleTimelineTime() for compensating for latency
188 const tracktion::TimePosition currentPos = transport.getPosition();
189 const double totalSeconds = currentPos.inSeconds();
190
191 // Calculate Minutes / Seconds / Millis
192 // We use std::abs to handle potential negative times (pre-roll) safely
193 const double absSeconds = std::abs (totalSeconds);
194 const int intSeconds = int (absSeconds);
195 pos.min = intSeconds / 60;
196 pos.sec = absSeconds - pos.min * 60;
197
198 // Calculate Musical Position (Bars & Beats)
199 tracktion::tempo::BarsAndBeats bab = tempoSeq.toBarsAndBeats (currentPos);
200
201 // Tracktion uses 0-based indexing for Bars and Beats internally.
202 pos.bar = bab.bars;
203 pos.beat = bab.getWholeBeats();
204
205 // Calculate Sub-beat divisions (Sixteenths and Ticks)
206 // bab.getFractionalBeats() returns the remainder of the beat (0.0 to 0.999...)
207 double fractionalBeat = bab.getFractionalBeats().inBeats();
208
209 // Sixteenths: There are 4 sixteenths in a beat
210 pos.sxth = int (fractionalBeat * 4.0);
211
212 // Ticks: Tracktion standard is 960 PPQ (Pulses Per Quarter note)
213 pos.tick = int (fractionalBeat * 960.0);
214
215 // Get Tempo and Time Signature at this specific moment
216 // (Tempo can change during the song, so we ask for the value *at* currentPos)
217 pos.bpm = tempoSeq.getBpmAt (currentPos);
218 auto &timesig = tempoSeq.getTimeSigAt (currentPos);
219 pos.snum = timesig.numerator;
220 pos.sden = timesig.denominator;
221
222 // Calculate Frames (SMPTE)
223 const te::TimecodeDisplayFormat tdf = project_.edit_->getTimecodeFormat();
224 pos.fps = tdf.getFPS();
225
226 // Simple frame calculation: seconds * fps
227 // (Note: This is a basic calculation. For Drop-frame SMPTE, use tdf.toFullTimecode(...))
228 pos.frame = int (absSeconds * pos.fps) % int (pos.fps);
229 }
230};
231
232
233// == ProjectImpl ==
234using StringPairS = std::vector<std::tuple<String,String>>;
235
237 String loading_file;
238 String writer_cachedir;
239 String anklang_dir;
240 StringPairS writer_files;
241 StringPairS asset_hashes;
243 ptrp_ (ptrp)
244 {
245 *ptrp_ = this;
246 }
247 ~PStorage()
248 {
249 *ptrp_ = nullptr;
250 }
251private:
252 PStorage **const ptrp_ = nullptr;
253};
254
255static void
256test_sfz (ProjectImpl *project, te::Edit *edit, const String &filename)
257{
258 TrackP track = project->create_track();
259 assert (track);
260 track->name ("LiquidSFZTest");
261
264
265 double start = 0;
266 double duration = 4;
267 ClipP clip = trackimpl->create_midi_clip ("NotesClip", start, duration);
268 assert (clip);
269
271 auto add_note = [&] (int start, int key, int duration)
272 {
274 note.id = -1;
275 note.key = key;
276 note.channel = 0;
277 note.tick = start * TRANSPORT_PPQN;
278 note.duration = duration * TRANSPORT_PPQN;
279 note.velocity = 0.8f;
280 batch.push_back (note);
281 };
282 add_note (0, 60, 1);
283 add_note (1, 64, 1);
284 add_note (2, 67, 1);
285 clip->change_batch (batch, "Add Note");
286
287 auto& engine = edit->engine;
288 engine.getPluginManager().createBuiltInType<LiquidSFZPlugin>();
289
290 auto plugin = trackimpl->create_plugin (LiquidSFZPlugin::xmlTypeName);
291 assert (plugin);
292
294 if (auto liquidsfz = dynamic_cast<LiquidSFZPlugin *> (pluginimpl->plugin()))
295 liquidsfz->load (filename);
296
297 auto &transport = edit->getTransport();
298 transport.setLoopRange({ tracktion::TimePosition::fromSeconds (start), tracktion::TimeDuration::fromSeconds (duration) });
299 transport.looping = true;
300 transport.setPosition (tracktion::TimePosition::fromSeconds (start));
301}
302
303ProjectImpl::ProjectImpl()
304{
305 bpm (120);
306 numerator (4);
307 denominator (4);
308 edit_ = std::make_unique<te::Edit> (*trkn_engine(), te::Edit::forEditing);
309 if (edit_) {
310 register_ase_obj (this, *edit_);
311 transport_listener_ = std::make_unique<TransportListener> (edit_->getTransport(), *this);
312 }
313 if (!edit_ || !transport_listener_)
314 fatal_error ("failed to create tracktion::engine::edit");
315
316 if (auto filename = getenv ("SFZ"))
317 test_sfz (this, edit_.get(), filename);
318
320
321 /* TODO: MusicalTuning
322 * group = _("Tuning");
323 * Prop ("musical_tuning", _("Musical Tuning"), _("Tuning"), MusicalTuning::OD_12_TET, {
324 * "descr="s + _("The tuning system which specifies the tones or pitches to be used. "
325 * "Due to the psychoacoustic properties of tones, various pitch combinations can "
326 * "sound \"natural\" or \"pleasing\" when used in combination, the musical "
327 * "tuning system defines the number and spacing of frequency values applied."), "" },
328 * enum_lister<MusicalTuning>);
329 */
330}
331
332void
333ProjectImpl::deactivate_edit()
334{
335 return_unless (!!edit_);
336 auto &transport = edit_->getTransport();
337 if (transport.isPlaying() || transport.isRecording())
338 transport.stop (true, true);
339 transport.freePlaybackContext();
341 edit_ = nullptr;
342}
343
344ProjectImpl::~ProjectImpl()
345{
346 unregister_ase_obj (this, edit_.get());
347 deactivate_edit();
348 transport_listener_ = nullptr;
349 edit_ = nullptr;
350}
351
352
353void
354ProjectImpl::force_shutdown_all ()
355{
356 rescan:
357 for (size_t i = 0; i < g_projects.size(); i++)
358 if (g_projects[i]->edit_) {
359 g_projects[i]->deactivate_edit();
360 goto rescan; // callbacks can change anything
361 }
362}
363
364String
365ProjectImpl::name() const
366{
367 // Edit.getName() requires af ProjectItem, which we dont use
368 return edit_ ? edit_->state.getProperty (tracktion_engine::IDs::name).toString().toStdString() : "";
369}
370
371void
372ProjectImpl::name (const std::string &nm)
373{
374 return_unless (!!edit_);
375 // tracktion_engine::getProjectItemForEdit (*edit_)->setName (nm, tracktion_engine::ProjectItem::SetNameMode::doDefault);
376 edit_->state.setProperty (tracktion_engine::IDs::name, juce::String (nm), &edit_->getUndoManager());
377}
378
381{
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));
392 return v;
393}
394
395void
396ProjectImpl::foreach_track (const std::function<bool(Track&,int)> &cb)
397{
398 std::function<bool(te::Track&,int)> foreach_track = [&] (te::Track &t, int depth)
399 {
400 const TrackImplP trackp = TrackImpl::from_trkn (t);
401 if (!trackp || !cb (*trackp, depth))
402 return false;
403 if (trackp->is_folder())
404 for (auto subtrack : dynamic_cast<te::FolderTrack*> (&t)->getAllSubTracks (false /*recursive*/))
405 if (subtrack &&
406 false == foreach_track (*subtrack, depth + 1))
408 return true;
409 };
410 edit_->visitAllTopLevelTracks ([&] (te::Track &t) { return foreach_track (t, 0); });
411}
412
413ProjectImplP
414ProjectImpl::create (const String &projectname)
415{
416 ProjectImplP project = ProjectImpl::make_shared();
417 g_projects.push_back (project);
418 project->name (projectname);
419 project->edit_->getUndoManager().clearUndoHistory();
420 return project;
421}
422
423void
425{
426 return_unless (!discarded_);
428 const size_t nerased = Aux::erase_first (g_projects, [this] (auto ptr) { return ptr.get() == this; });
429 if (nerased)
430 {} // resource cleanups
431 discarded_ = true;
432}
433
434void
436{
437 // Project has no parent; just emit the `removed` event
439}
440
441static bool
442is_anklang_dir (const String &path)
443{
444 return Path::check (Path::join (path, ".anklang.project"), "r");
445}
446
447static String
448find_anklang_parent_dir (const String &path)
449{
450 for (String p = path; !p.empty() && !Path::isroot (p); p = Path::dirname (p))
451 if (is_anklang_dir (p))
452 return p;
453 return "";
454}
455
456static bool
457make_anklang_dir (const String &path)
458{
459 String mime = Path::join (path, ".anklang.project");
460 return Path::stringwrite (mime, "# ANKLANG(1) project directory\n");
461}
462
463Error
465{
467 assert_return (storage_ == nullptr, Error::OPERATION_BUSY);
468 PStorage storage (&storage_); // storage_ = &storage;
469 const String dotanklang = ".anklang";
471 // check path is a file
472 if (path.back() == '/' ||
473 Path::check (path, "d")) // need file not directory
474 return Error::FILE_IS_DIR;
475 // force .anklang extension
476 if (!string_endswith (path, dotanklang))
477 path += dotanklang;
478 // existing files need proper project directories
479 if (Path::check (path, "e")) // existing file
480 {
481 const String dir = Path::dirname (path);
482 if (!is_anklang_dir (dir))
483 return Error::NO_PROJECT_DIR;
485 path = dir; // file inside project dir
486 }
487 else // new file name
488 {
490 const String parentdir = Path::dirname (path);
491 if (is_anklang_dir (parentdir))
492 path = parentdir;
493 else { // use projectfile stem as dir
494 assert_return (string_endswith (path, dotanklang), Error::INTERNAL);
495 path.resize (path.size() - dotanklang.size());
496 }
497 }
498 // create parent directory
499 if (!Path::mkdirs (path))
500 return ase_error_from_errno (errno);
501 // ensure path is_anklang_dir
502 if (!make_anklang_dir (path))
503 return ase_error_from_errno (errno);
504 storage_->anklang_dir = path;
505 const String abs_projectfile = Path::join (path, projectfile);
506 // create backups
507 if (Path::check (abs_projectfile, "e"))
508 {
509 const String backupdir = Path::join (path, "backup");
510 if (!Path::mkdirs (backupdir))
511 return ase_error_from_errno (errno ? errno : EPERM);
512 const StringPair parts = Path::split_extension (projectfile, true);
513 const String backupname = Path::join (backupdir, parts.first + now_strftime (" (%y%m%dT%H%M%S)") + parts.second);
514 const String backupglob = Path::join (backupdir, parts.first + " ([0-9]*[0-9]T[0-9]*[0-9])" + parts.second);
515 if (!Path::rename (abs_projectfile, backupname))
516 ASE_SERVER.user_note (string_format ("## Backup failed\n%s: \\\nFailed to create backup: \\\n%s",
517 backupname, ase_error_blurb (ase_error_from_errno (errno))));
518 else // successful backup, now prune
519 {
522 strings_version_sort (&backups, true);
523 const int bmax = 24;
524 while (backups.size() > bmax)
525 {
526 const String bfile = backups.back();
527 backups.pop_back();
529 }
530 }
531 }
532 // start writing
534 storage_->writer_cachedir = anklang_cachedir_create();
535 storage_->asset_hashes.clear();
536 StorageWriter ws (Storage::AUTO_ZSTD);
537 Error error = ws.open_with_mimetype (abs_projectfile, "application/x-anklang");
538 if (!error)
539 {
540 // serialize Project (TODO: use tracktion_engine saving & loading)
541 error = ws.store_file_data ("project.json", "{}\n", true);
542 }
543 if (!error)
544 for (const auto &[path, dest] : storage_->writer_files) {
545 error = ws.store_file (dest, path);
546 if (!!error) {
547 printerr ("%s: %s: %s: %s\n", program_alias(), __func__, path, ase_error_blurb (error));
548 break;
549 }
550 }
551 storage_->writer_files.clear();
552 if (!error)
553 error = ws.close();
554 if (!error)
555 saved_filename_ = abs_projectfile;
556 if (!!error)
557 ws.remove_opened();
558 anklang_cachedir_cleanup (storage_->writer_cachedir);
559 return error;
560}
561
562Error
563ProjectImpl::snapshot_project (String &json)
564{
565 assert_return (storage_ == nullptr, Error::OPERATION_BUSY);
566 // writer setup
567 PStorage storage (&storage_); // storage_ = &storage;
568 storage_->writer_cachedir = anklang_cachedir_create();
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();
573 // serialize Project (TODO: use tracktion_engine saving & loading)
574 json = "{}";
575 // cleanup
576 anklang_cachedir_cleanup (storage_->writer_cachedir);
577 return Error::NONE;
578}
579
580String
581ProjectImpl::writer_file_name (const String &fspath) const
582{
583 assert_return (storage_ != nullptr, "");
584 assert_return (!storage_->writer_cachedir.empty(), "");
585 return Path::join (storage_->writer_cachedir, fspath);
586}
587
588Error
589ProjectImpl::writer_add_file (const String &fspath)
590{
591 assert_return (storage_ != nullptr, Error::INTERNAL);
592 assert_return (!storage_->writer_cachedir.empty(), Error::INTERNAL);
593 if (!Path::check (fspath, "frw"))
594 return Error::FILE_NOT_FOUND;
595 if (!string_startswith (fspath, storage_->writer_cachedir))
596 return Error::FILE_OPEN_FAILED;
597 storage_->writer_files.push_back ({ fspath, Path::basename (fspath) });
598 return Error::NONE;
599}
600
601Error
602ProjectImpl::writer_collect (const String &fspath, String *hexhashp)
603{
604 assert_return (storage_ != nullptr, Error::INTERNAL);
605 assert_return (!storage_->anklang_dir.empty(), Error::INTERNAL);
606 if (!Path::check (fspath, "fr"))
607 return Error::FILE_NOT_FOUND;
608 // determine hash of file to collect
609 const String hexhash = string_to_hex (blake3_hash_file (fspath));
610 if (hexhash.empty())
611 return ase_error_from_errno (errno ? errno : EIO);
612 // resolve against existing hashes
613 for (const auto &hf : storage_->asset_hashes)
614 if (std::get<0> (hf) == hexhash)
615 {
616 *hexhashp = hexhash;
617 return Error::NONE;
618 }
619 // file may be within project directory
621 if (Path::dircontains (storage_->anklang_dir, fspath, &relpath))
622 {
623 storage_->asset_hashes.push_back ({ hexhash, relpath });
624 *hexhashp = hexhash;
625 return Error::NONE;
626 }
627 // determine unique path name
628 const size_t file_size = Path::file_size (fspath);
629 const String basedir = storage_->anklang_dir;
630 relpath = Path::join ("samples", Path::basename (fspath));
631 String dest = Path::join (basedir, relpath);
632 size_t i = 0;
633 while (Path::check (dest, "e"))
634 {
635 if (file_size == Path::file_size (dest))
636 {
637 const String althash = string_to_hex (blake3_hash_file (dest));
638 if (althash == hexhash)
639 {
640 // found file with same hash within project directory
641 storage_->asset_hashes.push_back ({ hexhash, relpath });
642 *hexhashp = hexhash;
643 return Error::NONE;
644 }
645 }
646 // add counter to create unique name
647 const StringPair parts = Path::split_extension (relpath, true);
648 dest = Path::join (basedir, string_format ("%s(%u)%s", parts.first, ++i, parts.second));
649 }
650 // create parent dir
652 return ase_error_from_errno (errno);
653 // copy into project dir
654 const bool copied = Path::copy_file (fspath, dest);
655 if (!copied)
656 return ase_error_from_errno (errno);
657 // success
658 storage_->asset_hashes.push_back ({ hexhash, relpath });
659 *hexhashp = hexhash;
660 return Error::NONE;
661}
662
663String
665{
666 return encodefs (saved_filename_);
667}
668
669Error
671{
672 const String filename = decodefs (utf8filename);
673 assert_return (storage_ == nullptr, Error::OPERATION_BUSY);
674 PStorage storage (&storage_); // storage_ = &storage;
675 String fname = filename;
676 // turn /dir/.anklang.project -> /dir/
677 if (Path::basename (fname) == ".anklang.project" && is_anklang_dir (Path::dirname (fname)))
679 // turn /dir/ -> /dir/dir.anklang
680 if (Path::check (fname, "d"))
681 fname = Path::join (fname, Path::basename (Path::strip_slashes (Path::normalize (fname)))) + ".anklang";
682 // add missing '.anklang' extension
683 if (!Path::check (fname, "e"))
684 fname += ".anklang";
685 // check for readable file
686 if (!Path::check (fname, "e"))
687 return ase_error_from_errno (errno);
688 // try reading .anklang container
689 StorageReader rs (Storage::AUTO_ZSTD);
690 Error error = rs.open_for_reading (fname);
691 if (!!error)
692 return error;
693 if (rs.stringread ("mimetype") != "application/x-anklang")
694 return Error::BAD_PROJECT;
695 // find project.json *inside* container
696 String jsd = rs.stringread ("project.json");
697 if (jsd.empty() && errno)
698 return Error::FORMAT_INVALID;
699 storage_->loading_file = fname;
700 storage_->anklang_dir = find_anklang_parent_dir (storage_->loading_file);
701#if 0 // unimplemented
703 // search in dirname or dirname/..
704 if (is_anklang_dir (dirname))
705 rs.search_dir (dirname);
706 else
707 {
709 if (is_anklang_dir (dirname))
710 rs.search_dir (dirname);
711 }
712#endif
713 // parse project (TODO: use tracktion_engine loading)
714 // jsd contains project state from tracktion_engine (currently {})
715 saved_filename_ = storage_->loading_file;
716 return Error::NONE;
717}
718
719StreamReaderP
720ProjectImpl::load_blob (const String &fspath)
721{
722 assert_return (storage_ != nullptr, nullptr);
723 assert_return (!storage_->loading_file.empty(), nullptr);
724 return stream_reader_zip_member (storage_->loading_file, fspath);
725}
726
728String
730{
731 return_unless (storage_ && storage_->asset_hashes.size(), "");
732 return_unless (!storage_->anklang_dir.empty(), "");
733 for (const auto& [hash,relpath] : storage_->asset_hashes)
734 if (hexhash == hash)
735 return Path::join (storage_->anklang_dir, relpath);
736 return "";
737}
738
739String
741{
742 String json;
743 Error error = snapshot_project (json);
744 if (!!error) {
745 warning ("Project: failed to serialize project: %s\n", ase_error_blurb (error));
746 return "";
747 }
748 return Re::grep (regex, json, group);
749}
750
751UndoScope::UndoScope (ProjectImplP projectp, const String &scopename) :
752 projectp_ (projectp),
753 scopename_ (scopename)
754{
756 assert_return (projectp->edit_);
757 projectp->edit_->getUndoManager().beginNewTransaction (juce::String (scopename));
758}
759
760UndoScope::~UndoScope()
761{
762 assert_return (projectp_);
763 assert_return (projectp_->edit_);
764 projectp_->edit_->getUndoManager().beginNewTransaction();
765}
766
767UndoScope
768ProjectImpl::undo_scope (const String &scopename)
769{
770 assert_warn (scopename != "");
771 return UndoScope (shared_ptr_cast<ProjectImpl> (this), scopename);
772}
773
774UndoScope
775ProjectImpl::add_undo_scope (const String &scopename)
776{
777 return UndoScope (shared_ptr_cast<ProjectImpl> (this), scopename);
778}
779
780void
782{
783 return_unless (!!edit_);
784 const bool had_undo = edit_->getUndoManager().canUndo();
785 edit_->getUndoManager().undo();
786 if (had_undo)
787 emit_notify ("dirty");
788}
789
790bool
792{
793 return_unless (!!edit_, false);
794 return edit_->getUndoManager().canUndo();
795}
796
797void
799{
800 return_unless (!!edit_);
801 const bool had_redo = edit_->getUndoManager().canRedo();
802 edit_->getUndoManager().redo();
803 if (had_redo)
804 emit_notify ("dirty");
805}
806
807bool
809{
810 return_unless (!!edit_, false);
811 return edit_->getUndoManager().canRedo();
812}
813
814double
816{
817 return_unless (!!edit_, 0.0);
818 return edit_->getLength().inSeconds();
819}
820
821double
823{
824 return_unless (!!edit_, 0.0);
825 auto volPlugin = edit_->getMasterVolumePlugin();
826 return_unless (!!volPlugin, 0.0);
827 return te::volumeFaderPositionToDB (volPlugin->volume.get());
828}
829
830void
832{
833 return_unless (!!edit_);
834 auto volPlugin = edit_->getMasterVolumePlugin();
836 const float sliderPos = te::decibelsToVolumeFaderPosition (db);
837 volPlugin->volume = sliderPos;
838 volPlugin->volParam->updateFromAttachedValue();
839}
840
841void
847
848void
854
855void
856ProjectImpl::clear_undo ()
857{
858 return_unless (!!edit_);
860 emit_notify ("dirty");
861}
862
863void
864ProjectImpl::bpm (double newbpm)
865{
866 return_unless (!!edit_);
867 const double nbpm = CLAMP (newbpm, MIN_BPM, MAX_BPM);
868 auto &tempoSeq = edit_->tempoSequence;
869 auto *tempo = tempoSeq.getTempo (0);
870 if (tempo && tempo->getBpm() != nbpm)
871 tempo->setBpm (nbpm);
872}
873
874double
875ProjectImpl::bpm () const
876{
877 return_unless (!!edit_, 120.0);
878 auto *tempo = edit_->tempoSequence.getTempo (0);
879 return tempo ? tempo->getBpm() : 120.0;
880}
881
882void
883ProjectImpl::numerator (double num)
884{
885 return_unless (!!edit_);
886 auto &tempoSeq = edit_->tempoSequence;
887 auto *timeSig = tempoSeq.getTimeSig (0);
888 if (timeSig && timeSig->numerator != num)
889 timeSig->numerator = num;
890}
891
892double
893ProjectImpl::numerator () const
894{
895 return_unless (!!edit_, 4.0);
896 auto *timeSig = edit_->tempoSequence.getTimeSig (0);
897 return timeSig ? timeSig->numerator : 4.0;
898}
899
900void
901ProjectImpl::denominator (double den)
902{
903 return_unless (!!edit_);
904 auto &tempoSeq = edit_->tempoSequence;
905 auto *timeSig = tempoSeq.getTimeSig (0);
906 if (timeSig && timeSig->denominator != den)
907 timeSig->denominator = den;
908}
909
910double
911ProjectImpl::denominator () const
912{
913 return_unless (!!edit_, 4.0);
914 auto *timeSig = edit_->tempoSequence.getTimeSig (0);
915 return timeSig ? timeSig->denominator : 4.0;
916}
917
918void
920{
921 assert_return (!discarded_);
923 if (edit_->getTransport().isPlayContextActive())
924 edit_->getTransport().play (false);
925}
926
927void
929{
930 if (edit_->getTransport().isPlaying())
931 edit_->getTransport().stop (false, false);
932}
933
934void
936{
937 edit_->getTransport().stop (false, true);
938 transport_listener_->run_when_stopped ([this] {
939 // wait until stopped, so the new position persists
940 edit_->getTransport().setPosition (tracktion::TimePosition::fromSeconds (0.0));
941 transport_listener_->poll_position();
942 });
943}
944
945bool
947{
948 return edit_->getTransport().isPlaying();
949}
950
951void
953{
954 if (is_playing() == play)
955 return;
956 if (is_playing())
958 else
960}
961
962TrackP
964{
965 return_unless (edit_ && !discarded_, nullptr);
966 auto t = edit_->insertNewAudioTrack (tracktion::TrackInsertPoint (nullptr, nullptr), nullptr);
967 if (!t) return nullptr;
968 TrackImplP track = TrackImpl::from_trkn (*t);
969 emit_event ("track", "insert", { { "track", track }, });
970 emit_notify ("all_tracks");
971 return track;
972}
973
974TrackS
976{
978 auto tf = [&] (Track &track, int depth)
979 {
980 tracks.push_back (shared_ptr_cast<TrackImpl> (&track));
981 return true;
982 };
983 foreach_track (tf);
984 return tracks;
985}
986
988ProjectImpl::track_index (const Track &child) const
989{
990 ssize_t index = 0;
991 ssize_t found = -1;
992 auto tf = [&] (Track &track, int depth)
993 {
994 if (&track == &child)
995 {
996 found = index;
997 return false;
998 }
999 index++;
1000 return true;
1001 };
1002 const_cast<ProjectImpl*> (this)->foreach_track (tf);
1003 return found;
1004}
1005
1006int64_t
1007ProjectImpl::bar_ticks () const
1008{
1009 return_unless (!!edit_, 0);
1010 auto &tempoSeq = edit_->tempoSequence;
1011 auto *timeSig = tempoSeq.getTimeSig (0);
1012 if (!timeSig)
1013 return 0;
1014
1015 const int beats_per_bar = timeSig->numerator;
1016 const int beat_unit = timeSig->denominator;
1017
1018 // Calculate beat ticks: SEMIQUAVER_TICKS * (16 / beat_unit)
1019 // SEMIQUAVER_TICKS = TRANSPORT_PPQN / 4 = 1209600
1020 const int64 SEMIQUAVER_TICKS = 1209600;
1021 const int semiquavers_per_beat = 16 / beat_unit;
1022 const int64 beat_ticks = SEMIQUAVER_TICKS * semiquavers_per_beat;
1023
1024 return beat_ticks * beats_per_bar;
1025}
1026
1027TrackP
1029{
1030 return_unless (!!edit_, nullptr);
1031 auto *masterTrack = edit_->getMasterTrack();
1032 return_unless (masterTrack, nullptr);
1033 return TrackImpl::from_trkn (*masterTrack);
1034}
1035
1038{
1039 return {}; // TODO: DeviceInfo
1040}
1041
1042} // Ase
#define EPERM
assert
T back(T... args)
T c_str(T... args)
void emit_notify(const String &detail) override
Emit notify:detail, multiple notifications maybe coalesced if a CoalesceNotifies instance exists.
Definition object.cc:164
void remove_self() override
Remove self from parent container.
Definition gadget.cc:113
void group_undo(const String &undoname) override
Merge upcoming undo steps.
Definition project.cc:842
void redo() override
Redo the last undo modification.
Definition project.cc:798
bool can_redo() override
Check if any redo steps have been recorded.
Definition project.cc:808
TrackP master_track() override
Retrieve the master track.
Definition project.cc:1028
void undo() override
Undo the last project modification.
Definition project.cc:781
String match_serialized(const String &regex, int group) override
Match regex against the serialized project state.
Definition project.cc:740
bool is_playing() const override
Check whether a project is currently playing (song sequencing).
Definition project.cc:946
void remove_self() override
Remove self from parent container.
Definition project.cc:435
bool can_undo() override
Check if any undo steps have been recorded.
Definition project.cc:791
TelemetryFieldS telemetry() const override
Retrieve project telemetry locations.
Definition project.cc:380
TrackS all_tracks() override
List all tracks of the project.
Definition project.cc:975
void ungroup_undo() override
Stop merging undo steps.
Definition project.cc:849
String saved_filename() override
Retrieve UTF-8 filename for save or from load.
Definition project.cc:664
TrackP create_track() override
Create and append a new Track.
Definition project.cc:963
Error save_project(const String &utf8filename, bool collect) override
Store Project and collect external files.
Definition project.cc:464
double master_volume() const override
Get master volume in dB.
Definition project.cc:822
String loader_resolve(const String &hexhash)
Find file from hash code, returns fspath.
Definition project.cc:729
void start_playback() override
Start playback of a project, requires active sound engine.
Definition project.hh:65
DeviceInfo device_info() override
Describe this Device type.
Definition project.cc:1037
void discard() override
Discard project and associated resources.
Definition project.cc:424
Error load_project(const String &utf8filename) override
Load project from file filename.
Definition project.cc:670
double length() const override
Get the end time of the last clip in seconds.
Definition project.cc:815
void pause_playback() override
Pause playback at the current position.
Definition project.cc:928
void stop_playback() override
Stop project playback.
Definition project.cc:935
static String grep(const String &regex, const String &input, int group=0, Flags=DEFAULT)
Find regex in input and return matching string.
Definition regex.cc:225
Container for Clip objects and sequencing information.
Definition api.hh:262
void beginNewTransaction()
bool canUndo() const
bool canRedo() const
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
juce::ValueTree state
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
TimeSigSetting * getTimeSig(int index) const
TempoSetting * getTempo(int index) const
void ensureContextAllocated(bool alwaysReallocate=false)
void play(bool justSendMMCIfEnabled)
void stop(bool discardRecordings, bool clearDevices, bool canSendMMCStop=true)
T clear(T... args)
#define ASE_CLASS_NON_COPYABLE(ClassName)
Delete copy ctor and assignment operator.
Definition cxxaux.hh:110
dirname
T empty(T... args)
errno
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
Definition internal.hh:28
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
Definition internal.hh:72
#define CLAMP(v, mi, ma)
Yield v clamped to [mi … ma].
Definition internal.hh:59
#define assert_warn(expr)
Issue an assertion warning if expr evaluates to false.
Definition internal.hh:32
#define _(...)
Retrieve the translation of a C or C++ string.
Definition internal.hh:17
typedef int
T load(T... args)
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.
Definition utils.hh:267
void glob(const String &pathpattern, StringS &dirs, StringS &files)
Create list with directories and filenames matching pathpattern with shell wildcards.
Definition path.cc:812
String basename(const String &path)
Strips all directory components from path and returns the resulting file name.
Definition path.cc:68
String dirname(const String &path)
Retrieve the directory part of the filename path.
Definition path.cc:60
bool mkdirs(const String &dirpath, uint mode)
Create the directories in dirpath with mode, check errno on false returns.
Definition path.cc:197
bool check(const String &file, const String &mode)
Definition path.cc:625
String strip_slashes(const String &path)
Strip trailing directory terminators.
Definition path.cc:118
String abspath(const String &path, const String &incwd)
Definition path.cc:134
void rmrf(const String &dir)
Recursively delete directory tree.
Definition path.cc:236
size_t file_size(const String &path)
Retrieve the on-disk size in bytes of path.
Definition path.cc:514
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.
Definition path.cc:244
bool isroot(const String &path, bool dos_drives)
Return wether path is an absolute pathname which identifies the root directory.
Definition path.cc:160
bool dircontains(const String &dirpath, const String &descendant, String *relpath)
Check if descendant belongs to the directory hierarchy under dirpath.
Definition path.cc:221
String normalize(const String &path)
Convert path to normal form.
Definition path.cc:86
The Anklang C++ API namespace.
Definition api.hh:8
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.
Definition storage.cc:106
String string_to_hex(const String &input)
Convert bytes in string input to hexadecimal numbers.
Definition strings.cc:1171
int64_t int64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:28
void register_ase_obj(VirtualBase *ase_impl, tracktion::Selectable &selectable)
Helper: register AseImpl with a tracktion Selectable via ase_obj_.
Definition trkn-utils.cc:62
Error
Enum representing Error states.
Definition api.hh:21
const char * ase_error_blurb(Error error)
Describe Error condition.
Definition server.cc:276
std::string decodefs(const std::string &utf8str)
Decode UTF-8 string back into file system path representation, extracting surrogate code points as by...
Definition unicode.cc:131
void anklang_cachedir_clean_stale()
Clean stale cache directories from past runtimes, may be called from any thread.
Definition storage.cc:161
String program_alias()
Retrieve the program name as used for logging or debug messages.
Definition platform.cc:817
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:34
constexpr const char STANDARD[]
STORAGE GUI READABLE WRITABLE.
Definition api.hh:13
constexpr const int64 TRANSPORT_PPQN
Maximum number of sample frames to calculate in Processor::render().
Definition transport.hh:52
void unregister_ase_obj(VirtualBase *ase_impl, tracktion::Selectable *selectable)
Helper: unregister AseImpl from a tracktion Selectable (selectable may be nullptr)
Definition trkn-utils.cc:70
bool string_endswith(const String &string, const String &fragment)
Returns whether string ends with fragment.
Definition strings.cc:863
void anklang_cachedir_cleanup(const String &cachedir)
Cleanup a cachedir previously created with anklang_cachedir_create().
Definition storage.cc:142
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...
Definition unicode.cc:112
bool string_startswith(const String &string, const String &fragment)
Returns whether string starts with fragment.
Definition strings.cc:846
Info for device types.
Definition api.hh:200
T push_back(T... args)
T resize(T... args)
T size(T... args)
Part specific note event representation.
Definition api.hh:227
Reference for an allocated memory block.
Definition memory.hh:89
typedef ssize_t