From c0c7f718305472a88e29e35a8aa514ef53b5856e Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Mon, 11 Feb 2008 17:42:15 -0600 Subject: [PATCH] Being to support saving of instrument definitions. --- instrument.C | 59 ++++++++++++++++++++++++++++++++++++++++------------ instrument.H | 3 ++- main.C | 15 +++++++++++++ non.H | 2 ++ 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/instrument.C b/instrument.C index 523894b..1dff4ad 100644 --- a/instrument.C +++ b/instrument.C @@ -26,9 +26,25 @@ #include "const.h" #include "config.h" +#include "non.H" + #include #include +/****** + Instrument definition file format is thus: + + "Name", n, v + + Where /n/ is a note number from 0 to 127 and /v/ is a percentage of + volume. + + When a system installed instrument definition is modified, the + modified version is saved in the user's $HOME. Therefore, when + loading instruments, user defined instruments always hide system + defined instruments of the same name. + + */ list Instrument::instruments; @@ -71,13 +87,6 @@ Instrument::open ( const char *name ) return new Instrument ( name ); } -void -Instrument::note ( int from, int to ) -{ -// _map[ from ].note = to; - WARNING( "what should this do now?" ); -} - void Instrument::note_name ( int n, char *s ) { @@ -97,9 +106,6 @@ Instrument::velocity ( int n, int v ) void Instrument::translate ( midievent *e ) const { -// int n = e->note(); - -// e->note( _map[ n ].note ); e->note_velocity( e->note_velocity() * _map[ e->note() ].velocity / 100 ); } @@ -121,7 +127,7 @@ Instrument::velocity ( int n ) const return _map[ n ].velocity; } -int +bool Instrument::read ( const char *s ) { FILE *fp; @@ -161,8 +167,6 @@ Instrument::read ( const char *s ) DEBUG( "name: \"%s\", note: %d, velocity: %d%%", m.name, note, m.velocity ); -// _map[ (64 + (n / 2)) - i ] = m; - _map[ note ] = m; } @@ -173,6 +177,35 @@ Instrument::read ( const char *s ) return true; } +bool +Instrument::write ( const char *s ) const +{ + FILE *fp; + + char pat[512]; + + sprintf( pat, "%s/%s%s.inst", config.user_config_dir, INSTRUMENT_DIR, s ); + + if ( ! ( fp = fopen( pat, "w" ) ) ) + return false; + + int n = 0; + for ( int i = 0; i < 127; ++i ) + { + if ( _map[ i ].name ) + { + fprintf( fp, "\"%s\", %d, %d\n", _map[ i ].name, i, _map[ i ].velocity ); + ++n; + } + } + + DEBUG( "wrote %d lines to instrument file \"%s\"", n, pat ); + + fclose( fp ); + + return true; +} + static int instrument_filter ( const struct dirent *d ) { diff --git a/instrument.H b/instrument.H index 0634dd7..8e776dc 100644 --- a/instrument.H +++ b/instrument.H @@ -40,7 +40,8 @@ class Instrument struct i_map _map[128]; /* note / velocity mappings */ Instrument ( const char *name ); - int read ( const char *s ); + bool read ( const char *s ); + bool write ( const char *s ) const; public: diff --git a/main.C b/main.C index c7068ae..e49d608 100644 --- a/main.C +++ b/main.C @@ -18,6 +18,9 @@ #include #include +#include +#include + #include "non.H" // #include "gui/input.H" #include "gui/ui.H" @@ -27,6 +30,8 @@ #include "pattern.H" #include "phrase.H" + + Canvas *pattern_c, *phrase_c, *trigger_c; sequence *playlist; @@ -134,6 +139,16 @@ main ( int argc, char **argv ) song.random.feel = 8; song.random.probability = 0.33; + asprintf( &config.user_config_dir, "%s/%s", getenv( "HOME" ), USER_CONFIG_DIR ); + + + mkdir( config.user_config_dir, 0777 ); + { + char pat[512]; + snprintf( pat, 512, "%s/%s", config.user_config_dir, INSTRUMENT_DIR ); + mkdir( pat, 0777 ); + } + printf( "%s %s -- %s\n", APP_TITLE, VERSION, COPYRIGHT ); playlist = new sequence; diff --git a/non.H b/non.H index 7726134..7661984 100644 --- a/non.H +++ b/non.H @@ -73,6 +73,8 @@ struct global_settings { bool visual_metronome; /* show visual metronome */ bool follow_playhead; + char *user_config_dir; + }; extern global_settings config;