Compare commits

...

1 Commits

Author SHA1 Message Date
01812e6bf9 yyc12345 2024-10-19 18:51:04 +08:00
6 changed files with 146 additions and 4 deletions

7
.editorconfig Normal file
View File

@ -0,0 +1,7 @@
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4

View File

@ -20,6 +20,12 @@ find_package(Qt6 6.6 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRE
find_package(KF6Codecs 6.1.0) find_package(KF6Codecs 6.1.0)
find_package(PkgConfig) find_package(PkgConfig)
option(KISSFFT_PKGCONFIG OFF)
option(KISSFFT_STATIC ON)
option(KISSFFT_TEST OFF)
option(KISSFFT_TOOLS OFF)
add_subdirectory(kissfft EXCLUDE_FROM_ALL)
if (USE_QTEXTCODEC) if (USE_QTEXTCODEC)
find_package(Qt6 6.6 COMPONENTS Core5Compat REQUIRED) find_package(Qt6 6.6 COMPONENTS Core5Compat REQUIRED)
endif() endif()
@ -36,6 +42,7 @@ set (PMUSIC_CPP_FILES
singleapplicationmanager.cpp singleapplicationmanager.cpp
lrcbar.cpp lrcbar.cpp
lyricsmanager.cpp lyricsmanager.cpp
fftspectrum.cpp
) )
set (PMUSIC_HEADER_FILES set (PMUSIC_HEADER_FILES
@ -45,6 +52,7 @@ set (PMUSIC_HEADER_FILES
singleapplicationmanager.h singleapplicationmanager.h
lrcbar.h lrcbar.h
lyricsmanager.h lyricsmanager.h
fftspectrum.h
) )
set (PMUSIC_UI_FILES set (PMUSIC_UI_FILES
@ -84,7 +92,7 @@ if (TARGET KF6::Codecs)
target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs) target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs)
endif () endif ()
target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network) target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network kissfft::kissfft)
if (USE_QTEXTCODEC) if (USE_QTEXTCODEC)
target_compile_definitions(${EXE_NAME} PRIVATE USE_QTEXTCODEC=1) target_compile_definitions(${EXE_NAME} PRIVATE USE_QTEXTCODEC=1)

89
fftspectrum.cpp Normal file
View File

@ -0,0 +1,89 @@
// SPDX-FileCopyrightText: 2024 Gary Wang <git@blumia.net>
//
// SPDX-License-Identifier: MIT
#include "fftspectrum.h"
#include <QAudioBuffer>
#include <QAudioBufferOutput>
#include <QPainter>
#include <kissfft.hh>
FFTSpectrum::FFTSpectrum(QWidget* parent)
: QWidget(parent)
, m_audioBufferOutput(new QAudioBufferOutput(this))
{
connect(this, &FFTSpectrum::mediaPlayerChanged, this, [=]() {
if (m_mediaPlayer) {
m_mediaPlayer->setAudioBufferOutput(m_audioBufferOutput);
}
});
connect(m_audioBufferOutput, &QAudioBufferOutput::audioBufferReceived, this, [=](const QAudioBuffer& buffer) {
const QAudioFormat&& fmt = buffer.format();
const QAudioFormat::SampleFormat sampleFormat = fmt.sampleFormat();
int channelCount = fmt.channelCount();
const int frameCount = buffer.frameCount();
kissfft<float> fft(frameCount, false);
std::vector<kissfft<float>::cpx_t> samples(frameCount);
std::vector<kissfft<float>::cpx_t> mass(frameCount);
if (sampleFormat == QAudioFormat::Int16 && channelCount == 1) {
const QAudioBuffer::S16M* data = buffer.constData<QAudioBuffer::S16M>();
for (int i = 0; i < frameCount; ++i) {
samples[i].real(data[i].value(QAudioFormat::FrontCenter) / float(32768));
samples[i].imag(0);
}
} else if (sampleFormat == QAudioFormat::Float && channelCount == 2) {
const QAudioBuffer::F32S* data = buffer.constData<QAudioBuffer::F32S>();
for (int i = 0; i < frameCount; ++i) {
samples[i].real(data[i].value(QAudioFormat::FrontLeft));
samples[i].imag(0);
}
}
fft.transform(samples.data(), mass.data());
m_freq.resize(frameCount);
for (int i = 0; i < frameCount; i++) {
m_freq[i] = sqrt(mass[i].real() * mass[i].real() + mass[i].imag() * mass[i].imag());
}
for (auto& val : m_freq) {
val = log10(val + 1);
}
qDebug() << m_freq.size() << "updated";
update();
qDebug() << sampleFormat << channelCount;
});
resize(490, 420);
}
FFTSpectrum::~FFTSpectrum()
{
}
void FFTSpectrum::setMediaPlayer(QMediaPlayer* player)
{
m_mediaPlayer = player;
emit mediaPlayerChanged();
}
void FFTSpectrum::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.fillRect(rect(), Qt::black);
if (!m_freq.empty()) {
int width = this->width();
int height = this->height();
int barWidth = std::max(1ULL, width / m_freq.size());
qDebug() << m_freq.size() << barWidth << m_freq << "freq";
for (int i = 0; i < m_freq.size(); i++) {
int barHeight = static_cast<int>(m_freq[i] * height);
painter.fillRect(i * barWidth, height - barHeight, barWidth, barHeight, Qt::green);
}
}
}

