controller: expand the api to offer functions for plain and GValue arrays
[platform/upstream/gstreamer.git] / tests / examples / controller / control-sources.c
index 71c7b5d..7d74a87 100644 (file)
@@ -4,8 +4,9 @@
  * Generates a datafile for various control sources.
  *
  * Needs gnuplot for plotting.
- * plot "ctrl_interpolation.dat" using 1:2 with points title 'none', "" using 1:3 with points title 'linear', "" using 1:4 with points title 'cubic'
- * plot "ctrl_lfo.dat" using 1:2 with points title 'sine', "" using 1:3 with points title 'saw', "" using 1:4 with points title 'square', "" using 1:5 with points title 'triangle'
+ * plot "ctrl_i1.dat" using 1:2 with points title 'none', "" using 1:3 with points title 'linear', "" using 1:4 with points title 'cubic', "ctrl_i2.dat" using 1:2 with lines title 'none', "" using 1:3 with lines title 'linear', "" using 1:4 with lines title 'cubic'
+ * plot "ctrl_l1.dat" using 1:2 with points title 'sine', "" using 1:3 with points title 'square', "" using 1:4 with points title 'saw', "" using 1:5 with points title 'revsaw', "" using 1:6 with points title 'triangle', "ctrl_l2.dat" using 1:2 with lines title 'sine', "" using 1:3 with lines title 'square', "" using 1:4 with lines title 'saw', "" using 1:5 with lines title 'revsaw', "" using 1:6 with lines title 'triangle'
+ * plot "ctrl_cl1.dat" using 1:2 with points title 'sine', "ctrl_cl2.dat" using 1:2 with lines title 'sine'
  */
 
 #include <stdio.h>
@@ -14,6 +15,7 @@
 #include <gst/gst.h>
 #include <gst/controller/gstinterpolationcontrolsource.h>
 #include <gst/controller/gstlfocontrolsource.h>
+#include <gst/controller/gstdirectcontrolbinding.h>
 
 /* local test element */
 
