##// END OF EJS Templates
Added audio input demo
Marek Rosa -
r2155:efb312b94196
parent child
Show More
@@ -0,0 +1,13
1 !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" )
2
3 QT += multimedia
4
5 TARGET = audio
6 TEMPLATE = app
7
8 SOURCES += main.cpp\
9 widget.cpp \
10 xyseriesiodevice.cpp
11
12 HEADERS += widget.h \
13 xyseriesiodevice.h
@@ -0,0 +1,6
1 #include "lineseriesdevice.h"
2
3 LineSeriesDevice::LineSeriesDevice(QObject *parent) :
4 QLineSeries(parent)
5 {
6 }
@@ -0,0 +1,21
1 #ifndef LINESERIESDEVICE_H
2 #define LINESERIESDEVICE_H
3
4 #include <QLineSeries>
5 #include <QIODevice>
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
9 class LineSeriesDevice : public QIODevice, public QLineSeries
10 {
11 Q_OBJECT
12 public:
13 explicit LineSeriesDevice(QObject *parent = 0);
14
15 signals:
16
17 public slots:
18
19 ;
20
21 #endif // LINESERIESDEVICE_H
@@ -0,0 +1,11
1 #include <QApplication>
2 #include "widget.h"
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 Widget w;
8 w.show();
9
10 return a.exec();
11 }
@@ -0,0 +1,59
1 #include "widget.h"
2 #include <QAudioDeviceInfo>
3 #include <QAudioInput>
4 #include <QChartView>
5 #include <QLineSeries>
6 #include <QChart>
7 #include <QVBoxLayout>
8 #include <QValueAxis>
9 #include "xyseriesiodevice.h"
10
11 QTCOMMERCIALCHART_USE_NAMESPACE
12
13 Widget::Widget(QWidget *parent)
14 : QWidget(parent),
15 m_device(0),
16 m_chart(0),
17 m_series(0),
18 m_audioInput(0)
19 {
20 m_chart = new QChart;
21 QChartView *chartView = new QChartView(m_chart);
22 chartView->setMinimumSize(800, 600);
23 m_series = new QLineSeries;
24 m_chart->addSeries(m_series);
25 QValueAxis *axisX = new QValueAxis;
26 axisX->setRange(0, 8000);
27 QValueAxis *axisY = new QValueAxis;
28 axisY->setRange(0, 256);
29 m_chart->setAxisX(axisX, m_series);
30 m_chart->setAxisY(axisY, m_series);
31 m_chart->setTitle("Data from microphone");
32
33 QVBoxLayout *mainLayout = new QVBoxLayout;
34 mainLayout->addWidget(chartView);
35 setLayout(mainLayout);
36
37 QAudioFormat formatAudio;
38 formatAudio.setFrequency(8000);
39 formatAudio.setChannels(1);
40 formatAudio.setSampleSize(8);
41 formatAudio.setCodec("audio/pcm");
42 formatAudio.setByteOrder(QAudioFormat::LittleEndian);
43 formatAudio.setSampleType(QAudioFormat::UnSignedInt);
44
45 QAudioDeviceInfo inputDevices = QAudioDeviceInfo::defaultInputDevice();
46 m_audioInput = new QAudioInput(inputDevices,formatAudio, this);
47
48 m_device = new XYSeriesIODevice(m_series, this);
49 m_device->open(QIODevice::WriteOnly);
50
51 m_audioInput->setBufferSize(10);
52 m_audioInput->start(m_device);
53 }
54
55 Widget::~Widget()
56 {
57 m_audioInput->stop();
58 m_device->close();
59 }
@@ -0,0 +1,32
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QWidget>
5 #include <QChartGlobal>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 class QLineSeries;
9 class QChart;
10 QTCOMMERCIALCHART_END_NAMESPACE
11
12 QTCOMMERCIALCHART_USE_NAMESPACE
13
14 class XYSeriesIODevice;
15 class QAudioInput;
16
17 class Widget : public QWidget
18 {
19 Q_OBJECT
20
21 public:
22 Widget(QWidget *parent = 0);
23 ~Widget();
24
25 private:
26 XYSeriesIODevice *m_device;
27 QChart *m_chart;
28 QLineSeries *m_series;
29 QAudioInput *m_audioInput;
30 };
31
32 #endif // WIDGET_H
@@ -0,0 +1,36
1 #include "xyseriesiodevice.h"
2 #include <QXYSeries>
3
4 XYSeriesIODevice::XYSeriesIODevice(QXYSeries * series, QObject *parent) :
5 QIODevice(parent),
6 m_series(series)
7 {
8 }
9
10 qint64 XYSeriesIODevice::readData(char * data, qint64 maxSize)
11 {
12 Q_UNUSED(data)
13 Q_UNUSED(maxSize)
14 return -1;
15 }
16
17 qint64 XYSeriesIODevice::writeData(const char * data, qint64 maxSize)
18 {
19 qint64 range = 8000;
20 QList<QPointF> oldPoints = m_series->points();
21 QList<QPointF> points;
22
23 if (oldPoints.count() < range) {
24 points = m_series->points();
25 } else {
26 for (int i = maxSize; i < oldPoints.count(); i++)
27 points.append(QPointF(i - maxSize, oldPoints.at(i).y()));
28 }
29
30 qint64 size = points.count();
31 for (int k = 0; k < maxSize; k++)
32 points.append(QPointF(k + size, (quint8)data[k]));
33
34 m_series->replace(points);
35 return maxSize;
36 }
@@ -0,0 +1,27
1 #ifndef XYSERIESIODEVICE_H
2 #define XYSERIESIODEVICE_H
3
4 #include <QIODevice>
5 #include <QChartGlobal>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 class QXYSeries;
9 QTCOMMERCIALCHART_END_NAMESPACE
10
11 QTCOMMERCIALCHART_USE_NAMESPACE
12
13 class XYSeriesIODevice : public QIODevice
14 {
15 Q_OBJECT
16 public:
17 explicit XYSeriesIODevice(QXYSeries * series, QObject *parent = 0);
18
19 protected:
20 qint64 readData(char * data, qint64 maxSize);
21 qint64 writeData(const char * data, qint64 maxSize);
22
23 private:
24 QXYSeries *m_series;
25 };
26
27 #endif // XYSERIESIODEVICE_H
@@ -14,7 +14,8 SUBDIRS += piechartcustomization \
14 qmlcustommodel \
14 qmlcustommodel \
15 chartinteractions \
15 chartinteractions \
16 qmlaxes \
16 qmlaxes \
17 qmlcustomlegend
17 qmlcustomlegend \
18 audio
18
19
19 contains(QT_CONFIG, opengl) {
20 contains(QT_CONFIG, opengl) {
20 SUBDIRS += chartthemes \
21 SUBDIRS += chartthemes \
General Comments 0
You need to be logged in to leave comments. Login now