/*
* Author: Harry van Haaren 2013
* harryhaaren@gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "diskwriter.hxx"
#include "config.hxx"
#include
#include
#include
#include
#include
#include
// to prompt the user of new filenames in case of overwrite
#include
#include "gui.hxx"
#include "gmastertrack.hxx"
#include "controller/genericmidi.hxx"
#include
extern Gui* gui;
using namespace std;
DiskWriter::DiskWriter()
{
sessionJson = cJSON_CreateObject();
audioJson = cJSON_CreateObject();
// setup default controller name / author etc
controllerInfo[CONTROLLER_NAME] = "no name";
controllerInfo[CONTROLLER_AUTHOR] = "no author";
controllerInfo[CONTROLLER_LINK] = "no link";
sessionDir = getenv("HOME");
sessionName = "session";
foldersCreated = false;
// create .config/openAV/loopp/ directory
stringstream dotConfig;
dotConfig << getenv("HOME") << "/.config/openAV/";
int dotConfigDir = mkdir( dotConfig.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
if ( errno == EEXIST ) {
//LOOPP_NOTE("dotConfigDir exists");
} else if ( dotConfigDir ) {
LOOPP_WARN("Error creating dotConfigDir: %s", strerror(errno));
} else {
LOOPP_NOTE("Creating .config/openAV/ directory");
}
stringstream dotConfigLoopp;
dotConfigLoopp << getenv("HOME") << "/.config/openAV/loopp";
int dotConfigLooppDir = mkdir( dotConfigLoopp.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
if ( errno == EEXIST ) {
//LOOPP_NOTE("dotConfigLooppDir exists");
} else if ( dotConfigLooppDir ) {
LOOPP_WARN("Error creating dotConfigLooppDir: %s", strerror(errno));
} else {
LOOPP_NOTE("Creating .config/openAV/loopp directory");
}
stringstream dotConfigCtlr;
dotConfigCtlr << getenv("HOME") << "/.config/openAV/loopp/controllers/";
int dotConfigCtlrDir = mkdir( dotConfigCtlr.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
if ( errno == EEXIST ) {
//LOOPP_NOTE("dotConfigCtlrDir exists");
} else if ( dotConfigCtlrDir ) {
LOOPP_WARN("Error creating dotConfigCtlrDir: %s", strerror(errno));
} else {
LOOPP_NOTE("Creating .config/openAV/loopp directory");
}
}
DiskWriter::~DiskWriter()
{
// reset the cJSON objects
cJSON_Delete( sessionJson );
cJSON_Delete( audioJson );
}
void DiskWriter::initialize(std::string path, std::string name )
{
sessionName = name;
sessionPath = path;
/* If project dir doesn't exist yet, create it */
int projectDir = mkdir( path.c_str(),
S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
if ( errno == EEXIST ) {
//LOOPP_NOTE("project dir %s exists, this is good", path.c_str());
} else if ( projectDir ) {
LOOPP_WARN("Error creating projectDir: %s", strerror(errno));
} else {
LOOPP_NOTE("Creating %s directory", path.c_str());
}
// write session.loopp JSON node to /.loopp
stringstream sessionDirStream;
sessionDirStream << path;
if ( !gui->getNsm() )
sessionDirStream << "/" << sessionName << ".loopp";
sessionDir = sessionDirStream.str();
LOOPP_NOTE( "Creating session dir %s", sessionDir.c_str() );
int sessionDirError = mkdir( sessionDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
if ( sessionDirError ) {
// handle by using different filename?
LOOPP_WARN("Error creating session directory. Does the path: %s exist?",path.c_str());
}
stringstream audioDirStream;
audioDirStream << sessionDir << "/audio";
audioDir = audioDirStream.str();
LOOPP_NOTE("Creating audio dir %s", audioDir.c_str() );
int audioDirError = mkdir( audioDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
if ( audioDirError ) {
LOOPP_WARN("Error creating sample directory");
}
foldersCreated = true;
}
std::string DiskWriter::getLastSaveName()
{
return sessionName;
}
std::string DiskWriter::getLastSavePath()
{
return sessionPath;
}
void DiskWriter::writeControllerInfo( CONTROLLER_INFO c, std::string s )
{
controllerInfo[c] = s;
}
int DiskWriter::writeControllerFile( Controller* c )
{
if ( c ) {
LOOPP_NOTE("DiskWriter Controller* id: %i", c->getID() );
} else {
LOOPP_ERROR("DiskWriter Controller* passed NULL" );
return LOOPP_RETURN_ERROR;
}
// check if controller of ID is actually a GenericMIDI controller
GenericMIDI* g = dynamic_cast( c );
if ( g ) {
LOOPP_NOTE("Creating JSON for .ctlr file...");
cJSON* controllerJson = cJSON_CreateObject();
cJSON_AddItemToObject( controllerJson, "name",
cJSON_CreateString( controllerInfo[CONTROLLER_NAME].c_str() ) );
cJSON_AddItemToObject( controllerJson, "author",
cJSON_CreateString( controllerInfo[CONTROLLER_AUTHOR].c_str() ) );
cJSON_AddItemToObject( controllerJson, "link",
cJSON_CreateString( controllerInfo[CONTROLLER_LINK].c_str() ) );
// input bindings
std::vector b = g->getMidiToAction();
cJSON* inputBindings = cJSON_CreateArray();
cJSON_AddItemToObject(controllerJson, "inputBindings", inputBindings );
for(unsigned int i = 0; i < b.size(); i++ ) {
// create binding
cJSON* binding = cJSON_CreateObject();
cJSON_AddItemToArray( inputBindings, binding );
// add metadata to binding
const char* actionName = Event::getPrettyName( b.at(i)->action );
if ( actionName ) {
cJSON_AddItemToObject( binding, "action", cJSON_CreateString( actionName ) );
cJSON_AddNumberToObject( binding, "status", b.at(i)->status );
cJSON_AddNumberToObject( binding, "data", b.at(i)->data );
// only add JSON elements if they're not the "invalid" defaults
if ( b.at(i)->track != -2 )
cJSON_AddNumberToObject( binding, "track", b.at(i)->track );
if ( b.at(i)->scene != -1 )
cJSON_AddNumberToObject( binding, "scene", b.at(i)->scene );
if ( b.at(i)->send != -1 )
cJSON_AddNumberToObject( binding, "send", b.at(i)->send );
if ( b.at(i)->active!= -1 )
cJSON_AddNumberToObject( binding, "active", b.at(i)->active );
LOOPP_NOTE("Creating Binding: action %i == %s!", b.at(i)->action, actionName );
} else {
LOOPP_WARN("Binding action %i has no prettyName!", b.at(i)->action );
}
}
// write the sample JSON node to /sample.cfg
stringstream controllerCfgPath;
controllerCfgPath << getenv("HOME") << "/.config/openAV/loopp/controllers/" << g->getName() << ".ctlr";
ifstream infile( controllerCfgPath.str().c_str() );
if ( infile.good() ) {
// file exists: ask user overwrite or rename?
//LOOPP_WARN("Controller filename exists: prompting user to overwrite y/n?");
int action = fl_choice("Controller exists, action?", "Cancel", "Rename", "Overwrite");
if ( action == 0 ) {
// return OK, as user has chosen to cancel writing the file
return LOOPP_RETURN_OK;
} else if ( action == 1 ) {
// rename here
const char* name = fl_input("New name for .ctlr file:");
if ( name ) {
// clear the filename
controllerCfgPath.str( "" );
controllerCfgPath << getenv("HOME") << "/.config/openAV/loopp/controllers/" << name << ".ctlr";
LOOPP_NOTE( "New .ctlr filename %s", controllerCfgPath.str().c_str() );
} else {
LOOPP_NOTE("No name entered for .ctlr file, canceling!");
return LOOPP_RETURN_ERROR;
}
} else {
// just overwrite the file, no action
}
}
LOOPP_NOTE("Writing %s.ctlr file to disk", g->getName().c_str() );
ofstream controllerCfgFile;
controllerCfgFile.open ( controllerCfgPath.str().c_str() );
controllerCfgFile << cJSON_Print( controllerJson );
controllerCfgFile.close();
} else {
LOOPP_WARN("Invalid Controller pointer: cannot write %s as is not a GenericMIDI controller!", c->getName().c_str() );
return LOOPP_RETURN_ERROR;
}
return LOOPP_RETURN_OK;
}
int DiskWriter::writeAudioBuffer(int track, int scene, AudioBuffer* ab,
const char *gui_path)
{
stringstream path;
if ( gui_path && strlen(gui_path) ) {
printf("saving single buffer to %s\n", gui_path);
path << gui_path;
} else if ( foldersCreated ) {
stringstream filename;
filename << "t_" << track << "_s_" << scene << ".wav";
// store the clip in clipData, we will write the session JSON for it in writeSession
clipData.push_back( ClipData( track, scene, filename.str() ) );
// add the AudioBuffer metadata to the sample JSON node
cJSON* sampleClip = cJSON_CreateObject();
cJSON_AddItemToObject(audioJson, filename.str().c_str(), sampleClip );
cJSON_AddNumberToObject(sampleClip,"beats", ab->getBeats() );
// get pretty name from GUI
std::string clipName = gui->getTrack(track)->getClipSelector()->clipName( scene );
cJSON_AddItemToObject ( sampleClip, "name", cJSON_CreateString( clipName.c_str() ));
// write the AudioBuffer contents to /audio/ as .wav
// or alternatively t_