1 import random
2 import glob
3
6
7
8 self.states={}
9
10
11
12
13 self.transitions={}
14
15 self.moodStack=[]
16
17 self.musicLibrary=musicLibrary
18
20 assert name not in self.states
21
22 self.states[name]=[]
23
25 pair=(fromState,toState)
26 assert pair not in self.transitions
27
28 self.transitions[pair]=[]
29
31 self.states[stateName].append(trackName)
32
34 exts=["*.mp3", "*.wav", "*.mid", "*.ogg"]
35 for e in exts:
36 g=directoryName+"/"+e
37 for gn in glob.glob(g):
38 self.states[stateName].append(gn)
39
40
42 self.transitions[(fromName,toName)].append(trackName)
43
45 exts=["*.mp3", "*.wav", "*.mid", "*.ogg"]
46 for e in exts:
47 g=directoryName+"/"+e
48 for gn in glob.glob(g):
49 self.transitions[(fromName,toName)].append(gn)
50
52 if len(self.moodStack)==0:
53 return None
54 return self.moodStack[0]
55
56
58 """
59 You might instead want to use pushState, which handles
60 transitions
61 """
62 self.moodStack=[stateName]
63
65 """
66 this adds a state to the end of the current mood stack -
67 transitions will be played to get the player to the
68 destination
69 """
70 print "pushing transition to:",stateName
71 self.moodStack.append(stateName)
72
74 """
75 if the music is still playing, do nothing.
76 if we're at the end of a song, if there's only one state on
77 the stack, pick a new song.
78
79 if there's more than one state, look at the top two - if there
80 is a transition, play that transition.
81 otherwise, if there's a wildcard transition, play that
82 otherwise, simply jump to the new state, and play a song
83 randomly there.
84
85 pop the top state off
86 """
87
88 if self.musicLibrary.isPlaying():
89
90 return
91
92 numMoods=len(self.moodStack)
93
94 print "moodStack size:",numMoods
95
96 if numMoods==0:
97 return
98
99 if numMoods==1:
100 newTrack=random.choice(self.states[self.moodStack[0]])
101 self.musicLibrary.play(newTrack)
102 return
103
104 oldMood=self.moodStack[0]
105 newMood=self.moodStack[1]
106
107 self.moodStack=self.moodStack[1:]
108
109 possTransitions=[(oldMood,newMood),
110 (oldMood,None),
111 (None,newMood)]
112
113 for pt in possTransitions:
114 if pt in self.transitions:
115 newTrack=random.choice(self.transitions[pt])
116 self.musicLibrary.play(newTrack)
117 return
118
119
120
121
122 newTrack=random.choice(self.states[newMood])
123 self.musicLibrary.play(newTrack)
124