def calculate_score_pp(self, beatmap_id, score): p = osu.parser() beatmap_url = "https://osu.ppy.sh/osu/{0}".format(beatmap_id) beatmap_raw = requests.get(beatmap_url).text f = codecs.open(Logic.temp_beatmap_name, "w+", "utf-8") f.write(beatmap_raw) f.close() f = codecs.open(Logic.temp_beatmap_name, encoding="utf-8") clean_beatmap = p.map(f) f.close() os.remove(Logic.temp_beatmap_name) stars = osu.diff_calc().calc( clean_beatmap, osu.mods_from_str(score.get_mods_string())) pp, _, _, _, acc = osu.ppv2(aim_stars=stars.aim, speed_stars=stars.speed, bmap=clean_beatmap, mods=osu.mods_from_str( score.get_mods_string()), combo=score.max_combo, n300=score.number_300s, n100=score.number_100s, n50=score.number_50s, nmiss=score.misses) return pp, acc
def __init__(self): self.beatmap_parser = pyttanko.parser() self.star_rating = "" self.pp = "" self.accuracy = "" self.acc_if_no_misses = "" self.possible_pp = []
def stdCalc(bmap, count0: int, count50: int, count100: int, count300: int, combo: int, mods: int, perfect: int, max_combo: int): p = pyt.parser() beatmap = p.map(bmap) objcount = beatmap.ncircles + beatmap.nsliders + beatmap.nspinners totalhits = count50 + count100 + count300 sr = pyt.diff_calc().calc(beatmap, mods) pp, _, _, _, _ = pyt.ppv2(sr.aim, sr.speed, bmap=beatmap, mods=mods, n300=count300, n100=count100, n50=count50, nmiss=count0, combo=combo) pp_fc = 0 if perfect == 0: pp_fc, _, _, _, _ = pyt.ppv2(sr.aim, sr.speed, bmap=beatmap, mods=mods, n300=objcount - totalhits, n100=count100, n50=count50, nmiss=0, combo=max_combo) return round(sr.total, 2), round(pp, 2), round(pp_fc, 2)
def __init__(self) -> None: self.setid: int self.id: int self.total_length: int self.hit_length: int self.version: str self.md5: str self.cs: float self.od: float self.ar: float self.hp: float self.mode: int self.artist: str self.title: str self.creator: Player self.max_combo: int self.difficulty: float self.bpm: float self.status: BeatmapStatus self.favs: int self.last_updated: str self.mods: Mods = Mods.NOMOD self._mapfile: Optional[BeatmapParser] = None self.mapfile: Optional[pyttanko.beatmap] = None self.parser: pyttanko.parser = pyttanko.parser() self.embed_pp_values: list[str] = []
def get_pyttanko(bmap_file, mods: int): """Uses pyttanko to parse the map each hitobject contains the strain values. Thanks Francesco""" bmap = pyttanko.parser().map(open(bmap_file)) stars = pyttanko.diff_calc().calc(bmap, mods=mods) return bmap
async def getbmaposu( self, beatmap_id: int, server: osu.Server = osu.Server.BANCHO) -> pyt.beatmap: async with aiohttp.ClientSession() as cs: async with cs.get(server.api_getbmaposu + str(beatmap_id)) as r: return pyt.parser().map(StringIO(await r.text()))
def run(): ERROR_MARGIN = 0.02 '''pp can be off by +- 2% margin is actually 3x for < 100pp, 2x for 100-200, 1.5x for 200-300''' p = pyttanko.parser() bmap = pyttanko.beatmap() stars = pyttanko.diff_calc() try: for s in suite.suite: print_score(s) with open("test/test_suite/%d.osu" % (s.id), "r") as f: p.map(f, bmap=bmap) stars.calc(bmap, s.mods) pp, _, _, _, _ = pyttanko.ppv2(stars.aim, stars.speed, bmap=bmap, mods=s.mods, n300=s.n300, n100=s.n100, n50=s.n50, nmiss=s.nmiss, combo=s.max_combo) margin = s.pp * ERROR_MARGIN if s.pp < 100: margin *= 3 elif s.pp < 200: margin *= 2 elif s.pp < 300: margin *= 1.5 if abs(pp - s.pp) >= margin: pyttanko.info("failed test: got %gpp, expected %g\n" % (pp, s.pp)) exit(1) except KeyboardInterrupt: pass except FileNotFoundError: pyttanko.info("please download the test suite by running " + "./download_suite\n") sys.exit(1) except Exception as e: if p.done: raise else: pyttanko.info("%s\n%s\n" % (traceback.format_exc(), str(p))) sys.exit(1)
def __init__(self, beatmap_id, m, misses, count50, count100, count300, combo, **kwargs): self.beatmap_id = beatmap_id self.mods = int(m) self.misses = int(misses) self.count50 = int(count50) self.count100 = int(count100) self.count300 = int(count300) self.combo = int(combo) self.parser = pyttanko.parser() self.bmap = None self.objcount = 0
def _write_data_to_tfrecord_worker(start, end, data_set, label, path, output): i = start total_writes = 0 filename = '{}_fragment_{}-{}.tfrecords'.format(label, str(start), str(end)) fullpath = os.path.join(path, filename) writer = tf.python_io.TFRecordWriter(fullpath) while i < end: try: # if not i % 50: print('{}: {}/{}'.format(filename, i-start, end-start)) # read beatmap file file = open(data_set[i][1]) p = pyttanko.parser() bmap = p.map(file) audio_data, is44khz = _read_audio(data_set[i][0]) if is44khz: # trim audio based on first beat and last beat first_beat_time = bmap.hitobjects[0].time last_beat_time = bmap.hitobjects[-1].time first_frame = math.floor(first_beat_time / 1000 * utils.sample_rate) last_frame = math.floor(last_beat_time / 1000 * utils.sample_rate) audio_data = audio_data[first_frame:last_frame] total_frames = math.ceil(audio_data.size / utils.frame_step) + 1 beatmap_data = process_beatmap(bmap, total_frames, utils.frame_rate, first_beat_time, mode=0) # audio clip length in seconds audio_clip_len = utils.audio_clip_len for j in range(math.floor(len(audio_data) / utils.sample_rate / audio_clip_len)): start_sample = math.floor(j * utils.sample_rate * audio_clip_len) end_sample = math.floor((j + 1) * utils.sample_rate * audio_clip_len) audio_clip = audio_data[start_sample:end_sample] start_frame = math.floor(start_sample / utils.frame_step) end_frame = math.floor(end_sample / utils.frame_step) - 1 beatmap_clip = beatmap_data[start_frame:end_frame] # print(len(audio_clip), len(beatmap_clip)) feature = {'audio': _float_feature(audio_clip), 'beatmap': _int64_feature(beatmap_clip)} example = tf.train.Example(features=tf.train.Features(feature=feature)) total_writes += 1 writer.write(example.SerializeToString()) except Exception: pass i += 1 output.put(total_writes)
def calc_if(osufile, mods_num, c50, c100, mapcb): p = osu.parser() with open(osufile, 'r', encoding='utf-8') as f: bmap = p.map(f) stars = osu.diff_calc().calc(bmap, mods_num) pp = osu.ppv2( stars.aim, stars.speed, mods=mods_num, n100=c100, n50=c50, nmiss=0, max_combo=mapcb, bmap=bmap ) return pp[0]
def calc_acc_pp(osufile, mods_num): acc_pp = [] p = osu.parser() with open(osufile, 'r', encoding='utf-8') as f: bmap = p.map(f) stars = osu.diff_calc().calc(bmap, mods_num) for acc in range(95, 101): c300, c100, c50 = osu.acc_round(acc, len(bmap.hitobjects), 0) pp, _, _, _, _ = osu.ppv2( stars.aim, stars.speed, mods=mods_num, n300=c300, n100=c100, n50=c50, bmap=bmap ) acc_pp.append(int(pp)) return acc_pp
def get_map_completion(beatmap_id, total_hits): osu_map_path = get_beatmap_path(beatmap_id) p = osu.parser() # with open(os.path.join(osu_map_path), "r", encoding="utf-8") as f: bmap = p.map(f) hitobj = [] if total_hits == 0: total_hits = len(bmap.hitobjects) numobj = total_hits - 1 num = len(bmap.hitobjects) for objects in bmap.hitobjects: hitobj.append(objects.time) timing = int(hitobj[num - 1]) - int(hitobj[0]) point = int(hitobj[numobj]) - int(hitobj[0]) map_completion = (point / timing) * 100 return round_func(map_completion)
def std(map_id, beatmap, mods, combo, n300, n100, n50, nmiss): """Get pp for a Standard play.""" if not beatmap["text"]: return None parser = pyttanko.parser() with io.StringIO(beatmap["text"]) as f: bmap = parser.map(f) stars = pyttanko.diff_calc().calc(bmap, mods) return pyttanko.ppv2( aim_stars=stars.aim, speed_stars=stars.speed, mods=mods, combo=combo, n300=n300, n100=n100, n50=n50, nmiss=nmiss, bmap=bmap, )[0]
def calculate_osu_pp(self, osu_file, mods=0): """ 计算pp :param osu_file: file osu后缀名的文件 :param mods: int 模式 HR HD DT NF等,可调用api进行查看 :param n300: int 300 :param n100: int 100 :param n50: int 50 :param nmiss: int miss :param combo: int 最大的连击数 :return: (str stars星级, str pp点) """ p = osu.parser() with open(osu_file, "r", encoding="utf-8") as f: bmap = p.map(f) stars = osu.diff_calc().calc(bmap) if self.is_search_map is True: pp, _, _, _, _ = osu.ppv2(stars.aim, stars.speed, bmap=bmap, mods=mods) else: pp, _, _, _, _ = osu.ppv2(stars.aim, stars.speed, bmap=bmap, mods=self.int_mods, n300=self.count300, n100=self.count100, n50=self.count50, nmiss=self.countmiss, combo=self.maxcombo) # pp, _, _, _, _ = osu.ppv2( # stars.aim, stars.speed, bmap=bmap, mods=mods, # n300=n300, n100=n100, n50=n50, nmiss=nmiss, # combo=combo # ) return "{:.1f}".format(stars.total), "{}".format(int(pp))
def calc_pp(osufile, mods_num, maxcb, c50, c100, c300, miss): p = osu.parser() with open(osufile, 'r', encoding='utf-8') as f: bmap = p.map(f) stars = osu.diff_calc().calc(bmap, mods_num) map_stars = stars.total pp, aim, speed, acc, accuracy = osu.ppv2( stars.aim, stars.speed, mods=mods_num, combo=maxcb, n300=c300, n100=c100, n50=c50, nmiss=miss, bmap=bmap ) cs = bmap.cs ar = bmap.ar od = bmap.od hp = bmap.hp play_pp = int(round(pp, 2)) aim_pp = int(round(aim, 2)) speed_pp = int(round(speed, 2)) acc_pp = int(round(acc, 2)) accuracy = round(accuracy, 2) return map_stars, cs, ar, od, hp, play_pp, aim_pp, speed_pp, acc_pp, accuracy
def bmapData(self, mapId, modsInt): if self.bmapDict.get("{}+{}".format(mapId, modsInt)) == None: map = requests.get("https://osu.ppy.sh/osu/{}".format(mapId)) map = map.content wrapper = io.BufferedReader(io.BytesIO(map)) wrapper = io.TextIOWrapper(wrapper, encoding="utf-8") parser = osu.parser() bmap = parser.map(wrapper) stars = osu.diff_calc().calc(bmap, modsInt) self.bmapDict["{}+{}".format(mapId, modsInt)] = { "ar": bmap.ar, "od": bmap.od, "maxcombo": bmap.max_combo(), "nobjects": len(bmap.hitobjects), "nsliders": bmap.nsliders, "ncircles": bmap.ncircles, "aim": stars.aim, "speed": stars.speed } with open("bmaps.json", "w") as file: file.write(json.dumps(self.bmapDict, indent=4)) return self.bmapDict["{}+{}".format(mapId, modsInt)] else: return self.bmapDict["{}+{}".format(mapId, modsInt)]
def _prep_data_worker(self, start, end, full_filepaths): """Processes a subset of the compressed beatmap sets in the range [start:end). :param start: An integer describing the start index of filenames to process (inclusive). :param end: An integer describing the end index of filenames to process (exclusive). :param full_filepaths: An list of filepaths of which a subset will be processed by this function. """ session = self.DBSession() i = start while i < end: print('{}: {}/{}'.format(full_filepaths[i], i - start, end - start)) extract_path = os.path.join(self.extract_root, full_filepaths[i].replace(" ", "_")) os.makedirs(extract_path, exist_ok=True) try: with zipfile.ZipFile( os.path.join(self.beatmaps_root, full_filepaths[i]), "r") as zip_ref: zip_info = zip_ref.infolist() # search for all beatmap files for info in zip_info: if '.osu' in info.filename: # extract beatmap data = zip_ref.read(info) bmfile_path = os.path.join( extract_path, os.path.basename(info.filename)) bmfile = open(bmfile_path, 'wb') bmfile.write(data) bmfile.close() # read the beatmap to find related audio file try: file = open(bmfile_path) p = pyttanko.parser() bmap = p.map(file) audio_file_name = bmap.audio_filename.strip( ).lower() audio_path = os.path.join( extract_path, audio_file_name) wave_filepath = audio_path + '.wav' for jifo in zip_info: if jifo.filename.lower( ) == audio_file_name and not os.path.isfile( os.path.join( extract_path, audio_file_name)): # extract audio for beatmap data = zip_ref.read(jifo) audio = open(audio_path, 'wb') audio.write(data) audio.close() # convert to wav stream = ffmpeg.input(audio_path) stream = ffmpeg.output(stream, wave_filepath, ac=1) ffmpeg.run(stream, quiet=True, overwrite_output=True) # calculate difficulty if bmap.mode == 0: file = open(bmfile_path) p = pyttanko.parser() bmap = p.map(file) diff = pyttanko.diff_calc().calc(bmap) file.close() # save metadata to mysql db date = datetime.datetime(*info.date_time[0:6]) new_bm_metadata = BeatmapMetadata( bmFilePath=bmfile_path, audioFilePath=wave_filepath, gamemodeType=bmap.mode, difficulty=diff.total, dateCreated=date) session.add(new_bm_metadata) except Exception as e: print( "error parsing beatmap or audiofile, deleting beatmap: ", e) os.remove(bmfile_path) except zipfile.BadZipFile: print("Bad zipfile: ", full_filepaths[i]) i += 1 session.commit()
def _prep_data_worker(self, start, end, filenames): session = self.DBSession() i = start total_writes = 0 while i < end: print('{}: {}/{}'.format(filenames[i], i - start, end - start)) extract_path = os.path.join(self.extract_root, filenames[i].replace(" ", "_")) os.makedirs(extract_path, exist_ok=True) try: with zipfile.ZipFile(os.path.join(self.beatmaps_root, filenames[i]), "r") as zip_ref: zip_info = zip_ref.infolist() beatmap_list = [] # search for all beatmap files for info in zip_info: if '.osu' in info.filename: # extract beatmap data = zip_ref.read(info) bmfile_path = os.path.join(extract_path, os.path.basename(info.filename)) bmfile = open(bmfile_path, 'wb') bmfile.write(data) bmfile.close() # read the beatmap to find related audio file try: file = open(bmfile_path) p = pyttanko.parser() bmap = p.map(file) audio_file_name = bmap.audio_filename.strip().lower() audio_path = os.path.join(extract_path, audio_file_name) wave_filepath = audio_path + '.wav' for jifo in zip_info: if jifo.filename.lower() == audio_file_name and not os.path.isfile( os.path.join(extract_path, audio_file_name)): # extract audio for beatmap data = zip_ref.read(jifo) audio = open(audio_path, 'wb') audio.write(data) audio.close() # convert to wav stream = ffmpeg.input(audio_path) stream = ffmpeg.output(stream, wave_filepath, ac=1) ffmpeg.run(stream, quiet=True, overwrite_output=True) # calculate difficulty if bmap.mode == 0: file = open(bmfile_path) p = pyttanko.parser() bmap = p.map(file) diff = pyttanko.diff_calc().calc(bmap) file.close() # save metadata to mysql db date = datetime.datetime(*info.date_time[0:6]) new_bm_metadata = BeatmapMetadata(bmFilePath=bmfile_path, audioFilePath=wave_filepath, gamemodeType=bmap.mode, difficulty=diff.total, dateCreated=date) session.add(new_bm_metadata) except Exception as e: print("error parsing beatmap or audiofile, deleting beatmap: ", e) os.remove(bmfile_path) except(zipfile.BadZipFile): print("Bad zipfile: ", filenames[i]) i += 1 session.commit()
async def get_pyttanko(self, map_id: str, accs=[100], mods=0, misses=0, combo=None, completion=None, fc=None): url = 'https://osu.ppy.sh/osu/{}'.format(map_id) file_path = os.getcwd() + '/temp/{}.osu'.format(map_id) await self.download_file(url, file_path) bmap = pyttanko.parser().map(open(file_path)) _, ar, od, cs, hp = pyttanko.mods_apply(mods, ar=bmap.ar, od=bmap.od, cs=bmap.cs, hp=bmap.hp) stars = pyttanko.diff_calc().calc(bmap, mods=mods) bmap.stars = stars.total bmap.aim_stars = stars.aim bmap.speed_stars = stars.speed if not combo: combo = bmap.max_combo() bmap.pp = [] bmap.aim_pp = [] bmap.speed_pp = [] bmap.acc_pp = [] bmap.acc = accs for acc in accs: n300, n100, n50 = pyttanko.acc_round(acc, len(bmap.hitobjects), misses) pp, aim_pp, speed_pp, acc_pp, _ = pyttanko.ppv2(bmap.aim_stars, bmap.speed_stars, bmap=bmap, mods=mods, n300=n300, n100=n100, n50=n50, nmiss=misses, combo=combo) bmap.pp.append(pp) bmap.aim_pp.append(aim_pp) bmap.speed_pp.append(speed_pp) bmap.acc_pp.append(acc_pp) if fc: n300, n100, n50 = pyttanko.acc_round(fc, len(bmap.hitobjects), 0) # print("-------------", n300, n100, n50) fc_pp, _, _, _, _ = pyttanko.ppv2(bmap.aim_stars, bmap.speed_stars, bmap=bmap, mods=mods, n300=n300 + misses, n100=n100, n50=n50, nmiss=0, combo=bmap.max_combo()) pyttanko_json = { 'version': bmap.version, 'title': bmap.title, 'artist': bmap.artist, 'creator': bmap.creator, 'combo': combo, 'max_combo': bmap.max_combo(), 'misses': misses, 'mode': bmap.mode, 'stars': bmap.stars, 'aim_stars': bmap.aim_stars, 'speed_stars': bmap.speed_stars, 'pp': bmap.pp, # list 'aim_pp': bmap.aim_pp, 'speed_pp': bmap.speed_pp, 'acc_pp': bmap.acc_pp, 'acc': bmap.acc, # list 'cs': cs, 'od': od, 'ar': ar, 'hp': hp } if completion: try: pyttanko_json['map_completion'] = (completion / len(bmap.hitobjects)) * 100 except: pyttanko_json['map_completion'] = "Error" os.remove(file_path) return pyttanko_json
import pyttanko import replay_parser p = pyttanko.parser() replay = replay_parser.open_file("./temp/wind.osr").replay with open("./temp/664841/Hard.osu") as f: mp = p.map(f) rep = replay_parser.ScoreReplay(mp, replay) a = rep.generate_score(mp.od, mp.cs, 1.5, 3) cir = a[a["object"] == "circle"] avg = cir.loc[:, "displacement"].sum() / len(cir) # revs = a.iloc[0].loc["displacement"] # u = revs.sum() / len(revs) print("miss's", len(a[a["hit"] == 0]) == 0, len(a[a["hit"] == 0])) print("50's", len(a[a["hit"] == 50]) == 3, len(a[a["hit"] == 50])) print("100's", len(a[a["hit"] == 100]) == 17, len(a[a["hit"] == 100])) print("300's", len(a[a["hit"] == 300]) == 224, len(a[a["hit"] == 300])) print("combo", a["combo"].max() == 429, a["combo"].max()) print(avg) # print(a) # print(u) # import pandas as pd
def getObjectCount(bmap): p = pyt.parser() beatmap = p.map(bmap) return beatmap.ncircles + beatmap.nsliders + beatmap.nspinners
def calculatePlay(bmap, mode: int = 0, count0: int = 0, count50: int = 0, count100: int = 0, count300: int = 0, countgeki: int = 0, countkatu: int = 0, combo: int = 0, mods: int = 0, perfect: int = 0, calcPP: int = 1): if mode == 0: #Standard modString = pyt.mods_str(mods) accuracy = acc.stdCalc(count0, count50, count100, count300) p = pyt.parser() beatmap = p.map(bmap) objcount = beatmap.ncircles + beatmap.nsliders + beatmap.nspinners totalhits = count50 + count100 + count0 sr = pyt.diff_calc().calc(beatmap, mods) pp = 0 if calcPP == 1: pp, _, _, _, _ = pyt.ppv2(sr.aim, sr.speed, bmap=beatmap, mods=mods, n300=count300, n100=count100, n50=count50, nmiss=count0, combo=combo) pp_fc = 0 if perfect == 0: pp_fc, _, _, _, _ = pyt.ppv2(sr.aim, sr.speed, bmap=beatmap, mods=mods, n300=objcount - totalhits, n100=count100, n50=count50, nmiss=0, combo=beatmap.max_combo()) accuracy_fc = acc.stdCalc(0, count50, count100, objcount - totalhits) beatmapDict = { "title": beatmap.title, "artist": beatmap.artist, "creator": beatmap.creator, "version": beatmap.version, "objcount": objcount, "mode": beatmap.mode, "maxcombo": beatmap.max_combo() } playDict = { "totalhits": totalhits + count0, "pp": round(pp, 2), "pp_fc": round(pp_fc, 2), "accuracy": accuracy, "accuracy_fc": 0 if perfect == 1 else accuracy_fc, "modString": modString if modString != "nomod" else "NM", "rating": round(sr.total, 2), "completion": round(((totalhits + count300) * 100) / objcount, 2), "mode_icon": "https://i.imgur.com/lT2nqls.png", "mode_name": "Standard" } elif mode == 1: #Taiko p = pyt.parser() beatmap = p.map(bmap) beatmapDict = { "title": beatmap.title, "artist": beatmap.artist, "creator": beatmap.creator, "version": beatmap.version, "objcount": 0, "mode": beatmap.mode, "maxcombo": None } playDict = { "totalhits": 0, "pp": "Not implemented.", "pp_fc": 0, "accuracy": acc.taikoCalc(count0, count100, count300), "accuracy_fc": 0 if perfect == 1 else acc.taikoCalc(0, count100, count300 + count0), "modString": modString if modString != "nomod" else "NM", "rating": "N/A", "completion": 100, "mode_icon": "https://i.imgur.com/G6bzM0X.png", "mode_name": "Taiko" } elif mode == 2: #CTB accuracy = acc.ctbCalc(count0, countkatu, count50, count100, count300) beatmap = Beatmap(bmap) p = pyt.parser() beatmapMetadata = p.map(bmap) difficulty = Difficulty(beatmap, mods) pp = 0 if calcPP == 1: pp = round(calculate_pp(difficulty, accuracy, combo, count0), 2) beatmapDict = { "title": beatmapMetadata.title, "artist": beatmapMetadata.artist, "creator": beatmapMetadata.creator, "version": beatmapMetadata.version, "objcount": len(beatmap.hitobjects), "mode": beatmapMetadata.mode, "maxcombo": beatmap.max_combo } playDict = { "totalhits": 0, "pp": pp, "pp_fc": 0, "accuracy": accuracy, "accuracy_fc": 0, "modString": modString if modString != "nomod" else "NM", "rating": round(difficulty.star_rating, 2), "completion": 100, "mode_icon": "https://i.imgur.com/EsanYkH.png", "mode_name": "Catch the Beat" } elif mode == 3: #Mania p = pyt.parser() beatmap = p.map(bmap) beatmapDict = { "title": beatmap.title, "artist": beatmap.artist, "creator": beatmap.creator, "version": beatmap.version, "objcount": 0, "mode": beatmap.mode, "maxcombo": None } playDict = { "totalhits": 0, "pp": "Not implemented.", "pp_fc": 0, "accuracy": acc.maniaCalc(count0, count50, count100, countkatu, count300, countgeki), "accuracy_fc": 0, "modString": modString if modString != "nomod" else "NM", "rating": "N/A", "completion": 100, "mode_icon": "https://i.imgur.com/0uZM1PZ.png", "mode_name": "Mania" } return beatmapDict, playDict
def _write_data_to_tfrecord_worker(start, end, data_set, label, path, output): i = start total_writes = 0 filename = '{}_fragment_{}-{}.tfrecords'.format(label, str(start), str(end)) fullpath = os.path.join(path, filename) writer = tf.python_io.TFRecordWriter(fullpath) while i < end: try: # if not i % 50: print('{}: {}/{}'.format(filename, i - start, end - start)) # read beatmap file file = open(data_set[i][1]) p = pyttanko.parser() bmap = p.map(file) audio_data, is44khz = _read_audio(data_set[i][0]) if is44khz: # trim audio based on first beat and last beat first_beat_time = bmap.hitobjects[0].time last_beat_time = bmap.hitobjects[-1].time first_frame = math.floor(first_beat_time / 1000 * utils.sample_rate) last_frame = math.floor(last_beat_time / 1000 * utils.sample_rate) audio_data = audio_data[first_frame:last_frame] total_frames = math.ceil( audio_data.size / utils.frame_step) + 1 beatmap_data = process_beatmap(bmap, total_frames, utils.frame_rate, first_beat_time, mode=0) # audio clip length in seconds audio_clip_len = utils.audio_clip_len for j in range( math.floor( len(audio_data) / utils.sample_rate / audio_clip_len)): start_sample = math.floor(j * utils.sample_rate * audio_clip_len) end_sample = math.floor( (j + 1) * utils.sample_rate * audio_clip_len) audio_clip = audio_data[start_sample:end_sample] start_frame = math.floor(start_sample / utils.frame_step) end_frame = math.floor(end_sample / utils.frame_step) - 1 beatmap_clip = beatmap_data[start_frame:end_frame] # print(len(audio_clip), len(beatmap_clip)) feature = { 'audio': _float_feature(audio_clip), 'beatmap': _int64_feature(beatmap_clip) } example = tf.train.Example(features=tf.train.Features( feature=feature)) total_writes += 1 writer.write(example.SerializeToString()) except Exception: pass i += 1 output.put(total_writes)
def _write_data_to_tfrecord_worker(start, end, data_set, label, path, output): """ Processes audio and beatmap files, writing a tfrecord shard :param start: an integer describing the start index of the data_set to process (inclusive). :param end: an integer describing the end index of the data_set to process (exclusive). :param data_set: a list of tuples, (audio_filepath, beatmap_filepath) :param label: a string that represents the type of data (train, val, test) :param path: the directory to write the tfrecord file :param output: a Queue object from the python multiprocesing library """ i = start total_writes = 0 filename = '{}_fragment_{}-{}.tfrecords'.format(label, str(start), str(end)) fullpath = os.path.join(path, filename) writer = tf.python_io.TFRecordWriter(fullpath) while i < end: try: # if not i % 50: print('{}: {}/{}'.format(filename, i-start, end-start)) # read beatmap file file = open(data_set[i][1]) p = pyttanko.parser() bmap = p.map(file) audio_data, is44khz = _read_audio(data_set[i][0]) if is44khz: # trim audio based on first beat and last beat first_beat_time = bmap.hitobjects[0].time last_beat_time = bmap.hitobjects[-1].time first_frame = math.floor(first_beat_time / 1000 * utils.sample_rate) last_frame = math.floor(last_beat_time / 1000 * utils.sample_rate) audio_data = audio_data[first_frame:last_frame] total_frames = math.ceil(audio_data.size / utils.frame_step) + 1 beatmap_data = process_beatmap(bmap, total_frames, utils.frame_rate, first_beat_time, beat_divison=4, mode=0) # audio clip length in seconds audio_clip_len = utils.audio_clip_len for j in range(math.floor(len(audio_data) / utils.sample_rate / audio_clip_len)): start_sample = math.floor(j * utils.sample_rate * audio_clip_len) end_sample = math.floor((j + 1) * utils.sample_rate * audio_clip_len) audio_clip = audio_data[start_sample:end_sample] start_frame = math.floor(start_sample / utils.frame_step) end_frame = math.floor(end_sample / utils.frame_step) - 1 beatmap_clip = beatmap_data[start_frame:end_frame] # print(len(audio_clip), len(beatmap_clip)) feature = {'audio': _float_feature(audio_clip), 'beatmap': _int64_feature(beatmap_clip)} example = tf.train.Example(features=tf.train.Features(feature=feature)) total_writes += 1 writer.write(example.SerializeToString()) except Exception: pass i += 1 output.put(total_writes)
def generateData(): #get random beatmap dir folder = path.dirname(__file__) data_folder = path.join(folder, "Training Songs") beatmap_folder = path.join(data_folder, random.choice(os.listdir(data_folder))) beatmap_list = [] for f in os.listdir(beatmap_folder): if f[-4:] == ".osu": beatmap_list.append(f) beatmap_path = path.join(beatmap_folder, random.choice(beatmap_list)) #open the beatmap beatmap = OsuReader.readBeatmap(beatmap_path) p = pyttanko.parser() bmap = p.map(open(beatmap_path, 'rt', encoding="utf8")) #find the audio audio_path = path.join(beatmap_folder, beatmap.AudioFilename) #open the audio wav_path = path.join(beatmap_folder, "audio.wav.wav") if not path.exists(wav_path): subprocess.call([ 'ffmpeg', '-i', audio_path, "-ar", str(sampling_rate), "-ac", "1", path.join(beatmap_folder, "audio.wav.wav") ]) audio = read(wav_path)[1] # audio = np.divide(audio, np.full((len(audio)), 32767, np.float64), dtype=np.float64) #find the star rating difficulty = pyttanko.diff_calc().calc(bmap) stars = np.array(difficulty.total).reshape([1, 1]) print(beatmap.Title, beatmap.Version) #make list of all timings and delays between timings in beatmap delay_list = [] timing_list = [] current_time = 0 for o in beatmap.HitObjects: if o.csstype == "circle": delay_list.append(o.time - current_time) current_time = o.time timing_list.append(current_time) elif o.csstype == "slider": delay_list.append(o.time - current_time) current_time = o.time timing_list.append(current_time) for t in range(o.repeat): delay_list.append(o.endTime) current_time += o.endTime timing_list.append(current_time) elif o.csstype == "spinner": delay_list.append(o.time - current_time) current_time = o.time timing_list.append(current_time) delay_list.append(o.endTime - current_time) current_time = o.endTime timing_list.append(current_time) #make x and y song_length = len(audio) / sampling_rate * 1000 audio = np.concatenate( (audio, np.zeros(audio_batch_size, dtype=np.float64)), axis=0) audio = np.concatenate( (np.zeros(audio_batch_size, dtype=np.float64), audio), axis=0) current_time = 0 current_timing = 0 array_x = [] array_y = [] array_label = [] while current_time < song_length: audio_index = int(current_time / 1000 * sampling_rate) if current_timing < len(timing_list): distance = timing_list[current_timing] - current_time if distance < place_range: array_label.append(np.array([1])) array_y.append(np.array(distance)) array_x.append(audio[audio_index:audio_index + audio_batch_size]) current_time = timing_list[current_timing] current_timing += 1 else: array_label.append(np.array([0])) array_y.append(np.array(1000)) array_x.append(audio[audio_index:audio_index + audio_batch_size]) current_time += 1000 else: array_label.append(np.array([0])) array_y.append(np.array(1000)) array_x.append(audio[audio_index:audio_index + audio_batch_size]) current_time += 1000 while not len(array_x) % truncated_backprop_length == 0: array_label.append(np.array([0])) array_y.append(np.array(1000)) array_x.append(np.zeros(audio_batch_size, dtype=np.float64)) x = np.vstack(array_x) y = np.vstack(array_y) labels = np.vstack(array_label) return (x, y, stars, labels)