Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ if conf.has('USE_QT')
'Stream Tuner (experimental)': get_variable('have_streamtuner', false),
'VU Meter': get_option('vumeter'),
'X11 Global Hotkeys': get_variable('have_qthotkey', false),
'Waveform': true,
}, section: 'Qt Support')
endif

Expand Down
2 changes: 2 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,5 @@ option('spectrum-analyzer', type: 'boolean', value: true,
description: 'Whether the Spectrum Analyzer (2D) plugin is enabled')
option('vumeter', type: 'boolean', value: true,
description: 'Whether the VU Meter visualization plugin is enabled')
option('waveform', type: 'boolean', value: true,
description: 'Whether the Waveform visualization plugin is enabled')
6 changes: 6 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ if conf.has('USE_QT')
if get_option('vumeter')
subdir('vumeter-qt')
endif

if get_option('waveform')
subdir('waveform-qt')
endif
endif


Expand Down Expand Up @@ -258,6 +262,8 @@ if conf.has('USE_GTK')
if get_option('vumeter')
subdir('vumeter')
endif

# TODO: add waveform for GTK
endif


Expand Down
13 changes: 13 additions & 0 deletions src/waveform-qt/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
PLUGIN = waveform-qt${PLUGIN_SUFFIX}

SRCS = waveform_qt.cc waveform_qt_widget.cc

include ../../buildsys.mk
include ../../extra.mk

plugindir := ${plugindir}/${VISUALIZATION_PLUGIN_DIR}

LD = ${CXX}
CFLAGS += ${PLUGIN_CFLAGS}
CPPFLAGS += ${PLUGIN_CPPFLAGS} -I../.. ${QT_CFLAGS}
LIBS += -lm ${QT_LIBS}
17 changes: 17 additions & 0 deletions src/waveform-qt/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
avformat_dep = dependency('libavformat')
avcodec_dep = dependency('libavcodec')
avutil_dep = dependency('libavutil')
swresample_dep = dependency('libswresample')
threads_dep = dependency('threads')
shared_module('waveform-qt',
'track_peaks.cc',
'preload.cc',
'track_state.cc',
'waveform_widget.cc',
'settings.cc',
'waveform_plugin.cc',
dependencies: [audacious_dep, qt_dep, avformat_dep, avcodec_dep, avutil_dep, swresample_dep, threads_dep],
name_prefix: '',
install: true,
install_dir: visualization_plugin_dir
)
128 changes: 128 additions & 0 deletions src/waveform-qt/preload.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2026 0000xFFFF.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "preload.h"

#include <atomic>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <vector>

#include <libaudcore/audstrings.h>
#include <libaudcore/playlist.h>

std::atomic<bool> g_preload_running{false};
std::atomic<int> g_preload_total{0};
std::atomic<int> g_preload_done{0};

bool preload_one_file(const char * filename, TrackPeaks * out)
{
TrackPeaks local;
TrackPeaks * peaks = out ? out : &local;

char * cpath = cache_path_for(filename);

bool ok = load_cache(cpath, peaks);
if (!ok)
{
StringBuf local_path = uri_to_filename(filename);
const char * decode_path =
local_path ? (const char *)local_path : filename;

ok = decode_peaks(decode_path, peaks);
if (ok)
save_cache(cpath, peaks);
}

delete[] cpath;
return ok;
}

