Randomly Generated Algorithmic Music
This was my first algorithmic music. It's actually more complicated than it sounds. One full randomly generated score uses all 12 keys of the major scale. There is no chord progression saved into the algorithm which probably could make the algorithm sound better if I did. It's simply 1 chord and Ionian Playing over those 1 chords. It also sticks to a majority of consonant intervals with a few dissonant intervals added.
Here is the code I made. I did use someone else's code but the majority of it is my own.
cmajorscale = {0,2,4,5,7,9,11}
gmajorscale = {7,9,11,0,2,4,6}
dmajorscale = {2,4,6,7,9,11,1}
amajorscale = {9,11,1,2,4,6,8}
emajorscale = {4,6,8,9,11,1,3}
bmajorscale = {11,1,3,4,6,8,10}
fsharpmajorscale = {6,8,10,11,1,3,5}
csharpmajorscale = {1,3,5,6,8,10,0}
gsharpmajorscale = {8,10,0,1,3,5,6,7}
eflatmajorscale = {3,5,6,7,8,10,2}
bflatmajorscale = {10,2,3,5,6,7,9}
fmajorscale = {5,6,7,9,10,2,4}
function playscales(sfxnum,scale)
local rand = 1
srand(s)
local rand2
local shift = 12
local rand3 = 1
local rand4 = 7
local arpeggio = 1
local vol
local tempo = 20
vol = 1
for t=0,31 do
rand2 = rnd()
rand = flr(rnd(3))+1
if rand2 > .5 then
if(t > 0) then
if(arpeggio == 1) then
arpeggio = 3
shift = 12
elseif arpeggio == 3 then
arpeggio = 5
shift = 12
elseif arpeggio == 5 then
arpeggio = 7
shift = 12
elseif arpeggio == 7 then
arpeggio = 1
shift = 12
end
else arpeggio = 1
end
if(scale == 1) then
note=make_note(cmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 2) then
note=make_note(gmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 3) then
note=make_note(dmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 4) then
note=make_note(amajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 5) then
note=make_note(emajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 6) then
note=make_note(bmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 7) then
note=make_note(fsharpmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 8) then
note=make_note(csharpmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 9) then
note=make_note(gsharpmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 10) then
note=make_note(eflatmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 11) then
note=make_note(bflatmajorscale[arpeggio]+shift-12,rand4,vol,0)
elseif(scale == 12) then
note=make_note(fmajorscale[arpeggio]+shift-12,rand4,vol,0)
end
set_note(sfxnum+26,t,note)
set_speed(sfxnum+26,tempo)
else
t += 1
end
end
vol = 1
for t=0,31 do
rand2 = rnd()
rand = flr(rnd(3))+1
if(rand2 > .5) then
if(t > 0) then
if rand3 == 1 then
if rand == 1 then rand3 = 3
elseif rand == 2 then rand3 = 7
elseif rand == 3 then rand3 = 4 end
elseif rand3 == 2 then
if rand == 1 then rand3 = 4
elseif rand == 2 then rand3 = 6
elseif rand == 3 then rand3 = 1 end
elseif rand3 == 3 then
if rand == 1 then rand3 = 5
elseif rand == 2 then rand3 = 7
elseif rand == 3 then rand3 = 1 end
elseif rand3 == 4 then
if rand == 1 then rand3 = 6
elseif rand == 2 then rand3 = 3
elseif rand == 3 then rand3 = 1 end
elseif rand3 == 5 then
if rand == 1 then rand3 = 2
elseif rand == 2 then rand3 = 1
elseif rand == 3 then rand3 = 7 end
elseif rand3 == 6 then
if rand == 1 then rand3 = 4
elseif rand == 2 then rand3 = 2
elseif rand == 3 then rand3 = 1 end
elseif rand3 == 7 then
if rand == 1 then rand3 = 5
elseif rand == 2 then rand3 = 3
elseif rand == 3 then rand3 = 2 end
end
else arpeggio = 1
end
if(scale == 1) then
note=make_note(cmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 2) then
note=make_note(gmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 3) then
note=make_note(dmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 4) then
note=make_note(amajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 5) then
note=make_note(emajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 6) then
note=make_note(bmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 7) then
note=make_note(fsharpmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 8) then
note=make_note(csharpmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 9) then
note=make_note(gsharpmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 10) then
note=make_note(eflatmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 11) then
note=make_note(bflatmajorscale[rand3]+shift,rand4,vol,0)
elseif(scale == 12) then
note=make_note(fmajorscale[rand3]+shift,rand4,vol,0)
end
set_note(sfxnum,t,note)
set_speed(sfxnum,tempo)
else t+=1
end
end
end
function write_new_song()
local i = flr(rnd(12))+1
local rand = flr(rnd(3))+1
for index = 1, 12 do
for y = 1, 2 do
if(y == 1) then
if(i <= 11) then
if rand == 1 then i+=1
elseif rand == 2 then
if i > 2 then i = i - 1
else i = 12 end
elseif rand == 3 then
if(i+9 <= 11) then i = i + 9
elseif i-4 >= 1 then i = i - 4
elseif i-4 <= 0 then
i = i-4 + 13
end
end
else i = 1 end
end
playscales(index+1+y,i)
end
end
music(3,-300)
end
function make_note(pitch, instr, vol, effect)
return { pitch + 64*(instr%4) , 16*effect + 2*vol + flr(instr/4) } -- flr may be redundant when this is poke'd into memory
end
function get_note(sfx, time)
local addr = 0x3200 + 68*sfx + 2*time
return { peek(addr) , peek(addr + 1) }
end
function set_note(sfx, time, note)
local addr = 0x3200 + 68*sfx + 2*time
poke(addr, note[1])
poke(addr+1, note[2])
end
function get_speed(sfx)
return peek(0x3200 + 68*sfx + 65)
end
function set_speed(sfx, speed)
poke(0x3200 + 68*sfx + 65, speed)
end
function get_loop_start(sfx)
return peek(0x3200 + 68*sfx + 66)
end
function get_loop_end(sfx)
return peek(0x3200 + 68*sfx + 67)
end
function set_loop(sfx, start, endt)
local addr = 0x3200 + 68*sfx
poke(addr + 66, start)
poke(addr + 67, endt)
end
Alex McCulloch
This is crazy :D I love it. You could probably add probabilities for melodic jumps vs. steps to make it sound more "musical". And conditions for resoluting dissonances like 7->8 and 2->3 or 4->3.
I like to play with random pitch in Sugar Bytes' Aparillo synth. Might be something for you as well. And with random pre-rendered and fully orchestrated chords in FMOD.
--
https://fardifferent.carrd.co/
It sounds a bit spooky in my opinion, I bet it would work great in a retro horror game :)
-https://red-voxel.itch.io/arrows-of-love #1 in the OpenGameArt Summer 2017 Game Jam-
Interesting. But what programming language is it?
Looks like Lua to me
I would be interested to make something like this. My program would take the value of bytes in a file (generated with a hardware random number generator), and translate these values into music notes. So yes, it means 256 values of notes !
I know very well the C language, and a bit of Java.
Do some of you know if there are some libraries in one of these languages which can :
- translate numbers into notes
- save the results into an mp3 or other type of music file?
Or maybe I could easily learn Lua and make some modifications to this code?
Any help is welcome. Yeah, I am a crazy programmer too :)
MIDI notes are actually just numbers between 0 and 127. I'm not a programmer, but shouldn't it be easy to create a MIDI file generator that uses random values? That file could be imported into any Audio Workstation to turn it into actual music. The most difficult part will be making the randomness sound musical.
--
https://fardifferent.carrd.co/
"The most difficult part will be making the randomness sound musical."
Not a problem, I do not intend to modify the result, but to keep it "as is". I just want to hear what randomness sounds like. Yeah, it may be a crazy idea :)
"MIDI notes are actually just numbers between 0 and 127. I'm not a programmer, but shouldn't it be easy to create a MIDI file generator that uses random values? That file could be imported into any Audio Workstation to turn it into actual music."
It is not that simple. I tried something, without any programming :
1) I created an empty midi file with a midi editor, then I saved it and closed it.
2) I reopened it with an hex editor. I kept the file header, and added the random bytes in the file.
3) I modified some of the bytes values representing the notes (the ones which were >127) : I divided them by 2, in order to have values between 0 and 127 only.
4) I saved the result as a midi file and closed it.
5) I opened the resulting midi file with the midi editor.
It did not open. The file could not be loaded again by the midi editor.
Anyway, I am making a pause on this project, I may come back on it after finishing some other projects.