Skip to main content

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password
Register
  • Home
  • Browse
    • 2D Art
    • 3D Art
    • Concept Art
    • Textures
    • Music
    • Sound Effects
    • Documents
    • Featured Tutorials
  • Submit Art
  • Collect
    • My Collections
    • Art Collections
  • Forums
  • FAQ
  • Leaderboards
    • All Time
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
    • Weekly
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
  • ❤ Donate
Audio

Randomly Generated Algorithmic Music

Pro Sensory
Sunday, October 7, 2018 - 00:44
Pro Sensory's picture

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. 


  • Log in or register to post comments
Pro Sensory
joined 8 years 4 months ago
Sunday, October 7, 2018 - 00:52
Pro Sensory's picture

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

  • Log in or register to post comments
Of Far Differen...
joined 5 years 7 months ago
Wednesday, November 13, 2019 - 11:08
Of Far Different Nature's picture

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/

  • Log in or register to post comments
Red_Voxel
joined 7 years 10 months ago
Wednesday, November 13, 2019 - 12:15
Red_Voxel's picture

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-

  • Log in or register to post comments
Xom Adept
joined 7 years 2 months ago
Thursday, November 14, 2019 - 09:38
Xom Adept's picture

Interesting. But what programming language is it?

  • Log in or register to post comments
theidiotmachine
joined 9 years 3 months ago
Thursday, November 14, 2019 - 12:09

Looks like Lua to me

  • Log in or register to post comments
Xom Adept
joined 7 years 2 months ago
Thursday, November 14, 2019 - 14:26
Xom Adept's picture

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 :)

  • Log in or register to post comments
Of Far Differen...
joined 5 years 7 months ago
Saturday, November 16, 2019 - 17:00
Of Far Different Nature's picture

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/

  • Log in or register to post comments
Xom Adept
joined 7 years 2 months ago
Sunday, November 17, 2019 - 06:01
Xom Adept's picture

"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.

 

  • Log in or register to post comments