Repository navigation
Expand file tree
/
Copy pathFilter.py
More file actions
150 lines (128 loc) · 6.6 KB
/
Copy pathFilter.py
File metadata and controls
150 lines (128 loc) · 6.6 KB
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
150
#! /usr/bin/env python
import sys, os.path
import aubio
import numpy as np
import matplotlib.pyplot as plt
import shutil
def lowpassfilter(inputfile, outputlow):
original = aubio.source(inputfile) # Open the original file
samplerate = original.samplerate # get samplerate for output file
writetofile = aubio.sink(outputlow, samplerate) # Create Output sink
# Biquad Filter as Low-Pass-Filter
# Center Frequency 150
# Samplerate 41000
# Q 0.75
calculatefilter = aubio.digital_filter(3)
calculatefilter.set_biquad(1.30104102 * 10 ** -4, 2.60208205 * 10 ** -4, 1.30104102 * 10 ** -4, -1.96929513, 0.96981555)
while True:
samples, read = original()
writetofile(calculatefilter(samples), read) # Calculate the filter and write it back to the file
if read < original.hop_size:
break
aubio.sink.close(writetofile)
def bandpassfilter(inputfile, outputlow):
original = aubio.source(inputfile) # Open the original file
samplerate = original.samplerate # get samplerate for output file
writetofile = aubio.sink(outputlow, samplerate) # Create Output sink
# Biquad Filter as Band-Pass-Filter (Combined Low-Pass-Filter and High-Pass-Filter for a better function
# Low-Pass-Filter
# Center Frequency 6000
# Samplerate 41000
# Q 0.7
# High-Pass-Filter
# Center Frequency 200
# Samplerate 41000
# Q 0.7
calculatelowfilter = aubio.digital_filter(3)
calculatelowfilter.set_biquad(0.12556056, 0.25112113, 0.12556056, -0.77321399, 0.27545624)
calculatehighfilter = aubio.digital_filter(3)
calculatehighfilter.set_biquad(0.97834987, -1.95669974, 0.97834987, -1.95624013, 0.95715934)
while True:
samples, read = original()
lowresult = calculatelowfilter(samples) # Calculate the Low-Pass-Filter
writetofile(calculatehighfilter(lowresult), read) # Calculate the High-Pass-Filter and write it back to the file
if read < original.hop_size:
break
aubio.sink.close(writetofile)
def highpassfilter(inputfile, outputlow):
original = aubio.source(inputfile) # Open the original file
samplerate = original.samplerate # get samplerate for output file
writetofile = aubio.sink(outputlow, samplerate) # Create Output sink
# Biquad Filter as high-Pass-Filter
# Center Frequency 6000
# Samplerate 41000
# Q 0.7
calculatefilter = aubio.digital_filter(3)
calculatefilter.set_biquad(0.51216756, -1.02433511, 0.51216756, -0.77321399, 0.27545624)
while True:
samples, read = original()
writetofile(calculatefilter(samples), read) # Calculate the filter and write it back to the file
if read < original.hop_size:
break
aubio.sink.close(writetofile)
if __name__ == '__main__':
os.makedirs('Musikgefiltert')
musicpath = os.getcwd()+"\\Musik" #Set Path to musicfiles
orig_stdout = sys.stdout #List all Songs from folder Musik
f = open('Song.txt', 'w')
sys.stdout = f
print(os.listdir(musicpath)) #Write it into Song.txt
sys.stdout = orig_stdout
f = open('Song.txt', 'r')
txt = f.readline()
txtcontent = ""
f.close()
for i in range(0, len(txt)):
if i != 0 and i != len(txt)-2:
txtcontent = txtcontent+txt[i]
os.remove('Song.txt')
txtcontent = txtcontent.replace("'", "")
txtcontent = txtcontent.replace(", ","\n")
p = open('Songs.txt', 'w')
p.write(txtcontent)
p.close()
p = open('Songs.txt', 'r')
os.chdir(musicpath) #Change working directory for filters
while True:
musicnametemp = p.readline() #Read lines in Songs.txt
if musicnametemp == "": #If p.readline is empty exit while
break
musicname = ""
for i in range(0, len(musicnametemp)):
if i < len(musicnametemp)-5:
musicname = musicname+musicnametemp[i]
inputfile = musicname+'.wav'
outputlowpass = musicname+' lowpass.wav'
outputlowpasstemp1 = musicname+' lowpasstemp1.wav'
outputlowpasstemp2 = musicname+' lowpasstemp2.wav'
outputbandpass = musicname+' bandpass.wav'
outputbandpasstemp1 = musicname+' bandpasstemp1.wav'
outputbandpasstemp2 = musicname+' bandpasstemp2.wav'
outputhighpass = musicname+' highpass.wav'
outputhighpasstemp1 = musicname+' highpasstemp1.wav'
outputhighpasstemp2 = musicname+' highpasstemp2.wav'
#bpmdetection(inputfile)
lowpassfilter(inputfile, outputlowpass) # Cascading 4 Biquad Low-Pass-Filter to get a sharp cut
lowpassfilter(outputlowpass, outputlowpasstemp1)
lowpassfilter(outputlowpasstemp1, outputlowpasstemp2)
lowpassfilter(outputlowpasstemp2, outputlowpass)
os.remove(musicname+' lowpasstemp1.wav')
os.remove(musicname+' lowpasstemp2.wav')
bandpassfilter(inputfile, outputbandpass) # Cascading 4 Biquad Band-Pass-Filter to get a sharp cut
bandpassfilter(outputbandpass, outputbandpasstemp1)
bandpassfilter(outputbandpasstemp1, outputbandpasstemp2)
bandpassfilter(outputbandpasstemp2, outputbandpass)
os.remove(musicname+' bandpasstemp1.wav')
os.remove(musicname+' bandpasstemp2.wav')
highpassfilter(inputfile, outputhighpass) # Cascading 4 Biquad High-Pass-Filter to get a sharp cut
highpassfilter(outputhighpass, outputhighpasstemp1)
highpassfilter(outputhighpasstemp1, outputhighpasstemp2)
highpassfilter(outputhighpasstemp2, outputhighpass)
os.remove(musicname+' highpasstemp1.wav')
os.remove(musicname+' highpasstemp2.wav') #Todo: Schöner machen
shutil.move(outputlowpass, '../Musikgefiltert') #Move filtered files to folder Musikgefiltert
shutil.move(outputbandpass, '../Musikgefiltert')
shutil.move(outputhighpass, '../Musikgefiltert')
p.close()
os.chdir('../') #Change directory back to the old folder
os.remove('Songs.txt') #Clean environment