2007-12-18 05:09:02 +01:00
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Copyright (C) 2008 Jonathan Moore Liles */
|
|
|
|
/* */
|
|
|
|
/* 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 2 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; see the file COPYING. If not,write to the Free Software */
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "instrument.H"
|
|
|
|
#include "common.h"
|
|
|
|
#include "const.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
2008-02-12 00:42:15 +01:00
|
|
|
#include "non.H"
|
|
|
|
|
2007-12-18 05:09:02 +01:00
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using std::list;
|
|
|
|
using std::string;
|
|
|
|
|
2008-02-12 00:42:15 +01:00
|
|
|
/******
|
2008-02-23 01:37:46 +01:00
|
|
|
Instrument definition file format is thus:
|
2008-02-12 00:42:15 +01:00
|
|
|
|
2008-02-23 01:37:46 +01:00
|
|
|
"Name", n, v
|
2008-02-12 00:42:15 +01:00
|
|
|
|
2008-02-23 01:37:46 +01:00
|
|
|
Where /n/ is a note number from 0 to 127 and /v/ is a percentage of
|
|
|
|
volume.
|
2008-02-12 00:42:15 +01:00
|
|
|
|
2008-02-23 01:37:46 +01:00
|
|
|
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.
|
2008-02-12 00:42:15 +01:00
|
|
|
|
2008-02-23 01:37:46 +01:00
|
|
|
*/
|
2007-12-18 05:09:02 +01:00
|
|
|
|
|
|
|
list <Instrument *> Instrument::instruments;
|
|
|
|
|
|
|
|
Instrument::Instrument ( const char *name )
|
|
|
|
{
|
|
|
|
|
|
|
|
for ( int i = 0; i < 128; i++ )
|
|
|
|
{
|
|
|
|
_map[i].name = NULL;
|
|
|
|
_map[i].velocity = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( name )
|
|
|
|
{
|
|
|
|
_name = strdup( name );
|
|
|
|
|
|
|
|
read( name );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_name = strdup( "not an instrument" );
|
|
|
|
_height = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instrument::instruments.push_front( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
Instrument *
|
|
|
|
Instrument::open ( const char *name )
|
|
|
|
{
|
|
|
|
list <Instrument *>::iterator i = Instrument::instruments.begin();
|
|
|
|
|
|
|
|
if ( name )
|
|
|
|
{
|
|
|
|
for ( ; i != Instrument::instruments.end(); i++ )
|
|
|
|
if ( 0 == strcmp( (*i)->_name, name ) )
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Instrument ( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Instrument::note_name ( int n, char *s )
|
|
|
|
{
|
|
|
|
if ( _map[ n ].name )
|
|
|
|
free( _map[ n ].name );
|
|
|
|
|
|
|
|
_map[ n ].name = s;
|
2008-02-12 05:06:00 +01:00
|
|
|
|
|
|
|
_dirty = true;
|
2007-12-18 05:09:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Instrument::velocity ( int n, int v )
|
|
|
|
{
|
|
|
|
_map[ n ].velocity = v;
|
2008-02-12 05:06:00 +01:00
|
|
|
|
|
|
|
_dirty = true;
|
2007-12-18 05:09:02 +01:00
|
|
|
}
|
|
|
|
|
2008-02-13 20:42:00 +01:00
|
|
|
/** Translate event, should only be passed NOTE ON/OFF events, returns
|
2008-02-23 01:37:46 +01:00
|
|
|
true if note is valid for this mapping */
|
2008-02-13 20:42:00 +01:00
|
|
|
bool
|
2007-12-18 05:09:02 +01:00
|
|
|
Instrument::translate ( midievent *e ) const
|
|
|
|
{
|
2008-02-13 20:42:00 +01:00
|
|
|
if ( ! note_name( e->note() ) )
|
|
|
|
return false;
|
|
|
|
|
2007-12-18 05:09:02 +01:00
|
|
|
e->note_velocity( e->note_velocity() * _map[ e->note() ].velocity / 100 );
|
2008-02-13 20:42:00 +01:00
|
|
|
|
|
|
|
return true;
|
2007-12-18 05:09:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
Instrument::note_name ( int n ) const
|
|
|
|
{
|
|
|
|
return _map[ n ].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Instrument::height ( void ) const
|
|
|
|
{
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Instrument::velocity ( int n ) const
|
|
|
|
{
|
|
|
|
return _map[ n ].velocity;
|
|
|
|
}
|
|
|
|
|
2008-02-12 00:42:15 +01:00
|
|
|
bool
|
2007-12-18 05:09:02 +01:00
|
|
|
Instrument::read ( const char *s )
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
char pat[512];
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
sprintf( pat, "%s%s.inst", config.user_config_dir, s );
|
2007-12-18 05:09:02 +01:00
|
|
|
|
|
|
|
if ( ! ( fp = fopen( pat, "r" ) ) )
|
2008-02-12 01:22:04 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
sprintf( pat, "%s%s.inst", SYSTEM_PATH INSTRUMENT_DIR, s );
|
|
|
|
|
|
|
|
if ( ! ( fp = fopen( pat, "r" ) ) )
|
|
|
|
return false;
|
|
|
|
}
|
2007-12-18 05:09:02 +01:00
|
|
|
|
|
|
|
struct i_map m;
|
|
|
|
char namebuf[256];
|
|
|
|
int note, velocity;
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
while ( 0 < fscanf( fp, "\"%[^\"]\", %d, %d\n", (char*)&namebuf, ¬e, &velocity ) ) n++;
|
|
|
|
|
|
|
|
rewind( fp );
|
|
|
|
|
|
|
|
MESSAGE( "reading %d lines from instrument file \"%s\"", n, s );
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for ( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
fscanf( fp, "\"%[^\"]\", %d, %d\n", (char *)&namebuf, ¬e, &velocity );
|
|
|
|
|
|
|
|
m.name = strdup( namebuf );
|
|
|
|
|
|
|
|
if ( velocity > 100 )
|
|
|
|
{
|
|
|
|
WARNING( "invalid volume percentage in instrument definition");
|
|
|
|
m.velocity = 100;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m.velocity = velocity;
|
|
|
|
|
|
|
|
DEBUG( "name: \"%s\", note: %d, velocity: %d%%", m.name, note, m.velocity );
|
|
|
|
|
|
|
|
_map[ note ] = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
_height = n;
|
|
|
|
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-12 00:42:15 +01:00
|
|
|
bool
|
|
|
|
Instrument::write ( const char *s ) const
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
char pat[512];
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
sprintf( pat, "%s/%s.inst", config.user_config_dir, s );
|
2008-02-12 00:42:15 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-02-12 05:06:00 +01:00
|
|
|
void
|
|
|
|
Instrument::save ( void ) const
|
|
|
|
{
|
|
|
|
if ( _dirty )
|
|
|
|
write( _name );
|
|
|
|
|
|
|
|
_dirty = false;
|
|
|
|
}
|
|
|
|
|
2007-12-18 05:09:02 +01:00
|
|
|
static int
|
|
|
|
instrument_filter ( const struct dirent *d )
|
|
|
|
{
|
|
|
|
char suffix[] = "*.inst";
|
|
|
|
|
|
|
|
return 0 == fnmatch( suffix, d->d_name, 0 );
|
|
|
|
}
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
static
|
|
|
|
list <string> *
|
|
|
|
get_listing( const char *dir )
|
2007-12-18 05:09:02 +01:00
|
|
|
{
|
2008-02-12 01:22:04 +01:00
|
|
|
list <string> *sl = new list <string>;
|
2007-12-18 05:09:02 +01:00
|
|
|
|
|
|
|
struct dirent **names;
|
|
|
|
int n;
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
if ( 0 > ( n = scandir( dir, &names, instrument_filter, alphasort ) ) )
|
2007-12-18 05:09:02 +01:00
|
|
|
{
|
|
|
|
WARNING( "couldn't open instrument directory" );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (n--)
|
|
|
|
{
|
|
|
|
char *c = rindex( names[n]->d_name, '.' );
|
|
|
|
|
|
|
|
if ( c )
|
|
|
|
*c = '\0';
|
|
|
|
|
|
|
|
MESSAGE( "found instrument: %s", names[n]->d_name );
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
string s( names[n]->d_name );
|
|
|
|
|
|
|
|
sl->push_back( s );
|
2007-12-18 05:09:02 +01:00
|
|
|
|
|
|
|
free( names[n] );
|
|
|
|
}
|
|
|
|
free( names );
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
return sl;
|
2007-12-18 05:09:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
/* Returns a list of available instruments */
|
|
|
|
char **
|
|
|
|
Instrument::listing ( void )
|
|
|
|
{
|
|
|
|
list <string> *sys = get_listing( SYSTEM_PATH INSTRUMENT_DIR );
|
|
|
|
list <string> *usr = get_listing( config.user_config_dir );
|
|
|
|
|
2008-02-23 01:37:46 +01:00
|
|
|
if ( ! ( usr || sys ) )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ( sys && usr )
|
|
|
|
usr->merge( *sys );
|
|
|
|
else
|
|
|
|
if ( sys && ! usr )
|
|
|
|
usr = sys;
|
|
|
|
|
2008-02-12 01:22:04 +01:00
|
|
|
usr->unique();
|
|
|
|
|
|
|
|
usr->sort();
|
|
|
|
|
2008-02-23 01:37:46 +01:00
|
|
|
if ( sys )
|
|
|
|
delete sys;
|
2008-02-12 01:22:04 +01:00
|
|
|
|
2008-02-13 02:37:26 +01:00
|
|
|
char **sa = (char**)malloc( (usr->size() + 1) * sizeof( char * ) );
|
2008-02-12 01:22:04 +01:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for ( list <string>::iterator s = usr->begin(); s != usr->end(); s++, i++ )
|
|
|
|
sa[i] = strdup( s->c_str() );
|
|
|
|
|
|
|
|
sa[i] = NULL;
|
|
|
|
|
|
|
|
delete usr;
|
|
|
|
|
|
|
|
return sa;
|
|
|
|
}
|
|
|
|
|
2007-12-18 05:09:02 +01:00
|
|
|
const char *
|
|
|
|
Instrument::name ( void ) const
|
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|