Skip to content

Commit

Permalink
ide: server, volume widgets & audio status box - unify imlementation
Browse files Browse the repository at this point in the history
the server class works as a model
  • Loading branch information
timblechmann committed May 5, 2016
1 parent 3cdfdfc commit 59f64e3
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 142 deletions.
57 changes: 37 additions & 20 deletions editors/sc-ide/core/sc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ void ScServer::createActions(Settings::Manager * settings)

mActions[Volume] = widgetAction = new QWidgetAction(this);
widgetAction->setDefaultWidget( mVolumeWidget );
connect( mVolumeWidget, SIGNAL(volumeChanged(float)), this, SLOT(sendVolume(float)) );
connect( mVolumeWidget, SIGNAL(volumeChanged(float)), this, SIGNAL(volumeChanged(float)) );

connect( mVolumeWidget, &VolumeWidget::volumeChangeRequested, [this](float newValue) {
setVolume( newValue );
});
connect( this, SIGNAL(volumeChanged(float)), mVolumeWidget, SLOT(setVolume(float)) );
connect( this, SIGNAL(volumeRangeChanged(float,float)), mVolumeWidget, SLOT(setVolumeRange(float,float)) );
emit volumeChanged( mVolume );
emit volumeRangeChanged( mVolumeMin, mVolumeMax );

mActions[VolumeUp] = action = new QAction(tr("Increase Volume"), this);
action->setShortcut(tr("Ctrl+Alt+PgUp", "Increase volume"));
Expand Down Expand Up @@ -264,11 +270,25 @@ void ScServer::setDumpingOSC( bool dumping )
sendDumpingOSC(dumping);
}

float ScServer::volume() const { return mVolumeWidget->volume(); }
float ScServer::volume() const { return mVolume; }

void ScServer::setVolume( float volume )
{
mVolumeWidget->setVolume( volume );
volume = qBound( mVolumeMin, volume, mVolumeMax );

if( volume != mVolume ) {
mVolume = volume;
sendVolume( volume );
emit volumeChanged( volume );
}
}


void ScServer::setVolumeRange(float min, float max)
{
mVolumeMin = min;
mVolumeMax = max;
emit volumeRangeChanged( min, max );
}

void ScServer::increaseVolume()
Expand Down Expand Up @@ -378,8 +398,8 @@ void ScServer::onScLangReponse( const QString & selector, const QString & data )
static QString unmutedSelector("serverUnmuted");
static QString ampSelector("serverAmp");
static QString ampRangeSelector("serverAmpRange");
static QString startDumpOSCSelector("dumpOSCStarted");
static QString stopDumpOSCSelector("dumpOSCStopped");
static QString startDumpOSCSelector("dumpOSCStarted");
static QString stopDumpOSCSelector("dumpOSCStopped");


if (selector == defaultServerRunningChangedSelector)
Expand All @@ -389,20 +409,18 @@ void ScServer::onScLangReponse( const QString & selector, const QString & data )
} else if (selector == unmutedSelector) {
mActions[Mute]->setChecked(false);
}
else if (selector == startDumpOSCSelector) {
else if (selector == startDumpOSCSelector) {
mActions[DumpOSC]->setChecked(true);
}
else if (selector == stopDumpOSCSelector) {
else if (selector == stopDumpOSCSelector) {
mActions[DumpOSC]->setChecked(false);
}
else if (selector == ampSelector) {
bool ok;
float volume = data.mid(1, data.size() - 2).toFloat(&ok);
if (ok) {
bool signals_blocked = mVolumeWidget->blockSignals(true);
volume = mVolumeWidget->setVolume(volume);
mVolumeWidget->blockSignals(signals_blocked);
emit volumeChanged(volume);
mVolume = volume;
emit volumeChanged( volume );
}
}
else if (selector == ampRangeSelector) {
Expand All @@ -414,10 +432,9 @@ void ScServer::onScLangReponse( const QString & selector, const QString & data )
if (!ok) return;
float max = dataList[1].toFloat(&ok);
if (!ok) return;
bool signals_blocked = mVolumeWidget->blockSignals(true);
mVolumeWidget->setRange( min, max );
mVolumeWidget->blockSignals(signals_blocked);
emit volumeChanged(mVolumeWidget->volume());
mVolumeMin = min;
mVolumeMax = max;
setVolumeRange( min, max );
}
}

Expand All @@ -443,16 +460,16 @@ void ScServer::handleRuningStateChangedMsg( const QString & data )

success = doc[2].Read(port);
if (!success) return; // LATER: report error?
success = doc[3].Read(serverUnresponsive);
if (!success) return; // LATER: report error?

success = doc[3].Read(serverUnresponsive);
if (!success) return; // LATER: report error?
}