/* Filenames are snapshotted on the calling (UI) thread -- Playlist's
* API is not meant to be called from worker threads -- then handed off
* to the worker pool below, so a beefier machine churns through the
* playlist proportionally faster. */
void run_playlist_preload()
{
if (g_preload_running.exchange(true))
return; /* already running */

Playlist pl = Playlist::active_playlist();
int n = pl.n_entries();

g_preload_total = n;
g_preload_done = 0;

if (n <= 0)
{
g_preload_running = false;
return;
}

auto * filenames = new std::vector<char *>();
filenames->reserve(n);
for (int i = 0; i < n; i++)
{
String fn = pl.entry_filename(i);
filenames->push_back(strdup(fn ? (const char *)fn : ""));
}

std::thread([filenames]() {
unsigned n_threads = std::thread::hardware_concurrency();
if (n_threads == 0)
n_threads = 2; /* hardware_concurrency() can return 0; fall back */

size_t total = filenames->size();
if (n_threads > total)
n_threads = (unsigned)total;

std::atomic<size_t> next_idx{0};

std::vector<std::thread> workers;
workers.reserve(n_threads);
for (unsigned t = 0; t < n_threads; t++)
{
workers.emplace_back([filenames, &next_idx, total]() {
for (;;)
{
size_t idx = next_idx.fetch_add(1);
if (idx >= total)
break;

const char * fn = (*filenames)[idx];
if (fn && fn[0])
preload_one_file(fn);

g_preload_done.fetch_add(1);
}
});
}

for (auto & w : workers)
w.join();

for (char * fn : *filenames)
free(fn);
delete filenames;

g_preload_running = false;
}).detach();
}
45 changes: 45 additions & 0 deletions src/waveform-qt/preload.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2026 0000xFFFF.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __PRELOAD_H__
#define __PRELOAD_H__

#include <atomic>

#include "track_peaks.h"

/* Progress counters, polled by the controls widget's status label. */
extern std::atomic<bool> g_preload_running;
extern std::atomic<int> g_preload_total;
extern std::atomic<int> g_preload_done;

/* Loads peaks for `filename` from cache if possible, otherwise decodes
* and writes the cache. If `out` is non-null, the resulting peak data
* is copied into it; otherwise it is decoded/loaded and then discarded.
* Shared by the bulk preloader and the single-track async loader. */
bool preload_one_file(const char * filename, TrackPeaks * out = nullptr);

/* Kicks off a background preload of every entry in the active
* playlist using a pool of worker threads sized to the number of CPU
* cores available. Safe to call repeatedly; a second call while one
* is already running is ignored. */
void run_playlist_preload();

#endif
71 changes: 71 additions & 0 deletions src/waveform-qt/settings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2026 0000xFFFF.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "settings.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTimer>

#include <libaudcore/i18n.h>

#include "preload.h"

WaveformSettings::WaveformSettings(QWidget * parent) : QWidget(parent)
{
auto * layout = new QHBoxLayout(this);
layout->setContentsMargins(4, 2, 4, 2);

m_button = new QPushButton(_("Preload Playlist Waveforms"), this);
m_label = new QLabel(this);
m_label->setMinimumWidth(90);

layout->addWidget(m_button);
layout->addWidget(m_label);
layout->addStretch(1);

QObject::connect(m_button, &QPushButton::clicked, this,
[]() { run_playlist_preload(); });

m_timer = new QTimer(this);
QObject::connect(m_timer, &QTimer::timeout, this, [this]() { refresh(); });
m_timer->start(150);

refresh();
}

void WaveformSettings::refresh()
{
bool running = g_preload_running.load();
int done = g_preload_done.load();
int total = g_preload_total.load();

m_button->setEnabled(!running);

if (running)
m_label->setText(QString("%1/%2").arg(done).arg(total));
else if (total > 0)
m_label->setText(QString(_("Done (%1/%2)")).arg(done).arg(total));
else
m_label->setText(QString());
}

void * waveform_get_settings_widget() { return new WaveformSettings(); }
50 changes: 50 additions & 0 deletions src/waveform-qt/settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2026 0000xFFFF.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __WAVEFORM_CONTROLS_H__
#define __WAVEFORM_CONTROLS_H__

#include <QWidget>

class QPushButton;
class QLabel;
class QTimer;

/* Shown on the plugin's preferences page: a button to preload the
* whole playlist's waveforms, and a label that polls preload progress
* while it runs. */
class WaveformSettings : public QWidget
{
public:
explicit WaveformSettings(QWidget * parent = nullptr);

private:
void refresh();

QPushButton * m_button;
QLabel * m_label;
QTimer * m_timer;
};

/* Factory matching the signature WidgetCustomQt() expects in
* libaudcore/preferences.h. */
void * waveform_get_settings_widget();

#endif
Loading
Loading