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 #include <ags/audio/fx/ags_fx_volume_audio.h>
21 
22 #include <ags/plugin/ags_plugin_port.h>
23 
24 #include <ags/i18n.h>
25 
26 void ags_fx_volume_audio_class_init(AgsFxVolumeAudioClass *fx_volume_audio);
27 void ags_fx_volume_audio_mutable_interface_init(AgsMutableInterface *mutable);
28 void ags_fx_volume_audio_init(AgsFxVolumeAudio *fx_volume_audio);
29 void ags_fx_volume_audio_set_property(GObject *gobject,
30 				      guint prop_id,
31 				      const GValue *value,
32 				      GParamSpec *param_spec);
33 void ags_fx_volume_audio_get_property(GObject *gobject,
34 				      guint prop_id,
35 				      GValue *value,
36 				      GParamSpec *param_spec);
37 void ags_fx_volume_audio_dispose(GObject *gobject);
38 void ags_fx_volume_audio_finalize(GObject *gobject);
39 
40 void ags_fx_volume_audio_set_muted(AgsMutable *mutable, gboolean muted);
41 
42 static AgsPluginPort* ags_fx_volume_audio_get_muted_plugin_port();
43 
44 /**
45  * SECTION:ags_fx_volume_audio
46  * @short_description: fx volume audio
47  * @title: AgsFxVolumeAudio
48  * @section_id:
49  * @include: ags/audio/fx/ags_fx_volume_audio.h
50  *
51  * The #AgsFxVolumeAudio class provides ports to the effect processor.
52  */
53 
54 static gpointer ags_fx_volume_audio_parent_class = NULL;
55 
56 const gchar *ags_fx_volume_audio_plugin_name = "ags-fx-volume";
57 
58 const gchar* ags_fx_volume_audio_specifier[] = {
59   "./muted[0]",
60   NULL,
61 };
62 
63 const gchar* ags_fx_volume_audio_control_port[] = {
64   "1/1",
65   NULL,
66 };
67 
68 enum{
69   PROP_0,
70   PROP_MUTED,
71 };
72 
73 GType
ags_fx_volume_audio_get_type()74 ags_fx_volume_audio_get_type()
75 {
76   static volatile gsize g_define_type_id__volatile = 0;
77 
78   if(g_once_init_enter (&g_define_type_id__volatile)){
79     GType ags_type_fx_volume_audio = 0;
80 
81     static const GTypeInfo ags_fx_volume_audio_info = {
82       sizeof (AgsFxVolumeAudioClass),
83       NULL, /* base_init */
84       NULL, /* base_finalize */
85       (GClassInitFunc) ags_fx_volume_audio_class_init,
86       NULL, /* class_finalize */
87       NULL, /* class_audio */
88       sizeof (AgsFxVolumeAudio),
89       0,    /* n_preallocs */
90       (GInstanceInitFunc) ags_fx_volume_audio_init,
91     };
92 
93     static const GInterfaceInfo ags_mutable_interface_info = {
94       (GInterfaceInitFunc) ags_fx_volume_audio_mutable_interface_init,
95       NULL, /* interface_finalize */
96       NULL, /* interface_data */
97     };
98 
99     ags_type_fx_volume_audio = g_type_register_static(AGS_TYPE_RECALL_AUDIO,
100 						      "AgsFxVolumeAudio",
101 						      &ags_fx_volume_audio_info,
102 						      0);
103 
104     g_type_add_interface_static(ags_type_fx_volume_audio,
105 				AGS_TYPE_MUTABLE,
106 				&ags_mutable_interface_info);
107 
108     g_once_init_leave(&g_define_type_id__volatile, ags_type_fx_volume_audio);
109   }
110 
111   return g_define_type_id__volatile;
112 }
113 
114 void
ags_fx_volume_audio_class_init(AgsFxVolumeAudioClass * fx_volume_audio)115 ags_fx_volume_audio_class_init(AgsFxVolumeAudioClass *fx_volume_audio)
116 {
117   GObjectClass *gobject;
118 
119   GParamSpec *param_spec;
120 
121   ags_fx_volume_audio_parent_class = g_type_class_peek_parent(fx_volume_audio);
122 
123   /* GObjectClass */
124   gobject = (GObjectClass *) fx_volume_audio;
125 
126   gobject->set_property = ags_fx_volume_audio_set_property;
127   gobject->get_property = ags_fx_volume_audio_get_property;
128 
129   gobject->dispose = ags_fx_volume_audio_dispose;
130   gobject->finalize = ags_fx_volume_audio_finalize;
131 
132   /* properties */
133   /**
134    * AgsFxVolumeAudio:muted:
135    *
136    * The beats per minute.
137    *
138    * Since: 3.3.0
139    */
140   param_spec = g_param_spec_object("muted",
141 				   i18n_pspec("muted of recall"),
142 				   i18n_pspec("The recall's muted"),
143 				   AGS_TYPE_PORT,
144 				   G_PARAM_READABLE | G_PARAM_WRITABLE);
145   g_object_class_install_property(gobject,
146 				  PROP_MUTED,
147 				  param_spec);
148 }
149 
150 void
ags_fx_volume_audio_mutable_interface_init(AgsMutableInterface * mutable)151 ags_fx_volume_audio_mutable_interface_init(AgsMutableInterface *mutable)
152 {
153   mutable->set_muted = ags_fx_volume_audio_set_muted;
154 }
155 
156 void
ags_fx_volume_audio_init(AgsFxVolumeAudio * fx_volume_audio)157 ags_fx_volume_audio_init(AgsFxVolumeAudio *fx_volume_audio)
158 {
159   AGS_RECALL(fx_volume_audio)->name = "ags-fx-volume";
160   AGS_RECALL(fx_volume_audio)->version = AGS_RECALL_DEFAULT_VERSION;
161   AGS_RECALL(fx_volume_audio)->build_id = AGS_RECALL_DEFAULT_BUILD_ID;
162   AGS_RECALL(fx_volume_audio)->xml_type = "ags-fx-volume-audio";
163 
164   /* muted */
165   fx_volume_audio->muted = g_object_new(AGS_TYPE_PORT,
166 					"plugin-name", ags_fx_volume_audio_plugin_name,
167 					"specifier", ags_fx_volume_audio_specifier[0],
168 					"control-port", ags_fx_volume_audio_control_port[0],
169 					"port-value-is-pointer", FALSE,
170 					"port-value-type", G_TYPE_FLOAT,
171 					"port-value-size", sizeof(gfloat),
172 					"port-value-length", 1,
173 					NULL);
174 
175   fx_volume_audio->muted->port_value.ags_port_float = (gfloat) FALSE;
176 
177   g_object_set(fx_volume_audio->muted,
178 	       "plugin-port", ags_fx_volume_audio_get_muted_plugin_port(),
179 	       NULL);
180 
181   ags_recall_add_port((AgsRecall *) fx_volume_audio,
182 		      fx_volume_audio->muted);
183 }
184 
185 void
ags_fx_volume_audio_set_property(GObject * gobject,guint prop_id,const GValue * value,GParamSpec * param_spec)186 ags_fx_volume_audio_set_property(GObject *gobject,
187 				 guint prop_id,
188 				 const GValue *value,
189 				 GParamSpec *param_spec)
190 {
191   AgsFxVolumeAudio *fx_volume_audio;
192 
193   GRecMutex *recall_mutex;
194 
195   fx_volume_audio = AGS_FX_VOLUME_AUDIO(gobject);
196 
197   /* get recall mutex */
198   recall_mutex = AGS_RECALL_GET_OBJ_MUTEX(fx_volume_audio);
199 
200   switch(prop_id){
201   case PROP_MUTED:
202   {
203     AgsPort *port;
204 
205     port = (AgsPort *) g_value_get_object(value);
206 
207     g_rec_mutex_lock(recall_mutex);
208 
209     if(port == fx_volume_audio->muted){
210       g_rec_mutex_unlock(recall_mutex);
211 
212       return;
213     }
214 
215     if(fx_volume_audio->muted != NULL){
216       g_object_unref(G_OBJECT(fx_volume_audio->muted));
217     }
218 
219     if(port != NULL){
220       g_object_ref(G_OBJECT(port));
221     }
222 
223     fx_volume_audio->muted = port;
224 
225     g_rec_mutex_unlock(recall_mutex);
226   }
227   break;
228   default:
229     G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, param_spec);
230     break;
231   }
232 }
233 
234 void
ags_fx_volume_audio_get_property(GObject * gobject,guint prop_id,GValue * value,GParamSpec * param_spec)235 ags_fx_volume_audio_get_property(GObject *gobject,
236 				 guint prop_id,
237 				 GValue *value,
238 				 GParamSpec *param_spec)
239 {
240   AgsFxVolumeAudio *fx_volume_audio;
241 
242   GRecMutex *recall_mutex;
243 
244   fx_volume_audio = AGS_FX_VOLUME_AUDIO(gobject);
245 
246   /* get recall mutex */
247   recall_mutex = AGS_RECALL_GET_OBJ_MUTEX(fx_volume_audio);
248 
249   switch(prop_id){
250   case PROP_MUTED:
251   {
252     g_rec_mutex_lock(recall_mutex);
253 
254     g_value_set_object(value, fx_volume_audio->muted);
255 
256     g_rec_mutex_unlock(recall_mutex);
257   }
258   break;
259   default:
260     G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, param_spec);
261     break;
262   }
263 }
264 
265 void
ags_fx_volume_audio_dispose(GObject * gobject)266 ags_fx_volume_audio_dispose(GObject *gobject)
267 {
268   AgsFxVolumeAudio *fx_volume_audio;
269 
270   fx_volume_audio = AGS_FX_VOLUME_AUDIO(gobject);
271 
272   /* muted */
273   if(fx_volume_audio->muted != NULL){
274     g_object_unref(G_OBJECT(fx_volume_audio->muted));
275 
276     fx_volume_audio->muted = NULL;
277   }
278 
279   /* call parent */
280   G_OBJECT_CLASS(ags_fx_volume_audio_parent_class)->dispose(gobject);
281 }
282 
283 void
ags_fx_volume_audio_finalize(GObject * gobject)284 ags_fx_volume_audio_finalize(GObject *gobject)
285 {
286   AgsFxVolumeAudio *fx_volume_audio;
287 
288   fx_volume_audio = AGS_FX_VOLUME_AUDIO(gobject);
289 
290   /* muted */
291   if(fx_volume_audio->muted != NULL){
292     g_object_unref(G_OBJECT(fx_volume_audio->muted));
293   }
294 
295   /* call parent */
296   G_OBJECT_CLASS(ags_fx_volume_audio_parent_class)->finalize(gobject);
297 }
298 
299 void
ags_fx_volume_audio_set_muted(AgsMutable * mutable,gboolean muted)300 ags_fx_volume_audio_set_muted(AgsMutable *mutable, gboolean muted)
301 {
302   AgsPort *port;
303 
304   GValue value = {0,};
305 
306   g_object_get(G_OBJECT(mutable),
307 	       "muted", &port,
308 	       NULL);
309 
310   g_value_init(&value,
311 	       G_TYPE_FLOAT);
312 
313   g_value_set_float(&value,
314 		    (muted ? 1.0: 0.0));
315 
316   ags_port_safe_write(port,
317 		      &value);
318 
319   g_value_unset(&value);
320   g_object_unref(port);
321 }
322 
323 static AgsPluginPort*
ags_fx_volume_audio_get_muted_plugin_port()324 ags_fx_volume_audio_get_muted_plugin_port()
325 {
326   static AgsPluginPort *plugin_port = NULL;
327 
328   static GMutex mutex;
329 
330   g_mutex_lock(&mutex);
331 
332   if(plugin_port == NULL){
333     plugin_port = ags_plugin_port_new();
334     g_object_ref(plugin_port);
335 
336     plugin_port->flags |= (AGS_PLUGIN_PORT_INPUT |
337 			   AGS_PLUGIN_PORT_CONTROL |
338 			   AGS_PLUGIN_PORT_TOGGLED);
339 
340     plugin_port->port_index = 0;
341 
342     /* range */
343     g_value_init(plugin_port->default_value,
344 		 G_TYPE_FLOAT);
345     g_value_init(plugin_port->lower_value,
346 		 G_TYPE_FLOAT);
347     g_value_init(plugin_port->upper_value,
348 		 G_TYPE_FLOAT);
349 
350     g_value_set_float(plugin_port->default_value,
351 		      0.0);
352     g_value_set_float(plugin_port->lower_value,
353 		      0.0);
354     g_value_set_float(plugin_port->upper_value,
355 		      1.0);
356   }
357 
358   g_mutex_unlock(&mutex);
359 
360   return(plugin_port);
361 }
362 
363 /**
364  * ags_fx_volume_audio_new:
365  * @audio: the #AgsAudio
366  *
367  * Create a new instance of #AgsFxVolumeAudio
368  *
369  * Returns: the new #AgsFxVolumeAudio
370  *
371  * Since: 3.3.0
372  */
373 AgsFxVolumeAudio*
ags_fx_volume_audio_new(AgsAudio * audio)374 ags_fx_volume_audio_new(AgsAudio *audio)
375 {
376   AgsFxVolumeAudio *fx_volume_audio;
377 
378   fx_volume_audio = (AgsFxVolumeAudio *) g_object_new(AGS_TYPE_FX_VOLUME_AUDIO,
379 						      "audio", audio,
380 						      NULL);
381 
382   return(fx_volume_audio);
383 }
384