Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/hydrogen-music/hydrogen i…
Browse files Browse the repository at this point in the history
…nto button_press_hold
  • Loading branch information
blablack committed Oct 31, 2015
2 parents db2f9fc + 863a747 commit 36a2268
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 75 deletions.
5 changes: 3 additions & 2 deletions src/core/include/hydrogen/basics/drumkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ class Drumkit : public H2Core::Object
* save a drumkit into an xml file
* \param dk_path the path to save the drumkit into
* \param overwrite allows to write over existing drumkit file
* \param component_id to chose the component to save or -1 for all
* \return true on success
*/
bool save_file( const QString& dk_path, bool overwrite=false );
bool save_file( const QString& dk_path, bool overwrite=false, int component_id=-1 );
/**
* save a drumkit instruments samples into a directory
* \param dk_dir the directory to save the samples into
Expand Down Expand Up @@ -178,7 +179,7 @@ class Drumkit : public H2Core::Object
* save the drumkit within the given XMLNode
* \param node the XMLNode to feed
*/
void save_to( XMLNode* node );
void save_to( XMLNode* node, int component_id=-1 );
/**
* load a drumkit from an XMLNode
* \param node the XMLDode to read from
Expand Down
2 changes: 1 addition & 1 deletion src/core/include/hydrogen/basics/instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Instrument : public H2Core::Object
* save the intrument within the given XMLNode
* \param node the XMLNode to feed
*/
void save_to( XMLNode* node );
void save_to( XMLNode* node, int component_id );
/**
* load an instrument from an XMLNode
* \param node the XMLDode to read from
Expand Down
2 changes: 1 addition & 1 deletion src/core/include/hydrogen/basics/instrument_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class InstrumentComponent : public H2Core::Object
InstrumentComponent( InstrumentComponent* other );
~InstrumentComponent();

void save_to( XMLNode* node );
void save_to( XMLNode* node, int component_id );
static InstrumentComponent* load_from( XMLNode* node, const QString& dk_path );

InstrumentLayer* operator[]( int idx );
Expand Down
2 changes: 1 addition & 1 deletion src/core/include/hydrogen/basics/instrument_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class InstrumentList : public H2Core::Object
* save the intrument list within the given XMLNode
* \param node the XMLNode to feed
*/
void save_to( XMLNode* node );
void save_to( XMLNode* node, int component_id );
/**
* load an instrument list from an XMLNode
* \param node the XMLDode to read from
Expand Down
20 changes: 11 additions & 9 deletions src/core/src/basics/drumkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ bool Drumkit::save( const QString& dk_dir, bool overwrite )
return ret;
}

bool Drumkit::save_file( const QString& dk_path, bool overwrite )
bool Drumkit::save_file( const QString& dk_path, bool overwrite, int component_id )
{
INFOLOG( QString( "Saving drumkit definition into %1" ).arg( dk_path ) );
if( Filesystem::file_exists( dk_path, true ) && !overwrite ) {
Expand All @@ -236,23 +236,25 @@ bool Drumkit::save_file( const QString& dk_path, bool overwrite )
XMLDoc doc;
doc.set_root( "drumkit_info", "drumkit" );
XMLNode root = doc.firstChildElement( "drumkit_info" );
save_to( &root );
save_to( &root, component_id );
return doc.write( dk_path );
}

void Drumkit::save_to( XMLNode* node )
void Drumkit::save_to( XMLNode* node, int component_id )
{
node->write_string( "name", __name );
node->write_string( "author", __author );
node->write_string( "info", __info );
node->write_string( "license", __license );
XMLNode components_node = node->ownerDocument().createElement( "componentList" );
for (std::vector<DrumkitComponent*>::iterator it = __components->begin() ; it != __components->end(); ++it) {
DrumkitComponent* pComponent = *it;
pComponent->save_to( &components_node );
if( component_id == -1 ) {
XMLNode components_node = node->ownerDocument().createElement( "componentList" );
for (std::vector<DrumkitComponent*>::iterator it = __components->begin() ; it != __components->end(); ++it) {
DrumkitComponent* pComponent = *it;
pComponent->save_to( &components_node );
}
node->appendChild( components_node );
}
node->appendChild( components_node );
__instruments->save_to( node );
__instruments->save_to( node, component_id );
}

bool Drumkit::save_samples( const QString& dk_dir, bool overwrite )
Expand Down
5 changes: 3 additions & 2 deletions src/core/src/basics/instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void Instrument::unload_samples()
}
}

void Instrument::save_to( XMLNode* node )
void Instrument::save_to( XMLNode* node, int component_id )
{
XMLNode InstrumentNode = node->ownerDocument().createElement( "instrument" );
InstrumentNode.write_int( "id", __id );
Expand Down Expand Up @@ -311,7 +311,8 @@ void Instrument::save_to( XMLNode* node )
}
for (std::vector<InstrumentComponent*>::iterator it = __components->begin() ; it != __components->end(); ++it) {
InstrumentComponent* pComponent = *it;
pComponent->save_to( &InstrumentNode );
if( component_id == -1 || pComponent->get_drumkit_componentID() == component_id )
pComponent->save_to( &InstrumentNode, component_id );
}
node->appendChild( InstrumentNode );
}
Expand Down
19 changes: 13 additions & 6 deletions src/core/src/basics/instrument_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,25 @@ InstrumentComponent* InstrumentComponent::load_from( XMLNode* node, const QStrin
return instrument_component;
}

void InstrumentComponent::save_to( XMLNode* node )
void InstrumentComponent::save_to( XMLNode* node, int component_id )
{
XMLNode component_node = node->ownerDocument().createElement( "instrumentComponent" );
component_node.write_int( "component_id", __related_drumkit_componentID );
component_node.write_float( "gain", __gain );
XMLNode component_node;
if( component_id == -1 ){
component_node = node->ownerDocument().createElement( "instrumentComponent" );
component_node.write_int( "component_id", __related_drumkit_componentID );
component_node.write_float( "gain", __gain );
}
for ( int n = 0; n < MAX_LAYERS; n++ ) {
InstrumentLayer* layer = get_layer( n );
if( layer ) {
layer->save_to( &component_node );
if( component_id == -1 )
layer->save_to( &component_node );
else
layer->save_to( node );
}
}
node->appendChild( component_node );
if( component_id == -1 )
node->appendChild( component_node );
}

};
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/basics/instrument_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ InstrumentList* InstrumentList::load_from( XMLNode* node, const QString& dk_path
return instruments;
}

void InstrumentList::save_to( XMLNode* node )
void InstrumentList::save_to( XMLNode* node, int component_id )
{
XMLNode instruments_node = node->ownerDocument().createElement( "instrumentList" );
for ( int i = 0; i < size(); i++ ) {
( *this )[i]->save_to( &instruments_node );
( *this )[i]->save_to( &instruments_node, component_id );
}
node->appendChild( instruments_node );
}
Expand Down
150 changes: 146 additions & 4 deletions src/gui/src/SoundLibrary/SoundLibraryExportDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#include <hydrogen/basics/adsr.h>
#include <hydrogen/basics/sample.h>
#include <hydrogen/basics/instrument.h>
#include <hydrogen/basics/instrument_list.h>
#include <hydrogen/basics/instrument_layer.h>
#include <hydrogen/basics/instrument_component.h>
#include <hydrogen/basics/drumkit_component.h>
#include <QFileDialog>
#include <memory>
#include <QtGui>
Expand Down Expand Up @@ -81,6 +85,34 @@ void SoundLibraryExportDialog::on_exportBtn_clicked()
QString drumkitDir = Filesystem::drumkit_dir_search( drumkitName );
QString saveDir = drumkitPathTxt->text();

Preferences *pref = Preferences::get_instance();
QDir qdTempFolder( pref->getTmpDirectory() );
bool TmpFileCreated = false;


int componentID = -1;
Drumkit* info;
if( versionList->currentIndex() == 1 ) {
for (uint i = 0; i < drumkitInfoList.size(); i++ ) {
info = drumkitInfoList[i];
if( info->get_name().compare( drumkitName ) == 0 ) {
QString temporaryDrumkitXML = qdTempFolder.filePath( "drumkit.xml" );
INFOLOG( "[ExportSoundLibrary]" );
INFOLOG( "Saving temporary file into: " + temporaryDrumkitXML );
TmpFileCreated = true;
for (std::vector<DrumkitComponent*>::iterator it = info->get_components()->begin() ; it != info->get_components()->end(); ++it) {
DrumkitComponent* pComponent = *it;
if( pComponent->get_name().compare( componentList->currentText() ) == 0) {
componentID = pComponent->get_id();
break;
}
}
info->save_file( temporaryDrumkitXML, true, componentID );
break;
}
}
}

#if defined(H2CORE_HAVE_LIBARCHIVE)
QString fullDir = drumkitDir + "/" + drumkitName;
QDir sourceDir(fullDir);
Expand All @@ -97,6 +129,7 @@ void SoundLibraryExportDialog::on_exportBtn_clicked()
int len;
int fd;


a = archive_write_new();

#if ARCHIVE_VERSION_NUMBER < 3000000
Expand All @@ -110,6 +143,37 @@ void SoundLibraryExportDialog::on_exportBtn_clicked()
for (int i = 0; i < filesList.size(); i++) {
QString filename = fullDir + "/" + filesList.at(i);
QString targetFilename = drumkitName + "/" + filesList.at(i);

if( versionList->currentIndex() == 1 ) {
if( filesList.at(i).compare( QString("drumkit.xml") ) == 0 ) {
filename = qdTempFolder.filePath( "drumkit.xml" );
}
else {
bool bFoundFileInRightComponent = false;
for( int j = 0; j < info->get_instruments()->size() ; j++){
InstrumentList instrList = info->get_instruments();
Instrument* instr = instrList[j];
for (std::vector<InstrumentComponent*>::iterator it = instr->get_components()->begin() ; it != instr->get_components()->end(); ++it) {
InstrumentComponent* component = *it;
if( component->get_drumkit_componentID() == componentID ){
for( int n = 0; n < MAX_LAYERS; n++ ) {
InstrumentLayer* layer = component->get_layer( n );
if( layer ) {
if( layer->get_sample()->get_filename().compare(filesList.at(i)) == 0 ) {
bFoundFileInRightComponent = true;
break;
}
}
}
}
}
}
if( !bFoundFileInRightComponent )
continue;
}
}


stat(filename.toUtf8().constData(), &st);
entry = archive_entry_new();
archive_entry_set_pathname(entry, targetFilename.toUtf8().constData());
Expand Down Expand Up @@ -139,8 +203,43 @@ void SoundLibraryExportDialog::on_exportBtn_clicked()
QApplication::restoreOverrideCursor();
QMessageBox::information( this, "Hydrogen", "Drumkit exported." );
#elif !defined(WIN32)
QString cmd = QString( "cd " ) + drumkitDir + "; tar czf \"" + saveDir + "/" + drumkitName + ".h2drumkit\" -- \"" + drumkitName + "\"";
int ret = system( cmd.toLocal8Bit() );


if(TmpFileCreated)
{
/*
* If a temporary drumkit.xml has been created:
* 1. move the original drumkit.xml to drumkit_backup.xml
* 2. copy the temporary file to drumkitDir/drumkit.xml
* 3. export the drumkit
* 4. move the drumkit_backup.xml to drumkit.xml
*/

int ret = 0;

//1.
QString cmd = QString( "cd " ) + drumkitDir + "; " + "cp " + drumkitName + "/drumkit.xml " + drumkitName + "/drumkit_097.xml";
ret = system( cmd.toLocal8Bit() );


//2.
cmd = QString( "cd " ) + drumkitDir + "; " + "mv " + qdTempFolder.filePath( "drumkit.xml" ) + " " + drumkitName + "/drumkit.xml";
ret = system( cmd.toLocal8Bit() );

//3.
cmd = QString( "cd " ) + drumkitDir + ";" + "tar czf \"" + saveDir + "/" + drumkitName + ".h2drumkit\" -- \"" + drumkitName + "\"";
ret = system( cmd.toLocal8Bit() );

//4.
cmd = QString( "cd " ) + drumkitDir + "; " + "mv " + drumkitName + "/drumkit_097.xml " + drumkitName + "/drumkit.xml";
ret = system( cmd.toLocal8Bit() );

} else {
QString cmd = QString( "cd " ) + drumkitDir + ";" + "tar czf \"" + saveDir + "/" + drumkitName + ".h2drumkit\" -- \"" + drumkitName + "\"";
int ret = system( cmd.toLocal8Bit() );
}



QApplication::restoreOverrideCursor();
QMessageBox::information( this, "Hydrogen", "Drumkit exported." );
Expand Down Expand Up @@ -175,6 +274,32 @@ void SoundLibraryExportDialog::on_browseBtn_clicked()
}
}

