XJ music engine  1.9.6
In-game runtime engine for XJ music.
Fabricator.h
Go to the documentation of this file.
1 // Copyright (c) XJ Music Inc. (https://xjmusic.com) All Rights Reserved.
2 
3 #ifndef XJMUSIC_FABRICATOR_H
4 #define XJMUSIC_FABRICATOR_H
5 
6 #include <optional>
7 #include <set>
8 #include <string>
9 #include <vector>
10 
29 #include "xjmusic/music/Chord.h"
30 #include "xjmusic/music/Note.h"
33 #include "xjmusic/segment/Chain.h"
45 
46 namespace XJ {
47 
48  class Fabricator {
49  public:
50  virtual ~Fabricator() = default;
51 
60  explicit Fabricator(
61  ContentEntityStore *contentEntityStore,
62  SegmentEntityStore *segmentEntityStore,
63  const SegmentRetrospective *segmentRetrospective,
64  int segmentId,
65  std::optional<Segment::Type> overrideSegmentType);
66 
70  enum class ControlMode {
71  Auto,
72  Macro,
73  Taxonomy
74  };
75 
82  virtual void addMessage(SegmentMessage::Type messageType, std::string body);
83 
89  virtual void addErrorMessage(std::string body);
90 
96  virtual void addWarningMessage(std::string body);
97 
103  virtual void addInfoMessage(std::string body);
104 
110  virtual void deletePick(const UUID &id);
111 
117  virtual std::set<const SegmentChoiceArrangement *> getArrangements();
118 
125  virtual std::set<const SegmentChoiceArrangement *> getArrangements(std::set<const SegmentChoice *> &choices);
126 
132  virtual Chain *getChain();
133 
141 
147  [[nodiscard]] virtual std::set<const SegmentChoice *> getChoices() const;
148 
157  virtual std::optional<const SegmentChoice *> getChoiceIfContinued(const ProgramVoice *voice);
158 
165  virtual std::set<const SegmentChoice *> getChoicesIfContinued(const Program::Type programType);
166 
173  virtual std::optional<const SegmentChoice *> getChoiceIfContinued(Instrument::Type instrumentType);
174 
181  virtual std::optional<const SegmentChoice *>
182  getChoiceIfContinued(Instrument::Type instrumentType, Instrument::Mode instrumentMode);
183 
191  virtual std::optional<const SegmentChord *> getChordAt(float position);
192 
198  virtual std::optional<const SegmentChoice *> getCurrentMainChoice();
199 
205  virtual std::optional<const ProgramSequence *> getCurrentMainSequence();
206 
212  virtual std::set<const SegmentChoice *> getCurrentDetailChoices();
213 
219  virtual std::optional<const SegmentChoice *> getCurrentBeatChoice();
220 
226  virtual std::set<Instrument::Type> getDistinctChordVoicingTypes();
227 
231  virtual long getElapsedMicros();
232 
242  virtual Chord getKeyForChoice(const SegmentChoice *choice);
243 
249  virtual std::optional<const SegmentChoice *> getMacroChoiceOfPreviousSegment();
250 
256  virtual std::optional<const SegmentChoice *> getPreviousMainChoice();
257 
265 
271  virtual std::optional<const ProgramSequence *> getPreviousMainSequence();
272 
279 
286 
295  virtual int getNextSequenceBindingOffset(const SegmentChoice *choice);
296 
304  static std::vector<std::string> getNotes(const SegmentChordVoicing *voicing);
305 
311  virtual std::set<const SegmentChoiceArrangementPick *> getPicks();
312 
319  virtual std::vector<const SegmentChoiceArrangementPick *> getPicks(const SegmentChoice *choice);
320 
326  virtual std::optional<const InstrumentAudio *> getPreferredAudio(const std::string &parentIdent, const std::string &ident);
327 
334  virtual std::optional<const Program *> getProgram(const SegmentChoice *choice);
335 
344  virtual std::vector<const ProgramSequenceChord *> getProgramSequenceChords(const ProgramSequence *programSequence);
345 
354  virtual NoteRange getProgramRange(const UUID &programId, Instrument::Type instrumentType);
355 
367  virtual int
368  getProgramRangeShiftOctaves(Instrument::Type instrumentType, NoteRange *sourceRange, NoteRange *targetRange);
369 
376  virtual std::optional<const ProgramSequence *> getProgramSequence(const SegmentChoice *choice);
377 
386  virtual int getProgramTargetShift(Instrument::Type instrumentType, const Chord *fromChord, const Chord *toChord);
387 
395  virtual Program::Type getProgramType(const ProgramVoice *voice);
396 
397 
409 
417  virtual NoteRange getProgramVoicingNoteRange(Instrument::Type instrumentType);
418 
424  virtual std::optional<const ProgramSequence *> getRandomlySelectedSequence(const Program *program);
425 
436  virtual std::optional<const ProgramSequencePattern *>
438 
446  virtual std::optional<const ProgramSequenceBinding *>
447  getRandomlySelectedSequenceBindingAtOffset(const Program *program, int offset);
448 
456  virtual std::optional<Note> getRootNoteMidRange(const std::string &voicingNotes, const Chord *chord);
457 
470  virtual long getSegmentMicrosAtPosition(float tempo, float position);
471 
475  virtual long getTotalSegmentMicros();
476 
482  virtual const Segment *getSegment();
483 
489  virtual std::vector<const SegmentChord *> getSegmentChords();
490 
496  virtual std::set<const SegmentChordVoicing *> getChordVoicings();
497 
503  virtual std::set<const SegmentMeme *> getSegmentMemes();
504 
514  virtual std::optional<const ProgramSequence *> getSequence(const SegmentChoice *choice);
515 
522  virtual int getSequenceBindingOffsetForChoice(const SegmentChoice *choice);
523 
531  virtual void putStickyBun(StickyBun bun);
532 
553  virtual std::optional<const StickyBun> getStickyBun(const UUID &eventId);
554 
562  virtual std::string getTrackName(const ProgramSequencePatternEvent *event);
563 
570  virtual Segment::Type getType();
571 
579  virtual std::optional<const SegmentChordVoicing *>
580  chooseVoicing(const SegmentChord *chord, Instrument::Type instrumentType);
581 
589  virtual bool hasMoreSequenceBindingOffsets(const SegmentChoice *choice, int N);
590 
598  virtual bool hasOneMoreSequenceBindingOffset(const SegmentChoice *choice);
599 
607  virtual bool hasTwoMoreSequenceBindingOffsets(const SegmentChoice *choice);
608 
615  virtual bool isContinuationOfMacroProgram();
616 
624  virtual bool isDirectlyBound(const Instrument *instrument);
625 
633  virtual bool isDirectlyBound(const InstrumentAudio *instrumentAudio);
634 
642  virtual bool isDirectlyBound(const Program *program);
643 
652  virtual bool isOneShot(const Instrument *instrument, const std::string &trackName);
653 
661  virtual bool isOneShot(const Instrument *instrument);
662 
670  virtual bool isOneShotCutoffEnabled(const Instrument *instrument);
671 
677  virtual bool isInitialSegment();
678 
693  virtual std::optional<const SegmentChoice *> put(const SegmentChoice &entity, const bool force);
694 
702  virtual const SegmentChoiceArrangement *put(const SegmentChoiceArrangement &entity);
703 
711  virtual const SegmentChoiceArrangementPick *put(const SegmentChoiceArrangementPick &entity);
712 
720  virtual const SegmentChord *put(const SegmentChord &entity);
721 
729  virtual const SegmentChordVoicing *put(const SegmentChordVoicing &entity);
730 
739  virtual std::optional<const SegmentMeme *> put(const SegmentMeme &entity, bool force);
740 
748  virtual const SegmentMessage *put(const SegmentMessage &entity);
749 
757  virtual const SegmentMeta *put(const SegmentMeta &entity);
758 
766  virtual void putPreferredAudio(
767  const std::string &parentIdent,
768  const std::string &ident,
769  const InstrumentAudio *instrumentAudio);
770 
779  virtual const Segment *updateSegment(Segment segment);
780 
786  virtual const SegmentRetrospective *getRetrospective();
787 
794 
801  virtual double getMicrosPerBeat(float tempo);
802 
809  virtual int getSecondMacroSequenceBindingOffset(const Program *macroProgram);
810 
815  virtual double getTempo();
816 
820  [[nodiscard]] virtual MemeTaxonomy getMemeTaxonomy() const;
821 
827  static int getSegmentId(const SegmentChoice *segmentChoice);
828 
832  static int getSegmentId(const SegmentChoiceArrangement *segmentChoiceArrangement);
833 
837  static int getSegmentId(const SegmentChoiceArrangementPick *segmentChoiceArrangementPick);
838 
842  static int getSegmentId(const SegmentChord *segmentChord);
843 
847  static int getSegmentId(const SegmentChordVoicing *segmentChordVoicing);
848 
852  static int getSegmentId(const SegmentMeme *segmentMeme);
853 
857  static int getSegmentId(const SegmentMessage *segmentMessage);
858 
862  static int getSegmentId(const SegmentMeta *segmentMeta);
863 
868  static std::string toString(ControlMode controlMode);
869 
875  static ControlMode parseControlMode(const char * str);
876 
877  private:
878  static const std::string KEY_VOICE_TRACK_TEMPLATE;
879  static const std::string NAME_SEPARATOR;
880  static const std::string UNKNOWN_KEY;
881  Chain *chain;
882  std::set<const TemplateBinding *> templateBindings;
883  ContentEntityStore *sourceMaterial;
884  std::map<double, std::optional<SegmentChord *>> chordAtPosition;
885  std::map<Instrument::Type, NoteRange> voicingNoteRange;
886  std::map<const SegmentChoice *, const ProgramSequence *> sequenceForChoice;
887  std::map<std::string, const InstrumentAudio *> preferredAudios;
888  std::map<std::string, InstrumentConfig> instrumentConfigs;
889  std::map<std::string, InstrumentConfig> pickInstrumentConfigs;
890  std::map<std::string, int> rangeShiftOctave;
891  std::map<std::string, int> targetShift;
892  std::map<std::string, NoteRange> rangeForChoice;
893  std::map<std::string, std::optional<Note>> rootNotesByVoicingAndChord;
894  std::map<UUID, std::vector<const ProgramSequenceChord *>> completeChordsForProgramSequence;
895  std::map<UUID, std::vector<const SegmentChoiceArrangementPick *>> picksForChoice;
896  SegmentEntityStore *store;
897  int segmentId;
898  const SegmentRetrospective * retrospective;
899  std::set<UUID> boundInstrumentIds;
900  std::set<UUID> boundProgramIds;
901  std::chrono::high_resolution_clock::time_point startAtSystemNanoTime;
902  std::optional<Segment::Type> type;
903 
904  std::optional<const SegmentChoice *> macroChoiceOfPreviousSegment;
905  std::optional<const SegmentChoice *> mainChoiceOfPreviousSegment;
906 
907  double microsPerBeat{};
908 
909  std::set<Instrument::Type> distinctChordVoicingTypes;
910 
916  [[nodiscard]] std::optional<const SegmentMeta *> getSegmentMeta(const std::string &key) const;
917 
924  [[nodiscard]] std::optional<const SegmentChoice *> getChoiceOfType(Program::Type programType) const;
925 
931  [[nodiscard]] std::set<const SegmentChoice *> getBeatChoices() const;
932 
940  static int computeLowestOptimalRangeShiftOctaves(const NoteRange &sourceRange, const NoteRange &targetRange);
941 
949  static std::string computeShipKey(const Chain *chain, const Segment *segment);
950 
954  void ensureShipKey();
955 
961  Segment::Type computeType();
962 
968  [[nodiscard]] int getPreviousSegmentDelta() const;
969 
975  std::map<std::string, const InstrumentAudio *> computePreferredInstrumentAudio();
976 
985  bool isValidChoiceAndMemesHaveBeenAdded(const SegmentChoice &choice, const MemeStack &memeStack, bool force);
986 
995  bool isValidMemeAddition(const SegmentMeme &meme, const MemeStack &memeStack, bool force);
996 
1003  static std::string computeCacheKeyForPreferredAudio(const std::string &parentIdent, const std::string &ident);
1004 
1010  std::string computeCacheKeyForVoiceTrack(const SegmentChoiceArrangementPick *pick);
1011 
1018  [[nodiscard]] NoteRange computeProgramRange(const UUID &programId, Instrument::Type instrumentType) const;
1019  };
1020 
1021 }// namespace XJ
1022 
1023 #endif//XJMUSIC_FABRICATOR_H
Definition: Chain.h:14
Definition: Chord.h:53
Definition: ContentEntityStore.h:41
Definition: Fabricator.h:48
virtual NoteRange getProgramVoicingNoteRange(Instrument::Type instrumentType)
Definition: Fabricator.cpp:597
virtual std::optional< const ProgramSequenceBinding * > getRandomlySelectedSequenceBindingAtOffset(const Program *program, int offset)
Definition: Fabricator.cpp:633
virtual std::set< Instrument::Type > getDistinctChordVoicingTypes()
Definition: Fabricator.cpp:183
virtual std::optional< const ProgramSequence * > getProgramSequence(const SegmentChoice *choice)
Definition: Fabricator.cpp:306
virtual const SegmentRetrospective * getRetrospective()
Definition: Fabricator.cpp:1026
virtual bool hasMoreSequenceBindingOffsets(const SegmentChoice *choice, int N)
Definition: Fabricator.cpp:881
virtual long getElapsedMicros()
Definition: Fabricator.cpp:207
virtual std::optional< const Program * > getProgram(const SegmentChoice *choice)
Definition: Fabricator.cpp:478
static ControlMode parseControlMode(const char *str)
Definition: Fabricator.cpp:1340
virtual void addErrorMessage(std::string body)
Definition: Fabricator.cpp:75
virtual std::set< const SegmentChordVoicing * > getChordVoicings()
Definition: Fabricator.cpp:785
virtual void putPreferredAudio(const std::string &parentIdent, const std::string &ident, const InstrumentAudio *instrumentAudio)
Definition: Fabricator.cpp:1004
virtual int getNextSequenceBindingOffset(const SegmentChoice *choice)
Definition: Fabricator.cpp:404
virtual std::set< const SegmentChoice * > getCurrentDetailChoices()
Definition: Fabricator.cpp:162
virtual bool isDirectlyBound(const Instrument *instrument)
Definition: Fabricator.cpp:937
virtual std::set< const SegmentChoice * > getChoices() const
Definition: Fabricator.cpp:132
virtual int getSecondMacroSequenceBindingOffset(const Program *macroProgram)
Definition: Fabricator.cpp:1043
static std::vector< std::string > getNotes(const SegmentChordVoicing *voicing)
Definition: Fabricator.cpp:424
virtual double getMicrosPerBeat(float tempo)
Definition: Fabricator.cpp:1036
virtual std::optional< const SegmentChoice * > getMacroChoiceOfPreviousSegment()
Definition: Fabricator.cpp:318
virtual Chord getKeyForChoice(const SegmentChoice *choice)
Definition: Fabricator.cpp:288
virtual const Segment * updateSegment(Segment segment)
Definition: Fabricator.cpp:1013
virtual Chain * getChain()
Definition: Fabricator.cpp:122
virtual MemeIsometry getMemeIsometryOfSegment()
Definition: Fabricator.cpp:399
virtual const Segment * getSegment()
Definition: Fabricator.cpp:767
virtual std::optional< const ProgramSequence * > getSequence(const SegmentChoice *choice)
Definition: Fabricator.cpp:795
Fabricator(ContentEntityStore *contentEntityStore, SegmentEntityStore *segmentEntityStore, const SegmentRetrospective *segmentRetrospective, int segmentId, std::optional< Segment::Type > overrideSegmentType)
Definition: Fabricator.cpp:19
virtual Instrument::Type getProgramVoiceType(const ProgramSequenceChordVoicing *voicing)
Definition: Fabricator.cpp:589
virtual void deletePick(const UUID &id)
Definition: Fabricator.cpp:90
virtual MemeTaxonomy getMemeTaxonomy() const
Definition: Fabricator.cpp:1058
static std::string toString(ControlMode controlMode)
Definition: Fabricator.cpp:1326
virtual NoteRange getProgramRange(const UUID &programId, Instrument::Type instrumentType)
Definition: Fabricator.cpp:519
virtual bool isOneShot(const Instrument *instrument, const std::string &trackName)
Definition: Fabricator.cpp:922
virtual std::optional< const SegmentChoice * > getCurrentBeatChoice()
Definition: Fabricator.cpp:178
virtual bool isOneShotCutoffEnabled(const Instrument *instrument)
Definition: Fabricator.cpp:932
virtual std::optional< const ProgramSequence * > getCurrentMainSequence()
Definition: Fabricator.cpp:349
virtual ~Fabricator()=default
virtual bool isInitialSegment()
Definition: Fabricator.cpp:947
virtual std::set< const SegmentChoiceArrangement * > getArrangements()
Definition: Fabricator.cpp:95
virtual std::optional< const ProgramSequence * > getRandomlySelectedSequence(const Program *program)
Definition: Fabricator.cpp:617
virtual std::set< const SegmentChoiceArrangementPick * > getPicks()
Definition: Fabricator.cpp:429
virtual ProgramConfig getCurrentMainProgramConfig()
Definition: Fabricator.cpp:332
virtual std::optional< const StickyBun > getStickyBun(const UUID &eventId)
Definition: Fabricator.cpp:695
virtual int getSequenceBindingOffsetForChoice(const SegmentChoice *choice)
Definition: Fabricator.cpp:825
virtual std::optional< const SegmentChoice * > getChoiceIfContinued(const ProgramVoice *voice)
Definition: Fabricator.cpp:214
virtual double getTempo()
Definition: Fabricator.cpp:1063
virtual void addWarningMessage(std::string body)
Definition: Fabricator.cpp:80
virtual bool hasOneMoreSequenceBindingOffset(const SegmentChoice *choice)
Definition: Fabricator.cpp:902
virtual std::string getTrackName(const ProgramSequencePatternEvent *event)
Definition: Fabricator.cpp:837
virtual void putStickyBun(StickyBun bun)
Definition: Fabricator.cpp:684
virtual std::vector< const ProgramSequenceChord * > getProgramSequenceChords(const ProgramSequence *programSequence)
Definition: Fabricator.cpp:483
virtual int getProgramRangeShiftOctaves(Instrument::Type instrumentType, NoteRange *sourceRange, NoteRange *targetRange)
Definition: Fabricator.cpp:531
virtual std::optional< const SegmentChoice * > put(const SegmentChoice &entity, const bool force)
Definition: Fabricator.cpp:952
virtual std::optional< const ProgramSequencePattern * > getRandomlySelectedPatternOfSequenceByVoiceAndType(const SegmentChoice *choice)
Definition: Fabricator.cpp:646
virtual bool hasTwoMoreSequenceBindingOffsets(const SegmentChoice *choice)
Definition: Fabricator.cpp:907
virtual long getSegmentMicrosAtPosition(float tempo, float position)
Definition: Fabricator.cpp:752
virtual std::optional< const ProgramSequence * > getPreviousMainSequence()
Definition: Fabricator.cpp:357
virtual ContentEntityStore * getSourceMaterial()
Definition: Fabricator.cpp:1031
virtual void addMessage(SegmentMessage::Type messageType, std::string body)
Definition: Fabricator.cpp:58
virtual void addInfoMessage(std::string body)
Definition: Fabricator.cpp:85
virtual std::optional< const SegmentChoice * > getPreviousMainChoice()
Definition: Fabricator.cpp:325
virtual std::set< const SegmentChoice * > getChoicesIfContinued(const Program::Type programType)
Definition: Fabricator.cpp:273
virtual MemeIsometry getMemeIsometryOfNextSequenceInPreviousMacro()
Definition: Fabricator.cpp:365
virtual Segment::Type getType()
Definition: Fabricator.cpp:847
virtual std::optional< const InstrumentAudio * > getPreferredAudio(const std::string &parentIdent, const std::string &ident)
Definition: Fabricator.cpp:466
virtual Program::Type getProgramType(const ProgramVoice *voice)
Definition: Fabricator.cpp:579
virtual std::optional< const SegmentChordVoicing * > chooseVoicing(const SegmentChord *chord, Instrument::Type instrumentType)
Definition: Fabricator.cpp:855
virtual std::optional< const SegmentChord * > getChordAt(float position)
Definition: Fabricator.cpp:137
virtual std::optional< const SegmentChoice * > getCurrentMainChoice()
Definition: Fabricator.cpp:157
virtual int getProgramTargetShift(Instrument::Type instrumentType, const Chord *fromChord, const Chord *toChord)
Definition: Fabricator.cpp:558
ControlMode
Definition: Fabricator.h:70
virtual std::set< const SegmentMeme * > getSegmentMemes()
Definition: Fabricator.cpp:790
static int getSegmentId(const SegmentChoice *segmentChoice)
Definition: Fabricator.cpp:1286
virtual std::vector< const SegmentChord * > getSegmentChords()
Definition: Fabricator.cpp:775
virtual TemplateConfig getTemplateConfig()
Definition: Fabricator.cpp:127
virtual bool isContinuationOfMacroProgram()
Definition: Fabricator.cpp:912
virtual long getTotalSegmentMicros()
Definition: Fabricator.cpp:757
virtual std::optional< Note > getRootNoteMidRange(const std::string &voicingNotes, const Chord *chord)
Definition: Fabricator.cpp:667
Definition: InstrumentAudio.h:13
Definition: Instrument.h:14
Mode
Definition: Instrument.h:29
Type
Definition: Instrument.h:16
Definition: MemeIsometry.h:21
Definition: MemeStack.h:27
Definition: MemeTaxonomy.h:112
Definition: NoteRange.h:15
Definition: ProgramConfig.h:11
Definition: ProgramSequenceChordVoicing.h:13
Definition: ProgramSequencePatternEvent.h:13
Definition: ProgramSequence.h:13
Definition: ProgramVoice.h:13
Definition: Program.h:14
Type
Definition: Program.h:16
Definition: SegmentChoiceArrangementPick.h:14
Definition: SegmentChoiceArrangement.h:14
Definition: SegmentChoice.h:15
Definition: SegmentChordVoicing.h:16
Definition: SegmentChord.h:13
Definition: SegmentEntityStore.h:43
Definition: SegmentMeme.h:13
Definition: SegmentMessage.h:14
Type
Definition: SegmentMessage.h:17
Definition: SegmentMeta.h:14
Definition: SegmentRetrospective.h:30
Definition: Segment.h:13
Type
Definition: Segment.h:16
Definition: StickyBun.h:20
Definition: TemplateConfig.h:16
Definition: ActiveAudio.h:11
std::string UUID
Definition: EntityUtils.h:28