Skip to content

Commit

Permalink
generator select outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
psmokotnin committed Jan 25, 2019
1 parent 330110d commit fb387d3
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 17 deletions.
54 changes: 41 additions & 13 deletions qml/GeneratorProperties.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,50 @@ Item {
anchors.fill: parent
spacing: 10

//list of available output devices:
ComboBox {
id: deviceSelect
model: generatorModel.devices
displayText: generatorModel.device;

implicitWidth: parent.width
currentIndex: model.indexOf(generatorModel.device)
onCurrentIndexChanged: function () {
generatorModel.device = model[currentIndex];
RowLayout {
//list of available output devices:
ComboBox {
id: deviceSelect
model: generatorModel.devices
displayText: generatorModel.device;

Layout.fillWidth: true
currentIndex: model.indexOf(generatorModel.device)
onCurrentIndexChanged: function () {
generatorModel.device = model[currentIndex];
}

ToolTip.visible: hovered
ToolTip.text: qsTr("audio output device")
}

ToolTip.visible: hovered
ToolTip.text: qsTr("audio output device")
}
ComboBox {
model: generatorModel.chanelsCount
currentIndex: generatorModel.chanel
onCurrentIndexChanged: generatorModel.chanel = currentIndex
displayText: "chanel: " + (currentIndex + 1)
delegate: ItemDelegate {
text: modelData + 1
width: parent.width
}
ToolTip.visible: hovered
ToolTip.text: qsTr("chanel number")
}

ComboBox {
model: generatorModel.chanelsCount
currentIndex: generatorModel.aux
onCurrentIndexChanged: generatorModel.aux = currentIndex
displayText: "aux: " + (currentIndex + 1)
delegate: ItemDelegate {
text: modelData + 1
width: parent.width
}
ToolTip.visible: hovered
ToolTip.text: qsTr("aux chanel number")
}

}
RowLayout {
spacing: 10
Layout.alignment: Qt.AlignTop
Expand Down
21 changes: 21 additions & 0 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Generator::Generator(QObject *parent) : QObject(parent),
connect(&m_thread, SIGNAL(typeChanged()), this, SIGNAL(typeChanged()), Qt::QueuedConnection);
connect(&m_thread, SIGNAL(frequencyChanged(int)), this, SIGNAL(frequencyChanged(int)), Qt::QueuedConnection);
connect(&m_thread, SIGNAL(gainChanged(float)), this, SIGNAL(gainChanged(float)), Qt::QueuedConnection);
connect(&m_thread, SIGNAL(chanelChanged(int)), this, SIGNAL(chanelChanged(int)), Qt::QueuedConnection);
connect(&m_thread, SIGNAL(auxChanged(int)), this, SIGNAL(auxChanged(int)), Qt::QueuedConnection);
connect(&m_thread, SIGNAL(chanelsCountChanged()), this, SIGNAL(chanelsCountChanged()), Qt::QueuedConnection);
}
Generator::~Generator()
{
Expand Down Expand Up @@ -78,3 +81,21 @@ void Generator::setGain(float gain)
Q_ARG(float, gain)
);
}
void Generator::setChanel(int chanel)
{
QMetaObject::invokeMethod(
&m_thread,
"setChanel",
Qt::QueuedConnection,
Q_ARG(int, chanel)
);
}
void Generator::setAux(int chanel)
{
QMetaObject::invokeMethod(
&m_thread,
"setAux",
Qt::QueuedConnection,
Q_ARG(int, chanel)
);
}
15 changes: 14 additions & 1 deletion src/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class Generator : public QObject
//Gain
Q_PROPERTY(float gain READ gain WRITE setGain NOTIFY gainChanged)

Q_PROPERTY(int chanelsCount READ chanelsCount NOTIFY chanelsCountChanged)
Q_PROPERTY(int chanel READ chanel WRITE setChanel NOTIFY chanelChanged)
Q_PROPERTY(int aux READ aux WRITE setAux NOTIFY auxChanged)

private:
GeneratorThread m_thread;

Expand All @@ -68,15 +72,24 @@ class Generator : public QObject
int frequency() {return m_thread.frequency();}
void setFrequency(int f);

int gain() {return m_thread.gain();}
float gain() {return m_thread.gain();}
void setGain(float gain);

int chanelsCount() const {return m_thread.chanelsCount();}
int chanel() const {return m_thread.chanel();}
void setChanel(int chanel);
int aux() const {return m_thread.aux();}
void setAux(int chanel);

signals:
void enabledChanged(bool);
void typeChanged();
void frequencyChanged(int f);
void deviceChanged();
void gainChanged(float);
void chanelsCountChanged();
void chanelChanged(int);
void auxChanged(int);

public slots:
};
Expand Down
31 changes: 30 additions & 1 deletion src/generatorthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ GeneratorThread::GeneratorThread(QObject *parent) :
m_gain(-6.f),
m_type(0),
m_frequency(1000),
m_chanelCount(1),
m_chanel(0),
m_aux(1),
m_enabled(false)
{
start();
Expand All @@ -41,7 +44,6 @@ void GeneratorThread::init()

m_device = QAudioDeviceInfo::defaultOutputDevice();
m_format.setSampleRate(48000);
m_format.setChannelCount(1);
m_format.setSampleSize(32);
m_format.setCodec("audio/pcm");
m_format.setByteOrder(QAudioFormat::LittleEndian);
Expand Down Expand Up @@ -97,9 +99,17 @@ void GeneratorThread::_selectDevice(QAudioDeviceInfo device)
m_audio->stop();
delete m_audio;
}
m_chanelCount = 1;
foreach (auto formatChanels, m_device.supportedChannelCounts()) {
if (formatChanels > m_chanelCount)
m_chanelCount = formatChanels;
}
m_format.setChannelCount(m_chanelCount);
m_audio = new QAudioOutput(m_device, m_format, this);

