Skip to content

Commit

Permalink
Enable different colors for oscilloscope channels (#7155)
Browse files Browse the repository at this point in the history
Enable to set different colors for the oscilloscope channels:
* Left channel color
* Right channel color
* Color of all other channels

The clipping color is now used per channel, i.e. if the left channel clips but the right does not then only the signal of the left channel is painted in the clipping color.

Enable setting the colors in the style sheets and adjust the style sheets of the default and classic theme accordingly.
  • Loading branch information
michaelgregorius authored Mar 26, 2024
1 parent 45fd326 commit a41da81
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 26 deletions.
4 changes: 3 additions & 1 deletion data/themes/classic/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ lmms--gui--GroupBox {
lmms--gui--Oscilloscope {
background: none;
border: none;
qproperty-normalColor: rgb(71, 253, 133);
qproperty-leftChannelColor: rgb(71, 253, 133);
qproperty-rightChannelColor: rgb(238, 253, 71);
qproperty-otherChannelsColor: rgb(71, 235, 253);
qproperty-clippingColor: rgb(255, 64, 64);
}

Expand Down
4 changes: 3 additions & 1 deletion data/themes/default/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ lmms--gui--GroupBox {
lmms--gui--Oscilloscope {
background: none;
border: none;
qproperty-normalColor: rgb(71, 253, 133);
qproperty-leftChannelColor: rgb(71, 253, 133);
qproperty-rightChannelColor: rgb(238, 253, 71);
qproperty-otherChannelsColor: rgb(71, 235, 253);
qproperty-clippingColor: rgb(255, 64, 64);
}

Expand Down
20 changes: 15 additions & 5 deletions include/Oscilloscope.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,24 @@ class Oscilloscope : public QWidget
{
Q_OBJECT
public:
Q_PROPERTY( QColor normalColor READ normalColor WRITE setNormalColor )
Q_PROPERTY( QColor leftChannelColor READ leftChannelColor WRITE setLeftChannelColor )
Q_PROPERTY( QColor rightChannelColor READ rightChannelColor WRITE setRightChannelColor )
Q_PROPERTY( QColor otherChannelsColor READ otherChannelsColor WRITE setOtherChannelsColor )
Q_PROPERTY( QColor clippingColor READ clippingColor WRITE setClippingColor )

Oscilloscope( QWidget * _parent );
~Oscilloscope() override;

void setActive( bool _active );

QColor const & normalColor() const;
void setNormalColor(QColor const & normalColor);
QColor const & leftChannelColor() const;
void setLeftChannelColor(QColor const & leftChannelColor);

QColor const & rightChannelColor() const;
void setRightChannelColor(QColor const & rightChannelColor);

QColor const & otherChannelsColor() const;
void setOtherChannelsColor(QColor const & otherChannelsColor);

QColor const & clippingColor() const;
void setClippingColor(QColor const & clippingColor);
Expand All @@ -62,7 +70,7 @@ protected slots:
void updateAudioBuffer( const lmms::surroundSampleFrame * buffer );

private:
QColor const & determineLineColor(float level) const;
bool clips(float level) const;

private:
QPixmap m_background;
Expand All @@ -71,7 +79,9 @@ protected slots:
sampleFrame * m_buffer;
bool m_active;

QColor m_normalColor;
QColor m_leftChannelColor;
QColor m_rightChannelColor;
QColor m_otherChannelsColor;
QColor m_clippingColor;
} ;

Expand Down
56 changes: 37 additions & 19 deletions src/gui/widgets/Oscilloscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ Oscilloscope::Oscilloscope( QWidget * _p ) :
m_background( embed::getIconPixmap( "output_graph" ) ),
m_points( new QPointF[Engine::audioEngine()->framesPerPeriod()] ),
m_active( false ),
m_normalColor(71, 253, 133),
m_leftChannelColor(71, 253, 133),
m_rightChannelColor(71, 253, 133),
m_otherChannelsColor(71, 253, 133),
m_clippingColor(255, 64, 64)
{
setFixedSize( m_background.width(), m_background.height() );
Expand Down Expand Up @@ -112,14 +114,34 @@ void Oscilloscope::setActive( bool _active )
}


QColor const & Oscilloscope::normalColor() const
QColor const & Oscilloscope::leftChannelColor() const
{
return m_normalColor;
return m_leftChannelColor;
}

void Oscilloscope::setNormalColor(QColor const & normalColor)
void Oscilloscope::setLeftChannelColor(QColor const & leftChannelColor)
{
m_normalColor = normalColor;
m_leftChannelColor = leftChannelColor;
}

QColor const & Oscilloscope::rightChannelColor() const
{
return m_rightChannelColor;
}

void Oscilloscope::setRightChannelColor(QColor const & rightChannelColor)
{
m_rightChannelColor = rightChannelColor;
}

QColor const & Oscilloscope::otherChannelsColor() const
{
return m_otherChannelsColor;
}

void Oscilloscope::setOtherChannelsColor(QColor const & otherChannelsColor)
{
m_otherChannelsColor = otherChannelsColor;
}

QColor const & Oscilloscope::clippingColor() const
Expand Down Expand Up @@ -147,11 +169,9 @@ void Oscilloscope::paintEvent( QPaintEvent * )

const fpp_t frames = audioEngine->framesPerPeriod();
AudioEngine::StereoSample peakValues = audioEngine->getPeakValues(m_buffer, frames);
const float max_level = qMax<float>( peakValues.left, peakValues.right );

// Set the color of the line according to the maximum level
float const maxLevelWithAppliedMasterGain = max_level * masterOutput;
p.setPen(QPen(determineLineColor(maxLevelWithAppliedMasterGain), 0.7));
auto const leftChannelClips = clips(peakValues.left * masterOutput);
auto const rightChannelClips = clips(peakValues.right * masterOutput);

p.setRenderHint( QPainter::Antialiasing );

Expand All @@ -162,8 +182,14 @@ void Oscilloscope::paintEvent( QPaintEvent * )
int x_base = 2;
const qreal y_base = height() / 2 - 0.5;

qreal const width = 0.7;
for( ch_cnt_t ch = 0; ch < DEFAULT_CHANNELS; ++ch )
{
QColor color = ch == 0 ? (leftChannelClips ? clippingColor() : leftChannelColor()) : // Check left channel
ch == 1 ? (rightChannelClips ? clippingColor() : rightChannelColor()) : // Check right channel
otherChannelsColor(); // Any other channel
p.setPen(QPen(color, width));

for( int frame = 0; frame < frames; ++frame )
{
sample_t const clippedSample = AudioEngine::clip(m_buffer[frame][ch]);
Expand Down Expand Up @@ -193,17 +219,9 @@ void Oscilloscope::mousePressEvent( QMouseEvent * _me )
}
}


QColor const & Oscilloscope::determineLineColor(float level) const
bool Oscilloscope::clips(float level) const
{
if( level <= 1.0f )
{
return normalColor();
}
else
{
return clippingColor();
}
return level > 1.0f;
}


Expand Down

0 comments on commit a41da81

Please sign in to comment.