aboutsummaryrefslogtreecommitdiffstats
path: root/ofxMaxim
diff options
context:
space:
mode:
authorChris Kiefer <chrisk13fer@gmail.com>2011-11-08 17:55:22 +0000
committerChris Kiefer <chrisk13fer@gmail.com>2011-11-08 17:55:22 +0000
commit385d5f3c0dab326a58a0b2d038482b0cc64c09d5 (patch)
treec9fc89ebdb85877b219dbd67f6c4236019616939 /ofxMaxim
parentd09cc45d859aabbf76306fee49c1cec202c4f808 (diff)
Load MPTK book and play back some atoms
Diffstat (limited to 'ofxMaxim')
-rw-r--r--ofxMaxim/ofxMaxim/libs/maxiAtoms.cpp79
-rw-r--r--ofxMaxim/ofxMaxim/libs/maxiAtoms.h43
2 files changed, 113 insertions, 9 deletions
diff --git a/ofxMaxim/ofxMaxim/libs/maxiAtoms.cpp b/ofxMaxim/ofxMaxim/libs/maxiAtoms.cpp
index 0dff9ec..7cb5a6c 100644
--- a/ofxMaxim/ofxMaxim/libs/maxiAtoms.cpp
+++ b/ofxMaxim/ofxMaxim/libs/maxiAtoms.cpp
@@ -9,8 +9,10 @@
#include "maxiAtoms.h"
#include <iostream>
+#include <fstream>
+#include "tinyxml.h"
-void maxiAtoms::createGabor(flArr &atom, const float freq, const float sampleRate, const uint length,
+void maxiCollider::createGabor(flArr &atom, const float freq, const float sampleRate, const uint length,
const float startPhase, const float kurtotis, const float amp) {
atom.resize(length);
float inc = 1.0 / length * 2.0;
@@ -36,11 +38,11 @@ void maxiAtoms::createGabor(flArr &atom, const float freq, const float sampleRat
-maxiAtomStream::maxiAtomStream() {
+maxiAccelerator::maxiAccelerator() {
sampleIdx = 0;
}
-void maxiAtomStream::addAtom(flArr &atom) {
+void maxiAccelerator::addAtom(flArr &atom) {
queuedAtom quAtom;
quAtom.atom.copyFromArray(atom);
quAtom.startTime = sampleIdx;
@@ -48,7 +50,7 @@ void maxiAtomStream::addAtom(flArr &atom) {
atomQueue.push_back(quAtom);
}
-void maxiAtomStream::fillNextBuffer(float *buffer, unsigned int bufferLength) {
+void maxiAccelerator::fillNextBuffer(float *buffer, unsigned int bufferLength) {
queuedAtomList::iterator it = atomQueue.begin();
while(it != atomQueue.end()) {
int atomStart = (*it).startTime + (*it).pos;
@@ -69,4 +71,71 @@ void maxiAtomStream::fillNextBuffer(float *buffer, unsigned int bufferLength) {
}
sampleIdx += bufferLength;
-} \ No newline at end of file
+}
+
+bool maxiAtomBook::loadMPTKXmlBook(string filename, maxiAtomBook &book) {
+ bool ok;
+ ifstream f;
+ f.open(filename.c_str());
+ if (f.fail()) {
+ cout << "I couldn't open " << filename << endl;
+ ok = false;
+ }else {
+ cout << "Reading " << filename << endl;
+ const int lineBufferSize = 1024;
+ char lineBuffer[lineBufferSize];
+ string line("");
+ //get dictionary definition xml
+ while("</dict>" != line) {
+ f.getline(lineBuffer, lineBufferSize);
+ line = lineBuffer;
+ cout << line << endl;
+ }
+ //this should be "txt"
+ f.getline(lineBuffer, lineBufferSize);
+ //get rest of data into one string
+ string xmlData;
+ while(!f.eof()) {
+ f.getline(lineBuffer, lineBufferSize);
+ xmlData.append(lineBuffer);
+ }
+ TiXmlDocument doc;
+ doc.Parse(xmlData.c_str());
+ cout << "Parsed xml, processing atoms...\n";
+ TiXmlHandle docHandle = TiXmlHandle(&doc);
+
+ TiXmlElement *root = docHandle.FirstChildElement("book").ToElement();
+ TiXmlAttribute *nAtomsAtt = root->FirstAttribute();
+ cout << nAtomsAtt->Name() << ", " << nAtomsAtt->Value() << endl;
+ int nAtoms = atoi(nAtomsAtt->Value());
+ TiXmlAttribute *numSamplesAtt = nAtomsAtt->Next()->Next();
+ cout << numSamplesAtt->Name() << ", " << numSamplesAtt->Value() << endl;
+ book.numSamples = atoi(numSamplesAtt->Value());
+ TiXmlAttribute *sampleRateAtt = numSamplesAtt->Next();
+ cout << sampleRateAtt->Name() << ", " << sampleRateAtt->Value() << endl;
+ book.sampleRate = atoi(sampleRateAtt->Value());
+
+ for(int atomIdx=0; atomIdx < nAtoms; atomIdx++) {
+ maxiGaborAtom *newAtom = new maxiGaborAtom();
+ newAtom->atomType = GABOR;
+ TiXmlElement *atom = docHandle.FirstChildElement("book").ChildElement("atom", atomIdx).ToElement();
+ TiXmlElement *node = atom->FirstChildElement()->NextSibling()->ToElement();
+ newAtom->position = atoi(node->FirstChildElement("p")->FirstChild()->ToText()->Value());
+ newAtom->length = atoi(node->FirstChildElement("l")->FirstChild()->ToText()->Value());
+ node = node->NextSibling()->ToElement();
+ newAtom->amp = atof(node->FirstChild()->ToText()->Value());
+ node = node->NextSibling()->NextSibling()->ToElement();
+ newAtom->frequency = atof(node->FirstChild()->ToText()->Value());
+ node = node->NextSibling()->NextSibling()->ToElement();
+ newAtom->phase = atof(node->FirstChild()->ToText()->Value());
+ //cout << "Atom: pos: " << newAtom->position << ", length: " << newAtom->length << ", amp: " << newAtom->amp << ", freq: " << newAtom->frequency << ", phase: " << newAtom->phase << endl;
+ book.atoms.push_back(newAtom);
+ }
+
+ }
+ return ok;
+}
+
+maxiAtomBook::~maxiAtomBook() {
+ for(int i=0; i < atoms.size(); i++) delete atoms[i];
+}
diff --git a/ofxMaxim/ofxMaxim/libs/maxiAtoms.h b/ofxMaxim/ofxMaxim/libs/maxiAtoms.h
index c7316b5..ec054b8 100644
--- a/ofxMaxim/ofxMaxim/libs/maxiAtoms.h
+++ b/ofxMaxim/ofxMaxim/libs/maxiAtoms.h
@@ -11,18 +11,38 @@
#include <iostream>
#include "maximilian.h"
#include <list>
+#include <vector>
using namespace std;
-class maxiAtoms {
+enum maxiAtomTypes {
+ GABOR
+};
+
+struct maxiAtom {
+ maxiAtomTypes atomType;
+ float length;
+ float position;
+ float amp;
+ static bool atomSortPositionAsc(maxiAtom* a, maxiAtom* b) {return a->position < b->position;}
+};
+
+struct maxiGaborAtom : maxiAtom {
+ float frequency;
+ float phase;
+};
+
+//create atoms
+class maxiCollider {
public:
static void createGabor(flArr &atom, const float freq, const float sampleRate, const uint length,
const float phase, const float kurtotis, const float amp);
};
-class maxiAtomStream {
+//queue atoms into an audio stream
+class maxiAccelerator {
public:
- maxiAtomStream();
+ maxiAccelerator();
void addAtom(flArr &atom);
void fillNextBuffer(float *buffer, unsigned int bufferLength);
private:
@@ -34,4 +54,19 @@ private:
};
typedef list<queuedAtom> queuedAtomList;
queuedAtomList atomQueue;
-}; \ No newline at end of file
+};
+
+//load a book in MPTK XML format
+//http://mptk.irisa.fr/
+
+class maxiAtomBook {
+public:
+ ~maxiAtomBook();
+ typedef vector<maxiAtom*> maxiAtomBookData;
+ unsigned int numSamples;
+ unsigned int sampleRate;
+ maxiAtomBookData atoms;
+ static bool loadMPTKXmlBook(string filename, maxiAtomBook &book);
+
+};
+