user1840007
user1840007

Reputation: 675

Use "g_array_index()" on GValueArray type

I have an application that uses the GstLevel element get and print RMS and peak values.

The program prints warnings when I read the level-values in level_message_cb(...) function. Seems like all the GstStructure fields are now GValueArray types.

static gboolean
level_message_cb (GstBus * bus, GstMessage * message, gpointer data) {

    if (message->type == GST_MESSAGE_ELEMENT) {
        const GstStructure *s = gst_message_get_structure (message);
        const gchar *name = gst_structure_get_name (s);

        if (strcmp (name, "level") == 0) {
            gint channels;
            GstClockTime endtime;
            gdouble rms_dB, peak_dB, decay_dB;
            gdouble rms;
            const GValue *array_val;
            const GValue *value;

            GValueArray *rms_arr, *peak_arr, *decay_arr;
            gint i;

            if (!gst_structure_get_clock_time (s, "endtime", &endtime))
                g_warning ("Could not parse endtime");

            array_val = gst_structure_get_value (s, "rms");
            rms_arr = (GValueArray *) g_value_get_boxed (array_val);

            array_val = gst_structure_get_value (s, "peak");
            peak_arr = (GValueArray *) g_value_get_boxed (array_val);

            array_val = gst_structure_get_value (s, "decay");
            decay_arr = (GValueArray *) g_value_get_boxed (array_val);

            channels = rms_arr->n_values;

            g_print ("endtime: %" GST_TIME_FORMAT ", channels: %d\n",
                 GST_TIME_ARGS (endtime), channels);

            for (i = 0; i < channels; ++i) {

                g_print ("channel %d\n", i);

                array_val = gst_structure_get_value (s, "rms");
                arr = (GValueArray *) g_value_get_boxed (array_val);

                value = g_value_array_get_nth (rms_arr, i);
                rms_dB = g_value_get_double (value);

                value = g_value_array_get_nth (peak_arr, i);
                peak_dB = g_value_get_double (value);

                value = g_value_array_get_nth (decay_arr, i);
                decay_dB = g_value_get_double (value);

                g_print ("RMS: %f dB, peak: %f dB, decay: %f dB\n",
                     rms_dB, peak_dB, decay_dB);

                rms = pow (10, rms_dB / 20);

                g_print ("normalized rms value: %f\n", rms);
            }
        }
    }
    return TRUE;
}

This is the warning I get:

warning: 'g_value_array_get_nth' is deprecated. Use 'g_array_index' instead

How can I use "g_array_index()" on GValueArray type. GArray has a g_array_index() method, but GValueArray has not.

I read somewhere that GValueArray is also deprecated or should be avoided. Please instruct me how to read the level values in right way in the level_message_cb() function.

Kindly Triniton.

Upvotes: 3

Views: 1366

Answers (1)

David Brown
David Brown

Reputation: 36249

This is all g_value_array_get_nth does (aside from validating its arguments):

return value_array->values + index;

So the relevant parts of your code becomes:

value = arr->values + i;

Upvotes: 3

Related Questions