View Javadoc

1   package com.jamesbryangraves.bob.music;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   public class Composer extends Thread {
7   	
8   	private List<MusicEventListener> listeners = new ArrayList<MusicEventListener>();
9   	private MusicContext musicContext;
10  	private List<MusicEventGenerator> musicGenerators;
11  	private long tick = 0;
12  	private boolean open;
13  	
14  	public void run() {
15  		
16  		try {
17  			while(isOpen()) {
18  				compose();
19  			}
20  		} catch(Exception e) {
21  			e.printStackTrace();
22  			System.exit(1);
23  		}
24  	}
25  	
26  	public void compose() throws Exception {
27  		for(int i = 0; i < musicGenerators.size(); i++) {
28  			MusicEventGenerator musicGenerator = musicGenerators.get(i);
29  			for(MusicEvent event : new Phrase()
30  				.generateEvents(musicGenerator, musicContext, tick)) {
31  				notifyMusicEventListeners(i, event);
32  			}
33  		
34  		}
35  		tick += musicContext.getTicksPerBar() * musicContext.getPhraseSize();
36  	}
37  		
38  	public void notifyMusicEventListeners(int index, MusicEvent event) {
39  		for(MusicEventListener listener : listeners)
40  			listener.handleMusicEvent(index, event);
41  	}
42  
43  	public MusicContext getMusicContext() {
44  		return musicContext;
45  	}
46  
47  	public void setMusicContext(MusicContext musicContext) {
48  		this.musicContext = musicContext;
49  	}
50  
51  	public List<MusicEventGenerator> getMusicGenerators() {
52  		return musicGenerators;
53  	}
54  
55  	public void setMusicGenerators(List<MusicEventGenerator> musicGenerators) {
56  		this.musicGenerators = musicGenerators;
57  	}
58  
59  	public long getTick() {
60  		return tick;
61  	}
62  
63  	public void setTick(long tick) {
64  		this.tick = tick;
65  	}
66  
67  	public List<MusicEventListener> getListeners() {
68  		return listeners;
69  	}
70  
71  	public void setListeners(List<MusicEventListener> listeners) {
72  		this.listeners = listeners;
73  	}
74  
75  	public boolean isOpen() {
76  		return open;
77  	}
78  
79  	public void open() {
80  		this.open = true;
81  	}
82  	
83  	public void close() {
84  		this.open = false;
85  	}
86  
87  }