-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundFont samples rename v0.2.py
97 lines (83 loc) · 3 KB
/
SoundFont samples rename v0.2.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 29 17:52:54 2018
@author: Luiz Guilherme de M. Ventura
"""
import os
import glob
import re
from scipy.io.wavfile import read as wvrd
import numpy as np
import matplotlib.pyplot as plt
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ atoi(c) for c in re.split('(\d+)', text) ]
from math import log2, pow
# https://www.johndcook.com/blog/2016/02/10/musical-pitch-notation/
A4 = 440
C0 = A4*pow(2, -4.75)
name = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
def pitch(freq):
h = round(12*log2(freq/C0))
octave = h // 12
n = h % 12
return name[n] + str(octave)
##-----------------------------
p = input("Enter the path to the directory with the files: ")
#if p[-1] == '\"' or p[-1] == '\'':
# p = p[:-1]
# p = p + '/' + p[0] # insert a / if it is missing
#else:
# p = p + '/'
p = p + '/'
p = p.replace('\'', '')
p = p.replace('\"','')
ext = input("Enter the file extension (like .wav) ")
listFiles = glob.glob(p + '*' + ext)
s = input("Enter the name prefix with a # where the numeration is: ")
l = input("How long is the number string? ")
#n = input("How many files in total? ")
print("So, your files are in the form: {",)
for i in range(3):
print(s.replace('#','%.{}d'.format(l) % (i+1)) + ext + ', ',)
print("}")
cfrm = input("Confirm? [y/n]: ")
listFiles.sort(key=natural_keys)
if cfrm == 'y':
i = 1
last_pitch = ''
for file in listFiles:
wv = wvrd(file)
wv_left = wv[1][:,0]
wv_right = wv[1][:,1]
srate = wv[0]
dt = 1.0/srate
wv_left_f = abs(np.fft.fft(wv_left))[0:int(len(wv_left)/2)]
wv_left_fn = wv_left_f/np.max(wv_left_f)
frq = wv_left_f/(len(wv_left)*dt)
thr = 0.06 # threshold for first harmonic
f_1harmonic = np.argmax(wv_left_fn > 0.06)/(len(wv_left)*dt)
current_pitch = pitch(f_1harmonic)
if last_pitch != current_pitch:
i = 1
cfrm2 = input(file[-12:] + " was identified as " + current_pitch + " vel " + str(i) + ". Proceed? [y/n]")
else:
i += 1
cfrm2 = input(file[-12:] + " was identified as " + current_pitch + " and last pitch was the same, so vel = " + str(i) + ". Proceed? [y/n]")
if cfrm2 == 'y':
os.rename(file, p + current_pitch + s.replace('#', '') + str(i) + ext)
print(file + ' -> ' + p + current_pitch + s.replace('#', '') + str(i) + ext)
last_pitch = current_pitch
else:
nt = input("So, which note should it be? ")
i = input("vel: ")
os.rename(file, p + str(nt) + s.replace('#', '') + str(i) + ext)
print(file + ' -> ' + p + str(nt) + s.replace('#', '') + str(i) + ext)
last_pitch = current_pitch