Musical: automatic music generation

Showcase your libraries, tools and other projects that help your fellow love users.
Jamie
Prole
Posts: 5
Joined: Fri Nov 23, 2012 4:26 am

Re: Musical: automatic music generation

Post by Jamie »

I am definitely not an expert in music theory. I just gave myself a crash course via google and it seems to have been enough so far.

I am also new to Love, and also to Lua but I have been doing software development for many years, much of it in signal processing, so it's not entirely foreign.

The original I took mostly as inspiration, and didn't end up using much of the code. I hereby release "mgen" v 0.1 under the MIT license for anyone to use and/or modify for any purpose, but with no warranty.

Comments or questions welcome
Attachments
mgen_v0.1.love
(2.22 KiB) Downloaded 209 times
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

Re: Musical: automatic music generation

Post by Saegor »

Jamie wrote:Comments or questions welcome
do you know it's AWESOME ? <3
Current work : Isömap
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Musical: automatic music generation

Post by micha »

Jamie wrote:Comments or questions welcome
Very well done. And so simple.

I am missing the 6th cord (A C E, "a minor") which is very common on a c-major scale. Try the following cadence: "66445511"

I have a couple of suggestions from a musicians point of view (in case you like to play around with it):
  • As far as I understood your code, at each time you have a fixed chord and from this you randomly select (one or three) notes and play these. You will probably get a more balanced sound if you divide the three voices in to a high voice, a middle voice and bass voice (or even make four voices like in a classical choir). Each voice should have it's own tonal range from which it selects notes. That way the highest voice will most probably create something like a melody.
  • The bass voice should almost always play the bass note (if the chord is a 5, the bass voice should play the 5, not the 7 or 2)
  • To create a greater variety of melodies, you can allow the top voice to play notes that are not contained in the chord. However this should only happen on offbeats (that is the 2nd and 4th beat of each bar).
  • Maybe you want to add rythmic patterns. By that I mean instead of playing a note at all four beats in a bar (like "1111") you can define a pattern like "1101" or "1010" or whatever you imagine and let the top voice only play a note on the beats marked with 1.
Jamie
Prole
Posts: 5
Joined: Fri Nov 23, 2012 4:26 am

Re: Musical: automatic music generation

Post by Jamie »

micha wrote:I am missing the 6th cord (A C E, "a minor") which is very common on a c-major scale. Try the following cadence: "66445511"

I have a couple of suggestions from a musicians point of view (in case you like to play around with it):
  • As far as I understood your code, at each time you have a fixed chord and from this you randomly select (one or three) notes and play these. You will probably get a more balanced sound if you divide the three voices in to a high voice, a middle voice and bass voice (or even make four voices like in a classical choir). Each voice should have it's own tonal range from which it selects notes. That way the highest voice will most probably create something like a melody.
  • The bass voice should almost always play the bass note (if the chord is a 5, the bass voice should play the 5, not the 7 or 2)
  • To create a greater variety of melodies, you can allow the top voice to play notes that are not contained in the chord. However this should only happen on offbeats (that is the 2nd and 4th beat of each bar).
  • Maybe you want to add rythmic patterns. By that I mean instead of playing a note at all four beats in a bar (like "1111") you can define a pattern like "1101" or "1010" or whatever you imagine and let the top voice only play a note on the beats marked with 1.
Great suggestions, this is just the kind of thing I need, from someone who knows more about music. I'll try these things. Thanks!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Musical: automatic music generation

Post by Robin »

Interesting stuff, Jamie. I might use some of that code for my game. :)

If you want to do more with mgen (and keep us informed) it might be a good idea to start a new thread for it, so people don't get confused and things.
Tesselode wrote:I can help you with musical stuff if you want.
Cool. You have a background in music theory? (Or music practice? ;))

It might take me a few months (six or so) before I'm able to work on this again, though, depending on how busy I'll be.
Help us help you: attach a .love.
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: Musical: automatic music generation

Post by Tesselode »

Robin wrote:Interesting stuff, Jamie. I might use some of that code for my game. :)

