Originally posted bySenekis93:(ex. the bass, which often comes out sounding like a fart =_=)Haha, so true.

You can also mess with the speed value at the top. Well, you can mess with pretty much everything, that’s what makes it so beautiful.

Nice job, Metabble.

Thanks. Although, I didn’t do much. Also, if anyone wants to change the key of the scale and octave in code, I think I know how. Observe how the frequency value of the piano and bass are MIDDLE_C. It’s declared as

MIDDLE_C = 220.0 * (2.0 ** (3.0 / 12.0))

in the BOOTSTRAP area. I’m not big on music theory, but I seem to have gotten a grasp of this formula. Correct me if I’m wrong, since before now I didn’t even know an octave was double the Hz of its predecessor. =_=

Simply, the way I see it in my mind, it’s the starting hertz of the octave times the twelfth root of two to the power of the note you want within the octave. Essentially, since an octave is double the hertz of the one below it, we’re splitting this increase into twelve parts (twelfth root of two, 2^[1/12]) and multiplying for every note above *A* we want. Of course, there’s no need to use powers when we can change the amount that we divide twelve into…

We represent the octave and key we want as:

hZ * (2.0 ** (sT / 12.0))

Let hZ be the starting hertz of the octave, and sT be the number of semitones away from the key belonging to that hertz (*A*).

220 Hz is the middle octave (that’s why it’s used for middle c) so halve it for every octave down you want to go, and double it for every octave up.

Semitones away from *A* is self explanatory.

Simple edition with tables to easily change the key and octave, first go to this line:

MIDDLE_C = 220.0 * (2.0 ** (3.0 / 12.0))

Replace it with this line:

MIDDLE_C = Hz * (2.0 ** (sT / 12.0))

where *Hz* is one of the values below, corresponding to the octave you want…

Middle+2 = 880 Hz Middle+1 = 440 Hz Middle = 220 Hz Middle-1 = 110 Hz Middle-2 = 55 Hz

..and *sT* is the number below belonging to the key you want.

A = 0 A#/Bb = 1 B = 2 C = 3 C#/Db = 4 D = 5 D#/Eb = 6 E = 7 F = 8 F#/Gb = 9 G = 10 G#/Ab = 11 A = 12 (octave higher, just double hertz and make it 0)

And you’re done. Keep in mind that this is misleading since MIDDLE_C is, well, no longer middle c. You can always define separate frequencies and change which one is used where the samples are generated (they’re the same lines as the ones I altered in my previous post). Also note that the scale, major or minor, is decided randomly, with a 50% chance of each being chosen, every time a tune is generated. To control that, too, you can change this line:

strat = Strategy_Main(random.randint(50,50+12-1)+12, Key_Minor if random.random() < 0.6 else Key_Major, 128, 32)

by replacing the second argument with Key_Minor or Key_Major.

Whew. That was a lot of work. Again, I’m not much into music theory so if I’ve gotten anything wrong, feel free to correct. :)

Also, to expand on this, you could define multiple octave/key combinations (maybe put them in an array) and then use randint to determine which one to use. That way, you can randomly generate the tune, instruments, base octave *and* key. xD