Skip to content

Commit

Permalink
Added ability to control the gaining factor on the fly
Browse files Browse the repository at this point in the history
The gaining factor is an independent to the regular volume and has goal to adjust the volume of too quite or too loud music source, and unlike the volume setting, it is a multiplication factor that has no upper limit.
  • Loading branch information
Wohlstand committed Nov 22, 2024
1 parent 0a3ac3c commit 0f7e551
Show file tree
Hide file tree
Showing 34 changed files with 654 additions and 297 deletions.
1 change: 1 addition & 0 deletions CHANGES.X.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ It is an additional changelog file. It extends the CHANGES.txt that lists change
* Added the ability to hook the file path opener.
* Fixed the detection of some MP3 files and improved the performance on low-end platforms.
* Fixed the inability to open PXTONE files when output has more than two channels.
* Added an ability to change the gaining factor on the fly (Added Mix_SetMusicGain() and Mix_GetMusicGain() calls)

2.6.0: (2023-11-23)
* Added new calls: Mix_ADLMIDI_getAutoArpeggio(), Mix_ADLMIDI_setAutoArpeggio(), Mix_OPNMIDI_getAutoArpeggio(), Mix_OPNMIDI_setAutoArpeggio(), Mix_QuerySpec(), Mix_SetMusicSpeed(), Mix_GetMusicSpeed(), Mix_SetMusicPitch(), Mix_GetMusicPitch(), Mix_GME_SetSpcEchoDisabled(), Mix_GME_GetSpcEchoDisabled()
Expand Down
40 changes: 37 additions & 3 deletions examples/MusPlay-Qt/MainWindow/musplayer_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ void MusPlayer_Qt::contextMenu(const QPoint &pos)
QAction *echo = x.addAction("Echo");
QAction *echoTuner = x.addAction("Echo tuner...");
QAction *musicFX = x.addAction("Music FX...");
QAction *resetGain = x.addAction("Reset gain");
QAction *resetTempo = x.addAction("Reset tempo");
QAction *resetSpeed = x.addAction("Reset speed");
QAction *resetPitch = x.addAction("Reset pitch");
resetGain->setEnabled(ui->gainFrame->isEnabled());
resetTempo->setEnabled(ui->tempoFrame->isEnabled());
resetSpeed->setEnabled(ui->speedFrame->isEnabled());
resetPitch->setEnabled(ui->pitchFrame->isEnabled());
Expand Down Expand Up @@ -307,6 +309,8 @@ void MusPlayer_Qt::contextMenu(const QPoint &pos)
on_actionTuneEcho_triggered();
else if(musicFX == ret)
on_actionMusicFX_triggered();
else if(resetGain == ret)
ui->gain->setValue(m_setupMidi->getGainDefault());
else if(resetTempo == ret)
ui->tempo->setValue(0);
else if(resetSpeed == ret)
Expand Down Expand Up @@ -445,20 +449,34 @@ void MusPlayer_Qt::on_play_clicked()

if(PGE_MusicPlayer::openFile(musicPath))
{
float gain = Mix_GetMusicGain(PGE_MusicPlayer::s_playMus);
int gainDefault = gain >= 0.0f ? static_cast<int>(gain * 100): 100;
double tempo = Mix_GetMusicTempo(PGE_MusicPlayer::s_playMus);
double speed = Mix_GetMusicSpeed(PGE_MusicPlayer::s_playMus);
double pitch = Mix_GetMusicPitch(PGE_MusicPlayer::s_playMus);

m_setupMidi->setGainDefault(gainDefault);
m_setupMidi->setGain(gainDefault);

ui->gain->blockSignals(true);
ui->gain->setValue(gainDefault);
ui->gain->blockSignals(false);
ui->gainFrame->setEnabled(gain >= 0.0f);

ui->tempo->blockSignals(true);
ui->tempo->setValue(0);
ui->tempo->blockSignals(false);
ui->tempoFrame->setEnabled(Mix_GetMusicTempo(PGE_MusicPlayer::s_playMus) >= 0.0);
ui->tempoFrame->setEnabled(tempo >= 0.0);

ui->speed->blockSignals(true);
ui->speed->setValue(0);
ui->speed->blockSignals(false);
ui->speedFrame->setEnabled(Mix_GetMusicSpeed(PGE_MusicPlayer::s_playMus) >= 0.0);
ui->speedFrame->setEnabled(speed >= 0.0);

ui->pitch->blockSignals(true);
ui->pitch->setValue(0);
ui->pitch->blockSignals(false);
ui->pitchFrame->setEnabled(Mix_GetMusicPitch(PGE_MusicPlayer::s_playMus) >= 0.0);
ui->pitchFrame->setEnabled(pitch >= 0.0);

PGE_MusicPlayer::changeVolume(ui->volume->value());
playSuccess = PGE_MusicPlayer::playMusic();
Expand Down Expand Up @@ -598,6 +616,22 @@ void MusPlayer_Qt::on_disableSpcEcho_clicked(bool checked)
Mix_GME_SetSpcEchoDisabled(PGE_MusicPlayer::s_playMus, checked ? 1 : 0);
}

void MusPlayer_Qt::on_gain_valueChanged(int gain)
{
#ifdef SDL_MIXER_X
if(Mix_PlayingMusicStream(PGE_MusicPlayer::s_playMus))
{
float gainFactor = 0.01 * float(gain);
Mix_SetMusicGain(PGE_MusicPlayer::s_playMus, gainFactor);
qDebug() << "Changed gain factor: " << gainFactor;
QToolTip::showText(QCursor::pos(), QString("%1").arg(gainFactor), this);
m_setupMidi->setGain(gain);
}
#else
Q_UNUSED(gain);
#endif
}

void MusPlayer_Qt::on_tempo_valueChanged(int tempo)
{
#ifdef SDL_MIXER_X
Expand Down
1 change: 1 addition & 0 deletions examples/MusPlay-Qt/MainWindow/musplayer_qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private slots:
void on_trackNext_clicked();
void on_disableSpcEcho_clicked(bool checked);

void on_gain_valueChanged(int tempo);
void on_tempo_valueChanged(int tempo);
void on_speed_valueChanged(int speed);
void on_pitch_valueChanged(int pitch);
Expand Down
Loading

0 comments on commit 0f7e551

Please sign in to comment.