1 /*
2  * Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of Zrythm
5  *
6  * Zrythm is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Zrythm is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 /**
21  * \file
22  *
23  * Handles recording.
24  */
25 
26 #ifndef __AUDIO_RECORDING_MANAGER_H__
27 #define __AUDIO_RECORDING_MANAGER_H__
28 
29 #include "utils/types.h"
30 #include "zix/sem.h"
31 
32 typedef struct ObjectPool ObjectPool;
33 typedef struct TrackProcessor TrackProcessor;
34 typedef struct MPMCQueue MPMCQueue;
35 
36 /**
37  * @addtogroup audio
38  *
39  * @{
40  */
41 
42 #define RECORDING_MANAGER (ZRYTHM->recording_manager)
43 
44 typedef struct RecordingManager
45 {
46   /** Number of recordings currently in progress. */
47   int                num_active_recordings;
48 
49   /** Event queue. */
50   MPMCQueue *        event_queue;
51 
52   /**
53    * Object pool of event structs to avoid real time
54    * allocation.
55    */
56   ObjectPool *       event_obj_pool;
57 
58   /** Cloned selections before starting recording. */
59   ArrangerSelections * selections_before_start;
60 
61   /** Source func ID. */
62   guint              source_id;
63 
64   /**
65    * Recorded region identifiers, to be used for
66    * creating the undoable actions.
67    *
68    * TODO use region pointers ?
69    */
70   RegionIdentifier   recorded_ids[8000];
71   int                num_recorded_ids;
72 
73   bool               currently_processing;
74   ZixSem             processing_sem;
75 
76   bool               freeing;
77 } RecordingManager;
78 
79 /**
80  * Handles the recording logic inside the process
81  * cycle.
82  *
83  * The MidiEvents are already dequeued at this
84  * point.
85  *
86  * @param g_frames_start Global start frames.
87  * @param nframes Number of frames to process. If
88  *   this is zero, a pause will be added. See \ref
89  *   RECORDING_EVENT_TYPE_PAUSE_TRACK_RECORDING and
90  *   RECORDING_EVENT_TYPE_PAUSE_AUTOMATION_RECORDING.
91  */
92 REALTIME
93 void
94 recording_manager_handle_recording (
95   RecordingManager *     self,
96   const TrackProcessor * track_processor,
97   const EngineProcessTimeInfo * const time_nfo);
98 
99 /**
100  * GSourceFunc to be added using idle add.
101  *
102  * This will loop indefinintely.
103  *
104  * It can also be called to process the events
105  * immediately. Should only be called from the
106  * GTK thread.
107  */
108 int
109 recording_manager_process_events (
110   RecordingManager * self);
111 
112 /**
113  * Creates the event queue and starts the event loop.
114  *
115  * Must be called from a GTK thread.
116  */
117 RecordingManager *
118 recording_manager_new (void);
119 
120 void
121 recording_manager_free (
122   RecordingManager * self);
123 
124 /**
125  * @}
126  */
127 
128 #endif
129