Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
6.6KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include "instrument.H"
  22. #include "common.h"
  23. #include "const.h"
  24. #include "config.h"
  25. #include "non.H"
  26. #include <fnmatch.h>
  27. #include <dirent.h>
  28. #include <list>
  29. #include <string>
  30. using std::list;
  31. using std::string;
  32. /******
  33. Instrument definition file format is thus:
  34. "Name", n, v
  35. Where /n/ is a note number from 0 to 127 and /v/ is a percentage of
  36. volume.
  37. When a system installed instrument definition is modified, the
  38. modified version is saved in the user's $HOME. Therefore, when
  39. loading instruments, user defined instruments always hide system
  40. defined instruments of the same name.
  41. */
  42. list <Instrument *> Instrument::instruments;
  43. Instrument::Instrument ( const char *name )
  44. {
  45. for ( int i = 0; i < 128; i++ )
  46. {
  47. _map[i].name = NULL;
  48. _map[i].velocity = 100;
  49. }
  50. if ( name )
  51. {
  52. _name = strdup( name );
  53. read( name );
  54. }
  55. else
  56. {
  57. _name = strdup( "not an instrument" );
  58. _height = 0;
  59. }
  60. Instrument::instruments.push_front( this );
  61. }
  62. Instrument *
  63. Instrument::open ( const char *name )
  64. {
  65. list <Instrument *>::iterator i = Instrument::instruments.begin();
  66. if ( name )
  67. {
  68. for ( ; i != Instrument::instruments.end(); i++ )
  69. if ( 0 == strcmp( (*i)->_name, name ) )
  70. return *i;
  71. }
  72. return new Instrument ( name );
  73. }
  74. void
  75. Instrument::note_name ( int n, char *s )
  76. {
  77. if ( _map[ n ].name )
  78. free( _map[ n ].name );
  79. _map[ n ].name = s;
  80. _dirty = true;
  81. }
  82. void
  83. Instrument::velocity ( int n, int v )
  84. {
  85. _map[ n ].velocity = v;
  86. _dirty = true;
  87. }
  88. /* Should only be passed NOTE ON/OFF events! */
  89. void
  90. Instrument::translate ( midievent *e ) const
  91. {
  92. e->note_velocity( e->note_velocity() * _map[ e->note() ].velocity / 100 );
  93. }
  94. const char *
  95. Instrument::note_name ( int n ) const
  96. {
  97. return _map[ n ].name;
  98. }
  99. int
  100. Instrument::height ( void ) const
  101. {
  102. return _height;
  103. }
  104. int
  105. Instrument::velocity ( int n ) const
  106. {
  107. return _map[ n ].velocity;
  108. }
  109. bool
  110. Instrument::read ( const char *s )
  111. {
  112. FILE *fp;
  113. char pat[512];
  114. sprintf( pat, "%s%s.inst", config.user_config_dir, s );
  115. if ( ! ( fp = fopen( pat, "r" ) ) )
  116. {
  117. sprintf( pat, "%s%s.inst", SYSTEM_PATH INSTRUMENT_DIR, s );
  118. if ( ! ( fp = fopen( pat, "r" ) ) )
  119. return false;
  120. }
  121. struct i_map m;
  122. char namebuf[256];
  123. int note, velocity;
  124. int n = 0;
  125. while ( 0 < fscanf( fp, "\"%[^\"]\", %d, %d\n", (char*)&namebuf, &note, &velocity ) ) n++;
  126. rewind( fp );
  127. MESSAGE( "reading %d lines from instrument file \"%s\"", n, s );
  128. int i;
  129. for ( i = 0; i < n; i++ )
  130. {
  131. fscanf( fp, "\"%[^\"]\", %d, %d\n", (char *)&namebuf, &note, &velocity );
  132. m.name = strdup( namebuf );
  133. if ( velocity > 100 )
  134. {
  135. WARNING( "invalid volume percentage in instrument definition");
  136. m.velocity = 100;
  137. }
  138. else
  139. m.velocity = velocity;
  140. DEBUG( "name: \"%s\", note: %d, velocity: %d%%", m.name, note, m.velocity );
  141. _map[ note ] = m;
  142. }
  143. _height = n;
  144. fclose( fp );
  145. return true;
  146. }
  147. bool
  148. Instrument::write ( const char *s ) const
  149. {
  150. FILE *fp;
  151. char pat[512];
  152. sprintf( pat, "%s/%s.inst", config.user_config_dir, s );
  153. if ( ! ( fp = fopen( pat, "w" ) ) )
  154. return false;
  155. int n = 0;
  156. for ( int i = 0; i < 127; ++i )
  157. {
  158. if ( _map[ i ].name )
  159. {
  160. fprintf( fp, "\"%s\", %d, %d\n", _map[ i ].name, i, _map[ i ].velocity );
  161. ++n;
  162. }
  163. }
  164. DEBUG( "wrote %d lines to instrument file \"%s\"", n, pat );
  165. fclose( fp );
  166. return true;
  167. }
  168. void
  169. Instrument::save ( void ) const
  170. {
  171. if ( _dirty )
  172. write( _name );
  173. _dirty = false;
  174. }
  175. static int
  176. instrument_filter ( const struct dirent *d )
  177. {
  178. char suffix[] = "*.inst";
  179. return 0 == fnmatch( suffix, d->d_name, 0 );
  180. }
  181. static
  182. list <string> *
  183. get_listing( const char *dir )
  184. {
  185. list <string> *sl = new list <string>;
  186. struct dirent **names;
  187. int n;
  188. if ( 0 > ( n = scandir( dir, &names, instrument_filter, alphasort ) ) )
  189. {
  190. WARNING( "couldn't open instrument directory" );
  191. return NULL;
  192. }
  193. else
  194. {
  195. while (n--)
  196. {
  197. char *c = rindex( names[n]->d_name, '.' );
  198. if ( c )
  199. *c = '\0';
  200. MESSAGE( "found instrument: %s", names[n]->d_name );
  201. string s( names[n]->d_name );
  202. sl->push_back( s );
  203. free( names[n] );
  204. }
  205. free( names );
  206. return sl;
  207. }
  208. }
  209. /* Returns a list of available instruments */
  210. char **
  211. Instrument::listing ( void )
  212. {
  213. list <string> *sys = get_listing( SYSTEM_PATH INSTRUMENT_DIR );
  214. list <string> *usr = get_listing( config.user_config_dir );
  215. usr->merge( *sys );
  216. usr->unique();
  217. usr->sort();
  218. delete sys;
  219. char **sa = (char**)malloc( usr->size() * sizeof( char * ) + 1 );
  220. int i = 0;
  221. for ( list <string>::iterator s = usr->begin(); s != usr->end(); s++, i++ )
  222. sa[i] = strdup( s->c_str() );
  223. sa[i] = NULL;
  224. delete usr;
  225. return sa;
  226. }
  227. const char *
  228. Instrument::name ( void ) const
  229. {
  230. return _name;
  231. }