View Javadoc

1   package com.jamesbryangraves.bob.music;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   
6   import com.jamesbryangraves.bob.util.Translator;
7   
8   
9   public class Melody extends DiatonicMusicEventGenerator {
10  	
11  	private int pitch = -1;
12  	
13  	public Collection<MusicEvent> generateEvents(MusicContext musicContext, long tick) throws Exception {
14  		ArrayList<MusicEvent> events = new ArrayList<MusicEvent>();
15  		long ticks = musicContext.getTicksPerBar();
16  		int key = Translator.translate(musicContext.getKey());
17  		Scale scale = musicContext.getScale();
18  		for(long i = 0; i < ticks;) {
19  			long value = getNoteValue(ticks - i);
20  			pitch = pitch == -1
21  					? getScalePitch(key, scale)
22  					: getNextPitch(pitch, key, scale);
23  			events.add(new MusicEvent(pitch, value, tick + i));			
24  			i += value;
25  		}
26  		return events;
27  	}
28  }