##// END OF EJS Templates
Audio example now takes every other value from mic input
Marek Rosa -
r2159:5fd29ccd7e15
parent child
Show More
@@ -1,59 +1,59
1 #include "widget.h"
1 #include "widget.h"
2 #include <QAudioDeviceInfo>
2 #include <QAudioDeviceInfo>
3 #include <QAudioInput>
3 #include <QAudioInput>
4 #include <QChartView>
4 #include <QChartView>
5 #include <QLineSeries>
5 #include <QLineSeries>
6 #include <QChart>
6 #include <QChart>
7 #include <QVBoxLayout>
7 #include <QVBoxLayout>
8 #include <QValueAxis>
8 #include <QValueAxis>
9 #include "xyseriesiodevice.h"
9 #include "xyseriesiodevice.h"
10
10
11 QTCOMMERCIALCHART_USE_NAMESPACE
11 QTCOMMERCIALCHART_USE_NAMESPACE
12
12
13 Widget::Widget(QWidget *parent)
13 Widget::Widget(QWidget *parent)
14 : QWidget(parent),
14 : QWidget(parent),
15 m_device(0),
15 m_device(0),
16 m_chart(0),
16 m_chart(0),
17 m_series(0),
17 m_series(0),
18 m_audioInput(0)
18 m_audioInput(0)
19 {
19 {
20 m_chart = new QChart;
20 m_chart = new QChart;
21 QChartView *chartView = new QChartView(m_chart);
21 QChartView *chartView = new QChartView(m_chart);
22 chartView->setMinimumSize(800, 600);
22 chartView->setMinimumSize(800, 600);
23 m_series = new QLineSeries;
23 m_series = new QLineSeries;
24 m_chart->addSeries(m_series);
24 m_chart->addSeries(m_series);
25 QValueAxis *axisX = new QValueAxis;
25 QValueAxis *axisX = new QValueAxis;
26 axisX->setRange(0, 8000);
26 axisX->setRange(0, 4000);
27 QValueAxis *axisY = new QValueAxis;
27 QValueAxis *axisY = new QValueAxis;
28 axisY->setRange(0, 256);
28 axisY->setRange(0, 256);
29 m_chart->setAxisX(axisX, m_series);
29 m_chart->setAxisX(axisX, m_series);
30 m_chart->setAxisY(axisY, m_series);
30 m_chart->setAxisY(axisY, m_series);
31 m_chart->legend()->hide();
31 m_chart->setTitle("Data from microphone");
32 m_chart->setTitle("Data from microphone");
32
33
33 QVBoxLayout *mainLayout = new QVBoxLayout;
34 QVBoxLayout *mainLayout = new QVBoxLayout;
34 mainLayout->addWidget(chartView);
35 mainLayout->addWidget(chartView);
35 setLayout(mainLayout);
36 setLayout(mainLayout);
36
37
37 QAudioFormat formatAudio;
38 QAudioFormat formatAudio;
38 formatAudio.setFrequency(8000);
39 formatAudio.setFrequency(8000);
39 formatAudio.setChannels(1);
40 formatAudio.setChannels(1);
40 formatAudio.setSampleSize(8);
41 formatAudio.setSampleSize(8);
41 formatAudio.setCodec("audio/pcm");
42 formatAudio.setCodec("audio/pcm");
42 formatAudio.setByteOrder(QAudioFormat::LittleEndian);
43 formatAudio.setByteOrder(QAudioFormat::LittleEndian);
43 formatAudio.setSampleType(QAudioFormat::UnSignedInt);
44 formatAudio.setSampleType(QAudioFormat::UnSignedInt);
44
45
45 QAudioDeviceInfo inputDevices = QAudioDeviceInfo::defaultInputDevice();
46 QAudioDeviceInfo inputDevices = QAudioDeviceInfo::defaultInputDevice();
46 m_audioInput = new QAudioInput(inputDevices,formatAudio, this);
47 m_audioInput = new QAudioInput(inputDevices,formatAudio, this);
47
48
48 m_device = new XYSeriesIODevice(m_series, this);
49 m_device = new XYSeriesIODevice(m_series, this);
49 m_device->open(QIODevice::WriteOnly);
50 m_device->open(QIODevice::WriteOnly);
50
51
51 m_audioInput->setBufferSize(10);
52 m_audioInput->start(m_device);
52 m_audioInput->start(m_device);
53 }
53 }
54
54
55 Widget::~Widget()
55 Widget::~Widget()
56 {
56 {
57 m_audioInput->stop();
57 m_audioInput->stop();
58 m_device->close();
58 m_device->close();
59 }
59 }
@@ -1,36 +1,36
1 #include "xyseriesiodevice.h"
1 #include "xyseriesiodevice.h"
2 #include <QXYSeries>
2 #include <QXYSeries>
3
3
4 XYSeriesIODevice::XYSeriesIODevice(QXYSeries * series, QObject *parent) :
4 XYSeriesIODevice::XYSeriesIODevice(QXYSeries * series, QObject *parent) :
5 QIODevice(parent),
5 QIODevice(parent),
6 m_series(series)
6 m_series(series)
7 {
7 {
8 }
8 }
9
9
10 qint64 XYSeriesIODevice::readData(char * data, qint64 maxSize)
10 qint64 XYSeriesIODevice::readData(char * data, qint64 maxSize)
11 {
11 {
12 Q_UNUSED(data)
12 Q_UNUSED(data)
13 Q_UNUSED(maxSize)
13 Q_UNUSED(maxSize)
14 return -1;
14 return -1;
15 }
15 }
16
16
17 qint64 XYSeriesIODevice::writeData(const char * data, qint64 maxSize)
17 qint64 XYSeriesIODevice::writeData(const char * data, qint64 maxSize)
18 {
18 {
19 qint64 range = 8000;
19 qint64 range = 4000;
20 QList<QPointF> oldPoints = m_series->points();
20 QList<QPointF> oldPoints = m_series->points();
21 QList<QPointF> points;
21 QList<QPointF> points;
22
22
23 if (oldPoints.count() < range) {
23 if (oldPoints.count() < range) {
24 points = m_series->points();
24 points = m_series->points();
25 } else {
25 } else {
26 for (int i = maxSize; i < oldPoints.count(); i++)
26 for (int i = maxSize/2; i < oldPoints.count(); i++)
27 points.append(QPointF(i - maxSize, oldPoints.at(i).y()));
27 points.append(QPointF(i - maxSize/2, oldPoints.at(i).y()));
28 }
28 }
29
29
30 qint64 size = points.count();
30 qint64 size = points.count();
31 for (int k = 0; k < maxSize; k++)
31 for (int k = 0; k < maxSize/2; k++)
32 points.append(QPointF(k + size, (quint8)data[k]));
32 points.append(QPointF(k + size, (quint8)data[2 * k]));
33
33
34 m_series->replace(points);
34 m_series->replace(points);
35 return maxSize;
35 return maxSize;
36 }
36 }
General Comments 0
You need to be logged in to leave comments. Login now