Allolib  1.0
C++ Components For Interactive Multimedia
al_SequenceRecorder.hpp
1 #ifndef AL_SEQUENCERECORDER_H
2 #define AL_SEQUENCERECORDER_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  Preset sequence recorder
41 
42  File author(s):
43  AndrĂ©s Cabrera mantaraya36@gmail.com
44 */
45 
46 #include <condition_variable>
47 #include <cstring>
48 #include <mutex>
49 #include <queue>
50 #include <string>
51 #include <thread>
52 #include <utility>
53 
54 #include "al/ui/al_PresetHandler.hpp"
55 #include "al/ui/al_PresetSequencer.hpp"
56 
57 namespace al {
58 
83  public:
85 
86  ~SequenceRecorder() override {}
87 
96  void startRecord(std::string name = "", bool overwrite = false);
97  void stopRecord();
98 
99  bool recording() { return mRecording; }
100 
101  void setMaxRecordTime(al_sec maxTime) { mMaxRecordTime = maxTime; }
102 
103  std::string lastSequenceName();
104  std::string lastSequenceSubDir();
105 
113  void setDirectory(std::string directory);
114 
115  std::string getCurrentPath() { return mPresetHandler->getCurrentPath(); }
116 
117  SequenceRecorder &operator<<(PresetHandler &handler) {
118  registerPresetHandler(handler);
119  return *this;
120  }
121 
122  void registerPresetHandler(PresetHandler &handler) {
123  mPresetHandler = &handler;
124  if (mPresetHandler) {
125  for (auto *presetParam : mPresetHandler->parameters()) {
126  for (auto *p : mParameters) {
127  if (presetParam->getFullAddress() == p->getFullAddress()) {
128  std::cerr << "ERROR: Parameters registered with sequence recorder "
129  "must not be part of registered presets: "
130  << p->getFullAddress() << std::endl;
131  }
132  }
133  }
134  mPresetHandler->registerPresetCallback(SequenceRecorder::presetChanged,
135  (void *)this);
136  }
137  }
138 
139  SequenceRecorder &operator<<(ParameterMeta &param) {
140  registerParameter(param);
141  return *this;
142  }
143 
144  void registerParameter(ParameterMeta &p);
145 
146  protected:
147  virtual bool consumeMessage(osc::Message &m,
148  std::string rootOSCPath) override;
149 
150  private:
151  static void presetChanged(int index, void *sender, void *userData);
152  static void recorderFunction(SequenceRecorder *recorder,
153  std::string sequenceName);
154 
155  // struct Step {
156  // std::string presetName;
157  // float delta; // The time to get to the preset
158  // float duration; // The time to stay in the preset before the
159  // next step
160  // };
161 
162  std::string mDirectory;
163  PresetHandler *mPresetHandler;
164  std::vector<ParameterMeta *> mParameters;
165  // std::string mPresetName;
166  bool mOverwrite;
167  std::string mLastSequenceName;
168  std::string mLastSequenceSubDir;
169 
170  std::mutex mSequenceLock;
171  std::condition_variable mSequenceConditionVar;
172 
173  bool mRecording;
174  std::thread *mRecorderThread;
175 
176  PresetSequencer::Step mStepToInsert;
177 
178  al_sec mMaxRecordTime;
179 };
180 
181 } // namespace al
182 
183 #endif // AL_SEQUENCERECORDER_H
The PresetHandler class handles sorting and recalling of presets.
void registerPresetCallback(std::function< void(int index, void *sender, void *userData)> cb, void *userData=nullptr)
Register a callback to be notified when a preset is loaded.
std::string getCurrentPath()
Path including subdirectory if any.
The SequenceRecorder class records preset changes in a ".sequence" file.
void startRecord(std::string name="", bool overwrite=false)
startRecord begins recording preset changes
virtual bool consumeMessage(osc::Message &m, std::string rootOSCPath) override
Returns true if message was consumed by this class.
void setDirectory(std::string directory)
setDirectory sets the working directory for the SequenceRecorder
Definition: al_App.hpp:23