Random music generator written in python.

Share and discuss your LMMS music projects here, and see what people think!
Listen: https://soundcloud.com/nadremy1jnwj/random-relax
Listen more wide range: https://soundcloud.com/nadremy1jnwj/random-guitar
Use: https://drive.google.com/open?id=0B1O2I ... zZvVU1PV1U
Do you like the result? Maybe I need to add some features?
It's Bezier curves generator+my old beat generator modified.
It gives three outputs: midi, spectrogram for Virtual ANS and wav.
You need scipy+numpy+pypng or my midi lib that's bundled with, or both. If you don't have both... I think you can figure out how to disable some functions.
Here's some code:

Code: Select all

music=[(0,random.randrange(nres),random.getrandbits(1)),]
def musadd(up,soc,fbit):
    global music
    yadd=soc
    if not up:
        yadd=-yadd
    if not 0<=(music[-1][1]+yadd)<nres:
        yadd=-yadd
    if not 0<=(music[-1][1]+yadd)<nres:
        yadd=0
    xadd=((yadd*yadd)^4095)**.5#change 4095 to 2**n-1 to get different result
    music.append((music[-1][0]+xadd,music[-1][1]+yadd,fbit),)
def composer(pres,nres):
    global music
    pressed=True
    bf=random.getrandbits(16)
    bf2=random.getrandbits(16)
    up=random.getrandbits(1)
    soc=random.randrange(-8,8)
    alg=[random.randrange(-2,2) for bt in range(16)]
    for patterns in range(pres):
        by=random.getrandbits(4)
        #print('test')
        for bt in range(16):
            if bt%8==0:
                xop=random.getrandbits(1)
            if bt%2:
                xop=not xop
            cur=(xop<<bt)#2**by
            if bool(by&(1<<(bt%4)))^((bt%8)>4):
                bf^=cur
                #if cur:
                #    print('swap')
                #else:
                #    print('noswap')
            #else:
            #    print('noswap')
        by=random.getrandbits(4)
        for bt in range(16):
            if bt%8==0:
                xop=random.getrandbits(1)
            if bt%2:
                xop=not xop
            cur=(xop<<bt)#2**by
            if bool(by&(1<<(bt%4)))^((bt%8)>4):
                bf2^=cur
                alg[bt]=random.randrange(-2,2)
        by=random.getrandbits(8)
        for bt in range(16):
            bp=1<<bt
            if bp&bf:
                up^=1
                musadd(up,soc,bool(bp&bf2))
            else:
                soc+=alg[bt]
                while abs(soc)>8:
                    up^=1
                    soc=random.randrange(-5,5)
                musadd(up,soc,bool(bp&bf2))
    musmin=[min(nn) for nn in zip(*music)]
    musmax=[max(nn) for nn in zip(*music)]
    musnor=[tuple(((it[0]-it[1])/(it[2]-it[1]) for it in zip(nn,musmin,musmax))) for nn in music]
    music=[(0,random.randrange(nres),random.getrandbits(1)),]
    return musnor
Add:
I forgot that there's not 7 notes per octave but 12. Changed that and music became more interesting.

Code: Select all

    soc=random.randrange(-12,12)

Code: Select all

                while abs(soc)>12:
                    up^=1
                    soc=random.randrange(-8,8)
With that line:

Code: Select all

    xadd=(288-(yadd*yadd))**.5
it's completely different style.
https://soundcloud.com/nadremy1jnwj/random-piano
Who don't recognize Pythagoras?
Because max yadd is 12, min relative time between notes is sqrt(288-(12*12))=12.
You can try set this to 144 to get *very* variable time. Then min rel delay is 0.
Absolute delay is interpolated at 4,5,6 or 7 taps, depending on the pitch.
Heeeeeey, I wrote one of those before! :lol:

I used to be really into music theory, so it could actually make decent songs as well! I have no idea where it is now though, neither do I know whether it still exists...

...anyway, I suggest reading a music theory book (TL;DR Music Theory is my favorite, it's an online book and it's completely free), and then throwing in chord progressions and such. I did that, and I eventually even started adding "chord mixture" (which is pretty much using accidentals in a smart way), though I never finished that part.

And for the rhythm, I suggest you try making it more common for the beginning of a measure to be the beginning of (or sometimes halfway through) a beat. That will make your rhythm sound less chaotic and disorienting.

You know, I should see if I can dig up that program out of my graveyard and bring it back to life, see if I can finish it... :)

EDIT: I wrote it in Python as well! All non-built-in libraries I used was Pygame, for my music player.
I'm Russian and don't understand well what is beginning of measure. If you mean keyframe(I prefer to use animation terms because "it's all in timing and spacing" in both), then the number of inbetweens is.

Code: Select all

    ml=int(freqarray[melarray[((round(y0))%12+12)]]/onetap)
So you telling me inbetweens closer to keyframe should be longer? Only to first, or to last also?
About music theory: I started to read "the dummies" one. Before I got completely bored, I learned classical notation. So I don't know the theory. But I always can hear music in my head. Not always original. Sometimes I can't just repeat it, I try hard to make it sound equal to whatever I imagined. I noticed that to get note I want I need to compare it with pre-previous. So I just need to count, then try up and down, easy to confuse. Then I noticed that my notes all lie on the curve. That really simplified my life, now I count rarely.
Then I was experimenting with pygame. Wanted to make a game, but now it's slow. Need a rewrite. But the parts of the unfinished game seemed useful. And I also have a beat generator. Just combined them.
Add: I think feelings are acceleration.
Add: If I understand you correctly, time of beat, or between keyframes including first, should be:

Code: Select all

1    *
.75       *       *
.5             *
Of course I should normalize sine to make sum of it length 1, so the line:

Code: Select all

    for s in range(ml):
should be replaced and s variable will be nonlinear. Also,

Code: Select all

fadeout=8
is a constant.
So, it should be replaced with real note duration.
Now I'm thinking: is that enough to make fadeout non-constant, or I also need sine modulation? Because my middle of beat is already longer, because I use circle bezier splines.
Add: if you don't need wav and png output, you need only my own midi lib and built-ins.
Add: I think I should do a program that does inverse: transforms music to curves and curves to acceleration. Then I can measure frequency acceleration of real music.
Well I did that you mention Douglas. You may compare interpolation before and after change. Tell me if I misunderstood you.
https://drive.google.com/open?id=1Ez3l5 ... gE4_qwDADQ
Found a bug. Link is the same, re-download second version when it's done.
https://soundcloud.com/nadremy1jnwj/random-jazz
Done. Four different ways to place a note. I really don't know which is best.
https://drive.google.com/open?id=1XNoWk ... 6ecR0VglWP
Also I disabled rotation flips because it was done the wrong way.
First, Bezier curve flip side if first and last coords flipped.
Second, acceleration already gives good smooth curves, so

Code: Select all

xadd=(288-yadd*yadd)**.5
is unnecessary complication that makes music worse. So

Code: Select all

xadd=1
is true.
But the concept of negative duration... makes the song polyphonic. But it's a bug.
It seems that bugs make song better, so maybe I should return the overlap bug back? ml+2 instead of ml+1.
For now, it's seventh reupload.
https://soundcloud.com/nadremy1jnwj/random-saw-strings
It's step backwards. Didn't got any better.
Next song won't be random. I have one abandoned.
Here. https://soundcloud.com/nadremy1jnwj/abandoned-1
It's verry exciting what you have developed.

But, well, I do not understand anything about programming. Do you use it in LMMS? Or with a third party program? How could I test this code?
bckpkol wrote:
Tue Nov 07, 2017 9:38 am
Well I did that you mention Douglas. You may compare interpolation before and after change. Tell me if I misunderstood you.
https://drive.google.com/open?id=1Ez3l5 ... gE4_qwDADQOmegle
Found a bug. Link is the same, re-download second version when it's done.
https://soundcloud.com/nadremy1jnwj/random-jazz
Done. Four different ways to place a note. I really don't know which is best.
https://drive.google.com/open?id=1XNoWk ... 6ecR0VglWP
Also I disabled rotation flips because it was done the wrong way.
Disabling rotation is a good solution if it was previously done incorrectly