What's a good way to do dynamically-generated music, note-by-note?

So, the game I’m working on is based around building some simple melodies during play. Algorithmically speaking, I’ve got some interesting ideas as to how to do that, but I’m having some technical trouble – as far as I can tell, the only way to accomplish this would be to import sound files for each note, attach them all to a container object, and treat each one as an AudioSource. MIDI would make more sense, of course, and I imagine that tracker files would present interesting opportunities as well, but I can’t find built-in functionality that would let me play notes “off-the-cuff” using either of those two technologies (and I can’t afford Unity Pro to get access to C libraries). Do you guys have any clever ideas as to how I might play notes, one-by-one, in Unity to create dynamic melodies?

Musical keyboards and even midi devices all use the same trick: they have a limited number of notes stored, and alter the pitch to create the notes in between. These notes usually have also a loop section defined, which can be repeated to extend the note. Unfortunately, this extension can’t be done in Unity, but maybe for your purposes it may not be necessary.

The idea is to have one or more long duration notes, set the audio.pitch to the note you want, then play it using audio.Play(). When you need to play another note, change the pitch and play it using audio.Play() again - the previous note will be stopped and replaced by the new one. To stop a note, use audio.Stop().

Just to give you an idea of what can be done, I created a little “piano” in the script below. The keys a s d f g h j k l play the notes C D E F G A B C D. Create an empty object, assign this script to it, add an Audio Source component, and set its audio clip to some long note (if you don’t have any, download it from here and import it to the assets):

var transpose = -4;  // transpose in semitones

function Update(){

	var note = -1; // invalid value to detect when note is pressed
	if (Input.GetKeyDown("a")) note = 0;  // C
	if (Input.GetKeyDown("s")) note = 2;  // D
	if (Input.GetKeyDown("d")) note = 4;  // E
	if (Input.GetKeyDown("f")) note = 5;  // F
	if (Input.GetKeyDown("g")) note = 7;  // G
	if (Input.GetKeyDown("h")) note = 9;  // A
	if (Input.GetKeyDown("j")) note = 11; // B
	if (Input.GetKeyDown("k")) note = 12; // C
	if (Input.GetKeyDown("l")) note = 14; // D
	if (note>=0){ // if some key pressed...
		audio.pitch =  Mathf.Pow(2, (note+transpose)/12.0);

You can use the basic audio.pitch idea to produce the notes. Use an array to specify which notes to play, and another one to determine how much time each one will last. Another solution would be to have a single float array, where the integer part of each number is the note, and the fractional part is the duration in seconds.

This guy’s tutorial worked for me:

not sure how you would go about doing that but if your worried about the quantity of audio files you could try making one audio file to play whole notes c1-c6 and then when the player triggers a specific note have it jump to that part of the audio file play the note and stop…not sure how you would do this either…but it sounds alot less complicated lol