📜 ⬆️ ⬇️

Infinite algorithmic melody based on prime numbers

image

Hi, Habr! In the last article, “an endless pattern on the basis of simple numbers,” I told about an algorithm that allows you to generate endless beautiful patterns, similar either to alien drawings, or to something technological, like a chip device. However, the algorithm for generating 2D patterns can also be used to create melodies. More under the cut.

Melody generation algorithm


To create melodies, I made a simple square wave generator with a fading signal. In this case, you can impose several generators of the meander on each other. Next, we set the periods of the meander generators in accordance with the frequencies of the notes, and set the amplitude of the generators depending on the presence of a “clamped” key. I used the pixel value of a 2D image taken from a vertical column that can be moved around the image as a signal to press a key. The column contains 14 pixels, which corresponds to 7 notes of the 2nd and 3rd octaves. The key has only two states - pressed or not.

The meander generator and functions for saving the result in a WAV file I implemented as a separate library for working with sound. She is here .

The program for generating music can be viewed here . It is called simple_sound. Ready exe can be found in the archive. At the output, the program creates a WAV file “example”.
')

Melody examples


One of the most interesting melodies, example_dx1000_dy1000_x2018_y8 . The title of the melody indicates the size of the shift in the X and Y coordinates, as well as the initial coordinates. The rest of the tunes:

example_dx_dy_x5678_y7768
example_dx_dy_y776897123
example_y47
example

In theory, you can modify the algorithm and use it for primitive games so that the melodies in the game never repeat.

Source: https://habr.com/ru/post/420225/


All Articles