31
fftspectrum.h Normal file
View File

@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2024 Gary Wang <git@blumia.net>
//
// SPDX-License-Identifier: MIT
#pragma once
#include <QWidget>
#include <QMediaPlayer>
class QAudioBufferOutput;
class FFTSpectrum : public QWidget
{
Q_OBJECT
Q_PROPERTY(QMediaPlayer * mediaPlayer MEMBER m_mediaPlayer WRITE setMediaPlayer NOTIFY mediaPlayerChanged)
public:
explicit FFTSpectrum(QWidget* parent);
~FFTSpectrum();
void setMediaPlayer(QMediaPlayer* player);
signals:
void mediaPlayerChanged();
protected:
void paintEvent(QPaintEvent* e) override;
private:
QMediaPlayer* m_mediaPlayer = nullptr;
QAudioBufferOutput* m_audioBufferOutput = nullptr;
std::vector<float> m_freq;
};

View File

@ -6,6 +6,7 @@
#include "./ui_mainwindow.h" #include "./ui_mainwindow.h"
#include "playlistmanager.h" #include "playlistmanager.h"
#include "fftspectrum.h"
#include "lrcbar.h" #include "lrcbar.h"
// taglib // taglib
@ -17,6 +18,7 @@
#include <QMediaPlayer> #include <QMediaPlayer>
#include <QMediaMetaData> #include <QMediaMetaData>
#include <QAudioOutput> #include <QAudioOutput>
#include <QAudioBuffer>
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QFileDialog> #include <QFileDialog>
#include <QTime> #include <QTime>
@ -41,13 +43,15 @@ MainWindow::MainWindow(QWidget *parent)
, m_mediaDevices(new QMediaDevices(this)) , m_mediaDevices(new QMediaDevices(this))
, m_mediaPlayer(new QMediaPlayer(this)) , m_mediaPlayer(new QMediaPlayer(this))
, m_audioOutput(new QAudioOutput(this)) , m_audioOutput(new QAudioOutput(this))
, m_fftSpectrum(new FFTSpectrum(this))
, m_lrcbar(new LrcBar(nullptr)) , m_lrcbar(new LrcBar(nullptr))
, m_playlistManager(new PlaylistManager(this)) , m_playlistManager(new PlaylistManager(this))
{ {
ui->setupUi(this); ui->setupUi(this);
m_playlistManager->setAutoLoadFilterSuffixes({ m_playlistManager->setAutoLoadFilterSuffixes({
"*.mp3", "*.wav", "*.aiff", "*.ape", "*.flac", "*.ogg", "*.oga", "*.mpga", "*.aac" "*.mp3", "*.wav", "*.aiff", "*.ape", "*.flac", "*.ogg", "*.oga", "*.mpga", "*.aac", "*.tta"
}); });
m_fftSpectrum->setMediaPlayer(m_mediaPlayer);
m_mediaPlayer->setAudioOutput(m_audioOutput); m_mediaPlayer->setAudioOutput(m_audioOutput);
m_mediaPlayer->setLoops(QMediaPlayer::Infinite); m_mediaPlayer->setLoops(QMediaPlayer::Infinite);
ui->playlistView->setModel(m_playlistManager->model()); ui->playlistView->setModel(m_playlistManager->model());
@ -438,6 +442,7 @@ void MainWindow::initConnections()
setAudioMetadataForDisplay(metadata.stringValue(QMediaMetaData::Title), setAudioMetadataForDisplay(metadata.stringValue(QMediaMetaData::Title),
metadata.stringValue(QMediaMetaData::Author), metadata.stringValue(QMediaMetaData::Author),
metadata.stringValue(QMediaMetaData::AlbumTitle)); metadata.stringValue(QMediaMetaData::AlbumTitle));
setAudioPropertyInfoForDisplay(-1, metadata.value(QMediaMetaData::AudioBitRate).toInt() / 1000, -1, metadata.stringValue(QMediaMetaData::FileFormat));
#endif // NO_TAGLIB #endif // NO_TAGLIB
QVariant coverArt(metadata.value(QMediaMetaData::ThumbnailImage)); QVariant coverArt(metadata.value(QMediaMetaData::ThumbnailImage));
if (!coverArt.isNull()) { if (!coverArt.isNull()) {

View File

@ -17,6 +17,7 @@ class QAudioOutput;
class QPropertyAnimation; class QPropertyAnimation;
QT_END_NAMESPACE QT_END_NAMESPACE
class FFTSpectrum;
class LrcBar; class LrcBar;
class PlaylistManager; class PlaylistManager;
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
@ -92,6 +93,7 @@ private:
QMediaDevices *m_mediaDevices; QMediaDevices *m_mediaDevices;
QMediaPlayer *m_mediaPlayer; QMediaPlayer *m_mediaPlayer;
QAudioOutput *m_audioOutput; QAudioOutput *m_audioOutput;
FFTSpectrum* m_fftSpectrum;
LrcBar *m_lrcbar; LrcBar *m_lrcbar;
QPropertyAnimation *m_fadeOutAnimation; QPropertyAnimation *m_fadeOutAnimation;
PlaylistManager *m_playlistManager; PlaylistManager *m_playlistManager;