From 68f1b54aace152707f2bb51e07f76ebd879becb1 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 30 Nov 2009 13:35:52 +0000 Subject: Samples sorter added. --- dgedit/Makefile.am | 6 +- dgedit/canvas.cc | 7 +++ dgedit/canvas.h | 6 +- dgedit/mainwindow.cc | 57 ++++++++++++++---- dgedit/mainwindow.h | 8 +++ dgedit/samplesorter.cc | 154 +++++++++++++++++++++++++++++++++++++++++++++++++ dgedit/samplesorter.h | 65 +++++++++++++++++++++ 7 files changed, 290 insertions(+), 13 deletions(-) create mode 100644 dgedit/samplesorter.cc create mode 100644 dgedit/samplesorter.h (limited to 'dgedit') diff --git a/dgedit/Makefile.am b/dgedit/Makefile.am index 5182fd5..a16a0e9 100644 --- a/dgedit/Makefile.am +++ b/dgedit/Makefile.am @@ -9,12 +9,14 @@ dgedit_SOURCES = $(shell ../tools/MocList cc ) \ dgedit.cc \ audioextractor.cc \ canvas.cc \ - mainwindow.cc + mainwindow.cc \ + samplesorter.cc EXTRA_DIST = \ audioextractor.h \ canvas.h \ - mainwindow.h + mainwindow.h \ + samplesorter.h dgedit_MOC = $(shell ../tools/MocList cc ) diff --git a/dgedit/canvas.cc b/dgedit/canvas.cc index 23cd96e..36900a3 100644 --- a/dgedit/canvas.cc +++ b/dgedit/canvas.cc @@ -181,6 +181,7 @@ void Canvas::mouseMoveEvent(QMouseEvent *event) if(val > active_selection->to) val = active_selection->to - 1; active_selection->from = val; update(); + emit selectionsChanged(_selections); return; } @@ -189,6 +190,7 @@ void Canvas::mouseMoveEvent(QMouseEvent *event) if(val < active_selection->from) val = active_selection->from + 1; active_selection->to = val; update(); + emit selectionsChanged(_selections); return; } @@ -230,12 +232,14 @@ void Canvas::mousePressEvent(QMouseEvent *event) if(abs(event->x() - mapX(i.value().from)) < 2) { active_selection = &i.value(); selection_is_moving_left = true; + emit activeSelectionChanged(i.value()); return; } if(abs(event->x() - mapX(i.value().to)) < 2) { active_selection = &i.value(); selection_is_moving_right = true; + emit activeSelectionChanged(i.value()); return; } @@ -249,6 +253,7 @@ void Canvas::mousePressEvent(QMouseEvent *event) event->x() < mapX(i.value().to)) { active_selection = &i.value(); update(); + emit activeSelectionChanged(i.value()); return; } @@ -494,6 +499,7 @@ void Canvas::autoCreateSelections() } } update(); + emit selectionsChanged(_selections); } void Canvas::clearSelections() @@ -503,6 +509,7 @@ void Canvas::clearSelections() selection_is_moving_right = false; setCursor(Qt::ArrowCursor); update(); + emit selectionsChanged(_selections); } Selections Canvas::selections() diff --git a/dgedit/canvas.h b/dgedit/canvas.h index 274e222..2a06d03 100644 --- a/dgedit/canvas.h +++ b/dgedit/canvas.h @@ -44,6 +44,8 @@ public: Selections selections(); signals: + void selectionsChanged(Selections selections); + void activeSelectionChanged(Selection selection); public slots: void setXScale(float scale); @@ -71,9 +73,11 @@ private: QImage wav; +public: float *data; - QMap mipmaps; size_t size; +private: + QMap mipmaps; float xscale; float yscale; diff --git a/dgedit/mainwindow.cc b/dgedit/mainwindow.cc index a15bf73..7af947a 100644 --- a/dgedit/mainwindow.cc +++ b/dgedit/mainwindow.cc @@ -36,6 +36,8 @@ #include #include #include +#include + #define MAXVAL 10000000L #define SINGLESTEP MAXVAL/100000 @@ -51,7 +53,6 @@ MainWindow::MainWindow() extractor = new AudioExtractor(this); canvas = new Canvas(this); - lh->addWidget(canvas); QWidget *dock = new QWidget(); yoffset = new QScrollBar(Qt::Vertical); @@ -78,6 +79,19 @@ MainWindow::MainWindow() xoffset->setSingleStep(SINGLESTEP); connect(xoffset, SIGNAL(valueChanged(int)), this, SLOT(setXOffset(int))); + sorter = new SampleSorter(); + connect(canvas, SIGNAL(selectionsChanged(Selections)), sorter, SLOT(setSelections(Selections))); + connect(canvas, SIGNAL(activeSelectionChanged(Selection)), sorter, SLOT(setActiveSelection(Selection))); + + lh->addWidget(canvas); + lh->addWidget(yscale); + lh->addWidget(yoffset); + lv->addLayout(lh, 100); + lv->addWidget(xscale, 100); + lv->addWidget(xoffset, 100); + lv->addWidget(sorter, 15); + + QHBoxLayout *btns = new QHBoxLayout(); QPushButton *autosel = new QPushButton(); @@ -104,6 +118,8 @@ MainWindow::MainWindow() QVBoxLayout *configs = new QVBoxLayout(); + configs->addLayout(btns); + configs->addWidget(new QLabel("Prefix:")); QLineEdit *prefix = new QLineEdit(); connect(prefix, SIGNAL(textChanged(const QString &)), @@ -126,13 +142,6 @@ MainWindow::MainWindow() addFile("/home/deva/aasimonster/tmp/china/OH R-20.wav", "oh-r"); configs->addWidget(filelist); - lh->addWidget(yscale); - lh->addWidget(yoffset); - lv->addLayout(lh); - lv->addWidget(xscale); - lv->addWidget(xoffset); - lv->addLayout(btns); - QDockWidget *dockWidget = new QDockWidget(tr("Dock Widget"), this); dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); dockWidget->setWidget(dock); @@ -144,10 +153,36 @@ MainWindow::MainWindow() xscale->setValue(0); xoffset->setValue(0); - resize(800, 600); + loadSettings(); statusBar()->showMessage("Ready"); } +void MainWindow::closeEvent(QCloseEvent *) +{ + saveSettings(); + QApplication::quit(); +} + +void MainWindow::loadSettings() +{ + QSettings settings("Aasimon.org", "DGEdit"); + + settings.beginGroup("MainWindow"); + resize(settings.value("size", QSize(700, 800)).toSize()); + move(settings.value("pos", QPoint(0, 0)).toPoint()); + settings.endGroup(); +} + +void MainWindow::saveSettings() +{ + QSettings settings("Aasimon.org", "DGEdit"); + + settings.beginGroup("MainWindow"); + settings.setValue("size", size()); + settings.setValue("pos", pos()); + settings.endGroup(); +} + void MainWindow::setXScale(int sz) { // range 0.0 - 1.0 @@ -181,7 +216,7 @@ void MainWindow::setYOffset(int of) void MainWindow::doExport() { - extractor->exportSelections(canvas->selections()); + extractor->exportSelections(sorter->selections()); } void MainWindow::loadFile() @@ -191,7 +226,9 @@ void MainWindow::loadFile() "", tr("Audio Files (*.wav)")); statusBar()->showMessage("Loading..."); qApp->processEvents(); + sorter->setWavData(NULL, 0); canvas->load(filename); + sorter->setWavData(canvas->data, canvas->size); statusBar()->showMessage("Ready"); } diff --git a/dgedit/mainwindow.h b/dgedit/mainwindow.h index 246a40f..42a9f18 100644 --- a/dgedit/mainwindow.h +++ b/dgedit/mainwindow.h @@ -33,6 +33,7 @@ #include "canvas.h" #include "audioextractor.h" +#include "samplesorter.h" class MainWindow : public QMainWindow { Q_OBJECT @@ -49,7 +50,14 @@ public slots: void doExport(); void loadFile(); +protected: + void closeEvent(QCloseEvent*); + private: + void loadSettings(); + void saveSettings(); + + SampleSorter *sorter; Canvas *canvas; AudioExtractor *extractor; QScrollBar *yoffset; diff --git a/dgedit/samplesorter.cc b/dgedit/samplesorter.cc new file mode 100644 index 0000000..7be869b --- /dev/null +++ b/dgedit/samplesorter.cc @@ -0,0 +1,154 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * samplesorter.cc + * + * Mon Nov 30 07:45:58 CET 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "samplesorter.h" + +#include +#include + +#include +#ifndef MAXFLOAT +#define MAXFLOAT (3.40282347e+38F) +#endif + +SampleSorter::SampleSorter() +{ + data = NULL; + size = 0; + attlen = 666; // Magical constants needs biblical proportions... +} + +void SampleSorter::setWavData(const float *data, size_t size) +{ + this->data = data; + this->size = size; + resort(); +} + +void SampleSorter::setSelections(Selections s) +{ + _selections = s; + resort(); +} + +int SampleSorter::attackLength() +{ + return attlen; +} + +void SampleSorter::setAttackLength(int len) +{ + attlen = len; + resort(); +} + +Selections SampleSorter::selections() +{ + Selections s; + int j = 0; + + QMap::iterator i = sorted.begin(); + while(i != sorted.end()) { + s[j++] = i.value(); + i++; + } + + return s; +} + +void SampleSorter::resort() +{ + sorted.clear(); + + min = MAXFLOAT; + max = 0.0; + + QMap::iterator i = _selections.begin(); + while(i != _selections.end()) { + float energy = 0.0; + Selection s = i.value(); + + for(size_t idx = s.from; + (idx < (size_t)s.from + (size_t)attackLength()) && (idx < (size_t)s.to) && (idx < size); + idx++) { + energy += data[idx] * data[idx]; + } + + while(sorted.find(energy) != sorted.end()) { + energy += 1; // Make sure that the key is unique. + } + + sorted[energy] = i.value(); + + if(energy < min) min = energy; + if(energy > max) max = energy; + + i++; + } + + update(); +} + +void SampleSorter::setActiveSelection(Selection s) +{ + sel = s; + update(); +} + +#define MAP(p) (height()-(int)(p*((float)height()/(float)width()))) + +static void drawCircle(QPainter &p, int x, int y) +{ + p.drawEllipse(x-2, y-2, 4, 4); +} + +void SampleSorter::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + + QColor colBg = QColor(180, 200, 180); + QColor colFg = QColor(160, 180, 160); + QColor colPt = QColor(255, 100, 100); + QColor colPtSel = QColor(255, 255, 100); + + painter.setPen(colBg); + painter.setBrush(colBg); + painter.drawRect(event->rect()); + + painter.setPen(colFg); + painter.drawLine(0,height(),width(),0); + + if(sorted.isEmpty()) return; + + QMap::iterator i = sorted.begin(); + while(i != sorted.end()) { + if(sel.to == i.value().to && sel.from == i.value().from) painter.setPen(colPtSel); + else painter.setPen(colPt); + float x = (i.key()/max)*(float)width(); + drawCircle(painter, x, MAP(x)); + i++; + } +} diff --git a/dgedit/samplesorter.h b/dgedit/samplesorter.h new file mode 100644 index 0000000..c010f43 --- /dev/null +++ b/dgedit/samplesorter.h @@ -0,0 +1,65 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * samplesorter.h + * + * Mon Nov 30 07:45:58 CET 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __DRUMGIZMO_SAMPLESORTER_H__ +#define __DRUMGIZMO_SAMPLESORTER_H__ + +#include +#include "selection.h" + +class SampleSorter : public QWidget { +Q_OBJECT +public: + SampleSorter(); + + Selections selections(); + +public slots: + void setSelections(Selections selections); + void setWavData(const float *data, size_t size); + void resort(); + void setAttackLength(int len); + int attackLength(); + void setActiveSelection(Selection s); + +protected: + void paintEvent(QPaintEvent *event); + +private: + Selections _selections; + QMap sorted; + float min; + float max; + int attlen; + + // Wav data + const float *data; + size_t size; + + Selection sel; +}; + +#endif/*__DRUMGIZMO_SAMPLESORTER_H__*/ -- cgit v1.2.3