Musical form dictates which themes (melodies) are placed where in the music, what key the themes are in, when they are repeated and whether the composer has a section in which she or he is given free reign to do musical gymnastics with pieces of the themes.
The two forms that are important to this discussion are theme and variation and sonata form. These two forms give the composer a lot of latitude. In theme and variation form, the theme is stated (played) and then followed by variations of the theme. These variations can be created by many different methods including stretching it, compressing it, changing the harmony, ornamenting the melody (with sonic curlicues) and if the theme was in a major (happy/bright) key, the variation could be in a minor (sad/dark) key or vice-versa.
In sonata form, a main theme is stated, followed by a secondary theme. Then comes the development (musical gymnastics) section where the composer takes material (melodic pieces) from the themes and varies this material, stretching it, compressing it, flipping it upside-down, turning it backward and taking it through various keys both major (happy/bright) and minor (sad/dark). Then we are brought home again with the restatement of the themes.
So if you are hearing “the same group of notes being played, but on a different scale, and so it can sound dark/sad one way, and happy another way”, chances are the music is written in one of the two forms outlined above.
©2016 Stephen L. Martin