QString qstrHostName( hostName.c_str() );

onRunningStateChanged( serverRunningState, qstrHostName, port);

emit runningStateChange( serverRunningState, qstrHostName, port, serverUnresponsive );
emit runningStateChanged( serverRunningState, qstrHostName, port, serverUnresponsive );
}

void ScServer::timerEvent(QTimerEvent * event)
Expand Down
8 changes: 7 additions & 1 deletion editors/sc-ide/core/sc_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ScProcess;
class VolumeWidget;
namespace Settings { class Manager; }


class ScServer : public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -69,8 +70,11 @@ class ScServer : public QObject

QAction *action(ActionRole role) { return mActions[role]; }

Q_PROPERTY( float volume READ volume WRITE setVolume NOTIFY volumeChanged )

float volume() const;
void setVolume( float volume );
void setVolumeRange( float min, float max );

bool isMuted() const;
void setMuted( bool muted );
Expand Down Expand Up @@ -103,11 +107,12 @@ public slots:
void setRecording( bool active );

signals:
void runningStateChange( bool running, QString const & hostName, int port, bool unresponsive );
void runningStateChanged( bool running, QString const & hostName, int port, bool unresponsive );
void updateServerStatus (int ugenCount, int synthCount,
int groupCount, int defCount,
float avgCPU, float peakCPU);
void volumeChanged( float volume );
void volumeRangeChanged( float min, float max);
void mutedChanged( bool muted );
void recordingChanged( bool recording );

Expand Down Expand Up @@ -162,6 +167,7 @@ private slots:

QAction * mActions[ActionCount];

