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.

227 lines
5.1KB

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