If you want to do more with mgen (and keep us informed) it might be a good idea to start a new thread for it, so people don't get confused and things.
Tesselode wrote:I can help you with musical stuff if you want.
Cool. You have a background in music theory? (Or music practice? ;))

It might take me a few months (six or so) before I'm able to work on this again, though, depending on how busy I'll be.
I know enough music theory to make things that sound nice. :P
User avatar
Codex
Party member
Posts: 106
Joined: Tue Mar 06, 2012 6:49 am

Re: Musical: automatic music generation

Post by Codex »

Jamie wrote:
Comments or questions welcome
I'm very impressed! :) Maybe add a setting where it generates a random song and then loops over and over again, instead of keeping it nonstop random.
kesac
Prole
Posts: 49
Joined: Tue Apr 03, 2012 12:29 am
Contact:

Re: Musical: automatic music generation

Post by kesac »

Jamie wrote:I hereby release "mgen" v 0.1 under the MIT license for anyone to use and/or modify for any purpose, but with no warranty.
That's so awesome! I extracted your mgen code into its own script to make it more modular and then wrote a short loop for it to play...
Attachments
mgen_obeytown.love
A small example of mgen being used to play "Obeytown"
(2.29 KiB) Downloaded 174 times
Have you met the Child of Winter?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Musical: automatic music generation

Post by kikito »

Let me start by saying that I love everything procedural, so I'm loving what I see on this thread :)

Now, I'd like to report a small issue. I don't know how to explain this well, since I lack the required vocabulary. So bare with me.

On the file the OP (musical.love) I'm getting very annoying 'static flicks' at the end of most notes. They sound like a PC speaker when you turn it off, only less audible. But they're very distracting nonetheless.

On the other two (mgen*.love) I'm getting the notes very well, with no static.

This is on a Mac, in case it helps.
When I write def I mean function.
Jamie
Prole
Posts: 5
Joined: Fri Nov 23, 2012 4:26 am

Re: Musical: automatic music generation

Post by Jamie »

kikito wrote:Let me start by saying that I love everything procedural, so I'm loving what I see on this thread :)

Now, I'd like to report a small issue. I don't know how to explain this well, since I lack the required vocabulary. So bare with me.

On the file the OP (musical.love) I'm getting very annoying 'static flicks' at the end of most notes. They sound like a PC speaker when you turn it off, only less audible. But they're very distracting nonetheless.

On the other two (mgen*.love) I'm getting the notes very well, with no static.

This is on a Mac, in case it helps.
I believe the soft tick sounds are due to the sample ending 'abruptly'. If the waveform ends at a point where the sin function has not returned to zero, then it has a discontinuity at the end where it effectively jumps to zero. It doesn't occur at the beginning of the notes because since sin() starts from zero, the waveform won't have a discontinuity at the beginning. The magnitude of the discontinuity at the end will depend on whether the sample length is close to an even multiple of the period, which I think is why it appears on some notes and not on others.

Within mgen, I have an extra smoothing at the beginning and end, where the attack and decay is not instantaneous, but goes from zero to full amplitude over a small but nonzero interval at the beginning and the end. The envelope function specifies this behavior and by default it has a quick ramp from 0 to 100% over 1/10th of the note duration.

Within musical.zip, if you replace 'instrument' with the following, it smooths out the discontinuity and the ticks go away:

Code: Select all

local function instrument(t, len, freq)
  local pct = t/len
  local env = math.min(1, pct*100, 100-100*pct)
  return env * (.5 * sin(c * t * freq) + .3 * sin(c * t * (freq + 1)) + .3 * sin(c * t * (freq - 1)))
end
I've added the minor 6th to mgen, and it works well just as micha had suggested. I'm getting a bit distracted with all the possibilities in chord progressions, so I haven't segmented the voices or added the in-between notes or rhythms yet. Pachabel's chord progression sounds very nice when played a small number of times, but it doesn't sequence well with other progressions. And I think it's not so good for video game music because when played over and over, after about 10-15 minutes you want to kill yourself.

-Jamie
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest