diff options
author | deva <deva> | 2010-01-03 14:00:36 +0000 |
---|---|---|
committer | deva <deva> | 2010-01-03 14:00:36 +0000 |
commit | 84bd1150e93658aea36db19cebdb6012f2f40a60 (patch) | |
tree | ec9db2a6b9d17243c4a3c736aeedacd831910ac3 | |
parent | 061e35d178350808bc4c2369c6d3c92fcb3b69d2 (diff) |
Some work on sample sorter
-rw-r--r-- | dgedit/audioextractor.cc | 86 | ||||
-rw-r--r-- | dgedit/audioextractor.h | 3 | ||||
-rw-r--r-- | dgedit/icons/file.png | bin | 173 -> 183 bytes | |||
-rw-r--r-- | dgedit/icons/master.png | bin | 325 -> 284 bytes | |||
-rw-r--r-- | dgedit/mainwindow.cc | 2 | ||||
-rw-r--r-- | dgedit/samplesorter.cc | 22 | ||||
-rw-r--r-- | dgedit/samplesorter.h | 1 |
7 files changed, 105 insertions, 9 deletions
diff --git a/dgedit/audioextractor.cc b/dgedit/audioextractor.cc index 92bd105..63273d1 100644 --- a/dgedit/audioextractor.cc +++ b/dgedit/audioextractor.cc @@ -26,6 +26,9 @@ */ #include "audioextractor.h" +#include <QDomDocument> +#include <QFile> + #include <sndfile.h> AudioExtractor::AudioExtractor(QObject *parent) @@ -56,15 +59,13 @@ float *AudioExtractor::load(QString file, size_t *size) return data; } -void AudioExtractor::exportSelection(QString name, +void AudioExtractor::exportSelection(QString filename, int index, float *data, size_t size, Selection sel) { - QString file = exportpath + "/" + prefix + "-" + name + "-" + QString::number(index) + ".wav"; - printf("Writing: %s (sz: %d, from %d to %d)\n", - file.toStdString().c_str(), size, sel.from, sel.to); + filename.toStdString().c_str(), size, sel.from, sel.to); if(sel.from > (int)size || sel.to > (int)size || sel.to < 0 || sel.from < 0 || sel.to < sel.from) { printf("Out of bounds\n"); @@ -88,7 +89,7 @@ void AudioExtractor::exportSelection(QString name, sf_info.samplerate = 44100; sf_info.channels = 1; - SNDFILE *fh = sf_open(file.toStdString().c_str(), SFM_WRITE, &sf_info); + SNDFILE *fh = sf_open(filename.toStdString().c_str(), SFM_WRITE, &sf_info); if(!fh) { printf("Open for write error...\n"); return; @@ -97,21 +98,29 @@ void AudioExtractor::exportSelection(QString name, sf_close(fh); } -void AudioExtractor::exportSelections(Selections selections) +void AudioExtractor::exportSelections(Selections selections, QVector<int> levels) { + // Do the actual exporting one file at the time. AudioFileList::iterator j = audiofiles.begin(); while(j != audiofiles.end()) { QString file = j->first; QString name = j->second; size_t size; + + // TODO: Use sf_seek instead... float *data = load(file, &size); if(!data) continue; int index = 0; QMap<int, Selection>::iterator i = selections.begin(); while(i != selections.end()) { - exportSelection(name, ++index, data, size, i.value()); + index++; + + QString file = exportpath + "/" + prefix + "/samples/" + + prefix + "-" + name + "-" + QString::number(index) + ".wav"; + + exportSelection(file, index, data, size, i.value()); i++; } @@ -119,6 +128,69 @@ void AudioExtractor::exportSelections(Selections selections) j++; } + + QDomDocument doc; + QDomProcessingInstruction header = + doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); + doc.appendChild(header); + + QDomElement instrument = doc.createElement("instrument"); + instrument.setAttribute("name", prefix); + doc.appendChild(instrument); + + QDomElement samples = doc.createElement("samples"); + instrument.appendChild(samples); + + // Do the adding to the xml file one sample at the time. + int index = 0; + QMap<int, Selection>::iterator i = selections.begin(); + while(i != selections.end()) { + index++; + + QDomElement sample = doc.createElement("sample"); + sample.setAttribute("name", prefix + "-" + QString::number(index)); + samples.appendChild(sample); + + AudioFileList::iterator j = audiofiles.begin(); + while(j != audiofiles.end()) { + + QString file = j->first; + QString name = j->second; + + QDomElement audiofile = doc.createElement("audiofile"); + audiofile.setAttribute("file", "samples/" + prefix + "-" + name + "-" + + QString::number(index) + ".wav"); + audiofile.setAttribute("channel", name); + sample.appendChild(audiofile); + + j++; + } + + i++; + } + + QDomElement velocities = doc.createElement("velocities"); + instrument.appendChild(velocities); + + QVector<int>::iterator k = levels.begin(); + while(k != levels.end()) { + QDomElement velocity = doc.createElement("velocity"); + velocity.setAttribute("lower", "0"); + velocity.setAttribute("upper", "127"); + velocities.appendChild(velocity); + + QDomElement sampleref = doc.createElement("sampleref"); + sampleref.setAttribute("name", "bleh"); + sampleref.setAttribute("probability", "0.1"); + velocity.appendChild(sampleref); + + k++; + } + + QFile xmlfile(exportpath + "/" + prefix + "/" + prefix + ".xml"); + xmlfile.open(QIODevice::WriteOnly); + xmlfile.write(doc.toByteArray()); + xmlfile.close(); } void AudioExtractor::addFile(QString file, QString name) diff --git a/dgedit/audioextractor.h b/dgedit/audioextractor.h index 39f0353..2f4ce98 100644 --- a/dgedit/audioextractor.h +++ b/dgedit/audioextractor.h @@ -31,6 +31,7 @@ #include <QSet> #include <QLinkedList> #include <QString> +#include <QVector> #include "selection.h" @@ -45,7 +46,7 @@ public slots: void addFile(QString file, QString name); void changeName(QString file, QString name); void removeFile(QString file, QString name); - void exportSelections(Selections selections); + void exportSelections(Selections selections, QVector<int> levels); void setExportPath(const QString &path); void setOutputPrefix(const QString &prefix); diff --git a/dgedit/icons/file.png b/dgedit/icons/file.png Binary files differindex fc8fc40..7b95ebf 100644 --- a/dgedit/icons/file.png +++ b/dgedit/icons/file.png diff --git a/dgedit/icons/master.png b/dgedit/icons/master.png Binary files differindex 27fe9c4..fe44ada 100644 --- a/dgedit/icons/master.png +++ b/dgedit/icons/master.png diff --git a/dgedit/mainwindow.cc b/dgedit/mainwindow.cc index cabe0d6..f9b0147 100644 --- a/dgedit/mainwindow.cc +++ b/dgedit/mainwindow.cc @@ -217,7 +217,7 @@ void MainWindow::setYOffset(int of) void MainWindow::doExport() { - extractor->exportSelections(sorter->selections()); + extractor->exportSelections(sorter->selections(), sorter->levels()); } void MainWindow::loadFile(QString filename) diff --git a/dgedit/samplesorter.cc b/dgedit/samplesorter.cc index 7be869b..e93754c 100644 --- a/dgedit/samplesorter.cc +++ b/dgedit/samplesorter.cc @@ -34,6 +34,8 @@ #define MAXFLOAT (3.40282347e+38F) #endif +#define NUM_LEVELS 6 + SampleSorter::SampleSorter() { data = NULL; @@ -79,6 +81,26 @@ Selections SampleSorter::selections() return s; } +QVector<int> SampleSorter::levels() +{ + QVector<int> lvls; + int idx = 0; + float next = min; + + QMap<float, Selection>::iterator i = sorted.begin(); + while(i != sorted.end()) { + if(i.key() >= next) { + lvls.push_back(idx); + next += (max - min) / NUM_LEVELS; + } + i++; + idx++; + } + + return lvls; +} + + void SampleSorter::resort() { sorted.clear(); diff --git a/dgedit/samplesorter.h b/dgedit/samplesorter.h index c010f43..a7c356d 100644 --- a/dgedit/samplesorter.h +++ b/dgedit/samplesorter.h @@ -36,6 +36,7 @@ public: SampleSorter(); Selections selections(); + QVector<int> levels(); public slots: void setSelections(Selections selections); |