Skip to content

Commit

Permalink
Left piano take 3 (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxshao authored Jan 25, 2022
1 parent 67723d1 commit 91c870b
Show file tree
Hide file tree
Showing 16 changed files with 340 additions and 187 deletions.
2 changes: 1 addition & 1 deletion src/editor/EditState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ MouseEditState::MouseEditState()
last_pitch(EVENTDEFAULT_KEY),
start_clock(0),
current_clock(0),
kind(MouseKeyboardEdit({0, 0})),
kind(MouseKeyboardEdit({MouseMainKeyboard{0}, 0})),
selection(std::nullopt) {}

QDataStream &operator<<(QDataStream &out, const MouseEditState &a) {
Expand Down
29 changes: 26 additions & 3 deletions src/editor/EditState.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,39 @@
#include "protocol/SerializeVariant.h"
#include "pxtone/pxtnMaster.h"

struct MouseKeyboardEdit {
struct MouseLeftKeyboard {
qint32 start_vel;
};
inline QDataStream &operator<<(QDataStream &out, const MouseLeftKeyboard &a) {
out << a.start_vel;
return out;
}
inline QDataStream &operator>>(QDataStream &in, MouseLeftKeyboard &a) {
in >> a.start_vel;
return in;
}

struct MouseMainKeyboard {
qint32 start_pitch;
};
inline QDataStream &operator<<(QDataStream &out, const MouseMainKeyboard &a) {
out << a.start_pitch;
return out;
}
inline QDataStream &operator>>(QDataStream &in, MouseMainKeyboard &a) {
in >> a.start_pitch;
return in;
}
struct MouseKeyboardEdit {
std::variant<MouseLeftKeyboard, MouseMainKeyboard> kind;
qint32 current_pitch;
};
inline QDataStream &operator<<(QDataStream &out, const MouseKeyboardEdit &a) {
out << a.start_pitch << a.current_pitch;
out << a.kind << a.current_pitch;
return out;
}
inline QDataStream &operator>>(QDataStream &in, MouseKeyboardEdit &a) {
in >> a.start_pitch >> a.current_pitch;
in >> a.kind >> a.current_pitch;
return in;
}

Expand Down
9 changes: 7 additions & 2 deletions src/editor/EditorScrollArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ EditorScrollArea::EditorScrollArea(QWidget *parent, bool match_scale)
mouseDown(false),
m_match_scale(match_scale),
lastPos(),
anim(new Animation(this)) {
anim(new Animation(this)),
m_scroll_with_mouse_x(true) {
setWidgetResizable(true);
setMouseTracking(true);
setFrameStyle(QFrame::NoFrame);
Expand All @@ -40,7 +41,7 @@ EditorScrollArea::EditorScrollArea(QWidget *parent, bool match_scale)
// scrolling.
if (!(QApplication::mouseButtons() & (Qt::LeftButton | Qt::RightButton)))
mouseDown = false;
if (mouseDown) scrollWithMouseX();
if (mouseDown && m_scroll_with_mouse_x) scrollWithMouseX();
});
}

Expand Down Expand Up @@ -195,6 +196,10 @@ void EditorScrollArea::ensureWithinMargin(int x, qreal minDistFromLeft,
}
}

void EditorScrollArea::setEnableScrollWithMouseX(bool b) {
m_scroll_with_mouse_x = b;
}

constexpr int JUMP_MAX = 10;
constexpr int MARGIN_MAX = 100;
constexpr double MARGIN_FRAC = 0.25;
Expand Down
2 changes: 2 additions & 0 deletions src/editor/EditorScrollArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EditorScrollArea : public QScrollArea {
void ensureWithinMargin(int x, qreal minDistFromLeft,
qreal jumpMinDistFromLeft, qreal jumpMaxDistFromLeft,
qreal maxDistFromLeft);
void setEnableScrollWithMouseX(bool);
signals:
void viewportChanged(const QRect &viewport);

Expand All @@ -27,6 +28,7 @@ class EditorScrollArea : public QScrollArea {
bool m_match_scale;
QPoint lastPos;
Animation *anim;
bool m_scroll_with_mouse_x;
bool event(QEvent *e) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
Expand Down
2 changes: 2 additions & 0 deletions src/editor/EditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ EditorWindow::EditorWindow(QWidget *parent)
m_scroll_area->setWidget(m_keyboard_view);

// TODO: find a better place for this.
connect(m_keyboard_view, &KeyboardView::setScrollOnClick, m_scroll_area,
&EditorScrollArea::setEnableScrollWithMouseX);
connect(m_keyboard_view, &KeyboardView::ensureVisibleX,
[this](int x, bool strict) {
if (!strict)
Expand Down
2 changes: 2 additions & 0 deletions src/editor/StyleEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ void loadConfig(const QString &path, Config &c) {
setConfigColor(styleConfig, c.color.KeyboardBlackNote, "keyboard/BlackNote");
setConfigColor(styleConfig, c.color.KeyboardWhiteLeft, "keyboard/WhiteLeft");
setConfigColor(styleConfig, c.color.KeyboardBlackLeft, "keyboard/BlackLeft");
setConfigColor(styleConfig, c.color.KeyboardBlackLeftInner,
"keyboard/BlackLeftInner");
setConfigColor(styleConfig, c.color.KeyboardBlack, "keyboard/Black");
setConfigColor(styleConfig, c.color.KeyboardMeasure, "keyboard/Measure");

Expand Down
1 change: 1 addition & 0 deletions src/editor/StyleEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct Config {
QColor KeyboardBlackNote;
QColor KeyboardWhiteLeft;
QColor KeyboardBlackLeft;
QColor KeyboardBlackLeftInner;
QColor KeyboardBlack;
QColor KeyboardMeasure;

Expand Down
22 changes: 13 additions & 9 deletions src/editor/audio/NotePreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,7 @@ NotePreview::NotePreview(const pxtnService *pxtn, const mooParams *moo_params,

// We don't constantly reset because sometimes the audio engine forces
// [life_count = 0] (say at the end of the sample)
if (m_unit != nullptr) {
std::shared_ptr<const pxtnWoice> woice = m_this_unit->get_woice();
for (int i = 0; i < woice->get_voice_num(); ++i) {
// TODO: calculating the life count should be more automatic.
auto tone = m_this_unit->get_tone(i);
tone->on_count = duration;
tone->life_count = duration + woice->get_instance(i)->env_release;
}
}
if (m_unit != nullptr) resetOn(duration);
if (m_moo_state != nullptr) {
for (auto &unit : m_moo_state->units) {
pxtnUnitTone *u = &unit;
Expand Down Expand Up @@ -109,6 +101,18 @@ void NotePreview::processEvent(EVENTKIND kind, int32_t value) {
m_moo_params->processNonOnEvent(m_this_unit, kind, value, m_pxtn);
}

void NotePreview::resetOn(int duration) {
std::shared_ptr<const pxtnWoice> woice = m_this_unit->get_woice();
for (int i = 0; i < woice->get_voice_num(); ++i) {
// TODO: calculating the life count should be more automatic.
auto tone = m_this_unit->get_tone(i);
*tone = pxtnVOICETONE(tone->env_release_clock, tone->offset_freq,
woice->get_instance(i)->env_size != 0);
tone->on_count = duration;
tone->life_count = duration + woice->get_instance(i)->env_release;
}
}

static EVERECORD ev(int32_t clock, EVENTKIND kind, int32_t value) {
EVERECORD e;
e.clock = clock;
Expand Down
1 change: 1 addition & 0 deletions src/editor/audio/NotePreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class NotePreview : public QObject {
int vel, int duration, std::shared_ptr<const pxtnWoice> woice,
int bufferSize, QObject *parent);
void processEvent(EVENTKIND kind, int32_t value);
void resetOn(int duration);
~NotePreview();

private:
Expand Down
Loading

0 comments on commit 91c870b

Please sign in to comment.