Created
April 28, 2017 18:23
-
-
Save macrat/0f6e8635fcf4dd0f18454c413eb92047 to your computer and use it in GitHub Desktop.
長さの違う音声ファイルのうち、重なっている部分を探すやつ。WIP。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import sys | |
import librosa | |
def scores(longer, shorter): | |
scores = [] | |
for i in range(len(longer) - len(shorter)): | |
print('\r{}/{}\033[K'.format(i, len(longer) - len(shorter)), end='') | |
sys.stdout.flush() | |
scores.append((i, sum((shorter - longer[i:i+len(shorter)]) ** 2))) | |
print('\r\033[K', end='') | |
return scores | |
def find(a, b, sample_rate): | |
print('resampling A...') | |
a = librosa.resample(a[0], a[1], sample_rate) | |
print('resampling B...') | |
b = librosa.resample(b[0], b[1], sample_rate) | |
print('done') | |
print('calcurating scores...') | |
ss = scores(a, b) | |
for x in ss: | |
print(x[0] / sample_rate, ',', x[1], file=sys.stderr) | |
ss.sort(key=lambda x: x[1]) | |
print('done') | |
candidates = [] | |
for shift, score in ss[:10]: | |
time = datetime.timedelta(seconds=shift / sample_rate) | |
print('{0}: {1}'.format(time, score)) | |
candidates.append(time) | |
return candidates | |
if __name__ == '__main__': | |
print('loading A...') | |
a = librosa.load('a.mp3') | |
print('done: {}'.format(datetime.timedelta(seconds=len(a[0])/a[1]))) | |
print('loading B...') | |
b = librosa.load('b.mp3') | |
print('done: {}'.format(datetime.timedelta(seconds=len(b[0])/b[1]))) | |
if a[1] != b[1]: | |
print('sample rate deferent.') | |
print('A: {}, B: {}'.format(a[1], b[1])) | |
sys.exit(1) | |
if len(a[0]) < len(b[0]): | |
print('A is shorter than B.') | |
print('A: {}, B: {}'.format(datetime.timedelta(seconds=len(a[0])/a[1]), datetime.timedelta(seconds=len(b[0])/b[1]))) | |
sys.exit(1) | |
find(a, b, 512) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment