1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2005-2020 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU 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  * GSequencer 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __AGS_CHANNEL_H__
21 #define __AGS_CHANNEL_H__
22 
23 #include <glib.h>
24 #include <glib-object.h>
25 
26 #include <ags/libags.h>
27 
28 #include <ags/audio/ags_sound_enums.h>
29 #include <ags/audio/ags_recall_id.h>
30 #include <ags/audio/ags_recall.h>
31 #include <ags/audio/ags_recycling.h>
32 #include <ags/audio/ags_notation.h>
33 
34 #include <math.h>
35 
36 G_BEGIN_DECLS
37 
38 #define AGS_TYPE_CHANNEL                (ags_channel_get_type())
39 #define AGS_CHANNEL(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), AGS_TYPE_CHANNEL, AgsChannel))
40 #define AGS_CHANNEL_CLASS(class)        (G_TYPE_CHECK_CLASS_CAST((class), AGS_TYPE_CHANNEL, AgsChannelClass))
41 #define AGS_IS_CHANNEL(obj)             (G_TYPE_CHECK_INSTANCE_TYPE((obj), AGS_TYPE_CHANNEL))
42 #define AGS_IS_CHANNEL_CLASS(class)     (G_TYPE_CHECK_CLASS_TYPE ((class), AGS_TYPE_CHANNEL))
43 #define AGS_CHANNEL_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS((obj), AGS_TYPE_CHANNEL, AgsChannelClass))
44 
45 #define AGS_CHANNEL_GET_OBJ_MUTEX(obj) (&(((AgsChannel *) obj)->obj_mutex))
46 #define AGS_CHANNEL_GET_PLAY_MUTEX(obj) (&(((AgsChannel *) obj)->play_mutex))
47 #define AGS_CHANNEL_GET_RECALL_MUTEX(obj) (&(((AgsChannel *) obj)->recall_mutex))
48 
49 #define AGS_CHANNEL_MINIMUM_OCTAVE (0)
50 #define AGS_CHANNEL_MAXIMUM_OCTAVE (10)
51 #define AGS_CHANNEL_DEFAULT_OCTAVE (AGS_CHANNEL_MINIMUM_OCTAVE)
52 
53 #define AGS_CHANNEL_MINIMUM_OCTAVE_SEMITONE (0)
54 #define AGS_CHANNEL_MAXIMUM_OCTAVE_SEMITONE (12)
55 #define AGS_CHANNEL_DEFAULT_OCTAVE_SEMITONE (AGS_CHANNEL_MINIMUM_OCTAVE_SEMITONE)
56 
57 #define AGS_CHANNEL_MINIMUM_SEMITONE (0)
58 #define AGS_CHANNEL_MAXIMUM_SEMITONE (128)
59 #define AGS_CHANNEL_DEFAULT_SEMITONE (AGS_CHANNEL_MINIMUM_SEMITONE)
60 
61 #define AGS_CHANNEL_MINIMUM_NOTE_FREQUENCY (440.0 * exp((-69.0 / 12.0) * log(2.0)))
62 #define AGS_CHANNEL_MAXIMUM_NOTE_FREQUENCY (440.0 * exp((58.0 / 12.0) * log(2.0)))
63 #define AGS_CHANNEL_DEFAULT_NOTE_FREQUENCY (AGS_CHANNEL_MINIMUM_NOTE_FREQUENCY)
64 
65 #define AGS_CHANNEL_MINIMUM_MIDI_NOTE (0)
66 #define AGS_CHANNEL_MAXIMUM_MIDI_NOTE (127)
67 #define AGS_CHANNEL_DEFAULT_MIDI_NOTE (AGS_CHANNEL_MINIMUM_MIDI_NOTE)
68 
69 typedef struct _AgsChannel AgsChannel;
70 typedef struct _AgsChannelClass AgsChannelClass;
71 
72 /**
73  * AgsChannelFlags:
74  * @AGS_CHANNEL_ADDED_TO_REGISTRY: the channel was added to registry, see #AgsConnectable::add_to_registry()
75  * @AGS_CHANNEL_CONNECTED: indicates the channel was connected by calling #AgsConnectable::connect()
76  * @AGS_CHANNEL_BYPASS: don't apply any data
77  *
78  * Enum values to control the behavior or indicate internal state of #AgsChannel by
79  * enable/disable as flags.
80  */
81 typedef enum{
82   AGS_CHANNEL_ADDED_TO_REGISTRY  = 1,
83   AGS_CHANNEL_CONNECTED          = 1 <<  1,
84   AGS_CHANNEL_BYPASS             = 1 <<  2,
85 }AgsChannelFlags;
86 
87 #define AGS_CHANNEL_ERROR (ags_channel_error_quark())
88 
89 typedef enum{
90   AGS_CHANNEL_ERROR_LOOP_IN_LINK,
91 }AgsChannelError;
92 
93 struct _AgsChannel
94 {
95   GObject gobject;
96 
97   guint flags;
98   guint ability_flags;
99   guint behaviour_flags;
100   guint staging_flags[AGS_SOUND_SCOPE_LAST];
101 
102   gboolean staging_completed[AGS_SOUND_SCOPE_LAST];
103 
104   GRecMutex obj_mutex;
105 
106   AgsUUID *uuid;
107 
108   GObject *audio;
109 
110   GObject *output_soundcard;
111   gint output_soundcard_channel;
112 
113   GObject *input_soundcard;
114   gint input_soundcard_channel;
115 
116   guint samplerate;
117   guint buffer_size;
118   guint format;
119 
120   guint pad;
121   guint audio_channel;
122   guint line;
123 
124   gint octave;
125   guint key;
126   gint absolute_key;
127 
128   gdouble note_frequency;
129   gchar *note_key;
130 
131   guint midi_note;
132 
133   AgsChannel *prev;
134   AgsChannel *prev_pad;
135   AgsChannel *next;
136   AgsChannel *next_pad;
137 
138   AgsChannel *link;
139 
140   AgsRecycling *first_recycling;
141   AgsRecycling *last_recycling;
142 
143   GObject *playback;
144 
145   GList *pattern;
146 
147   GList *recall_id;
148   GList *recycling_context;
149 
150   GList *recall_container;
151 
152   GRecMutex play_mutex;
153 
154   GList *play;
155 
156   GRecMutex recall_mutex;
157 
158   GList *recall;
159 
160   gpointer line_widget;
161   gpointer file_data;
162 };
163 
164 struct _AgsChannelClass
165 {
166   GObjectClass gobject;
167 
168   void (*recycling_changed)(AgsChannel *channel,
169 			    AgsRecycling *old_start_region, AgsRecycling *old_end_region,
170 			    AgsRecycling *new_start_region, AgsRecycling *new_end_region,
171 			    AgsRecycling *old_start_changed_region, AgsRecycling *old_end_changed_region,
172 			    AgsRecycling *new_start_changed_region, AgsRecycling *new_end_changed_region);
173 
174   GList* (*add_effect)(AgsChannel *channel,
175 		       gchar *filename,
176 		       gchar *effect);
177   void (*remove_effect)(AgsChannel *channel,
178 			guint nth);
179 
180   void (*duplicate_recall)(AgsChannel *channel,
181 			   AgsRecallID *recall_id);
182   void (*resolve_recall)(AgsChannel *channel,
183 			 AgsRecallID *recall_id);
184 
185   void (*init_recall)(AgsChannel *channel,
186 		      AgsRecallID *recall_id, guint staging_flags);
187   void (*play_recall)(AgsChannel *channel,
188 		      AgsRecallID *recall_id, guint staging_flags);
189 
190   void (*done_recall)(AgsChannel *channel,
191 		      AgsRecallID *recall_id);
192   void (*cancel_recall)(AgsChannel *channel,
193 			AgsRecallID *recall_id);
194 
195   void (*cleanup_recall)(AgsChannel *channel,
196 			 AgsRecallID *recall_id);
197 
198   GList* (*start)(AgsChannel *channel,
199 		  gint sound_scope);
200   void (*stop)(AgsChannel *channel,
201 	       GList *recall_id, gint sound_scope);
202 
203   GList* (*check_scope)(AgsChannel *channel, gint sound_scope);
204 
205   void (*recursive_run_stage)(AgsChannel *channel,
206 			      gint sound_scope, guint staging_flags);
207 };
208 
209 GType ags_channel_get_type();
210 GType ags_channel_flags_get_type();
211 
212 GQuark ags_channel_error_quark();
213 
214 GRecMutex* ags_channel_get_obj_mutex(AgsChannel *channel);
215 GRecMutex* ags_channel_get_play_mutex(AgsChannel *channel);
216 GRecMutex* ags_channel_get_recall_mutex(AgsChannel *channel);
217 
218 gboolean ags_channel_test_flags(AgsChannel *channel, guint flags);
219 void ags_channel_set_flags(AgsChannel *channel, guint flags);
220 void ags_channel_unset_flags(AgsChannel *channel, guint flags);
221 
222 gboolean ags_channel_test_ability_flags(AgsChannel *channel, guint ability_flags);
223 void ags_channel_set_ability_flags(AgsChannel *channel, guint ability_flags);
224 void ags_channel_unset_ability_flags(AgsChannel *channel, guint ability_flags);
225 
226 gboolean ags_channel_test_behaviour_flags(AgsChannel *channel, guint behaviour_flags);
227 void ags_channel_set_behaviour_flags(AgsChannel *channel, guint behaviour_flags);
228 void ags_channel_unset_behaviour_flags(AgsChannel *channel, guint behaviour_flags);
229 
230 gboolean ags_channel_test_staging_flags(AgsChannel *channel, gint sound_scope,
231 					guint staging_flags);
232 void ags_channel_set_staging_flags(AgsChannel *channel, gint sound_scope,
233 				   guint staging_flags);
234 void ags_channel_unset_staging_flags(AgsChannel *channel, gint sound_scope,
235 				     guint staging_flags);
236 
237 gboolean ags_channel_test_staging_completed(AgsChannel *channel, gint sound_scope);
238 void ags_channel_set_staging_completed(AgsChannel *channel, gint sound_scope);
239 void ags_channel_unset_staging_completed(AgsChannel *channel, gint sound_scope);
240 
241 /* parent */
242 GObject* ags_channel_get_audio(AgsChannel *channel);
243 void ags_channel_set_audio(AgsChannel *channel, GObject *audio);
244 
245 /* channels */
246 AgsChannel* ags_channel_next(AgsChannel *channel);
247 AgsChannel* ags_channel_prev(AgsChannel *channel);
248 
249 AgsChannel* ags_channel_next_pad(AgsChannel *channel);
250 AgsChannel* ags_channel_prev_pad(AgsChannel *channel);
251 
252 AgsChannel* ags_channel_first(AgsChannel *channel);
253 AgsChannel* ags_channel_last(AgsChannel *channel);
254 AgsChannel* ags_channel_nth(AgsChannel *channel, guint nth);
255 
256 AgsChannel* ags_channel_pad_first(AgsChannel *channel);
257 AgsChannel* ags_channel_pad_last(AgsChannel *channel);
258 AgsChannel* ags_channel_pad_nth(AgsChannel *channel, guint nth);
259 
260 AgsChannel* ags_channel_first_with_recycling(AgsChannel *channel);
261 AgsChannel* ags_channel_last_with_recycling(AgsChannel *channel);
262 AgsChannel* ags_channel_prev_with_recycling(AgsChannel *channel);
263 AgsChannel* ags_channel_next_with_recycling(AgsChannel *channel);
264 
265 AgsChannel* ags_channel_get_link(AgsChannel *channel);
266 
267 void ags_channel_set_link(AgsChannel *channel, AgsChannel *link,
268 			  GError **error);
269 void ags_channel_reset_recycling(AgsChannel *channel,
270 				 AgsRecycling *first_recycling, AgsRecycling *last_recycling);
271 
272 void ags_channel_recycling_changed(AgsChannel *channel,
273 				   AgsRecycling *old_start_region, AgsRecycling *old_end_region,
274 				   AgsRecycling *new_start_region, AgsRecycling *new_end_region,
275 				   AgsRecycling *old_start_changed_region, AgsRecycling *old_end_changed_region,
276 				   AgsRecycling *new_start_changed_region, AgsRecycling *new_end_changed_region);
277 
278 /* soundcard */
279 GObject* ags_channel_get_output_soundcard(AgsChannel *channel);
280 void ags_channel_set_output_soundcard(AgsChannel *channel, GObject *output_soundcard);
281 
282 GObject* ags_channel_get_input_soundcard(AgsChannel *channel);
283 void ags_channel_set_input_soundcard(AgsChannel *channel, GObject *input_soundcard);
284 
285 /* presets */
286 guint ags_channel_get_samplerate(AgsChannel *channel);
287 void ags_channel_set_samplerate(AgsChannel *channel, guint samplerate);
288 
289 guint ags_channel_get_buffer_size(AgsChannel *channel);
290 void ags_channel_set_buffer_size(AgsChannel *channel, guint buffer_size);
291 
292 guint ags_channel_get_format(AgsChannel *channel);
293 void ags_channel_set_format(AgsChannel *channel, guint format);
294 
295 /* alignment */
296 guint ags_channel_get_pad(AgsChannel *channel);
297 void ags_channel_set_pad(AgsChannel *channel, guint pad);
298 
299 guint ags_channel_get_audio_channel(AgsChannel *channel);
300 void ags_channel_set_audio_channel(AgsChannel *channel, guint audio_channel);
301 
302 guint ags_channel_get_line(AgsChannel *channel);
303 void ags_channel_set_line(AgsChannel *channel, guint line);
304 
305 /* key */
306 gint ags_channel_get_octave(AgsChannel *channel);
307 void ags_channel_set_octave(AgsChannel *channel, gint octave);
308 
309 guint ags_channel_get_key(AgsChannel *channel);
310 void ags_channel_set_key(AgsChannel *channel, guint key);
311 
312 gint ags_channel_get_absolute_key(AgsChannel *channel);
313 void ags_channel_set_absolute_key(AgsChannel *channel, gint absolute_key);
314 
315 /* children */
316 GList* ags_channel_get_pattern(AgsChannel *channel);
317 void ags_channel_set_pattern(AgsChannel *channel, GList *pattern);
318 
319 void ags_channel_add_pattern(AgsChannel *channel, GObject *pattern);
320 void ags_channel_remove_pattern(AgsChannel *channel, GObject *pattern);
321 
322 /* recall related */
323 GObject* ags_channel_get_playback(AgsChannel *channel);
324 void ags_channel_set_playback(AgsChannel *channel, GObject *playback);
325 
326 GList* ags_channel_get_recall_id(AgsChannel *channel);
327 void ags_channel_set_recall_id(AgsChannel *channel, GList *recall_id);
328 
329 void ags_channel_add_recall_id(AgsChannel *channel, AgsRecallID *recall_id);
330 void ags_channel_remove_recall_id(AgsChannel *channel, AgsRecallID *recall_id);
331 
332 GList* ags_channel_get_recall_container(AgsChannel *channel);
333 void ags_channel_set_recall_container(AgsChannel *channel, GList *recall_container);
334 
335 void ags_channel_add_recall_container(AgsChannel *channel, GObject *recall_container);
336 void ags_channel_remove_recall_container(AgsChannel *channel, GObject *recall_container);
337 
338 GList* ags_channel_get_play(AgsChannel *channel);
339 void ags_channel_set_play(AgsChannel *channel, GList *play);
340 
341 GList* ags_channel_get_recall(AgsChannel *channel);
342 void ags_channel_set_recall(AgsChannel *channel, GList *recall);
343 
344 void ags_channel_add_recall(AgsChannel *channel, GObject *recall,
345 			    gboolean play_context);
346 void ags_channel_insert_recall(AgsChannel *channel, GObject *recall,
347 			       gboolean play_context,
348 			       gint position);
349 void ags_channel_remove_recall(AgsChannel *channel, GObject *recall,
350 			       gboolean play_context);
351 
352 /* add/remove effect */
353 GList* ags_channel_add_effect(AgsChannel *channel,
354 			      char *filename,
355 			      gchar *effect);
356 void ags_channel_remove_effect(AgsChannel *channel,
357 			       guint nth);
358 
359 /* stages */
360 void ags_channel_duplicate_recall(AgsChannel *channel,
361 				  AgsRecallID *recall_id);
362 void ags_channel_resolve_recall(AgsChannel *channel,
363 				AgsRecallID *recall_id);
364 
365 void ags_channel_init_recall(AgsChannel *channel,
366 			     AgsRecallID *recall_id, guint staging_flags);
367 void ags_channel_play_recall(AgsChannel *channel,
368 			     AgsRecallID *recall_id, guint staging_flags);
369 
370 void ags_channel_done_recall(AgsChannel *channel,
371 			     AgsRecallID *recall_id);
372 void ags_channel_cancel_recall(AgsChannel *channel,
373 			       AgsRecallID *recall_id);
374 
375 void ags_channel_cleanup_recall(AgsChannel *channel,
376 				AgsRecallID *recall_id);
377 
378 /* control */
379 GList* ags_channel_start(AgsChannel *channel,
380 			 gint sound_scope);
381 
382 void ags_channel_stop(AgsChannel *channel,
383 		      GList *recall_id, gint sound_scope);
384 
385 /* query */
386 GList* ags_channel_check_scope(AgsChannel *channel, gint sound_scope);
387 
388 GList* ags_channel_collect_all_channel_ports(AgsChannel *channel);
389 
390 GList* ags_channel_collect_all_channel_ports_by_specifier_and_context(AgsChannel *channel,
391 								      gchar *specifier,
392 								      gboolean play_context);
393 
394 /* recursive functions */
395 AgsChannel* ags_channel_get_level(AgsChannel *channel);
396 
397 void ags_channel_recursive_set_property(AgsChannel *channel,
398 					gint n_params,
399 					gchar **parameter_name, GValue *value);
400 
401 void ags_channel_recursive_run_stage(AgsChannel *channel,
402 				     gint sound_scope, guint staging_flags);
403 
404 /* instantiate */
405 AgsChannel* ags_channel_new(GObject *audio);
406 
407 G_END_DECLS
408 
409 #endif /*__AGS_CHANNEL_H__*/
410