Getting Raw Audio Data with libVLC on Qt Application

This forum is about all development around libVLC.
brianuuu
New Cone
New Cone
Posts: 4
Joined: 15 Mar 2022 18:44

Getting Raw Audio Data with libVLC on Qt Application

Postby brianuuu » 15 Mar 2022 19:04

Hello, I have been developing a Qt Application that uses libVLC to display output from game capture cards for video/audio analysis.
I have all the video output working fine for a while now and I want to start looking into how to get audio data to first doing visualizations and ultimately do audio analysis.

The tutorial I'm following is from here:
https://wiki.videolan.org/Stream_to_mem ... _tutorial/

However, when I initialize vlc instance with the smem options, the callback functions doesn't get called at all, I'm not sure what I'm missing...
Here's my code, I also include the part where it starts the mediaplayer with capture card in case there are things that affects it:

Code: Select all

// Audio prerender callback static void cbAudioPrerender (void* p_audio_data, uint8_t** pp_pcm_buffer , unsigned int size) { qDebug() << "test" } // Audio postrender callback static void cbAudioPostrender(void* p_audio_data, uint8_t* p_pcm_buffer, unsigned int channels, unsigned int rate, unsigned int nb_samples, unsigned int bits_per_sample, unsigned int size, int64_t pts ) { } VLCWrapper::VLCWrapper(QLabel* videoWidget, QSlider* volumeSlider, QWidget *parent) : QWidget(parent) , m_volumeSlider(volumeSlider) , m_videoWidget(videoWidget) { char smem_options[256]; sprintf(smem_options , "#transcode{acodec=s16l}:smem{" "audio-prerender-callback=%lld," "audio-postrender-callback=%lld," "audio-data=%lld}" , (long long int)(intptr_t)(void*)&cbAudioPrerender , (long long int)(intptr_t)(void*)&cbAudioPostrender , (long long int)&ctxAudio); const char* const vlc_args[] = { "--intf", "dummy", "--vout", "dummy", "--sout", smem_options }; m_instance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args); m_mediaPlayer = libvlc_media_player_new(m_instance); ctx.m_label = m_videoWidget; ctx.m_pixels = new uchar[VIDEO_WIDTH * VIDEO_HEIGHT * 4]; memset(ctx.m_pixels, 0, VIDEO_WIDTH * VIDEO_HEIGHT * 4); } bool VLCWrapper::start(const QString &vdev, const QString &adev) { if (vdev.isEmpty() && adev.isEmpty()) return false; m_media = libvlc_media_new_location(m_instance, "dshow://"); // Video QString vdevOption = ":dshow-vdev="; vdevOption += vdev.isEmpty() ? "none" : vdev; libvlc_media_add_option(m_media, vdevOption.toStdString().c_str()); // Audio QString adevOption = ":dshow-adev="; adevOption += adev.isEmpty() ? "none" : adev; libvlc_media_add_option(m_media, adevOption.toStdString().c_str()); // Aspect ratio, resolution libvlc_media_add_option(m_media, ":dshow-aspect-ratio=16:9"); // Caching libvlc_media_add_option(m_media, ":live-caching=0"); // Pass to player and release media libvlc_media_player_set_media(m_mediaPlayer, m_media); libvlc_media_release(m_media); // Set the callback to extract the frame or display it on the screen ctx.m_frame = QImage(VIDEO_WIDTH, VIDEO_HEIGHT, QImage::Format_ARGB32); ctx.m_frame.fill(QColor(0,0,0)); libvlc_video_set_callbacks(m_mediaPlayer, lock, unlock, display, &ctx); libvlc_video_set_format(m_mediaPlayer, "BGRA", VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * 4); // Play media int result = libvlc_media_player_play(m_mediaPlayer); if (result == -1) { return false; } else { libvlc_video_set_adjust_int(m_mediaPlayer, libvlc_video_adjust_option_t::libvlc_adjust_Enable, true); return true; } }

Rémi Denis-Courmont
Developer
Developer
Posts: 15267
Joined: 07 Jun 2004 16:01
VLC version: master
Operating System: Linux
Contact:

Re: Getting Raw Audio Data with libVLC on Qt Application

Postby Rémi Denis-Courmont » 15 Mar 2022 21:16

You can't use both at the same time. smem is not part of the supported LibVLC API.

Please refer to the official API documentation, not old wiki articles.
Rémi Denis-Courmont
https://www.remlab.net/
Private messages soliciting support will be systematically discarded

brianuuu
New Cone
New Cone
Posts: 4
Joined: 15 Mar 2022 18:44

Re: Getting Raw Audio Data with libVLC on Qt Application

Postby brianuuu » 15 Mar 2022 22:25

