Skip to content

Instantly share code, notes, and snippets.

@saidinesh5
Created September 10, 2012 18:29
Show Gist options
  • Select an option

  • Save saidinesh5/3692753 to your computer and use it in GitHub Desktop.

Select an option

Save saidinesh5/3692753 to your computer and use it in GitHub Desktop.
A simple test app to test Phonon's Audio data out: To Compile: qmake && make , To Run: ./audiodataout /path/to/audiofile
#include <QtGui>
#include <phonon/audiodataoutput.h>
class AudioClass: public QWidget{
Q_OBJECT
QMap<Phonon::AudioDataOutput::Channel, QVector<qint16> > m_audiodata;
public slots:
void dataReceived(const QMap<Phonon::AudioDataOutput::Channel, QVector<qint16> >& data){
//qDebug()<<"Hii"<<data[Phonon::AudioDataOutput::LeftChannel][512];
m_audiodata = data;
update();
}
void paintEvent(QPaintEvent *event){
// qDebug()<<"In Paint Event";
int w = size().width();
int h = size().height();
QPainter painter;
painter.begin(this);
painter.setBrush(Qt::white);
//Draw A Background Rectangle
painter.drawRect(0,0,w,h);
//Draw the wave form
QPainterPath leftChannelPath,rightChannelPath;
int y = 0;
int x = 0;
int nSamples = m_audiodata[Phonon::AudioDataOutput::LeftChannel].size();
for(int i =0; i < nSamples ;i++){
//One for the left channel
y = (m_audiodata[Phonon::AudioDataOutput::LeftChannel][i]*h)/131070 + h*0.25;
x = (i*w)/nSamples;
leftChannelPath.lineTo(x,y);
//One for the right channel
y= (m_audiodata[Phonon::AudioDataOutput::RightChannel][i]*h)/131070 + h*0.75;
rightChannelPath.lineTo(x,y);
}
//Draw the Audio data
painter.setBrush(Qt::transparent);
painter.setPen(Qt::blue);
painter.drawPath(leftChannelPath);
painter.setPen(Qt::red);
painter.drawPath(rightChannelPath);
painter.setPen(Qt::black);
//Draw the axes
painter.drawLine(0,0,0,h);
painter.drawLine(0,h*0.25,w,h*0.25);
painter.drawLine(0,h*0.5,w,h*0.5);
painter.drawLine(0,h*0.75,w,h*0.75);
painter.end();
}
};
######################################################################
# Automatically generated by qmake (2.01a) Sat Feb 11 03:09:54 2012
######################################################################
QT += phonon
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
SOURCES += main.cpp
HEADERS += audioclass.h
#include <QtGui>
#include <phonon/audiooutput.h>
#include <phonon/audiodataoutput.h>
#include <phonon/mediaobject.h>
#include <QDebug>
#include <audioclass.h>
int main(int argc, char * argv[]) {
QApplication app(argc, argv);
app.setApplicationName("simpleplayer");
Phonon::AudioOutput output(Phonon::MusicCategory, &app);
Phonon::MediaObject media(&app);
Phonon::AudioDataOutput dataout(&app);
if(dataout.isValid()){
qDebug()<<"AudioDataOutput::isValid() : True";
Phonon::createPath(&media, &dataout);
media.connect(&media, SIGNAL(finished()), &app, SLOT(quit()));
Phonon::createPath(&media, &output);
AudioClass monitor;
media.connect( &dataout, SIGNAL(dataReady(const QMap<Phonon::AudioDataOutput::Channel, QVector<qint16> >&)),
&monitor, SLOT(dataReceived(const QMap<Phonon::AudioDataOutput::Channel, QVector<qint16> >&)));
media.setCurrentSource(app.arguments().at(1));
media.play();
monitor.show();
return app.exec();
}
else{
qDebug() << "AudioDataOutput::isValid() : False";
return -1;
}
}
@kripton
Copy link
Copy Markdown

kripton commented Jun 7, 2013

Helped me very much. Thank you!

@kangqf
Copy link
Copy Markdown

kangqf commented Nov 28, 2013

very good thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment