1
votes

I'm trying to understand how I can generate a waveform from an audio (or video) file to display to the user.

I've been googling around for quite a while now and can't determine if this is even possible in Qt without using something like FFmpeg. I've seen all of these classes: QMediaPlayer, QMediaContent, QMediaResource, QAudioProbe and experimented with the Qt Media Player Example but am just not seeing where I can access the actual audio buffer.

So I have 2 questions:

  1. Is what I want to do even possible without 3rd party libraries?
  2. If it is possible, can some kind soul outline what I need to read and understand in order to access the audio data

I have tried the suggestions from this question (Audio visualization with QMediaPlayer) but the result of audioProbe->setSource(player) is always false and the method processBuffer never gets called.

audioProbe = new QAudioProbe(this);
bool success = audioProbe->setSource(player);
qDebug() << success;

connect(audioProbe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(processBuffer(QAudioBuffer)));

Update: Adding some additional detail in the hope of clarifying things.

  • For testing/learning I am using the Media Player Example which ships with Qt, so it is set up correctly with Q_OBJECT etc.

  • For audio, I tested with both .mp3 and .wav files. FWIW, the player example won't play video for some reason (.mp4, .avi were tested)

  • The player in the code is QMediaPlayer – which inherits from QMediaObject. The example code for the Player class is here. I added my code (in original comment above) right after the player is instantiated. I also tried adding it once media is loaded.

  • I tried declaring my slot first as private, then as public – either way, it is never called.

Frustrating that such a simple thing is so hard.

2
which platform are you on?phyatt

2 Answers

1
votes

Going the "no external library" route will likely just lead to more of a headache and more work than is necessary. The other advantage of going with an established library is you won't be bound to one file format, as not all formats store their data the same way. If the audio format is uncompressed (wav or other) you can read the header until you get to the data chunk. An answer to this question here details this in C. You should be able to get an idea for the file format from this to apply it to another language.

You will want to understand how many channels are in the wav file, bit depth, and also the sampling rate before you can do anything worthwhile with the data. All this info can be grabbed from the header.

1
votes

It turns out that QAudioProbe is not supported on OSX – the platform I am working on. Took quite a while (a "Qt while. . .") to ferret that info out so I am posting it here explicitly.

See this document for full details: Qt 5.5.0 Multimedia Backends