[Solved] How to get the duration of a raw audio file?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Solved] How to get the duration of a raw audio file?

mdbtfu
This post was updated on .
Hello,


I have to play raw audio files with GStreamer and manage the time position of the audio stream (setting and monitoring the time position). To manage that time position, I also need the duration of the audio file, which GStreamer apparently cannot get or calculate. I understand that raw data doesn't have a header with meta-data (like wav files for example) but I thought that maybe with the capabilities given (signed 16 bit, little-endian, ...), GStreamer would succeed to get that duration.

So here is my question: how can I get that duration? Or how can I force GStreamer to set a given duration to the file I'm playing?

Below is my code that doesn't manage to get a proper duration (it always returns 0 as a duration)


I'll be really thankful for any help or advice!


--
Thibaud



[Code]


Here is the function that plays the raw audio file.

{
        // Create pipeline for raw audio data
        QString str_audioPipeline = "filesrc location=myrawaudiofile.raw ! audio/x-raw,format=S16LE,rate=48000,channels=2 ! directsoundsink";
       
        // Gstreamer initialisation
        gst_init(NULL, NULL);

        // Parse pipeline
        GError * message = NULL;
        audioPlaybackPipeline = gst_parse_launch(str_audioPipeline.toStdString().c_str(), &message);

        // Set callback to get position and length of the stream
        playbackTrackerID = g_timeout_add(200, (GSourceFunc)get_position, audioPlaybackPipeline);

        // Set bus to get messages notifications
        GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(audioPlaybackPipeline));
        gst_bus_add_watch(bus, playback_bus_callback, NULL);
        gst_object_unref(bus);

        // Set the playback pipeline state to PLAYING
        gst_element_set_state(audioPlaybackPipeline, GST_STATE_PLAYING);

        // Check if the pipeline state changed with success
        if (gst_element_get_state(audioPlaybackPipeline, NULL, NULL, -1) == GST_STATE_CHANGE_FAILURE)
        {
                // AUDIO Playback::ERROR: Failed to go into PLAYING state
                return false;
        }
        else
        {
                // Create a GLib Main Loop and set it to run
                playbackLoop = g_main_loop_new(NULL, NULL);
                g_main_loop_run(playbackLoop);

                return true;
        }
}


Here is the callback used to get the position and duration of the stream

gboolean get_position(GstElement *pipeline)
{
        // Get the current position
        gst_element_query_position(pipeline, GST_FORMAT_TIME, &playbackPosition);

        // Get the duration
        gst_element_query_duration(pipeline, GST_FORMAT_TIME, &playbackDuration);
}
Reply | Threaded
Open this post in threaded view
|

Re: How to get the duration of a raw audio file?

Tim Müller
On Thu, 2016-08-18 at 04:56 -0700, mdbtfu wrote:

Hi,

If you add an 'audioparse' element (or 'rawaudioparse' in >= 1.9/git
master) after filesrc and configure it with the right properties, it
should be able to report the duration correctly.

Cheers
 -Tim

-- 
Tim Müller, Centricular Ltd - http://www.centricular.com
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to get the duration of a raw audio file?

mdbtfu
Hi!

I added that plugin after filesrc and it worked like a charm!

Thank you a lot!


--
Thibaud