def init(self, music_dirs): def calculate_weight_conservative(conductor, score, user_score): ease = lambda x, length, height: (math.tanh(x * (math.pi / length)) + 1) * (height / 2) num_chains = len(conductor.chains) return ease(user_score, 10 * num_chains, 10) * math.pow(2, score) def calculate_weight_eager(conductor, score, user_score): return math.pow(1 + (1.0 / len(conductor.chains)), user_score) * (math.sqrt(score) + 1) self.conductor = MarkovConductor( "/tmp/conductor-demo.db", {"weight_function": calculate_weight_eager}) self.conductor.load() self.conductor.init_chain("trackid", "trackid") self.conductor.init_chain("albumid", "albumid") self.conductor.init_chain("albumid", "trackid") self.conductor.init_chain("albumid", "artistid") self.conductor.init_chain("artistid", "artistid") self.conductor.init_chain("artistid", "trackid") self.conductor.init_chain("genreid", "genreid") self.library = Library(self.conductor) for dir in music_dirs: self.library.load_files(dir)
def main(args): c = MarkovConductor("/tmp/conductor-test.db") c.load() tracks = [{ "title": "Blue", "album": "Cold", "artist": "Colors", "genre": "Electronic" }, { "title": "Cyan", "album": "Cold", "artist": "Colors", "genre": "Electronic" }, { "title": "Green", "album": "Cold", "artist": "Colors", "genre": "Electronic" }, { "title": "Purple", "album": "Cold", "artist": "Colors", "genre": "Electronic" }, { "title": "One", "album": "Low", "artist": "Numbers", "genre": "Math Rock" }] for prev, cur in izip([None] + tracks, tracks): c.record_transition(prev, cur) c.unload()
def main(args): c = MarkovConductor("/tmp/conductor-test.db") c.load() tracks = [{"title": "Blue", "album": "Cold", "artist": "Colors", "genre": "Electronic"}, {"title": "Cyan", "album": "Cold", "artist": "Colors", "genre": "Electronic"}, {"title": "Green", "album": "Cold", "artist": "Colors", "genre": "Electronic"}, {"title": "Purple", "album": "Cold", "artist": "Colors", "genre": "Electronic"}, {"title": "One", "album": "Low", "artist": "Numbers", "genre": "Math Rock"}] for prev, cur in izip([None]+tracks, tracks): c.record_transition(prev, cur) c.unload()
def main(args): c = MarkovConductor("/tmp/conductor-test.db") c.load() tracks = [{"title": "Blue", "album": "Cold", "artist": "Colors", "genre": "Electronic"}, {"title": "Cyan", "album": "Cold", "artist": "Colors", "genre": "Electronic"}, {"title": "Green", "album": "Cold", "artist": "Colors", "genre": "Electronic"}, {"title": "Purple", "album": "Cold", "artist": "Colors", "genre": "Electronic"}, {"title": "One", "album": "Low", "artist": "Numbers", "genre": "Math Rock"}] prev = None for i in range(0, 200): cur = random.choice(tracks) print cur, prev c.record_transition(prev, cur) prev = cur c.unload()
def init(self, music_dirs): def calculate_weight_conservative(conductor, score, user_score): ease = lambda x, length, height: (math.tanh(x*(math.pi/length))+1)*(height/2) num_chains = len(conductor.chains) return ease(user_score, 10 * num_chains, 10) * math.pow(2, score) def calculate_weight_eager(conductor, score, user_score): return math.pow(1 + (1.0/len(conductor.chains)), user_score) * (math.sqrt(score) + 1) self.conductor = MarkovConductor("/tmp/conductor-demo.db", {"weight_function": calculate_weight_eager}) self.conductor.load() self.conductor.init_chain("trackid", "trackid") self.conductor.init_chain("albumid", "albumid") self.conductor.init_chain("albumid", "trackid") self.conductor.init_chain("albumid", "artistid") self.conductor.init_chain("artistid", "artistid") self.conductor.init_chain("artistid", "trackid") self.conductor.init_chain("genreid", "genreid") self.library = Library(self.conductor) for dir in music_dirs: self.library.load_files(dir)
def main(args): sample_path = args[0] c = MarkovConductor("/tmp/conductor-markov-demo.db") c.load() c.init_chain("trackid", "trackid") load_files(c, sample_path) prev = c.choose_next_track() c.record_transition(None, prev) cur = c.choose_next_track(prev) cmd = None playcount = 0 while True: c.record_transition(prev, cur, False) if playcount == 0: os.system(PLAYCMD % os.path.join(sample_path, prev["title"])) os.system(PLAYCMD % os.path.join(sample_path, cur["title"])) print previd = c.get_track(prev).id curid = c.get_track(cur).id print "%s -> %s" % (previd, curid) print "---" scores = c.get_transitions_from_id(previd) print_histogram(c, scores) if playcount > 0: playcount -= 1 else: cmd = read_chr().lower() if cmd == "g": c.record_user_feedback(True) elif cmd == "b": c.record_user_feedback(False) elif cmd == "p": playcount = 10 elif cmd == "q": sys.exit() # If the last choice wasn't bad, continue to the next track if not cmd == "b": prev = cur cur = c.choose_next_track(prev) c.unload()
class SimpleConsolePlayer: def __init__(self): self.conductor = None self.library = None self.current_track = None self.previous_track = None def init(self, music_dirs): def calculate_weight_conservative(conductor, score, user_score): ease = lambda x, length, height: (math.tanh(x*(math.pi/length))+1)*(height/2) num_chains = len(conductor.chains) return ease(user_score, 10 * num_chains, 10) * math.pow(2, score) def calculate_weight_eager(conductor, score, user_score): return math.pow(1 + (1.0/len(conductor.chains)), user_score) * (math.sqrt(score) + 1) self.conductor = MarkovConductor("/tmp/conductor-demo.db", {"weight_function": calculate_weight_eager}) self.conductor.load() self.conductor.init_chain("trackid", "trackid") self.conductor.init_chain("albumid", "albumid") self.conductor.init_chain("albumid", "trackid") self.conductor.init_chain("albumid", "artistid") self.conductor.init_chain("artistid", "artistid") self.conductor.init_chain("artistid", "trackid") self.conductor.init_chain("genreid", "genreid") self.library = Library(self.conductor) for dir in music_dirs: self.library.load_files(dir) def prompt_track(self): track = None while track is None: title = raw_input("Track title to play next: ") track = self.library.find_title(title) if not track: print "Sorry, that track could not be found. Please try again." return track def prompt_tracks(self): titles = raw_input("Track titles (title1, title2, ...) to play next: ").split(",") tracks = [] for title in titles: title = title.strip() track = self.library.find_title(title) if track: tracks.append(track) return tracks def play_track(self, next, userchoice): self.previous_track = self.current_track self.current_track = next self.conductor.record_transition(self.previous_track, self.current_track, userchoice) print "%s -> %s" % (self.previous_track["title"] if self.previous_track else "[Start]", self.current_track["title"]) print if self.previous_track and PLAYPREV and playcount == 0: os.system(PLAYCMD % self.library.get_track_path(self.previous_track)) os.system(PLAYCMD % self.library.get_track_path(self.current_track)) def run(self, args): self.init(args) self.previous_track = None self.current_track = None playcount = 0 # Command line loop while True: if playcount > 0: playcount -= 1 elif self.current_track is not None: print "Please enter a key (G = Good, B = Bad): " cmd = read_chr().lower() if self.current_track is not None: if cmd == "g": # Rate "Good!" print "User: that choice was GOOD!" print self.conductor.record_user_feedback(True) elif cmd == "b": # Rate "Bad!" print "User: that choice was BAD!" print self.conductor.record_user_feedback(False) # Return to the previous track to try again. self.current_track = self.previous_track elif cmd == "p": # Play the next 10 tracks without pausing playcount = 10 print "Playing %s tracks..." % playcount elif cmd == "c": # Manually choose next track. self.play_track(self.prompt_track(), userchoice=True) continue elif cmd == "s": # Manually choose next tracks. for track in self.prompt_tracks(): self.play_track(track, userchoice=True) continue elif cmd == "x": # Forget about the previous track to simulate a session beginning. self.previous_track = None print "New session started." continue elif cmd == "q": # "Quit" print "Bye!" sys.exit() curid = self.conductor.get_track(self.current_track).id if self.current_track else None scores = self.conductor.get_transitions_from_id(curid) print "---" print "%s -> ..." % (self.current_track["title"] if self.current_track else "[Start]") print_histogram(self.conductor, scores) print "---" self.play_track(self.conductor.choose_next_track(self.current_track), userchoice=False) self.conductor.unload()
class SimpleConsolePlayer: def __init__(self): self.conductor = None self.library = None self.current_track = None self.previous_track = None def init(self, music_dirs): def calculate_weight_conservative(conductor, score, user_score): ease = lambda x, length, height: (math.tanh(x * (math.pi / length)) + 1) * (height / 2) num_chains = len(conductor.chains) return ease(user_score, 10 * num_chains, 10) * math.pow(2, score) def calculate_weight_eager(conductor, score, user_score): return math.pow(1 + (1.0 / len(conductor.chains)), user_score) * (math.sqrt(score) + 1) self.conductor = MarkovConductor( "/tmp/conductor-demo.db", {"weight_function": calculate_weight_eager}) self.conductor.load() self.conductor.init_chain("trackid", "trackid") self.conductor.init_chain("albumid", "albumid") self.conductor.init_chain("albumid", "trackid") self.conductor.init_chain("albumid", "artistid") self.conductor.init_chain("artistid", "artistid") self.conductor.init_chain("artistid", "trackid") self.conductor.init_chain("genreid", "genreid") self.library = Library(self.conductor) for dir in music_dirs: self.library.load_files(dir) def prompt_track(self): track = None while track is None: title = raw_input("Track title to play next: ") track = self.library.find_title(title) if not track: print "Sorry, that track could not be found. Please try again." return track def prompt_tracks(self): titles = raw_input( "Track titles (title1, title2, ...) to play next: ").split(",") tracks = [] for title in titles: title = title.strip() track = self.library.find_title(title) if track: tracks.append(track) return tracks def play_track(self, next, userchoice): self.previous_track = self.current_track self.current_track = next self.conductor.record_transition(self.previous_track, self.current_track, userchoice) print "%s -> %s" % (self.previous_track["title"] if self.previous_track else "[Start]", self.current_track["title"]) print if self.previous_track and PLAYPREV and playcount == 0: os.system(PLAYCMD % self.library.get_track_path(self.previous_track)) os.system(PLAYCMD % self.library.get_track_path(self.current_track)) def run(self, args): self.init(args) self.previous_track = None self.current_track = None playcount = 0 # Command line loop while True: if playcount > 0: playcount -= 1 elif self.current_track is not None: print "Please enter a key (G = Good, B = Bad): " cmd = read_chr().lower() if self.current_track is not None: if cmd == "g": # Rate "Good!" print "User: that choice was GOOD!" print self.conductor.record_user_feedback(True) elif cmd == "b": # Rate "Bad!" print "User: that choice was BAD!" print self.conductor.record_user_feedback(False) # Return to the previous track to try again. self.current_track = self.previous_track elif cmd == "p": # Play the next 10 tracks without pausing playcount = 10 print "Playing %s tracks..." % playcount elif cmd == "c": # Manually choose next track. self.play_track(self.prompt_track(), userchoice=True) continue elif cmd == "s": # Manually choose next tracks. for track in self.prompt_tracks(): self.play_track(track, userchoice=True) continue elif cmd == "x": # Forget about the previous track to simulate a session beginning. self.previous_track = None print "New session started." continue elif cmd == "q": # "Quit" print "Bye!" sys.exit() curid = self.conductor.get_track( self.current_track).id if self.current_track else None scores = self.conductor.get_transitions_from_id(curid) print "---" print "%s -> ..." % (self.current_track["title"] if self.current_track else "[Start]") print_histogram(self.conductor, scores) print "---" self.play_track(self.conductor.choose_next_track( self.current_track), userchoice=False) self.conductor.unload()