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.

260 lines
5.8KB

  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. /******
  29. Instrument definition file format is thus:
  30. "Name", n, v
  31. Where /n/ is a note number from 0 to 127 and /v/ is a percentage of
  32. volume.
  33. When a system installed instrument definition is modified, the
  34. modified version is saved in the user's $HOME. Therefore, when
  35. loading instruments, user defined instruments always hide system
  36. defined instruments of the same name.
  37. */
  38. list <Instrument *> Instrument::instruments;
  39. Instrument::Instrument ( const char *name )
  40. {
  41. for ( int i = 0; i < 128; i++ )
  42. {
  43. _map[i].name = NULL;
  44. _map[i].velocity = 100;
  45. }
  46. if ( name )
  47. {
  48. _name = strdup( name );
  49. read( name );
  50. }
  51. else
  52. {
  53. _name = strdup( "not an instrument" );
  54. _height = 0;
  55. }
  56. Instrument::instruments.push_front( this );
  57. }
  58. Instrument *
  59. Instrument::open ( const char *name )
  60. {
  61. list <Instrument *>::iterator i = Instrument::instruments.begin();
  62. if ( name )
  63. {
  64. for ( ; i != Instrument::instruments.end(); i++ )
  65. if ( 0 == strcmp( (*i)->_name, name ) )
  66. return *i;
  67. }
  68. return new Instrument ( name );
  69. }
  70. void
  71. Instrument::note_name ( int n, char *s )
  72. {
  73. if ( _map[ n ].name )
  74. free( _map[ n ].name );
  75. _map[ n ].name = s;
  76. }
  77. void
  78. Instrument::velocity ( int n, int v )
  79. {
  80. _map[ n ].velocity = v;
  81. }
  82. /* Should only be passed NOTE ON/OFF events! */
  83. void
  84. Instrument::translate ( midievent *e ) const
  85. {
  86. e->note_velocity( e->note_velocity() * _map[ e->note() ].velocity / 100 );
  87. }
  88. const char *
  89. Instrument::note_name ( int n ) const
  90. {
  91. return _map[ n ].name;
  92. }
  93. int
  94. Instrument::height ( void ) const
  95. {
  96. return _height;
  97. }
  98. int
  99. Instrument::velocity ( int n ) const
  100. {
  101. return _map[ n ].velocity;
  102. }
  103. bool
  104. Instrument::read ( const char *s )
  105. {
  106. FILE *fp;
  107. char pat[512];
  108. sprintf( pat, "%s%s.inst", SYSTEM_PATH INSTRUMENT_DIR, s );
  109. if ( ! ( fp = fopen( pat, "r" ) ) )
  110. return false;
  111. struct i_map m;
  112. char namebuf[256];
  113. int note, velocity;
  114. int n = 0;
  115. while ( 0 < fscanf( fp, "\"%[^\"]\", %d, %d\n", (char*)&namebuf, &note, &velocity ) ) n++;
  116. rewind( fp );
  117. MESSAGE( "reading %d lines from instrument file \"%s\"", n, s );
  118. int i;
  119. for ( i = 0; i < n; i++ )
  120. {
  121. fscanf( fp, "\"%[^\"]\", %d, %d\n", (char *)&namebuf, &note, &velocity );
  122. m.name = strdup( namebuf );
  123. if ( velocity > 100 )
  124. {
  125. WARNING( "invalid volume percentage in instrument definition");
  126. m.velocity = 100;
  127. }
  128. else
  129. m.velocity = velocity;
  130. DEBUG( "name: \"%s\", note: %d, velocity: %d%%", m.name, note, m.velocity );
  131. _map[ note ] = m;
  132. }
  133. _height = n;
  134. fclose( fp );
  135. return true;
  136. }
  137. bool
  138. Instrument::write ( const char *s ) const
  139. {
  140. FILE *fp;
  141. char pat[512];
  142. sprintf( pat, "%s/%s%s.inst", config.user_config_dir, INSTRUMENT_DIR, s );
  143. if ( ! ( fp = fopen( pat, "w" ) ) )
  144. return false;
  145. int n = 0;
  146. for ( int i = 0; i < 127; ++i )
  147. {
  148. if ( _map[ i ].name )
  149. {
  150. fprintf( fp, "\"%s\", %d, %d\n", _map[ i ].name, i, _map[ i ].velocity );
  151. ++n;
  152. }
  153. }
  154. DEBUG( "wrote %d lines to instrument file \"%s\"", n, pat );
  155. fclose( fp );
  156. return true;
  157. }
  158. static int
  159. instrument_filter ( const struct dirent *d )
  160. {
  161. char suffix[] = "*.inst";
  162. return 0 == fnmatch( suffix, d->d_name, 0 );
  163. }
  164. /* Returns a list of available instruments */
  165. char **
  166. Instrument::listing ( void )
  167. {
  168. char **sa;
  169. struct dirent **names;
  170. int n;
  171. if ( 0 > ( n = scandir( SYSTEM_PATH INSTRUMENT_DIR, &names, instrument_filter, alphasort ) ) )
  172. {
  173. WARNING( "couldn't open instrument directory" );
  174. return NULL;
  175. }
  176. else
  177. {
  178. sa = (char **)malloc( sizeof( char * ) * (n + 1) );
  179. sa[n] = NULL;
  180. while (n--)
  181. {
  182. char *c = rindex( names[n]->d_name, '.' );
  183. if ( c )
  184. *c = '\0';
  185. MESSAGE( "found instrument: %s", names[n]->d_name );
  186. sa[n] = strdup( names[n]->d_name );
  187. free( names[n] );
  188. }
  189. free( names );
  190. return sa;
  191. }
  192. }
  193. const char *
  194. Instrument::name ( void ) const
  195. {
  196. return _name;
  197. }