From 84bd1150e93658aea36db19cebdb6012f2f40a60 Mon Sep 17 00:00:00 2001 From: deva Date: Sun, 3 Jan 2010 14:00:36 +0000 Subject: Some work on sample sorter --- dgedit/audioextractor.cc | 86 +++++++++++++++++++++++++++++++++++++++++++---- dgedit/audioextractor.h | 3 +- dgedit/icons/file.png | Bin 173 -> 183 bytes dgedit/icons/master.png | Bin 325 -> 284 bytes dgedit/mainwindow.cc | 2 +- dgedit/samplesorter.cc | 22 ++++++++++++ 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 +#include + #include 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 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::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::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::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 #include #include +#include #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 levels); void setExportPath(const QString &path); void setOutputPrefix(const QString &prefix); diff --git a/dgedit/icons/file.png b/dgedit/icons/file.png index fc8fc40..7b95ebf 100644 Binary files a/dgedit/icons/file.png and b/dgedit/icons/file.png differ diff --git a/dgedit/icons/master.png b/dgedit/icons/master.png index 27fe9c4..fe44ada 100644 Binary files a/dgedit/icons/master.png and b/dgedit/icons/master.png differ 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 SampleSorter::levels() +{ + QVector lvls; + int idx = 0; + float next = min; + + QMap::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 levels(); public slots: void setSelections(Selections selections); -- cgit v1.2.3