当前位置: 首页>>代码示例>>Python>>正文


Python Song.write_file方法代码示例

本文整理汇总了Python中song.Song.write_file方法的典型用法代码示例。如果您正苦于以下问题:Python Song.write_file方法的具体用法?Python Song.write_file怎么用?Python Song.write_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在song.Song的用法示例。


在下文中一共展示了Song.write_file方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from song import Song [as 别名]
# 或者: from song.Song import write_file [as 别名]
class Generator:

    def __init__(self):
        self.music = Music()

    def write_arffs(self):
        for next_file in os.listdir("midi"):
            if os.path.isfile("midi/" + next_file):
                print "adding midi/" + next_file
                self.music.load_song("midi/" + next_file)

        print "generating arff files"
        self.music.write_arff("test")

    def train(self):
        print "loading arff files"
        instrument_data, note_data, velocity_data, duration_data, time_delta_data = self.music.read_arff(
            "test")
        
        self.transform_instrument = preprocessing.OneHotEncoder(categorical_features=[0,1,2,5,6,7,10,11,12,15,16,17,20,21,22])
        self.transform_instrument.fit(instrument_data[:, :-1])

        self.transform_note = preprocessing.OneHotEncoder(categorical_features=[0,1,2,5,6,7,10,11,12,15,16,17,20,21,22])
        self.transform_note.fit(note_data[:, :-1])

        self.transform_duration = preprocessing.OneHotEncoder(categorical_features=[0,1,2,5,6,7,10,11,12,15,16,17,20,21,22])
        self.transform_duration.fit(duration_data[:, :-1])

        self.transform_time_delta = preprocessing.OneHotEncoder(categorical_features=[0,1,2,5,6,7,10,11,12,15,16,17,20,21,22])
        self.transform_time_delta.fit(time_delta_data[:, :-1])

        print "training instruments"
        self.instrument_clf = RandomForestClassifier(n_estimators=10)
        self.instrument_clf = self.instrument_clf.fit(
            self.transform_instrument.transform(instrument_data[:, :-1]), instrument_data[:, -1:])
        print "training notes"
        self.note_clf = RandomForestRegressor(n_estimators=10)
        print note_data[:, :-1]
        self.note_clf = self.note_clf.fit(self.transform_note.transform(note_data[:, :-1]), note_data[:, -1:])
        print "training velocity"
        self.velocity_clf = RandomForestClassifier(n_estimators=10)
        self.velocity_clf = self.velocity_clf.fit(
            velocity_data[:, :-1], velocity_data[:, -1:])
        print "training duration"
        self.duration_clf = RandomForestClassifier(n_estimators=10)
        self.duration_clf = self.duration_clf.fit(
            self.transform_duration.transform(duration_data[:, :-1]), duration_data[:, -1:])
        print "training time_delta"
        self.time_delta_clf = RandomForestClassifier(n_estimators=10)
        self.time_delta_clf = self.time_delta_clf.fit(
            self.transform_time_delta.transform(time_delta_data[:, :-1]), time_delta_data[:, -1:])

    def generate_song(self, filename):
        self.song = Song()

        self.song.add_note(self.get_seed_note())
        self.song.add_note(self.get_seed_note())

        print "generating new song"
        for _ in range(5000):
            notes = self.song.get_last_notes()
            notes = np.array(notes).reshape(1, -1)
            instrument = self.instrument_clf.predict(self.transform_instrument.transform(notes))[0]
            note = self.note_clf.predict(self.transform_note.transform(notes))[0]
            velocity = self.velocity_clf.predict(notes)[0]
            note_duration = self.duration_clf.predict(self.transform_duration.transform(notes))[0] * 2
            time_delta = self.time_delta_clf.predict(self.transform_time_delta.transform(notes))[0] * 0.75

            self.song.add_note(Note(int(instrument), int(note), int(
                velocity), int(time_delta), duration=int(note_duration)))

        print "writing song to " + filename
        self.song.write_file(filename)

    def get_seed_note(self):
        return Note(0, random.randrange(0, 126), 100, 0, duration=random.randrange(1, 10) * 10)
开发者ID:chrisranderson,项目名称:music-generation,代码行数:78,代码来源:generator.py


注:本文中的song.Song.write_file方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。