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.

218 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 "scale.H"
  19. #include "common.h"
  20. #include "stdlib.h"
  21. #include <MIDI/midievent.H>
  22. using namespace MIDI;
  23. /* Define some scales. These don't really need to be stored on
  24. disk. Scales don't change that often. */
  25. const char *Scale::chromatic_names[] =
  26. /* { "A", */
  27. /* "A#/Bb", */
  28. /* "B", */
  29. /* "C", */
  30. /* "C#/Db", */
  31. /* "D", */
  32. /* "D#/Eb", */
  33. /* "E", */
  34. /* "F", */
  35. /* "F#/Gb", */
  36. /* "G", */
  37. /* "G#/Ab" }; */
  38. /* { "C", */
  39. /* "C#/Db", */
  40. /* "D", */
  41. /* "D#/Eb", */
  42. /* "E", */
  43. /* "F", */
  44. /* "F#/Gb", */
  45. /* "G", */
  46. /* "G#/Ab", */
  47. /* "A", */
  48. /* "A#/Bb", */
  49. /* "B" }; */
  50. { "C",
  51. "C#",
  52. "D",
  53. "D#",
  54. "E",
  55. "F",
  56. "F#",
  57. "G",
  58. "G#",
  59. "A",
  60. "A#",
  61. "B" };
  62. Scale Scale::builtin_scales[] = {
  63. { "Major",
  64. 7,
  65. { 0, 2, 4, 5, 7, 9, 11 } },
  66. { "Natural Minor",
  67. 7,
  68. { 0, 2, 3, 5, 7, 8, 10 } },
  69. { "Harmonic Minor",
  70. 7,
  71. { 0, 2, 3, 5, 7, 8, 11 } },
  72. { "Melodic Minor",
  73. 7,
  74. { 0, 2, 3, 5, 7, 9, 11 } },
  75. { "Major Pentatonic",
  76. 5,
  77. { 0, 2, 4, 7, 9 } },
  78. { "Minor Pentatonic",
  79. 5,
  80. { 0, 3, 5, 7, 10 } },
  81. { "Chromatic",
  82. 12,
  83. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 } }
  84. };
  85. /* const Scale builtin_chords[] = { */
  86. /* { "Major Triad", */
  87. /* { 0, 4, 7, -1 } }, */
  88. /* { "Major Sixth", */
  89. /* { 0, 4, 7, 9, -1 } }, */
  90. /* { "Major Seventh", */
  91. /* { 0, 4, 7, 11, -1 } }, */
  92. /* { "Major Ninth", */
  93. /* { 0, 4, 7, 11, 14, -1 } }, */
  94. /* { "Major 6/9", */
  95. /* { 0, 4, 7, 9, 14, -1 } }, */
  96. /* { "Major Eleventh", */
  97. /* { 0, 4, 7, 11, 14, 17, -1 } }, */
  98. /* { "Major Thirteenth", */
  99. /* { 0, 4, 7, 11, 14, 17, 21, -1 } }, */
  100. /* { "Minor Triad", */
  101. /* { 0, 3, 7, -1 } }, */
  102. /* { "Minor Sixth", */
  103. /* { 0, 3, 7, 9, -1 } }, */
  104. /* { "Minor Seventh", */
  105. /* { 0, 3, 7, 10, -1 } }, */
  106. /* { "Minor Ninth", */
  107. /* { 0, 3, 7, 10, 14, -1 } }, */
  108. /* { "Minor 6/9", */
  109. /* { 0, 3, 7, 9, 14, -1 } }, */
  110. /* { "Minor Eleventh", */
  111. /* { 0, 3, 7, 10, 14, 17, -1 } }, */
  112. /* { "Minor Thirteenth", */
  113. /* { 0, 3, 7, 10, 14, 17, 21, -1 } }, */
  114. /* { "Augmented Triad", */
  115. /* { 0, 4, 8, -1 } }, */
  116. /* { "Diminished Triad", */
  117. /* { 0, 3, 6, -1 } } */
  118. /* }; */
  119. /* "Opens" a scale for compatibility with Instrument class */
  120. Scale *
  121. Scale::open ( const char *name )
  122. {
  123. for ( int i = elementsof( builtin_scales ) - 1; i >= 0; --i )
  124. if ( 0 == strcmp( name, builtin_scales[i]._name ) )
  125. return &builtin_scales[i];
  126. return NULL;
  127. }
  128. /* Returns a list of available scales */
  129. char **
  130. Scale::listing ( void )
  131. {
  132. char **sa;
  133. int n = elementsof( builtin_scales );
  134. sa = (char **)malloc( sizeof ( char * ) * ( n + 1 ) );
  135. sa[n] = NULL;
  136. while ( n-- )
  137. sa[n] = strdup( builtin_scales[n]._name );
  138. return sa;
  139. }
  140. const char *
  141. Scale::chromatic_name ( int n )
  142. {
  143. return chromatic_names[ n % 12 ];
  144. }
  145. int
  146. Scale::octave ( int n )
  147. {
  148. return n / 12;
  149. }
  150. int
  151. Scale::_degree ( int k, int n ) const
  152. {
  153. return k + _degrees[ n % _notes ];
  154. }
  155. /* translate NOTE event. Behavior is undefined for other event types */
  156. bool
  157. Scale::translate ( int k, midievent *e ) const
  158. {
  159. if ( ! note_name( k, e->note() ) )
  160. return false;
  161. else
  162. return true;
  163. }
  164. const char *
  165. Scale::note_name ( int k, int n ) const
  166. {
  167. /* all the magic is here */
  168. static char s[5];
  169. const int mod_n = n % 12;
  170. // FIXME: searching is not efficient!
  171. for ( int i = _notes; i-- ; )
  172. if ( mod_n == (_degrees[ i ] + k) % 12 )
  173. {
  174. snprintf( s, sizeof(s), "%s%i",
  175. chromatic_names[ mod_n ],
  176. n / 12 );
  177. return s;
  178. }
  179. return NULL;
  180. }
  181. const char *
  182. Scale::name ( void ) const
  183. {
  184. return _name;
  185. }