Allolib  1.0
C++ Components For Interactive Multimedia
al_Composition.hpp
1 #ifndef AL_COMPOSITION_H
2 #define AL_COMPOSITION_H
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012-2016. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39  File description:
40  Composition player
41 
42  File author(s):
43  AndrĂ©s Cabrera mantaraya36@gmail.com
44 */
45 
46 #include <string>
47 #include <thread>
48 #include <vector>
49 
50 #include "al/ui/al_PresetHandler.hpp"
51 #include "al/ui/al_PresetSequencer.hpp"
52 
53 namespace al {
54 
58  public:
59  std::string sequenceName;
60  float deltaTime;
61 };
62 
66  public:
67  Composition(std::string fileName, std::string path);
68  ~Composition();
69 
70  void play();
71  void stop();
72 
73  bool playArchive(std::string archiveName);
74 
75  std::string getName();
76 
77  // /// Archive current compostion
78  // bool archiveComposition();
79 
80  void setSubDirectory(std::string subDir) { mSubDirectory = subDir; }
81 
82  static bool archive(std::string compositionName, std::string path = "",
83  bool overwrite = true);
84  static bool restore(std::string compositionName, std::string path = "",
85  bool overwrite = true);
86 
87  int size();
88  const CompositionStep getStep(int index);
89  void insertStep(std::string name, float deltaTime, int index);
90  void deleteStep(int index);
91 
92  void write();
93 
94  void registerBeginCallback(
95  std::function<void(Composition *sender, void *userData)> beginCallback,
96  void *userData = nullptr);
97 
98  void enableBeginCallback(bool enable) { mBeginCallbackEnabled = enable; }
99 
100  void registerEndCallback(
101  std::function<void(bool finished, Composition *sender, void *userData)>
102  endCallback,
103  void *userData = nullptr);
104 
105  void enableEndCallback(bool enable) { mEndCallbackEnabled = enable; }
106 
107  Composition &registerSequencer(PresetSequencer &sequencer) {
108  mSequencer = &sequencer;
109  return *this;
110  }
111  Composition &operator<<(PresetSequencer &sequencer) {
112  return registerSequencer(sequencer);
113  }
114 
115  protected:
116  virtual bool consumeMessage(osc::Message &m,
117  std::string rootOSCPath) override;
118 
119  private:
120  std::vector<CompositionStep> mCompositionSteps;
121  std::string mPath;
122  std::string mSubDirectory;
123  std::string mCompositionName;
124 
125  std::thread *mPlayThread;
126  bool mPlaying;
127  PresetSequencer *mSequencer;
128  std::mutex mPlayerLock;
129  std::mutex mPlayWaitLock;
130  std::condition_variable mPlayWaitVariable;
131 
132  // This interface is shared with PresetSequencer, perhaps this should be
133  // abstracted?
134  bool mBeginCallbackEnabled;
135  std::function<void(Composition *, void *userData)> mBeginCallback;
136  void *mBeginCallbackData;
137  bool mEndCallbackEnabled;
138  std::function<void(bool, Composition *, void *userData)> mEndCallback;
139  void *mEndCallbackData;
140 
141  std::function<void(bool, PresetSequencer *)> mSequencerEndCallbackCache;
142 
143  std::vector<CompositionStep> loadCompositionSteps(
144  std::string compositionSteps);
145  std::string getRootPath();
146  std::string getCurrentPath();
147 
148  static void playbackThread(Composition *composition);
149 };
150 
151 } // namespace al
152 
153 #endif // AL_COMPOSITION_H
virtual bool consumeMessage(osc::Message &m, std::string rootOSCPath) override
Returns true if message was consumed by this class.
The PresetSequencer class allows triggering presets from a PresetHandler over time.
Definition: al_App.hpp:23