Skip to content

Commit

Permalink
Feat add keyboard event
Browse files Browse the repository at this point in the history
  • Loading branch information
Greedysky committed Sep 6, 2022
1 parent 0a2541f commit e748c51
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 7 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.user
*.user.*
*.aps
build-*/
*.*-pre1
*.autosave
*.*~
*.uxf
*.bak
Thumbs.db
ehthumbs.db
Desktop.ini
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# qt-spek
# Qt-spek
基于Qt的频谱分析器

修改于Spek[官网地址](http://spek.cc), [Github仓库地址](https://github.com/alexkay/spek)

![0](https://github.com/Greedysky/qt-spek/blob/master/demo.jpg?raw=true)
![1](https://github.com/Greedysky/qt-spek/blob/master/demo2.jpg?raw=true)
![1](https://github.com/Greedysky/qt-spek/blob/master/demo2.jpg?raw=true)

## Spectrogram

`c`, `C`
: Change the audio channel.

`f`, `F`
: Change the DFT window function.

`l`, `L`
: Change the lower limit of the dynamic range in dBFS.

`p`, `P`
: Change the palette.

`s`, `S`
: Change the audio stream.

`u`, `U`
: Change the upper limit of the dynamic range in dBFS.

`w`, `W`
: Change the DFT window size.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main(int argc, char *argv[])
SpekSpectrogram w;
w.show();

w.open("F:\\KuGou\\J.Fla - Shape Of You.mp3");
w.open("test.mp3");

return a.exec();
}
10 changes: 6 additions & 4 deletions qt-spek.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ TEMPLATE = app
QMAKE_CXXFLAGS += -std=c++11

#change to your libav or ffmpeg lib
INCLUDEPATH += $$PWD/libav/include
LIBS += -L"$$PWD/libav/lib" -lavcodec -lavformat -lavutil
INCLUDEPATH += $$PWD/libffmpeg/include
LIBS += -L"$$PWD/libffmpeg/lib" -lavcodec -lavformat -lavutil -lswresample

SOURCES += main.cpp\
spek-ruler.cc \
spek-spectrogram.cc \
spek-audio.cc \
spek-fft.cc \
spek-palette.cc \
spek-pipeline.cc
spek-pipeline.cc \
spek-utils.cc

HEADERS += \
spek-audio.h \
spek-fft.h \
spek-palette.h \
spek-pipeline.h \
spek-ruler.h \
spek-spectrogram.h
spek-spectrogram.h \
spek-utils.h
1 change: 1 addition & 0 deletions spek-palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef enum palette {
PALETTE_SPECTROGRAM,
PALETTE_SOX,
PALETTE_MONO,
PALETTE_COUNT,
PALETTE_DEFAULT = PALETTE_SPECTROGRAM,
}Palette;

Expand Down
70 changes: 70 additions & 0 deletions spek-spectrogram.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "spek-audio.h"
#include "spek-fft.h"
#include "spek-ruler.h"
#include "spek-utils.h"
#include "spek-spectrogram.h"

#include <cmath>
#include <QPainter>
#include <QDateTime>
#include <QKeyEvent>
#include <QApplication>

enum
Expand Down Expand Up @@ -63,6 +65,74 @@ void SpekSpectrogram::open(const QString &path)
start();
}

void SpekSpectrogram::keyPressEvent(QKeyEvent *event)
{
switch(event->key()) {
case Qt::Key_C:
if (this->channels) {
if(event->modifiers() == Qt::NoModifier) { // 'c'
this->channel = (this->channel + 1) % this->channels;
} else if(event->modifiers() == Qt::ShiftModifier) { // 'C'
this->channel = (this->channel - 1 + this->channels) % this->channels;
}
}
break;
case Qt::Key_F:
if(event->modifiers() == Qt::NoModifier) { // 'f'
this->window_function = (enum window_function) ((this->window_function + 1) % WINDOW_COUNT);
} else if(event->modifiers() == Qt::ShiftModifier) { // 'F'
this->window_function = (enum window_function) ((this->window_function - 1 + WINDOW_COUNT) % WINDOW_COUNT);
}
break;
case Qt::Key_L:
if(event->modifiers() == Qt::NoModifier) { // 'l'
this->lrange = spek_min(this->lrange + 1, this->urange - 1);
} else if(event->modifiers() == Qt::ShiftModifier) { // 'L'
this->lrange = spek_max(this->lrange - 1, MIN_RANGE);
}
break;
case Qt::Key_P:
if(event->modifiers() == Qt::NoModifier) { // 'p'
this->palette = (enum palette) ((this->palette + 1) % PALETTE_COUNT);
this->create_palette();
} else if(event->modifiers() == Qt::ShiftModifier) { // 'P'
this->palette = (enum palette) ((this->palette - 1 + PALETTE_COUNT) % PALETTE_COUNT);
this->create_palette();
}
break;
case Qt::Key_S:
if (this->streams) {
if(event->modifiers() == Qt::NoModifier) { // 's'
this->stream = (this->stream + 1) % this->streams;
} else if(event->modifiers() == Qt::ShiftModifier) { // 'S'
this->stream = (this->stream - 1 + this->streams) % this->streams;
}
}
break;
case Qt::Key_U:
if(event->modifiers() == Qt::NoModifier) { // 'u'
this->urange = spek_min(this->urange + 1, MAX_RANGE);
} else if(event->modifiers() == Qt::ShiftModifier) { // 'U'
this->urange = spek_max(this->urange - 1, this->lrange + 1);
}
break;
case Qt::Key_W:
if(event->modifiers() == Qt::NoModifier) { // 'w'
this->fft_bits = spek_min(this->fft_bits + 1, MAX_FFT_BITS);
this->create_palette();
} else if(event->modifiers() == Qt::ShiftModifier) { // 'W'
this->fft_bits = spek_max(this->fft_bits - 1, MIN_FFT_BITS);
this->create_palette();
}
break;
default:
event->ignore();
return;
}

start();
}

void SpekSpectrogram::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
Expand Down
1 change: 1 addition & 0 deletions spek-spectrogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SpekSpectrogram : public QWidget
Palette getPalette() const { return palette; }

private:
virtual void keyPressEvent(QKeyEvent *event) override;
virtual void paintEvent(QPaintEvent *event) override;
virtual void resizeEvent(QResizeEvent *event) override;

Expand Down
46 changes: 46 additions & 0 deletions spek-utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <assert.h>
#include <stdlib.h>

#include "spek-utils.h"

int spek_vercmp(const char *a, const char *b)
{
assert(a && b);

if (!*a && !*b) {
return 0;
}
if (!*a) {
return -1;
}
if (!*b) {
return 1;
}

char *i, *j;
while(1) {
i = j = NULL;
long x = strtol(a, &i, 10);
long y = strtol(b, &j, 10);

if (x < y) {
return -1;
}
if (x > y) {
return 1;
}

if (!*i && !*j) {
return 0;
}
if (!*i) {
return -1;
}
if (!*j) {
return 1;
}

a = i + 1;
b = j + 1;
}
}
14 changes: 14 additions & 0 deletions spek-utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

inline int spek_max(int a, int b)
{
return a > b ? a : b;
}

inline int spek_min(int a, int b)
{
return a < b ? a : b;
}

// Compare version numbers, e.g. 1.9.2 < 1.10.0
int spek_vercmp(const char *a, const char *b);

0 comments on commit e748c51

Please sign in to comment.