Merge remote-tracking branch 'origin/master' into main
This commit is contained in:
+18
-2
@@ -9,12 +9,14 @@
|
|||||||
#include <qendian.h>
|
#include <qendian.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Measurer::Measurer(QAudioDeviceInfo input, QAudioDeviceInfo output, MeasureChannel channel)
|
Measurer::Measurer(QAudioDeviceInfo input, QAudioDeviceInfo output, MeasureChannel channel, bool debug)
|
||||||
: input(input), output(output), channel(channel)
|
: input(input), output(output), channel(channel), debug(debug)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,6 +74,15 @@ float Measurer::ProcessData(QByteArray data)
|
|||||||
int t = qFromLittleEndian<qint16>(data.data() + i);
|
int t = qFromLittleEndian<qint16>(data.data() + i);
|
||||||
samples.push_back(t / 32767.0);
|
samples.push_back(t / 32767.0);
|
||||||
}
|
}
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
|
ostringstream oss;
|
||||||
|
for (int i = 0; i < (int)samples.size(); i++)
|
||||||
|
{
|
||||||
|
oss << samples[i] << " ";
|
||||||
|
}
|
||||||
|
cerr << "Samples read: " << oss.str() << "\n";
|
||||||
|
}
|
||||||
// cut off starting and ending silence
|
// cut off starting and ending silence
|
||||||
int left_pos = 0;
|
int left_pos = 0;
|
||||||
while (left_pos < (int)samples.size() && abs(samples[left_pos]) < 1e-4) left_pos++;
|
while (left_pos < (int)samples.size() && abs(samples[left_pos]) < 1e-4) left_pos++;
|
||||||
@@ -102,6 +113,11 @@ float Measurer::ProcessData(QByteArray data)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
float desired = TONE_FREQUENCY * pow / SAMPLE_RATE;
|
float desired = TONE_FREQUENCY * pow / SAMPLE_RATE;
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
|
cerr << "Measure: desired = " << desired << " argmax = " << argmax << " max = "
|
||||||
|
<< max << " left_pos = " << left_pos << " right_pos = " << right_pos << "\n";
|
||||||
|
}
|
||||||
if (abs(desired - argmax) < 1e-3)
|
if (abs(desired - argmax) < 1e-3)
|
||||||
{
|
{
|
||||||
return max / pow;
|
return max / pow;
|
||||||
|
|||||||
+2
-1
@@ -16,10 +16,11 @@ class Measurer
|
|||||||
|
|
||||||
MeasureChannel channel;
|
MeasureChannel channel;
|
||||||
QAudioDeviceInfo input, output;
|
QAudioDeviceInfo input, output;
|
||||||
|
bool debug;
|
||||||
|
|
||||||
float ProcessData(QByteArray data);
|
float ProcessData(QByteArray data);
|
||||||
public:
|
public:
|
||||||
Measurer(QAudioDeviceInfo input, QAudioDeviceInfo output, MeasureChannel channel);
|
Measurer(QAudioDeviceInfo input, QAudioDeviceInfo output, MeasureChannel channel, bool debug);
|
||||||
float Measure(float duration);
|
float Measure(float duration);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -49,19 +49,27 @@ QAudioDeviceInfo GetOutputDevice(QString name)
|
|||||||
throw (string)("Unable to find device " + name.toStdString());
|
throw (string)("Unable to find device " + name.toStdString());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Measure(QString input_device_name, QString output_device_name, float duration)
|
void Measure(QString input_device_name, QString output_device_name, float duration, bool debug)
|
||||||
{
|
{
|
||||||
auto input = GetInputDevice(input_device_name);
|
auto input = GetInputDevice(input_device_name);
|
||||||
auto output = GetOutputDevice(output_device_name);
|
auto output = GetOutputDevice(output_device_name);
|
||||||
Measurer left(input, output, MeasureChannel::Left);
|
Measurer left(input, output, MeasureChannel::Left, debug);
|
||||||
float left_result = left.Measure(duration);
|
float left_result = left.Measure(duration);
|
||||||
Measurer right(input, output, MeasureChannel::Right);
|
Measurer right(input, output, MeasureChannel::Right, debug);
|
||||||
float right_result = right.Measure(duration);
|
float right_result = right.Measure(duration);
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
|
cerr << "Measure complete, left_result = " << left_result << " right_result = " << right_result << "\n";
|
||||||
|
}
|
||||||
float k = 2 * right_result;
|
float k = 2 * right_result;
|
||||||
float v0v = left_result / k;
|
float v0v = left_result / k;
|
||||||
// assume r2 = 10K
|
// assume r2 = 10K
|
||||||
float r1 = 10000 / v0v - 10000;
|
float r1 = 10000 / v0v - 10000;
|
||||||
float t = 1.0 / (log(r1 / 10000) / 4300 + 1 / 298.0) - 273.0;
|
float t = 1.0 / (log(r1 / 10000) / 4300 + 1 / 298.0) - 273.0;
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
|
cerr << "v0v = " << v0v << " r1 = " << r1 << " t = " << t << "\n";
|
||||||
|
}
|
||||||
cout.setf(ios::fixed | ios::showpoint);
|
cout.setf(ios::fixed | ios::showpoint);
|
||||||
cout.precision(4);
|
cout.precision(4);
|
||||||
cout << t << "\n";
|
cout << t << "\n";
|
||||||
@@ -94,9 +102,11 @@ int main(int argc, char *argv[])
|
|||||||
QCommandLineOption input_option(QStringList() << "i" << "input", "Name of device to receive input from", "input");
|
QCommandLineOption input_option(QStringList() << "i" << "input", "Name of device to receive input from", "input");
|
||||||
QCommandLineOption output_option(QStringList() << "o" << "output", "Name of device to send output to", "output");
|
QCommandLineOption output_option(QStringList() << "o" << "output", "Name of device to send output to", "output");
|
||||||
QCommandLineOption duration_option(QStringList() << "d" << "duration", "How many seconds to record (uses twice this time)", "duration");
|
QCommandLineOption duration_option(QStringList() << "d" << "duration", "How many seconds to record (uses twice this time)", "duration");
|
||||||
|
QCommandLineOption debug_option("debug", "Turn on additional logging");
|
||||||
parser.addOption(input_option);
|
parser.addOption(input_option);
|
||||||
parser.addOption(output_option);
|
parser.addOption(output_option);
|
||||||
parser.addOption(duration_option);
|
parser.addOption(duration_option);
|
||||||
|
parser.addOption(debug_option);
|
||||||
parser.process(a);
|
parser.process(a);
|
||||||
if (!parser.isSet(input_option))
|
if (!parser.isSet(input_option))
|
||||||
{
|
{
|
||||||
@@ -115,7 +125,9 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
duration = parser.value(duration_option).toFloat();
|
duration = parser.value(duration_option).toFloat();
|
||||||
}
|
}
|
||||||
Measure(input_device_name, output_device_name, duration);
|
bool debug = false;
|
||||||
|
if (parser.isSet(debug_option)) debug = true;
|
||||||
|
Measure(input_device_name, output_device_name, duration, debug);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -123,5 +135,5 @@ int main(int argc, char *argv[])
|
|||||||
parser.process(a);
|
parser.process(a);
|
||||||
parser.showHelp(0);
|
parser.showHelp(0);
|
||||||
}
|
}
|
||||||
return a.exec();
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user