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.

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