Spaces:
Running
Running
File size: 4,204 Bytes
9df2e22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
from typing import List
import numpy as np
from librosa.util import peak_pick
from preprocess import *
def smooth(x, window_len=11, window="hanning"):
if x.ndim != 1:
raise ValueError
if x.size < window_len:
raise ValueError
if window_len < 3:
return x
if not window in ["flat", "hanning", "hamming", "bartlett", "blackman"]:
raise ValueError
s = np.r_[x[window_len - 1 : 0 : -1], x, x[-2 : -window_len - 1 : -1]]
# print(len(s))
if window == "flat": # moving average
w = np.ones(window_len, "d")
else:
w = eval("np." + window + "(window_len)")
y = np.convolve(w / w.sum(), s, mode="valid")
return y
def detect(don_inference, ka_inference, delta=0.05):
don_inference = smooth(don_inference, 5)
ka_inference = smooth(ka_inference, 5)
don_timestamp = (
peak_pick(
x=don_inference,
pre_max=1,
post_max=2,
pre_avg=4,
post_avg=5,
delta=delta,
wait=3,
)
+ 7
) # 実際は7フレーム目のところの音
ka_timestamp = (
peak_pick(
x=ka_inference,
pre_max=1,
post_max=2,
pre_avg=4,
post_avg=5,
delta=delta,
wait=3,
)
+ 7
)
print(don_timestamp)
print(ka_timestamp)
don_timestamp = don_timestamp[
np.where(don_inference[don_timestamp] > ka_inference[don_timestamp])
]
ka_timestamp = ka_timestamp[
np.where(ka_inference[ka_timestamp] > don_inference[ka_timestamp])
]
return don_timestamp, ka_timestamp
# def note_to_drumroll(timestamp, max_gap=5, min_note=3):
# drumroll = []
# note = 0
# for i in range(1, len(timestamp)):
# if timestamp[i] - timestamp[i - 1] <= max_gap:
# note += 1
# else:
# if note >= min_note:
# drumroll.append((timestamp[i - note - 1], timestamp[i - 1]))
# note = 0
# if note >= min_note:
# drumroll.append((timestamp[-note - 1], timestamp[-1]))
# return drumroll
def synthesize(don_timestamp, ka_timestamp, song, filepath):
song.don_timestamp = don_timestamp
song.timestamp = song.don_timestamp * 512 / song.samplerate
# print(len(song.timestamp))
song.synthesize(diff="don")
# song.ka_timestamp = song.don_timestamp
song.ka_timestamp = ka_timestamp
song.timestamp = song.ka_timestamp * 512 / song.samplerate
# print(len(song.timestamp))
song.synthesize(diff="ka")
song.save(filepath)
def create_tja(
song,
timestamps: List[tuple],
title="untitled",
subtitle="--",
wave="untitled.ogg",
safezone=2,
):
tja = f"TITLE: {title}\nSUBTITLE: {subtitle}\nBPM: 240\nWAVE:{wave}\nOFFSET:0\n\n"
for i, (don, ka) in enumerate(timestamps):
try:
level = [3, 5, 7, 8, 9][i]
scroll = [0.6, 0.7, 0.8, 0.9, 1.0][i]
don_timestamp = np.rint(don * 512 / song.samplerate * 100).astype(np.int32)
ka_timestamp = np.rint(ka * 512 / song.samplerate * 100).astype(np.int32)
length = np.max(
(
don_timestamp[-1] if don_timestamp.size > 0 else 0,
ka_timestamp[-1] if ka_timestamp.size > 0 else 0,
)
)
safezone_keep = 0
tja += f"COURSE:{i}\nLEVEL:{level}\n\n#START\n#SCROLL {scroll}\n"
for time in range(length):
if np.isin(time, don_timestamp) == True and safezone_keep <= 0:
tja += "1"
safezone_keep = safezone
elif np.isin(time, ka_timestamp) == True and safezone_keep <= 0:
tja += "2"
safezone_keep = safezone
else:
tja += "0"
safezone_keep -= 1
if time % 100 == 99:
tja += ",\n"
if length % 100 != 0:
tja += "0" * (100 - (length % 100)) + ",\n"
tja += "#END\n\n"
except:
pass
return tja
|