implemented new audio monitor widget
This commit is contained in:
@@ -26,7 +26,8 @@
|
||||
AudioHybridDevice::AudioHybridDevice(QObject *parent) :
|
||||
QIODevice(parent),
|
||||
device_(nullptr),
|
||||
sample_index_(0)
|
||||
sample_index_(0),
|
||||
enable_sending_samples_(false)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -67,7 +68,47 @@ bool AudioHybridDevice::IsIdle()
|
||||
return device_ == nullptr && pushed_samples_.isEmpty();
|
||||
}
|
||||
|
||||
void AudioHybridDevice::SetEnableSendingSamples(bool e)
|
||||
{
|
||||
enable_sending_samples_ = e;
|
||||
}
|
||||
|
||||
qint64 AudioHybridDevice::readData(char *data, qint64 maxSize)
|
||||
{
|
||||
qint64 read_size = read_internal(data, maxSize);
|
||||
|
||||
if (enable_sending_samples_ && read_size > 0) {
|
||||
// FIXME: Assumes float and stereo
|
||||
float* samples = reinterpret_cast<float*>(data);
|
||||
int sample_count = static_cast<int>(read_size / static_cast<int>(sizeof(float)));
|
||||
int channels = 2;
|
||||
|
||||
// Create array of samples to send
|
||||
QVector<double> averages(channels);
|
||||
averages.fill(0);
|
||||
|
||||
// Add all samples together
|
||||
for (int i=0;i<sample_count;i++) {
|
||||
averages[i%channels] = qMax(averages[i%channels], static_cast<double>(qAbs(samples[i])));
|
||||
}
|
||||
|
||||
emit SentSamples(averages);
|
||||
}
|
||||
|
||||
return read_size;
|
||||
}
|
||||
|
||||
qint64 AudioHybridDevice::writeData(const char *data, qint64 maxSize)
|
||||
{
|
||||
Q_UNUSED(data)
|
||||
Q_UNUSED(maxSize)
|
||||
|
||||
// This device doesn't support writing
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
qint64 AudioHybridDevice::read_internal(char *data, qint64 maxSize)
|
||||
{
|
||||
// If a device is connected, passthrough to it
|
||||
if (device_ != nullptr) {
|
||||
@@ -103,13 +144,3 @@ qint64 AudioHybridDevice::readData(char *data, qint64 maxSize)
|
||||
memset(data, 0, static_cast<size_t>(maxSize));
|
||||
return maxSize;
|
||||
}
|
||||
|
||||
qint64 AudioHybridDevice::writeData(const char *data, qint64 maxSize)
|
||||
{
|
||||
Q_UNUSED(data)
|
||||
Q_UNUSED(maxSize)
|
||||
|
||||
// This device doesn't support writing
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -81,12 +81,27 @@ public:
|
||||
*/
|
||||
bool IsIdle();
|
||||
|
||||
/**
|
||||
* @brief If enabled, this will emit the SentSamples() signal whenever samples are sent to the output device
|
||||
*/
|
||||
void SetEnableSendingSamples(bool e);
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief Signal emitted when this leaves "idle" state and has valid audio data that is ready to be sent
|
||||
*/
|
||||
void HasSamples();
|
||||
|
||||
/**
|
||||
* @brief Signal emitted when samples are sent to the output device
|
||||
*
|
||||
* This sends an array of values corresponding to the channel count. Each value is an average of all the samples just
|
||||
* sent to that channel. Useful for connecting to AudioMonitor.
|
||||
*
|
||||
* Can be disabled with SetEnableSendingSamples() to save CPU usage.
|
||||
*/
|
||||
void SentSamples(QVector<double> averages);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief Internal QIODevice function for reading
|
||||
@@ -115,11 +130,15 @@ protected:
|
||||
virtual qint64 writeData(const char *data, qint64 maxSize) override;
|
||||
|
||||
private:
|
||||
qint64 read_internal(char *data, qint64 maxSize);
|
||||
|
||||
QIODevice* device_;
|
||||
|
||||
QByteArray pushed_samples_;
|
||||
qint64 sample_index_;
|
||||
|
||||
bool enable_sending_samples_;
|
||||
|
||||
};
|
||||
|
||||
#endif // AUDIOHYBRIDDEVICE_H
|
||||
|
||||
@@ -133,7 +133,9 @@ AudioManager::AudioManager() :
|
||||
RefreshDevices();
|
||||
|
||||
connect(&output_manager_, SIGNAL(HasSamples()), this, SLOT(OutputManagerHasSamples()));
|
||||
connect(&output_manager_, SIGNAL(SentSamples(QVector<double>)), this, SIGNAL(SentSamples(QVector<double>)));
|
||||
|
||||
output_manager_.SetEnableSendingSamples(true);
|
||||
output_manager_.open(AudioHybridDevice::ReadOnly);
|
||||
}
|
||||
|
||||
|
||||
@@ -94,6 +94,8 @@ public:
|
||||
signals:
|
||||
void DeviceListReady();
|
||||
|
||||
void SentSamples(QVector<double> averages);
|
||||
|
||||
private:
|
||||
AudioManager();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user