Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/***************************************************************************
* Copyright (C) 2015 by Tomasz Bojczuk *
* tomaszbojczuk@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include "tqtaudioin.h"
#include "tpitchfinder.h"
#include <QAudioInput>
#include <QIODevice>
#include <QThread>
#include <QDebug>
TaudioIN::TaudioIN(TaudioParams* params, QObject *parent) :
TcommonListener(params, parent),
m_inDevice(0),
m_buffer(0),
m_thread(new QThread)
{
QAudioDeviceInfo defaultIn = QAudioDeviceInfo::defaultInputDevice();
QAudioFormat format;
format.setChannelCount(1);
format.setSampleRate(48000);
format.setSampleType(QAudioFormat::SignedInt);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
if (!defaultIn.isFormatSupported(format)) {
qDebug() << "Format 48000/16 mono is not suported";
format = defaultIn.nearestFormat(format);
qDebug() << "Format is" << format.sampleRate() << format.channelCount() << format.sampleSize();
}
m_audioIN = new QAudioInput(defaultIn, format, this);
m_audioIN->setBufferSize(2048);
finder()->setSampleRate(m_audioIN->format().sampleRate()); // framesPerChunk is determined here
// moveToThread(m_thread);
// connect(m_thread, &QThread::started, this, &TaudioIN::dataReady);
}
TaudioIN::~TaudioIN() {
stopListening();
m_thread->deleteLater();
}
void TaudioIN::startListening() {
if (detectingState() != e_detecting) {
m_inDevice = m_audioIN->start();
if (m_inDevice) {
m_buffer = new qint16[m_audioIN->bufferSize()];
connect(m_inDevice, &QIODevice::readyRead, this, &TaudioIN::dataReady);
// connect(m_inDevice, &QIODevice::readyRead, m_thread, &QThread::start);
setState(e_detecting);
qDebug() << "started with buffer" << m_audioIN->bufferSize();
}
}
}
void TaudioIN::stopListening() {
if (detectingState() != e_stopped) {
m_audioIN->stop();
delete m_buffer;
m_buffer = 0;
resetVolume();
resetChunkPitch();
setState(e_stopped);
finder()->resetFinder();
}
}
void TaudioIN::dataReady() {
int bytesReady = m_audioIN->bytesReady();
// qDebug() << bytesReady << "ready to read";
int toRead = qMin(bytesReady, m_audioIN->bufferSize());
int dataRead = m_inDevice->read((char*)m_buffer, toRead) / 2;
// if (dataRead > 1024) {
// dataRead = 1024;
// qDebug() << dataRead << "Audio data was cut off. Buffer is too small !!!!";
// }
for (int i = 0; i < dataRead; i++) {
finder()->fillBuffer(*(m_buffer + i) / 32760.0f);
}
}