Commit d94e53b0 authored by Matthieu Gallien's avatar Matthieu Gallien 🎵

alsmost allow replay gain to be configurable

parent 30fd76c3
Pipeline #36061 passed with stage
in 22 minutes and 9 seconds
......@@ -62,8 +62,21 @@ class ELISALIB_EXPORT AudioWrapper : public QObject
READ seekable
NOTIFY seekableChanged)
Q_PROPERTY(ReplayGainSetting replayGainMode
READ replayGainMode
WRITE setReplayGainMode
NOTIFY replayGainModeChanged)
public:
enum ReplayGainSetting {
NoReplayGain,
ReplayGainAlbum,
ReplayGainTrack,
};
Q_ENUM(ReplayGainSetting)
explicit AudioWrapper(QObject *parent = nullptr);
~AudioWrapper() override;
......@@ -86,6 +99,8 @@ public:
bool seekable() const;
ReplayGainSetting replayGainMode() const;
Q_SIGNALS:
void mutedChanged(bool muted);
......@@ -114,6 +129,8 @@ Q_SIGNALS:
void stopped();
void replayGainModeChanged();
public Q_SLOTS:
void setMuted(bool muted);
......@@ -136,6 +153,8 @@ public Q_SLOTS:
void seek(qint64 position);
void setReplayGainMode(AudioWrapper::ReplayGainSetting mode);
private Q_SLOTS:
void mediaStatusChanged();
......@@ -165,6 +184,8 @@ private:
void playerSeekableSignalChanges(bool isSeekable);
void resetVlcLayer();
friend class AudioWrapperPrivate;
std::unique_ptr<AudioWrapperPrivate> d;
......
......@@ -64,6 +64,8 @@ public:
bool mHasSavedPosition = false;
AudioWrapper::ReplayGainSetting mReplayGainMode = AudioWrapper::NoReplayGain;
void vlcEventCallback(const struct libvlc_event_t *p_event);
void mediaIsEnded();
......@@ -94,40 +96,7 @@ static void vlc_callback(const struct libvlc_event_t *p_event, void *p_data)
AudioWrapper::AudioWrapper(QObject *parent) : QObject(parent), d(std::make_unique<AudioWrapperPrivate>())
{
d->mParent = this;
const char *arguments[] = {"--audio-replay-gain-mode=album"};
d->mInstance = libvlc_new(1, arguments);
if (!d->mInstance) {
qDebug() << "AudioWrapper::AudioWrapper" << "failed creating libvlc instance" << libvlc_errmsg();
return;
}
libvlc_set_user_agent(d->mInstance, "elisa", "Elisa Music Player");
libvlc_set_app_id(d->mInstance, "org.kde.elisa", ELISA_VERSION_STRING, "elisa");
d->mPlayer = libvlc_media_player_new(d->mInstance);
if (!d->mPlayer) {
qCDebug(orgKdeElisaPlayerVlc) << "AudioWrapper::AudioWrapper" << "failed creating player" << libvlc_errmsg();
return;
}
d->mPlayerEventManager = libvlc_media_player_event_manager(d->mPlayer);
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerOpening, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerBuffering, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerPlaying, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerPaused, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerStopped, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerEndReached, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerEncounteredError, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerPositionChanged, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerSeekableChanged, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerLengthChanged, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerMuted, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerUnmuted, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerAudioVolume, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerAudioDevice, &vlc_callback, d.get());
resetVlcLayer();
}
AudioWrapper::~AudioWrapper()
......@@ -191,6 +160,11 @@ bool AudioWrapper::seekable() const
return d->mIsSeekable;
}
AudioWrapper::ReplayGainSetting AudioWrapper::replayGainMode() const
{
return d->mReplayGainMode;
}
QMediaPlayer::State AudioWrapper::playbackState() const
{
return d->mPreviousPlayerState;
......@@ -222,26 +196,30 @@ void AudioWrapper::setVolume(qreal volume)
void AudioWrapper::setSource(const QUrl &source)
{
resetVlcLayer();
if (source.isLocalFile()) {
qCDebug(orgKdeElisaPlayerVlc) << "AudioWrapper::setSource reading local resource";
d->mMedia = libvlc_media_new_path(d->mInstance, QDir::toNativeSeparators(source.toLocalFile()).toUtf8().constData());
if (!d->mMedia) {
qCDebug(orgKdeElisaPlayerVlc) << "AudioWrapper::setSource"
<< "failed creating media"
<< libvlc_errmsg()
<< QDir::toNativeSeparators(source.toLocalFile()).toUtf8().constData();
return;
}
} else {
qCDebug(orgKdeElisaPlayerVlc) << "AudioWrapper::setSource reading remote resource";
d->mMedia = libvlc_media_new_location(d->mInstance, source.url().toUtf8().constData());
}
if (!d->mMedia) {
qCDebug(orgKdeElisaPlayerVlc) << "AudioWrapper::setSource"
<< "failed creating media"
<< libvlc_errmsg()
<< QDir::toNativeSeparators(source.toLocalFile()).toUtf8().constData();
d->mMedia = libvlc_media_new_path(d->mInstance, QDir::toNativeSeparators(source.toLocalFile()).toLatin1().constData());
if (!d->mMedia) {
qCDebug(orgKdeElisaPlayerVlc) << "AudioWrapper::setSource"
<< "failed creating media"
<< libvlc_errmsg()
<< QDir::toNativeSeparators(source.toLocalFile()).toLatin1().constData();
<< "failed creating media"
<< libvlc_errmsg()
<< source.url().toUtf8().constData();
return;
}
}
......@@ -323,6 +301,17 @@ void AudioWrapper::seek(qint64 position)
setPosition(position);
}
void AudioWrapper::setReplayGainMode(AudioWrapper::ReplayGainSetting mode)
{
if (d->mReplayGainMode == mode) {
return;
}
d->mReplayGainMode = mode;
Q_EMIT replayGainModeChanged();
}
void AudioWrapper::mediaStatusChanged()
{
}
......@@ -396,6 +385,71 @@ void AudioWrapper::playerSeekableSignalChanges(bool isSeekable)
QMetaObject::invokeMethod(this, [this, isSeekable]() {Q_EMIT seekableChanged(isSeekable);}, Qt::QueuedConnection);
}
void AudioWrapper::resetVlcLayer()
{
if (d->mInstance) {
libvlc_release(d->mInstance);
d->mInstance = nullptr;
}
if (d->mPlayer) {
libvlc_media_player_release(d->mPlayer);
d->mPlayer = nullptr;
d->mPlayerEventManager = nullptr;
}
switch (d->mReplayGainMode)
{
case NoReplayGain:
d->mInstance = libvlc_new(0, nullptr);
break;
case ReplayGainAlbum:
{
const char *arguments[] = {"--audio-replay-gain-mode=album"};
d->mInstance = libvlc_new(1, arguments);
break;
}
case ReplayGainTrack:
{
const char *arguments[] = {"--audio-replay-gain-mode=track"};
d->mInstance = libvlc_new(1, arguments);
break;
}
}
if (!d->mInstance) {
qDebug() << "AudioWrapper::AudioWrapper" << "failed creating libvlc instance" << libvlc_errmsg();
return;
}
libvlc_set_user_agent(d->mInstance, "elisa", "Elisa Music Player");
libvlc_set_app_id(d->mInstance, "org.kde.elisa", "0.3.80", "elisa");
d->mPlayer = libvlc_media_player_new(d->mInstance);
if (!d->mPlayer) {
qDebug() << "AudioWrapper::AudioWrapper" << "failed creating player" << libvlc_errmsg();
return;
}
d->mPlayerEventManager = libvlc_media_player_event_manager(d->mPlayer);
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerOpening, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerBuffering, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerPlaying, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerPaused, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerStopped, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerEndReached, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerEncounteredError, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerPositionChanged, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerSeekableChanged, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerLengthChanged, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerMuted, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerUnmuted, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerAudioVolume, &vlc_callback, d.get());
libvlc_event_attach(d->mPlayerEventManager, libvlc_MediaPlayerAudioDevice, &vlc_callback, d.get());
}
void AudioWrapperPrivate::vlcEventCallback(const struct libvlc_event_t *p_event)
{
const auto eventType = static_cast<libvlc_event_e>(p_event->type);
......
......@@ -29,6 +29,8 @@ public:
bool mHasSavedPosition = false;
AudioWrapper::ReplayGainSetting mReplayGainMode = AudioWrapper::NoReplayGain;
};
AudioWrapper::AudioWrapper(QObject *parent) : QObject(parent), d(std::make_unique<AudioWrapperPrivate>())
......@@ -95,6 +97,11 @@ bool AudioWrapper::seekable() const
return d->mPlayer.isSeekable();
}
AudioWrapper::ReplayGainSetting AudioWrapper::replayGainMode() const
{
return d->mReplayGainMode;
}
QMediaPlayer::State AudioWrapper::playbackState() const
{
return d->mPlayer.state();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment