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.

327 lines
6.9KB

  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. /** Translate event, should only be passed NOTE ON/OFF events, returns
  89. true if note is valid for this mapping */
  90. bool
  91. Instrument::translate ( midievent *e ) const
  92. {
  93. if ( ! note_name( e->note() ) )
  94. return false;
  95. e->note_velocity( e->note_velocity() * _map[ e->note() ].velocity / 100 );
  96. return true;
  97. }
  98. const char *
  99. Instrument::note_name ( int n ) const
  100. {
  101. return _map[ n ].name;
  102. }
  103. int
  104. Instrument::height ( void ) const
  105. {
  106. return _height;
  107. }
  108. int
  109. Instrument::velocity ( int n ) const
  110. {
  111. return _map[ n ].velocity;
  112. }
  113. bool
  114. Instrument::read ( const char *s )
  115. {
  116. FILE *fp;
  117. char pat[512];
  118. sprintf( pat, "%s%s.inst", config.user_config_dir, s );
  119. if ( ! ( fp = fopen( pat, "r" ) ) )
  120. {
  121. sprintf( pat, "%s%s.inst", SYSTEM_PATH INSTRUMENT_DIR, s );
  122. if ( ! ( fp = fopen( pat, "r" ) ) )
  123. return false;
  124. }
  125. struct i_map m;
  126. char namebuf[256];
  127. int note, velocity;
  128. int n = 0;
  129. while ( 0 < fscanf( fp, "\"%[^\"]\", %d, %d\n", (char*)&namebuf, &note, &velocity ) ) n++;
  130. rewind( fp );
  131. MESSAGE( "reading %d lines from instrument file \"%s\"", n, s );
  132. int i;
  133. for ( i = 0; i < n; i++ )
  134. {
  135. fscanf( fp, "\"%[^\"]\", %d, %d\n", (char *)&namebuf, &note, &velocity );
  136. m.name = strdup( namebuf );
  137. if ( velocity > 100 )
  138. {
  139. WARNING( "invalid volume percentage in instrument definition");
  140. m.velocity = 100;
  141. }
  142. else
  143. m.velocity = velocity;
  144. DMESSAGE( "name: \"%s\", note: %d, velocity: %d%%", m.name, note, m.velocity );
  145. _map[ note ] = m;
  146. }
  147. _height = n;
  148. fclose( fp );
  149. return true;
  150. }
  151. bool
  152. Instrument::write ( const char *s ) const
  153. {
  154. FILE *fp;
  155. char pat[512];
  156. sprintf( pat, "%s/%s.inst", config.user_config_dir, s );
  157. if ( ! ( fp = fopen( pat, "w" ) ) )
  158. return false;
  159. int n = 0;
  160. for ( int i = 0; i < 127; ++i )
  161. {
  162. if ( _map[ i ].name )
  163. {
  164. fprintf( fp, "\"%s\", %d, %d\n", _map[ i ].name, i, _map[ i ].velocity );
  165. ++n;
  166. }
  167. }
  168. DMESSAGE( "wrote %d lines to instrument file \"%s\"", n, pat );
  169. fclose( fp );
  170. return true;
  171. }
  172. void
  173. Instrument::save ( void ) const
  174. {
  175. if ( _dirty )
  176. write( _name );
  177. _dirty = false;
  178. }
  179. static int
  180. instrument_filter ( const struct dirent *d )
  181. {
  182. char suffix[] = "*.inst";
  183. return 0 == fnmatch( suffix, d->d_name, 0 );
  184. }
  185. static
  186. list <string> *
  187. get_listing( const char *dir )
  188. {
  189. list <string> *sl = new list <string>;
  190. struct dirent **names;
  191. int n;
  192. if ( 0 > ( n = scandir( dir, &names, instrument_filter, alphasort ) ) )
  193. {
  194. WARNING( "couldn't open instrument directory" );
  195. return NULL;
  196. }
  197. else
  198. {
  199. while (n--)
  200. {
  201. char *c = rindex( names[n]->d_name, '.' );
  202. if ( c )
  203. *c = '\0';
  204. MESSAGE( "found instrument: %s", names[n]->d_name );
  205. string s( names[n]->d_name );
  206. sl->push_back( s );
  207. free( names[n] );
  208. }
  209. free( names );
  210. return sl;
  211. }
  212. }
  213. /* Returns a list of available instruments */
  214. char **
  215. Instrument::listing ( void )
  216. {
  217. list <string> *sys = get_listing( SYSTEM_PATH INSTRUMENT_DIR );
  218. list <string> *usr = get_listing( config.user_config_dir );
  219. if ( ! ( usr || sys ) )
  220. return NULL;
  221. if ( sys && usr )
  222. usr->merge( *sys );
  223. else
  224. if ( sys && ! usr )
  225. usr = sys;
  226. usr->unique();
  227. usr->sort();
  228. if ( sys )
  229. delete sys;
  230. char **sa = (char**)malloc( (usr->size() + 1) * sizeof( char * ) );
  231. int i = 0;
  232. for ( list <string>::iterator s = usr->begin(); s != usr->end(); s++, i++ )
  233. sa[i] = strdup( s->c_str() );
  234. sa[i] = NULL;
  235. delete usr;
  236. return sa;
  237. }
  238. const char *
  239. Instrument::name ( void ) const
  240. {
  241. return _name;
  242. }