Package MeatEngine :: Package MoodMusic :: Module player
[hide private]
[frames] | no frames]

Source Code for Module MeatEngine.MoodMusic.player

  1  import random 
  2  import glob 
  3   
4 -class Player:
5 - def __init__(self, musicLibrary):
6 7 # states will be keyed by name 8 self.states={} 9 10 # transitions will be keyed by a tuple (from,to) of names of 11 # the states being transitioned between. Instead of a state 12 # name, None may also be used as a wildcard 13 self.transitions={} 14 15 self.moodStack=[] 16 17 self.musicLibrary=musicLibrary
18
19 - def addState(self, name):
20 assert name not in self.states 21 22 self.states[name]=[]
23
24 - def addTransition(self, fromState, toState):
25 pair=(fromState,toState) 26 assert pair not in self.transitions 27 28 self.transitions[pair]=[]
29
30 - def addMusicToState(self, stateName, trackName):
31 self.states[stateName].append(trackName)
32
33 - def addMusicDirectoryToState(self, stateName, directoryName):
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
41 - def addMusicToTransition(self, fromName, toName, trackName):
42 self.transitions[(fromName,toName)].append(trackName)
43
44 - def addMusicDirectoryToTransition(self, fromName, toName, directoryName):
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
51 - def getState(self):
52 if len(self.moodStack)==0: 53 return None 54 return self.moodStack[0]
55 56
57 - def setState(self, stateName):
58 """ 59 You might instead want to use pushState, which handles 60 transitions 61 """ 62 self.moodStack=[stateName]
63
64 - def pushState(self, stateName):
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
73 - def tick(self):
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 #print "is playing" 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 # otherwise, there's no transition - just play a random song 120 # from the new state 121 122 newTrack=random.choice(self.states[newMood]) 123 self.musicLibrary.play(newTrack)
124