float mVolume = 0, mVolumeMin = -90, mVolumeMax = 6;
VolumeWidget *mVolumeWidget;
QTimer mRecordTimer;
boost::chrono::system_clock::time_point mRecordTime;
Expand Down
95 changes: 42 additions & 53 deletions editors/sc-ide/widgets/audio_status_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@
namespace ScIDE {

AudioStatusBox::AudioStatusBox(ScServer *server, QWidget *parent):
StatusBox(parent),
mServer(server),
m_avg_cpu(0.f),
m_peak_cpu(0.f),
m_ugens(0),
m_synths(0),
m_groups(0),
m_synthdefs(0)
StatusBox(parent)
{
mStatisticsLabel = new StatusLabel;
mVolumeLabel = new StatusLabel;
Expand All @@ -58,7 +51,6 @@ AudioStatusBox::AudioStatusBox(ScServer *server, QWidget *parent):
server->action(ScServer::Mute)->setProperty("keep_menu_open", true);
server->action(ScServer::DumpOSC)->setProperty("keep_menu_open", true);

QAction *separator;
addAction( server->action(ScServer::ToggleRunning) );
addAction( server->action(ScServer::Reboot) );
addAction( server->action(ScServer::KillAll) );
Expand All @@ -77,72 +69,69 @@ AudioStatusBox::AudioStatusBox(ScServer *server, QWidget *parent):
addAction( server->action(ScServer::Mute) );
addAction( server->action(ScServer::Volume) );

connect(server, SIGNAL(runningStateChange(bool,QString,int,bool)),
this, SLOT(onServerRunningChanged(bool,QString,int,bool)));
connect(server, SIGNAL(updateServerStatus(int,int,int,int,float,float)),
this, SLOT(onServerStatusReply(int,int,int,int,float,float)));
connect(server, SIGNAL(volumeChanged(float)), this, SLOT(updateVolumeLabel(float)));
connect(server, SIGNAL(mutedChanged(bool)), this, SLOT(updateMuteLabel(bool)));
connect(server, SIGNAL(recordingChanged(bool)), this, SLOT(updateRecordLabel(bool)));
// server -> box
connect( server, SIGNAL(runningStateChanged(bool,QString,int,bool)), this, SLOT(onServerRunningChanged(bool,QString,int,bool)) );
connect( server, SIGNAL(updateServerStatus(int,int,int,int,float,float)),
this, SLOT(updateStatistics(int,int,int,int,float,float)));
connect( server, SIGNAL(volumeChanged(float)), this, SLOT(updateVolumeLabel(float)) );
connect( server, SIGNAL(mutedChanged(bool)), this, SLOT(updateMuteLabel(bool)) );
connect( server, SIGNAL(recordingChanged(bool)), this, SLOT(updateRecordLabel(bool)) );

onServerRunningChanged(false, "", 0, false);
updateVolumeLabel( mServer->volume() );
updateMuteLabel( mServer->isMuted() );
updateRecordLabel( mServer->isRecording() );
updateVolumeLabel( server->volume() );
updateMuteLabel( server->isMuted() );
updateRecordLabel( server->isRecording() );

// box to server
connect( this, &AudioStatusBox::decreaseVolume, [=] () {
server->changeVolume( -0.5 );
});

connect( this, &AudioStatusBox::increaseVolume, [=] () {
server->changeVolume( +0.5 );
});
}

void AudioStatusBox::onServerStatusReply(int ugens, int synths, int groups, int synthDefs,
float avgCPU, float peakCPU)
{
m_avg_cpu = avgCPU;
m_peak_cpu = peakCPU;
m_ugens = ugens;
m_synths = synths;
m_groups = groups;
m_synthdefs = synthDefs;

updateStatistics();
}

void AudioStatusBox::onServerRunningChanged(bool running, const QString &, int, bool unresponsive)
{

if (unresponsive) {
mStatisticsLabel->setTextColor(Qt::yellow);
mVolumeLabel->setTextColor(Qt::yellow);
} else if(running) {
mStatisticsLabel->setTextColor(Qt::green);
mVolumeLabel->setTextColor(Qt::green);
} else {
mStatisticsLabel->setTextColor(Qt::white);
mVolumeLabel->setTextColor(Qt::white);
};

if (unresponsive) {
mStatisticsLabel->setTextColor(Qt::yellow);
mVolumeLabel->setTextColor(Qt::yellow);
} else if(running) {
mStatisticsLabel->setTextColor(Qt::green);
mVolumeLabel->setTextColor(Qt::green);
} else {
mStatisticsLabel->setTextColor(Qt::white);
mVolumeLabel->setTextColor(Qt::white);
};

if (!running)
onServerStatusReply(0, 0, 0, 0, 0, 0);
updateStatistics(0, 0, 0, 0, 0, 0);
}

void AudioStatusBox::wheelEvent(QWheelEvent * event)
{
if (event->orientation() == Qt::Vertical) {
if (event->delta() > 0)
mServer->changeVolume(0.2);
emit increaseVolume();
else
mServer->changeVolume(-0.2);
emit decreaseVolume();
}
StatusBox::wheelEvent(event);
}

void AudioStatusBox::updateStatistics()
void AudioStatusBox::updateStatistics( int ugens, int synths, int groups, int synthDefs,
float avgCPU, float peakCPU )
{
QString statusString =
QStringLiteral("%1% %2% %3u %4s %5g %6d ")
.arg(m_avg_cpu, 5, 'f', 2)
.arg(m_peak_cpu, 5, 'f', 2)
.arg(m_ugens, 4)
.arg(m_synths, 4)
.arg(m_groups, 4)
.arg(m_synthdefs, 4);
.arg(avgCPU, 5, 'f', 2)
.arg(peakCPU, 5, 'f', 2)
.arg(ugens, 4)
.arg(synths, 4)
.arg(groups, 4)
.arg(synthDefs, 4);

mStatisticsLabel->setText(statusString);
}
Expand Down
21 changes: 8 additions & 13 deletions editors/sc-ide/widgets/audio_status_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,23 @@ class AudioStatusBox : public StatusBox
Q_OBJECT
public:
AudioStatusBox(ScServer *, QWidget *parent = 0);

private slots:
void onServerStatusReply(int ugens, int synths, int groups, int synthDefs,
float avgCPU, float peakCPU);
void onServerRunningChanged( bool running, QString const & hostName, int port, bool unresponsive );

void updateStatistics();
void updateStatistics( int ugens, int synths, int groups, int synthDefs,
float avgCPU, float peakCPU );

void updateVolumeLabel(float volume);
void updateMuteLabel(bool muted);
void updateRecordLabel(bool recording);

protected:
void wheelEvent(QWheelEvent *);
signals:
void increaseVolume();
void decreaseVolume();

private:
class ScServer * mServer;

float m_avg_cpu;
float m_peak_cpu;
int m_ugens;
int m_synths;
int m_groups;
int m_synthdefs;
void wheelEvent(QWheelEvent *);

StatusLabel *mStatisticsLabel;
StatusLabel *mVolumeLabel;
Expand Down
Loading

0 comments on commit 59f64e3

Please sign in to comment.