From 0440798324ccc1a81a9047b772d49798afb3715d Mon Sep 17 00:00:00 2001 From: Harry van Haaren Date: Wed, 4 Sep 2013 22:32:46 +0100 Subject: [PATCH] -Implemented DiskReader and file loading. --- src/config.hxx | 3 ++ src/diskreader.cxx | 115 +++++++++++++++++++++++++++++++++++++++++++++ src/diskreader.hxx | 38 +++++++++++++++ src/diskwriter.cxx | 5 +- src/gui.cxx | 1 + src/gui.hxx | 6 ++- 6 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/diskreader.cxx create mode 100644 src/diskreader.hxx diff --git a/src/config.hxx b/src/config.hxx index bd286ac..ddb180a 100644 --- a/src/config.hxx +++ b/src/config.hxx @@ -18,6 +18,9 @@ // Saving state #define DEBUG_SAVE 1 +// Loading state +#define DEBUG_LOAD 1 + /// General Options #define NTRACKS 8 #define NSCENES 10 diff --git a/src/diskreader.cxx b/src/diskreader.cxx new file mode 100644 index 0000000..841b929 --- /dev/null +++ b/src/diskreader.cxx @@ -0,0 +1,115 @@ + +#include "diskreader.hxx" + +#include +#include +#include +#include +#include +#include + +#include "event.hxx" +#include "eventhandler.hxx" +#include "worker.hxx" + +using namespace std; + +DiskReader::DiskReader() +{ + // TODO : get from user input + + stringstream s; + s << getenv("HOME") << "/" << "sessionName" << "/"; + readSession( s.str() ); +}; + +void DiskReader::readSession( std::string path ) +{ + cout << "DiskReader::readSession() " << path << endl; + sessionPath = path; + + char *sampleChar; + std::string sessionString; + + + stringstream s; + s << path << "session.luppp"; + + cout << "session path " << s.str().c_str() << endl; + + + // open file, store entire contents into string + std::ifstream file( s.str().c_str(), std::ios_base::in|std::ios_base::ate); + long file_length = file.tellg(); + file.seekg(0, std::ios_base::beg); + file.clear(); + char *string = new char[file_length]; + file.read(string, file_length); + + cout << "sessionFile string:\n " << string << endl; + + + session = cJSON_Parse( string ); + + if (!session) + { + printf("Error before: [%s]\n",cJSON_GetErrorPtr()); + return; + } + + //cout << "readSession: " << cJSON_Print( session ) << endl; + + + cJSON* clip = cJSON_GetObjectItem( session, "clip"); + if ( clip ) + { + // get metadata for Clip + cJSON* track = cJSON_GetObjectItem( clip, "track"); + cJSON* scene = cJSON_GetObjectItem( clip, "scene"); + cJSON* file = cJSON_GetObjectItem( clip, "file"); + int t = track->valueint; + int s = scene->valueint; + stringstream sampleFilePath; + sampleFilePath << path << "/samples/" << file->valuestring; +#ifdef DEBUG_LOAD + cout << "clip has track " << t << " scene " << s << " file: " << + sampleFilePath.str() << endl; +#endif + + + + // load it + AudioBuffer* ab = Worker::loadSample( sampleFilePath.str() ); + EventLooperLoad e = EventLooperLoad( t, s, ab ); + writeToDspRingbuffer( &e ); + } + else + { + cout << "DiskReader: Error getting clip" << endl; + } + + + + /* + for( int i = 0; i < cJSON_GetArraySize( session ); i++ ) + { + cJSON* subitem = cJSON_GetArrayItem( session, i); + + + + name = cJSON_GetObjectItem(subitem, "name"); + index = cJSON_GetObjectItem(subitem, "index"); + optional = cJSON_GetObjectItem(subitem, "optional"); + + cout << "i = " << i << " session: " << subitem->valueint << endl; + + }*/ + + + + cJSON_Delete( session ); + + + free ( string ); + +} diff --git a/src/diskreader.hxx b/src/diskreader.hxx new file mode 100644 index 0000000..ff23605 --- /dev/null +++ b/src/diskreader.hxx @@ -0,0 +1,38 @@ + +#ifndef LUPPP_DISK_READER_H +#define LUPPP_DISK_READER_H + +#include + +#include "cjson/cJSON.h" + +class AudioBuffer; + +/** DiskReader + * This class reads a previously saved session from disk, restoring Luppp's + * internal state to that of when the save took place. + * + * The directory is the main point of loading. The user selects + * that directory to load the session from. .luppp is the name of + * the session file: it contains all info about the session: Volumes, loaded + * samples etc. + * + * Samples are read from the directory /samples, in which there is a + * sample.cfg file, which can be parsed to get sample metadata. +**/ +class DiskReader +{ + public: + DiskReader(); + + void readSession( std::string path ); + + private: + cJSON* session; + cJSON* sample; + + std::string sessionName; + std::string sessionPath; +}; + +#endif // LUPPP_DISK_READER_H diff --git a/src/diskwriter.cxx b/src/diskwriter.cxx index 75f089f..047efb2 100644 --- a/src/diskwriter.cxx +++ b/src/diskwriter.cxx @@ -24,6 +24,9 @@ void DiskWriter::initialize(std::string path, std::string name ) session = cJSON_CreateObject(); sample = cJSON_CreateObject(); + cJSON* lupppSession = cJSON_CreateObject(); + cJSON_AddItemToObject(session, "lupppSession", lupppSession ); + // add session metadata cJSON_AddItemToObject ( session, "session", cJSON_CreateString( sessionName.c_str() )); cJSON_AddNumberToObject( session, "version_major", 1 ); @@ -85,7 +88,7 @@ void DiskWriter::writeSession( std::string path, std::string sessionName ) int sampleDirError = mkdir( sampleDir.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ); stringstream sessionLuppp; - sessionLuppp << sessionDir.str() << "/" << sessionName << ".luppp"; + sessionLuppp << sessionDir.str() << "/session.luppp"; //cout << "Session dir: " << sessionDir.str() << "\n" << "Sample dir : " << sampleDir.str() << endl; diff --git a/src/gui.cxx b/src/gui.cxx index 75b6a5f..e4d9003 100644 --- a/src/gui.cxx +++ b/src/gui.cxx @@ -50,6 +50,7 @@ static void gui_static_read_rb(void* inst) Gui::Gui() : window(1110,650), + diskReader( new DiskReader ), diskWriter( new DiskWriter ) { window.color(FL_BLACK); diff --git a/src/gui.hxx b/src/gui.hxx index b4c8778..10258db 100644 --- a/src/gui.hxx +++ b/src/gui.hxx @@ -9,9 +9,11 @@ #include "config.hxx" #include "gtrack.hxx" #include "gunittrack.hxx" -#include "diskwriter.hxx" #include "gmastertrack.hxx" +#include "diskwriter.hxx" +#include "diskreader.hxx" + #include #include @@ -26,6 +28,7 @@ class Gui GMasterTrack* getMasterTrack(){return master;} DiskWriter* getDiskWriter(){return diskWriter;} + DiskReader* getDiskReader(){return diskReader;} // for pushing strings to tooltip area void setTooltip( std::string s ); @@ -34,6 +37,7 @@ class Gui Fl_Double_Window window; Fl_Box* box; + DiskReader* diskReader; DiskWriter* diskWriter; GMasterTrack* master;