Writing a good melody is difficult. Writing a pair, a trio, a quartet of melodies that can be played all at the same time to good effect is astronomically difficult. One has to observe several different rules, many of which are quite restrictive, such as avoiding parallel Perfect Fifths and handling dissonances properly. Yesterday, I sat down to a project that I had long wanted to start. With the help of Google Gemini, I began writing the code for a computer program that turns what is astronomically difficult into a quick and simple matter. The program is now more or less entirely complete. What does the program do? It takes as input a folder full of MIDI files, each containing a melodic passage and, in a matter of seconds, finds potentially dozens of superimpositions of these melodies that follow the rules of good counterpoint. Different combinations of these melodies, along with simple transformations of these melodies (i.e. time-delayed versions, transposed versions, sped-up versions, and slowed-down versions) are quickly tested, one after another, and those that pass are added to a pool for the user to listen to and export for use in larger works. The user can also force the program to try combining melodies with one specific melody (possibly including itself) rather than combining them entirely at random. All of this makes the program ideally suited for writing compositions that involve not only counterpoint but extensive imitation (imitation means the repetition in one voice/instrument of a melodic idea that appears in another voice/instrument), such as appears extensively in that venerable type of music called the fugue. Right now, my task is simply to compose as many different pleasant-sounding melodic passages as possible, so that I can feed these to the program and have it superimpose them in ways that I never would have come up with without its assistance. For those interested, the code makes use of a genetic algorithm and an artificial intelligence that learns how to avoid melodic combinations that are likely to fail, based on prior experience, speeding up the program's workings and improving the quality of its output. Here is a screenshot of a very short and simple passage that this program wove out of three motifs. The harmony in this passage is almost entirely two-voice, but the program can write passages that are arbitrarily dense, up to the point that it would be theoretically impossible to thicken the musical texture without breaking the rules of counterpoint. It can also write much longer passages, although listening through them would become increasingly time consuming as they got lengthened. Much better to write short passages and weave them together within a larger work. (You'll notice that there is one perfect consonance by similar motion. I decided to permit this because it isn't strictly forbidden, and in some cases is unavoidable. The program is coded only to rule out completely those passages which break the hard rules, such as ones that include parallel Octaves and unacceptable handling of dissonance. If the program writes a passage that breaks a suggestion rather than a hard rule, and the user doesn't like this, the passage may simply be discarded. I kept this one because I like how it sounds.)