-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundFont samples rename v0.2.3.py
132 lines (118 loc) · 4.83 KB
/
SoundFont samples rename v0.2.3.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
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
#!/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)
def isInt(s):
try:
int(s)
return True
except ValueError:
return False
##-----------------------------
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]: ")
num_seq = input("Is there a fixed number of sequences? If so, how many? ")
listFiles.sort(key=natural_keys)
if cfrm == 'y':
i = 1
last_pitch = ''
for k, file in enumerate(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)
f_1harmonic = np.argmax(wv_left_fn > 0.06)/(len(wv_left)*dt) # correction
m_1harmonic = np.max(wv_left_fn > 0.06) # correction
f_1harmonic_l = (np.argmax(wv_left_fn > 0.06) - 1)/(len(wv_left)*dt) # correction
m_1harmonic_l = wv_left_fn[np.argmax(wv_left_fn > 0.06) - 1] # correction
f_1harmonic_r = (np.argmax(wv_left_fn > 0.06) + 1)/(len(wv_left)*dt) # correction
m_1harmonic_r = wv_left_fn[np.argmax(wv_left_fn > 0.06) + 1] # correction
f_1harmonic = (f_1harmonic*m_1harmonic + f_1harmonic_l*m_1harmonic_l + f_1harmonic_r*m_1harmonic_r)/(m_1harmonic + m_1harmonic_l + m_1harmonic_r)
current_pitch = pitch(f_1harmonic)
if num_seq == 'n':
if last_pitch != current_pitch:
#i = 1
cfrm2 = input(file[-12:] + " was identified as " + current_pitch + " seq " + str(i) + ". Proceed? [y/n]")
if cfrm2 == 'y':
i = 1
else:
i += 1
cfrm2 = input(file[-12:] + " was identified as " + current_pitch + " and last pitch was the same, so seq = " + 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? ")
if last_pitch == nt:
i += 1
print("seq proceeding the count: " + str(i))
current_pitch = nt # so that it uses the actual pitch corrected by user for the count
else:
i = int(input("seq: "))
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
elif isInt(num_seq):
if k % int(num_seq) == 0:
i = 1
cfrm2 = input(file[-12:] + " was identified as " + current_pitch + " seq " + str(i) + ". Proceed? [y/n]")
if cfrm2 == 'y':
nt = current_pitch
else:
nt = input("So, which note should it be? ")
os.rename(file, p + str(nt) + s.replace('#', '') + str(i) + ext)
print(file + ' -> ' + p + str(nt) + s.replace('#', '') + str(i) + ext)
i += 1
else:
print("Not valid…")