Random music generator

To use, put Music:init() in setup and Music:play() in draw


function notewrap(input)
while input < 1 do input = input + 7 end
while input > 7 do input = input - 7 end
return input end

function genpitches()
    local me = {}
    for i = 1,255 do
        me[i] = math.min( math.max(0,(2^((i-48)/24))*0.19),1)
        end
        return me
        
        end

function randomsong()
local map = {}
local refa = math.random(0,7)
local set = {}
local setb = {}
local derp = 0
for j = 1,16 do
            set[j] = math.random(0,7)
            setb[j] = math.random(0,7)
        map[j] = math.random(0,1)
end
local songy = {}
for i = 1,16 do
    songy[i] = {}
        for j = 1,16 do
            derp = math.random(0,7)
            if math.random(0,j) < 1 then derp = setb[j] end
            if math.random(1,4) >= 2 then
                derp = derp * map[j]
            end
            
            if math.random(1,3) == 3 then
                derp = set[j]
            end
            if math.random(1,2) == 2 then
                derp = derp * map[j]
            end
            songy[i][j] = derp
    end
    songy[i][1] = refa
    songy[i][9] = refa

end
songy[9] = songy[1]
songy[13] = songy[5]
for i = 0,math.random(1,3) do
songy[math.random(1,16)] = songy[math.random(1,16)] end
return songy
end



Music = class()

-- 16 steps, 12 bars

function Music:init()
    Music.wav = math.random(0,1)
    Music.octave = 12*(math.random(0,1)-math.random(0,1))
    Music.comp = randomsong()
    Music.compb = randomsong()
    bob = math.random(0,11)
    Music.pitches = genpitches()
    Music.key = {1,3,5,6,8,10,12}
    for k, note in pairs(Music.key) do
        note = notewrap(note-bob)
    end
    Music.quant = math.random(19,28)/160
    Music.currentquant = 0
    Music.bassline = math.random(16)
    Music.currentstep = 0
    Music.currentbar = 1
end

function Music:play()
    Music.currentquant = Music.currentquant + DeltaTime
    for i = 0,3 do
    if Music.currentquant > Music.quant then
        Music.currentquant = Music.currentquant - Music.quant
        Music.currentstep = Music.currentstep + 1
        if math.random(0,2) == 2 then
            Music.octave = 12*(math.random(0,1)-math.random(0,1)) end
        if Music.currentstep == 17 then
        Music.currentstep = 1
        Music.currentbar = Music.currentbar + 1
        if Music.currentbar == 16 then
            Music.bassline = math.random(16)
              Music.currentbar = 1 end
        end
        local step = Music.currentstep
        if step > 8 then step = step -8 end
        Music:playnote() 
    if step == 2 or step == 6 then 
   sound(DATA, "ZgJAHgIpQGM3QEBAAAAAAAAAgD9CspM+QgBAf0BKQEBAQEBA")
    end

end
end
end
function Music:playnote() 
            if math.random(0,44) == 2 then
            Music.octave = 12*(math.random(0,1)-math.random(0,1)) end
    local ting = Music.comp[Music.currentbar][Music.currentstep]
    if ting == 0 then else
    sound(
    {Waveform = Music.wav,
    StartFrequency = Music.pitches[60 +Music.key[ting]],
    SustainTime = 0.2,
    DecayTime = 0.1,
    AttackTime = 0
    }
    )
    end
    if Music.currentstep/ 3 == math.floor(Music.currentstep/3) or math.random(0,22) == 3 then
    local ting = Music.compb[Music.bassline][Music.currentstep]
    if ting == 0 then else
    sound(
    {Waveform = 2,
    StartFrequency = Music.pitches[24 +Music.key[ting]],
    SustainTime = 0.25,
    DecayTime = 0.2,
    AttackTime = 0
    }
    ) end
    end end

Do i need to have that Music.wav file somehow?
And is it right that theres no beat?
Bit its very cool and it makes nice tunes… :slight_smile:

@Inviso - Love your water demo :smiley: Are you talking about the class variable wav ? As in Music.wav = math.random(0,1) :stuck_out_tongue:

Also, what do you mean by the beat?
this looks like it:

if step == 2 or step == 6 then 
   sound(DATA, "ZgJAHgIpQGM3QEBAAAAAAAAAgD9CspM+QgBAf0BKQEBAQEBA")
end

The sound used is hard to dissociate though, using sound(SOUND_HIT, 31568) makes it sound much more musical imo

BTW nice work again @KMEB :smiley:

The music.wav is just whether it’s a square or saw waveform.

Thanks @Xavier, @KMEB

Yep, now i understand :smiley:

and yes, now i get KMEB’s Beat…
i made mine a little more Boom Tschak, Boom Boom Tschank :slight_smile:

I tried to make it as easy to edit as possible. The beat is done in the part called “steps”

If you want to add another sound, just add another if steps == statement. I was going to make it random but forgot.