1 /*
2  * Copyright (C) 2019-2021 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  * Sample processor.
24  */
25 
26 #ifndef __AUDIO_SAMPLE_PROCESSOR_H__
27 #define __AUDIO_SAMPLE_PROCESSOR_H__
28 
29 #include "audio/fader.h"
30 #include "audio/port.h"
31 #include "audio/sample_playback.h"
32 #include "utils/types.h"
33 
34 typedef enum MetronomeType MetronomeType;
35 typedef struct SupportedFile SupportedFile;
36 typedef struct Tracklist Tracklist;
37 typedef struct PluginSetting PluginSetting;
38 typedef struct MidiEvents MidiEvents;
39 
40 /**
41  * @addtogroup audio
42  *
43  * @{
44  */
45 
46 #define SAMPLE_PROCESSOR_SCHEMA_VERSION 1
47 
48 #define SAMPLE_PROCESSOR \
49   (AUDIO_ENGINE->sample_processor)
50 
51 #define sample_processor_is_in_active_project(self) \
52   (self->audio_engine \
53    && \
54    engine_is_in_active_project (self->audio_engine))
55 
56 /**
57  * A processor to be used in the routing graph for
58  * playing samples independent of the timeline.
59  *
60  * Also used for auditioning files.
61  */
62 typedef struct SampleProcessor
63 {
64   int               schema_version;
65 
66   /** An array of samples currently being played. */
67   SamplePlayback    current_samples[256];
68   int               num_current_samples;
69 
70   /** Tracklist for file auditioning. */
71   Tracklist *       tracklist;
72 
73   /** Instrument for MIDI auditioning. */
74   PluginSetting *   instrument_setting;
75 
76   MidiEvents *      midi_events;
77 
78   /** Fader connected to the main output. */
79   Fader *           fader;
80 
81   /** Playhead for the tracklist (used when
82    * auditioning files). */
83   Position          playhead;
84 
85   /**
86    * Position the file ends at.
87    *
88    * Once this position is reached,
89    * SampleProcessor.roll will be set to false.
90    */
91   Position          file_end_pos;
92 
93   /** Whether to roll or not. */
94   bool              roll;
95 
96   /** Pointer to owner audio engin, if any. */
97   AudioEngine *     audio_engine;
98 } SampleProcessor;
99 
100 static const cyaml_schema_field_t
101 sample_processor_fields_schema[] =
102 {
103   YAML_FIELD_INT (
104     SampleProcessor, schema_version),
105   YAML_FIELD_MAPPING_PTR (
106     SampleProcessor, fader,
107     fader_fields_schema),
108 
109   CYAML_FIELD_END
110 };
111 
112 static const cyaml_schema_value_t
113 sample_processor_schema =
114 {
115   YAML_VALUE_PTR (
116     SampleProcessor,
117     sample_processor_fields_schema),
118 };
119 
120 /**
121  * Initializes a SamplePlayback with a sample to
122  * play back.
123  */
124 COLD
125 WARN_UNUSED_RESULT
126 SampleProcessor *
127 sample_processor_new (
128   AudioEngine * engine);
129 
130 COLD
131 void
132 sample_processor_init_loaded (
133   SampleProcessor * self,
134   AudioEngine *     engine);
135 
136 /**
137  * Clears the buffers.
138  */
139 void
140 sample_processor_prepare_process (
141   SampleProcessor * self,
142   const nframes_t   nframes);
143 
144 /**
145  * Process the samples for the given number of
146  * frames.
147  *
148  * @param offset The local offset in the processing
149  *   cycle.
150  */
151 void
152 sample_processor_process (
153   SampleProcessor * self,
154   const nframes_t   offset,
155   const nframes_t   nframes);
156 
157 /**
158  * Removes a SamplePlayback from the array.
159  */
160 void
161 sample_processor_remove_sample_playback (
162   SampleProcessor * self,
163   SamplePlayback *  sp);
164 
165 /**
166  * Queues a metronomem tick at the given offset.
167  *
168  * Used for countin.
169  */
170 void
171 sample_processor_queue_metronome_countin (
172   SampleProcessor * self);
173 
174 /**
175  * Queues a metronomem tick at the given local
176  * offset.
177  *
178  * Realtime function.
179  */
180 void
181 sample_processor_queue_metronome (
182   SampleProcessor * self,
183   MetronomeType     type,
184   nframes_t         offset);
185 
186 /**
187  * Adds a sample to play to the queue from a file
188  * path.
189  */
190 void
191 sample_processor_queue_sample_from_file (
192   SampleProcessor * self,
193   const char *      path);
194 
195 /**
196  * Adds a file (audio or MIDI) to the queue.
197  */
198 void
199 sample_processor_queue_file (
200   SampleProcessor *     self,
201   const SupportedFile * file);
202 
203 /**
204  * Stops playback of files (auditioning).
205  */
206 void
207 sample_processor_stop_file_playback (
208   SampleProcessor *     self);
209 
210 void
211 sample_processor_disconnect (
212   SampleProcessor * self);
213 
214 /**
215  * To be used for serialization.
216  */
217 SampleProcessor *
218 sample_processor_clone (
219   const SampleProcessor * src);
220 
221 void
222 sample_processor_free (
223   SampleProcessor * self);
224 
225 /**
226  * @}
227  */
228 
229 #endif
230