PDA

View Full Version : how to extract audio format from a sound file



cduffy
12th April 2013, 20:15
Given a sound file (e.g., .wav), is there a way in Qt 4.7 to extract the audio format from it?

I generate a sound in a way that essentially uses all this example code (http://doc.qt.digia.com/4.7/qaudiooutput.html#details) (from the detailed documentation to the QAudioOutput class).

This involves hard-coding the audio format with this helper method:

QAudioFormat audioFormat()
{
QAudioFormat format;

format.setSampleRate(8000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");

format.setSampleType(QAudioFormat::UnSignedInt);

return format;
}

I give the format to the QAudioOutput instance:

QAudioFormat format = audioFormat();
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());

audioOutput_ = new QAudioOutput(info, format, this); // audioOutput_ is defined already as a QAudioOutput*

My code works with a .wav file with the right sampling rate.
I would prefer to extract the format from the .wav file instead - that way, I'm not so constrained with what files I have to use. After getting the format, I could say:

format = info.nearestFormat(format);

to get the format to conform to my system. But how do I extract the format from the file?

Thanks!

wysota
13th April 2013, 09:49
If you generate the sound yourself then how come you don't know what format you generate it in? Or did you mean something else?

If you just want to play a sound then use QMediaPlayer from QtMultimediaKit module.

cduffy
15th April 2013, 16:15
If you generate the sound yourself then how come you don't know what format you generate it in? Or did you mean something else?

If you just want to play a sound then use QMediaPlayer from QtMultimediaKit module.

Thank you for the reply.

I would like the flexibility to allow the application's user to submit an arbitrary sound file. (I know the format so long as I restrict myself to a limited set of files I have picked out. In either case, I am not generating the sound "from scratch", but merely playing a sound file that either I or the user have supplied.)

QMediaPlayer seems to be new to Qt 5. I am using Qt 4.7.

wysota
15th April 2013, 18:33
QMediaPlayer is part of Qt4's QtMultimediaKit (QtMobility) set.