_updateAudio();
emit deviceChanged();
emit chanelsCountChanged();
}
void GeneratorThread::_updateAudio()
{
Expand All @@ -108,6 +118,9 @@ void GeneratorThread::_updateAudio()
m_sources[m_type]->open(QIODevice::ReadOnly);
}
m_sources[m_type]->setGain(m_gain);
m_sources[m_type]->setChanel(m_chanel);
m_sources[m_type]->setAux(m_aux);
m_sources[m_type]->setChanelCount(m_chanelCount);
m_sources[m_type]->setSamplerate(m_format.sampleRate());
m_audio->start(m_sources[m_type]);
} else {
Expand Down Expand Up @@ -144,3 +157,19 @@ void GeneratorThread::setGain(float gain)
emit gainChanged(gain);
}
}
void GeneratorThread::setChanel(int chanel)
{
if (m_chanel != chanel) {
m_chanel = chanel;
m_sources[m_type]->setChanel(m_chanel);
emit chanelChanged(m_chanel);
}
}
void GeneratorThread::setAux(int chanel)
{
if (m_aux != chanel) {
m_aux = chanel;
m_sources[m_type]->setAux(m_aux);
emit auxChanged(m_aux);
}
}
11 changes: 11 additions & 0 deletions src/generatorthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class GeneratorThread : public QThread
float m_gain;
int m_type;
int m_frequency;
int m_chanelCount, m_chanel, m_aux;
bool m_enabled;

void _selectDevice(QAudioDeviceInfo device);
Expand Down Expand Up @@ -66,12 +67,22 @@ public slots:
float gain() {return m_gain;}
void setGain(float gain);

int chanelsCount() const {return m_chanelCount;}
int chanel() const {return m_chanel;}
void setChanel(int chanel);

int aux() const {return m_aux;}
void setAux(int chanel);

signals:
void enabledChanged(bool);
void deviceChanged();
void typeChanged();
void frequencyChanged(int f);
void gainChanged(float);
void chanelChanged(int);
void auxChanged(int);
void chanelsCountChanged();
};

#endif // GENERATORTHREAD_H
20 changes: 18 additions & 2 deletions src/outputdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,33 @@
#include <math.h>

OutputDevice::OutputDevice(QObject *parent) : QIODevice(parent),
m_chanel(1),
m_chanelCount(1),
m_gain(1.f)
{
connect(parent, SIGNAL(gainChanged(float)), this, SLOT(setGain(float)));
}
qint64 OutputDevice::readData(char *data, qint64 maxlen)
{
qint64 total = 0;
int chanel = m_chanelCount;
const float zero = 0.f;
Sample src;

while (maxlen - total > 0) {
const Sample src = this->sample();
memcpy(data + total, &src.f, sizeof(float));
if (chanel >= m_chanelCount) {
chanel = 0;
src = this->sample();
}

if (chanel == m_chanel || chanel == m_aux) {
memcpy(data + total, &src.f, sizeof(float));
} else {
memcpy(data + total, &zero, sizeof(float));
}
total += sizeof(float);

++chanel;
}
return total;
}
Expand Down
4 changes: 4 additions & 0 deletions src/outputdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class OutputDevice : public QIODevice

protected:
int sampleRate;
int m_chanel, m_aux, m_chanelCount;
float m_gain;

public:
Expand All @@ -43,6 +44,9 @@ class OutputDevice : public QIODevice

public slots:
void setGain(float gaindB);
void setChanel(int chanel) {m_chanel = chanel;}
void setAux(int chanel) {m_aux = chanel;}
void setChanelCount(int count) {m_chanelCount = count;}

};

Expand Down

0 comments on commit fb387d3

Please sign in to comment.