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