diff --git a/src/diskwriter.cxx b/src/diskwriter.cxx index 66491cc..e28523f 100644 --- a/src/diskwriter.cxx +++ b/src/diskwriter.cxx @@ -1,47 +1,107 @@ #include "diskwriter.hxx" +#include +#include #include +#include +#include + +#include "worker.hxx" using namespace std; DiskWriter::DiskWriter() { - session = cJSON_CreateObject(); - audioConfig = cJSON_CreateObject(); + initialize( getenv("HOME"), "sessionName" ); }; +void DiskWriter::initialize(std::string path, std::string name ) +{ + sessionPath = path; + sessionName = name; + + session = cJSON_CreateObject(); + sample = cJSON_CreateObject(); + + // add session metadata + cJSON_AddItemToObject ( session, "session", cJSON_CreateString( sessionName.c_str() )); + cJSON_AddNumberToObject( session, "version_major", 1 ); + cJSON_AddNumberToObject( session, "version_minor", 0 ); + cJSON_AddNumberToObject( session, "version_patch", 0 ); + cJSON_AddNumberToObject( session, "bpm", 120 ); +} + void DiskWriter::writeAudioBuffer(int track, int scene, AudioBuffer* ab ) { // add the track / scene / name combo to session JSON node - - cJSON* clip = cJSON_CreateObject(); cJSON_AddItemToObject(session, "clip", clip ); cJSON_AddNumberToObject(clip,"track", track); cJSON_AddNumberToObject(clip,"scene", scene); - cJSON_AddStringToObject(clip,"file", "filenameHere.wav"); + cJSON_AddStringToObject(clip,"file", "filenameHere.wav"); + // add the AudioBuffer metadata to the sample JSON node + cJSON* sampleClip = cJSON_CreateObject(); + cJSON_AddItemToObject(sample, "sample", sampleClip ); + + stringstream filename; + filename << "t_" << track << "_s_" << scene << ".wav"; + + cJSON_AddStringToObject(sampleClip,"file", filename.str().c_str() ); + cJSON_AddNumberToObject(sampleClip,"beats", ab->getBeats() ); + // write the AudioBuffer contents to /samples/ as .wav // or alternatively t__s_.wav + stringstream path; + path << sessionPath << "/" << sessionName << "/samples/" << filename.str(); + + Worker::writeSample( path.str(), ab ); + + + + // de allocate the AudioBuffer here!! + } void DiskWriter::writeSession( std::string path, std::string sessionName ) { - // write the audioConfig JSON node to /samples/sample.cfg - - // write session.luppp JSON node to /.luppp - cJSON_AddItemToObject ( session, "session", cJSON_CreateString( sessionName.c_str() )); - cJSON_AddNumberToObject( session, "version_major", 1 ); - cJSON_AddNumberToObject( session, "version_minor", 0 ); - cJSON_AddNumberToObject( session, "version_patch", 0 ); + stringstream sessionDir; + sessionDir << getenv("HOME") << "/" << sessionName; + int sessionDirError = mkdir( sessionDir.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ); + + if ( sessionDirError ) + { + // handle by using different filename? + } + + stringstream sampleDir; + sampleDir << sessionDir.str() << "/samples"; + int sampleDirError = mkdir( sampleDir.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ); + + stringstream sessionLuppp; + sessionLuppp << sessionDir.str() << "/" << sessionName << ".luppp"; + + //cout << "Session dir: " << sessionDir.str() << "\n" << "Sample dir : " << sampleDir.str() << endl; + + ofstream sessionFile; + sessionFile.open ( sessionLuppp.str().c_str() ); + sessionFile << cJSON_Print( session ); + sessionFile.close(); + + + // write the sample JSON node to /samples/sample.cfg + stringstream sampleConfig; + sampleConfig << sampleDir.str() << "/sample.cfg"; + + ofstream sampleFile; + sampleFile.open ( sampleConfig.str().c_str() ); + sampleFile << cJSON_Print( sample ); + sampleFile.close(); - cJSON_AddNumberToObject( session, "bpm", 120 ); - char* out = cJSON_Print( session ); - cout << out << endl; } diff --git a/src/diskwriter.hxx b/src/diskwriter.hxx index a624802..5008911 100644 --- a/src/diskwriter.hxx +++ b/src/diskwriter.hxx @@ -18,6 +18,8 @@ class DiskWriter public: DiskWriter(); + void initialize( std::string path, std::string sessionName ); + /// writes a single audio buffer to disk void writeAudioBuffer(int track, int scene, AudioBuffer* ab ); @@ -25,7 +27,11 @@ class DiskWriter private: cJSON* session; - cJSON* audioConfig; + cJSON* sample; + + std::string sessionName; + std::string sessionPath; + }; #endif // LUPPP_DISK_WRITER_H diff --git a/src/worker.hxx b/src/worker.hxx index 8baa123..7892e60 100644 --- a/src/worker.hxx +++ b/src/worker.hxx @@ -13,7 +13,6 @@ using namespace std; namespace Worker { - /// loads a sample into a new AudioBuffer, returning the buffer static AudioBuffer* loadSample( string path ) { @@ -37,6 +36,17 @@ namespace Worker return 0; } + + static int writeSample( string path, AudioBuffer* ab ) + { + SndfileHandle outfile( path, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_FLOAT, 1, 44100); + + cout << "Worker::writeSample() " << path << " size: " << ab->getData().size() << endl; + outfile.write( &ab->getData()[0], ab->getData().size() ); + } + + + } #endif // LUPPP_WORKER_H