You can't use both at the same time. smem is not part of the supported LibVLC API.

Please refer to the official API documentation, not old wiki articles.
Thanks for the reply, I can see there's libvlc_audio_set_callbacks() that can give me raw audio data as well? But in this case I will need to implement playing audio manually is that correct?
In VLC Player you can visualize the audio with spectrum etc., was it done in a similar way?

Rémi Denis-Courmont
Developer
Developer
Posts: 15267
Joined: 07 Jun 2004 16:01
VLC version: master
Operating System: Linux
Contact:

Re: Getting Raw Audio Data with libVLC on Qt Application

Postby Rémi Denis-Courmont » 16 Mar 2022 12:11

Yes, yes, and no.
Rémi Denis-Courmont
https://www.remlab.net/
Private messages soliciting support will be systematically discarded

brianuuu
New Cone
New Cone
Posts: 4
Joined: 15 Mar 2022 18:44

Re: Getting Raw Audio Data with libVLC on Qt Application

Postby brianuuu » 17 Mar 2022 16:01

Yes, yes, and no.
I see, so I have manage to play the audio using Qt with raw PCM data, but when I try to change the audio format, it always forced to use s16l, the code I'm using:

Code: Select all

libvlc_audio_set_callbacks(m_mediaPlayer, cbAudioPlay, nullptr, nullptr, nullptr, nullptr, &ctxAudio); libvlc_audio_set_format(m_mediaPlayer, "f32l", 48000, 2);
I tried changing the sample rate and it does change accordingly, but format doesn't change.

And here's the output:

Code: Select all

main generic debug: creating audio output main audio output debug: looking for audio output module matching "amem,none": 6 candidates main audio output debug: using audio output module "amem" ----> main audio output debug: output 's16l' 48000 Hz Stereo frame=1 samples/4 bytes main volume debug: looking for audio volume module matching "any": 2 candidates main volume debug: using audio volume module "integer_mixer" main audio output debug: input 's16l' 44100 Hz Stereo frame=1 samples/4 bytes main audio filter debug: looking for audio filter module matching "scaletempo": 16 candidates scaletempo audio filter debug: format: 44100 rate, 2 nch, 4 bps, fl32 scaletempo audio filter debug: params: 30 stride, 0.200 overlap, 14 search scaletempo audio filter debug: 1.000 scale, 1323.000 stride_in, 1323 stride_out, 1059 standing, 264 overlap, 617 search, 2204 queue, fl32 mode main audio filter debug: using audio filter module "scaletempo" main audio output debug: conversion: 's16l'->'f32l' 44100 Hz->44100 Hz Stereo->Stereo main audio converter debug: looking for audio converter module matching "any": 8 candidates audio_format audio converter debug: s16l->f32l, bits per sample: 16->32 main audio converter debug: using audio converter module "audio_format" main audio output debug: conversion pipeline complete main audio output debug: conversion: 'f32l'->'s16l' 44100 Hz->44100 Hz Stereo->Stereo main audio converter debug: looking for audio converter module matching "any": 8 candidates audio_format audio converter debug: f32l->s16l, bits per sample: 32->16 main audio converter debug: using audio converter module "audio_format" main audio output debug: conversion pipeline complete main audio resampler debug: looking for audio resampler module matching "any": 3 candidates main audio resampler debug: using audio resampler module "ugly"

Rémi Denis-Courmont
Developer
Developer
Posts: 15267
Joined: 07 Jun 2004 16:01
VLC version: master
Operating System: Linux
Contact:

Re: Getting Raw Audio Data with libVLC on Qt Application

Postby Rémi Denis-Courmont » 17 Mar 2022 16:31

There is a bug in the documentation. You need to use "FL32".
Rémi Denis-Courmont
https://www.remlab.net/
Private messages soliciting support will be systematically discarded

brianuuu
New Cone
New Cone
Posts: 4
Joined: 15 Mar 2022 18:44

Re: Getting Raw Audio Data with libVLC on Qt Application

Postby brianuuu » 17 Mar 2022 17:28

There is a bug in the documentation. You need to use "FL32".
I have tried FL32 too, but still doesn't change, I even tried others like S16N, S32N, S32L, nothing affects it :(
For reference I'm using revision 3.0.16-0-g5e70837d8d

unidan
Developer
Developer
Posts: 1493
Joined: 25 Mar 2018 01:00

Re: Getting Raw Audio Data with libVLC on Qt Application

Postby unidan » 18 Mar 2022 12:42

Hi, you can also create a visualization plugin if you want an entrypoint where you get the audio stream without blocking the audio playback. That's how visualization are working in VLC, to answer your question.


Return to “Development around libVLC”

Who is online

Users browsing this forum: No registered users and 25 guests