title: Transposing music notes by scale indices
body: <p>I'm trying to create an algorithm that transposes musical notes up or down according to their given scale and transpose factor.</p>
  
  <p><strong>Current Algorithm:</strong></p>
  
  <pre><code>public Note getNoteByScale(Scale scale, int transposeFactor) {
      int newPitch = scale.getScaleIndex(this.pitch) + transposeFactor;
      int newOctave = this.octave;
  
      if (newPitch &gt; 6) {
          newPitch -= 7;
          newOctave++;
      }
      if (newPitch &lt; 0) {
          newPitch += 7;
      }
  
      return Note.createNote(scale.getNotesInScale().get(newPitch),
              newOctave, this.duration);
  }
  </code></pre>
  
  <p>Every scale (denoted as a major scale for this purpose) has 7 notes. The C-Major Scale for example has the notes:</p>
  
  <pre><code>C, D, E, F, G, A, and B
  </code></pre>
  
  <p>If you are to use this scale with the algorithm above to transpose notes such as, for example, a 'B' note on an octave of 5 one up in the C-Major Scale, the algorithm would work as intended and return the note 'C' (index 0 of the scale) on an octave of 6.</p>
  
  <p>However, say that we used the D-Major Scale which consists of the notes</p>
  
  <pre><code>D, E, F♯, G, A, B, and C♯
  </code></pre>
  
  <p>For this scale, the last note 'C♯' is supposed to be considered an octave higher than the rest of the notes in the scale. If we were to use the algorithm above to transpose the note 'B' (index 6) on an octave of 5 one up, for example, the algorithm would actually give the note 'C♯' on an octave of 5 which is completely wrong: it should be on the octave of 6.</p>
  
  <pre><code>Note original = Note.createNote(Pitch.B, 5, Duration.WHOLE);
  // Prints: [Note: [pitch: C#],[octave: 5]]
  System.out.println(original.getNoteByScale(Scale.D_MAJOR, 1)); 
  </code></pre>
  
  <p>Is there any way to fix the algorithm above so that it would support such cases as the above?</p>
  
  <p>The Java classes which I am using for the <code>Note</code> and <code>Pitch</code> and <code>Scale</code> class can be found here: <a href="https://code.google.com/p/melodycomposition/source/browse/trunk/melodycomposition/#melodycomposition%2Fclient%2Fsrc%2Fmain%2Fjava%2Fnl%2Fjamiecraane%2Fmelodygeneration" rel="nofollow noreferrer">Jamie Craane Melody Generation - GoogleCode</a></p>