@@ -116,25 +118,25 @@ gst_test_obj_class_init (GstTestObjClass * klass)
   g_object_class_install_property (gobject_class, PROP_INT,
       g_param_spec_int ("int",
           "int prop",
-          "int number parameter for the TEST_OBJ",
+          "int number parameter",
           0, 100, 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
 
   g_object_class_install_property (gobject_class, PROP_FLOAT,
       g_param_spec_float ("float",
           "float prop",
-          "float number parameter for the TEST_OBJ",
+          "float number parameter",
           0.0, 100.0, 0.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
 
   g_object_class_install_property (gobject_class, PROP_DOUBLE,
       g_param_spec_double ("double",
           "double prop",
-          "double number parameter for the TEST_OBJ",
+          "double number parameter",
           0.0, 100.0, 0.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
 
   g_object_class_install_property (gobject_class, PROP_BOOLEAN,
       g_param_spec_boolean ("boolean",
           "boolean prop",
-          "boolean parameter for the TEST_OBJ",
+          "boolean parameter",
           FALSE, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
 }
 
@@ -177,47 +179,83 @@ static void
 test_interpolation (void)
 {
   GstObject *e;
-  GstInterpolationControlSource *ics;
   GstTimedValueControlSource *tvcs;
   GstControlSource *cs;
   gint t, i1, i2, i3;
+  GValue *v1, *v2, *v3;
+  gint n_values;
   FILE *f;
 
   e = (GstObject *) gst_element_factory_make ("testobj", NULL);
 
-  ics = gst_interpolation_control_source_new ();
-  tvcs = (GstTimedValueControlSource *) ics;
-  cs = (GstControlSource *) ics;
+  cs = gst_interpolation_control_source_new ();
+  tvcs = (GstTimedValueControlSource *) cs;
 
-  gst_object_set_control_source (e, "int", cs);
+  gst_object_add_control_binding (e, gst_direct_control_binding_new (e, "int",
+          cs));
 
   gst_timed_value_control_source_set (tvcs, 0 * GST_SECOND, 0.0);
   gst_timed_value_control_source_set (tvcs, 10 * GST_SECOND, 1.0);
   gst_timed_value_control_source_set (tvcs, 20 * GST_SECOND, 0.5);
   gst_timed_value_control_source_set (tvcs, 30 * GST_SECOND, 0.2);
 
-  if (!(f = fopen ("ctrl_interpolation.dat", "w")))
+  /* test single values */
+  if (!(f = fopen ("ctrl_i1.dat", "w")))
     exit (-1);
   fprintf (f, "# Time None Linear Cubic\n");
 
   for (t = 0; t < 40; t++) {
-    g_object_set (ics, "mode", GST_INTERPOLATION_MODE_NONE, NULL);
+    g_object_set (cs, "mode", GST_INTERPOLATION_MODE_NONE, NULL);
     gst_object_sync_values (e, t * GST_SECOND);
     i1 = GST_TEST_OBJ (e)->val_int;
 
-    g_object_set (ics, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
+    g_object_set (cs, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
     gst_object_sync_values (e, t * GST_SECOND);
     i2 = GST_TEST_OBJ (e)->val_int;
 
-    g_object_set (ics, "mode", GST_INTERPOLATION_MODE_CUBIC, NULL);
+    g_object_set (cs, "mode", GST_INTERPOLATION_MODE_CUBIC, NULL);
     gst_object_sync_values (e, t * GST_SECOND);
     i3 = GST_TEST_OBJ (e)->val_int;
 
-    fprintf (f, "%d %d %d %d\n", t, i1, i2, i3);
+    fprintf (f, "%4.1f %d %d %d\n", (gfloat) t, i1, i2, i3);
   }
 
   fclose (f);
 
+  /* test value arrays */
+  if (!(f = fopen ("ctrl_i2.dat", "w")))
+    exit (-1);
+  fprintf (f, "# Time None Linear Cubic\n");
+  n_values = 40 * 10;
+
+  g_object_set (cs, "mode", GST_INTERPOLATION_MODE_NONE, NULL);
+  v1 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v1);
+
+  g_object_set (cs, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
+  v2 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v2);
+
+  g_object_set (cs, "mode", GST_INTERPOLATION_MODE_CUBIC, NULL);
+  v3 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v3);
+
+  for (t = 0; t < n_values; t++) {
+    i1 = g_value_get_int (&v1[t]);
+    i2 = g_value_get_int (&v2[t]);
+    i3 = g_value_get_int (&v3[t]);
+    fprintf (f, "%4.1f %d %d %d\n", (gfloat) t / 10.0, i1, i2, i3);
+    g_value_unset (&v1[t]);
+    g_value_unset (&v2[t]);
+    g_value_unset (&v3[t]);
+  }
+  g_free (v1);
+  g_free (v2);
+  g_free (v3);
+
+  fclose (f);
+
+  gst_object_unref (cs);
   gst_object_unref (e);
 }
 
@@ -225,52 +263,176 @@ static void
 test_lfo (void)
 {
   GstObject *e;
-  GstLFOControlSource *lfocs;
   GstControlSource *cs;
-  gint t, i1, i2, i3, i4;
+  gint t, i1, i2, i3, i4, i5;
+  GValue *v1, *v2, *v3, *v4, *v5;
+  gint n_values;
   FILE *f;
 
   e = (GstObject *) gst_element_factory_make ("testobj", NULL);
 
-  lfocs = gst_lfo_control_source_new ();
-  cs = (GstControlSource *) lfocs;
+  cs = gst_lfo_control_source_new ();
 
-  gst_object_set_control_source (e, "int", cs);
+  gst_object_add_control_binding (e, gst_direct_control_binding_new (e, "int",
+          cs));
 
-  g_object_set (lfocs,
+  g_object_set (cs,
       "frequency", (gdouble) 0.05,
       "timeshift", (GstClockTime) 0,
       "amplitude", (gdouble) 0.5, "offset", (gdouble) 0.5, NULL);
 
-  if (!(f = fopen ("ctrl_lfo.dat", "w")))
+  /* test single values */
+  if (!(f = fopen ("ctrl_l1.dat", "w")))
     exit (-1);
-  fprintf (f, "# Time Sine Saw Square Triangle\n");
+  fprintf (f, "# Time Sine Square Saw RevSaw Triangle\n");
 
   for (t = 0; t < 40; t++) {
-    g_object_set (lfocs, "waveform", GST_LFO_WAVEFORM_SINE, NULL);
+    g_object_set (cs, "waveform", GST_LFO_WAVEFORM_SINE, NULL);
     gst_object_sync_values (e, t * GST_SECOND);
     i1 = GST_TEST_OBJ (e)->val_int;
 
-    g_object_set (lfocs, "waveform", GST_LFO_WAVEFORM_SAW, NULL);
+    g_object_set (cs, "waveform", GST_LFO_WAVEFORM_SQUARE, NULL);
     gst_object_sync_values (e, t * GST_SECOND);
     i2 = GST_TEST_OBJ (e)->val_int;
 
-    g_object_set (lfocs, "waveform", GST_LFO_WAVEFORM_SQUARE, NULL);
+    g_object_set (cs, "waveform", GST_LFO_WAVEFORM_SAW, NULL);
     gst_object_sync_values (e, t * GST_SECOND);
     i3 = GST_TEST_OBJ (e)->val_int;
 
-    g_object_set (lfocs, "waveform", GST_LFO_WAVEFORM_TRIANGLE, NULL);
+    g_object_set (cs, "waveform", GST_LFO_WAVEFORM_REVERSE_SAW, NULL);
     gst_object_sync_values (e, t * GST_SECOND);
     i4 = GST_TEST_OBJ (e)->val_int;
 
-    fprintf (f, "%d %d %d %d %d\n", t, i1, i2, i3, i4);
+    g_object_set (cs, "waveform", GST_LFO_WAVEFORM_TRIANGLE, NULL);
+    gst_object_sync_values (e, t * GST_SECOND);
+    i5 = GST_TEST_OBJ (e)->val_int;
+
+    fprintf (f, "%4.1f %d %d %d %d %d\n", (gfloat) t, i1, i2, i3, i4, i5);
+  }
+
+  fclose (f);
+
+  /* test value arrays */
+  if (!(f = fopen ("ctrl_l2.dat", "w")))
+    exit (-1);
+  fprintf (f, "# Time Sine Square Saw RevSaw Triangle\n");
+  n_values = 40 * 10;
+
+  g_object_set (cs, "waveform", GST_LFO_WAVEFORM_SINE, NULL);
+  v1 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v1);
+
+  g_object_set (cs, "waveform", GST_LFO_WAVEFORM_SQUARE, NULL);
+  v2 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v2);
+
+  g_object_set (cs, "waveform", GST_LFO_WAVEFORM_SAW, NULL);
+  v3 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v3);
+
+  g_object_set (cs, "waveform", GST_LFO_WAVEFORM_REVERSE_SAW, NULL);
+  v4 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v4);
+
+  g_object_set (cs, "waveform", GST_LFO_WAVEFORM_TRIANGLE, NULL);
+  v5 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v5);
+
+  for (t = 0; t < n_values; t++) {
+    i1 = g_value_get_int (&v1[t]);
+    i2 = g_value_get_int (&v2[t]);
+    i3 = g_value_get_int (&v3[t]);
+    i4 = g_value_get_int (&v4[t]);
+    i5 = g_value_get_int (&v5[t]);
+    fprintf (f, "%4.1f %d %d %d %d %d\n", (gfloat) t / 10.0, i1, i2, i3, i4,
+        i5);
+    g_value_unset (&v1[t]);
+    g_value_unset (&v2[t]);
+    g_value_unset (&v3[t]);
+    g_value_unset (&v4[t]);
+    g_value_unset (&v5[t]);
   }
+  g_free (v1);
+  g_free (v2);
+  g_free (v3);
+  g_free (v4);
+  g_free (v5);
 
   fclose (f);
 
+  gst_object_unref (cs);
   gst_object_unref (e);
 }
 
+static void
+test_chained_lfo (void)
+{
+  GstObject *e;
+  GstControlSource *cs1, *cs2;
+  gint t, i1;
+  GValue *v1;
+  gint n_values;
+  FILE *f;
+
+  e = (GstObject *) gst_element_factory_make ("testobj", NULL);
+
+  cs1 = gst_lfo_control_source_new ();
+
+  gst_object_add_control_binding (e, gst_direct_control_binding_new (e, "int",
+          cs1));
+
+  g_object_set (cs1,
+      "waveform", GST_LFO_WAVEFORM_SINE,
+      "frequency", (gdouble) 0.05,
+      "timeshift", (GstClockTime) 0, "offset", (gdouble) 0.5, NULL);
+
+  cs2 = gst_lfo_control_source_new ();
+
+  gst_object_add_control_binding ((GstObject *) cs1,
+      gst_direct_control_binding_new ((GstObject *) cs1, "amplitude", cs2));
+
+  g_object_set (cs2,
+      "waveform", GST_LFO_WAVEFORM_SINE,
+      "frequency", (gdouble) 0.05,
+      "timeshift", (GstClockTime) 0,
+      "amplitude", (gdouble) 0.5, "offset", (gdouble) 0.5, NULL);
+
+  /* test single values */
+  if (!(f = fopen ("ctrl_cl1.dat", "w")))
+    exit (-1);
+  fprintf (f, "# Time Sine\n");
+
+  for (t = 0; t < 40; t++) {
+    gst_object_sync_values (e, t * GST_SECOND);
+    i1 = GST_TEST_OBJ (e)->val_int;
+
+    fprintf (f, "%4.1f %d\n", (gfloat) t, i1);
+  }
+
+  fclose (f);
+
+  /* test value arrays */
+  if (!(f = fopen ("ctrl_cl2.dat", "w")))
+    exit (-1);
+  fprintf (f, "# Time Sine\n");
+  n_values = 40 * 10;
+
+  v1 = g_new0 (GValue, n_values);
+  gst_object_get_g_value_array (e, "int", 0, GST_SECOND / 10, n_values, v1);
+
+  for (t = 0; t < n_values; t++) {
+    i1 = g_value_get_int (&v1[t]);
+    fprintf (f, "%4.1f %d\n", (gfloat) t / 10.0, i1);
+    g_value_unset (&v1[t]);
+  }
+  g_free (v1);
+
+  fclose (f);
+
+  gst_object_unref (cs1);
+  gst_object_unref (cs2);
+  gst_object_unref (e);
+}
 
 gint
 main (gint argc, gchar ** argv)
@@ -282,5 +444,7 @@ main (gint argc, gchar ** argv)
   test_interpolation ();
   test_lfo ();
 
+  test_chained_lfo ();
+
   return 0;
 }