void SoundLibraryExportDialog::on_cancelBtn_clicked()
{
accept();
}

void SoundLibraryExportDialog::on_drumkitList_currentIndexChanged( QString str )
{
componentList->clear();

QStringList p_compoList = kit_components[str];

for (QStringList::iterator it = p_compoList.begin() ; it != p_compoList.end(); ++it) {
QString p_compoName = *it;

componentList->addItem( p_compoName );
}
}

void SoundLibraryExportDialog::on_versionList_currentIndexChanged( int index )
{
if( index == 0 )
componentList->setEnabled( false );
else if( index == 1 )
componentList->setEnabled( true );
}

void SoundLibraryExportDialog::updateDrumkitList()
{
INFOLOG( "[updateDrumkitList]" );
Expand All @@ -194,6 +319,12 @@ void SoundLibraryExportDialog::updateDrumkitList()
if (info) {
drumkitInfoList.push_back( info );
drumkitList->addItem( info->get_name() );
QStringList p_components;
for (std::vector<DrumkitComponent*>::iterator it = info->get_components()->begin() ; it != info->get_components()->end(); ++it) {
DrumkitComponent* p_compo = *it;
p_components.append(p_compo->get_name());
}
kit_components[info->get_name()] = p_components;
}
}

Expand All @@ -204,6 +335,12 @@ void SoundLibraryExportDialog::updateDrumkitList()
if (info) {
drumkitInfoList.push_back( info );
drumkitList->addItem( info->get_name() );
QStringList p_components;
for (std::vector<DrumkitComponent*>::iterator it = info->get_components()->begin() ; it != info->get_components()->end(); ++it) {
DrumkitComponent* p_compo = *it;
p_components.append(p_compo->get_name());
}
kit_components[info->get_name()] = p_components;
}
}

Expand All @@ -213,8 +350,13 @@ void SoundLibraryExportDialog::updateDrumkitList()
*/

int index = drumkitList->findText( preselectedKit );
if ( index >= 0)
if ( index >= 0) {
drumkitList->setCurrentIndex( index );
else
}
else {
drumkitList->setCurrentIndex( 0 );
}

on_drumkitList_currentIndexChanged( drumkitList->currentText() );
on_versionList_currentIndexChanged( 0 );
}
4 changes: 4 additions & 0 deletions src/gui/src/SoundLibrary/SoundLibraryExportDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ class SoundLibraryExportDialog : public QDialog, public Ui_SoundLibraryExportDia
private slots:
void on_exportBtn_clicked();
void on_browseBtn_clicked();
void on_cancelBtn_clicked();
void on_versionList_currentIndexChanged( int index );
void on_drumkitList_currentIndexChanged( QString str );
void on_drumkitPathTxt_textChanged( QString str );
void updateDrumkitList();
private:
std::vector<H2Core::Drumkit*> drumkitInfoList;
QString preselectedKit;
QHash<QString, QStringList> kit_components;
};


Expand Down
Loading

0 comments on commit 36a2268

Please sign in to comment.