-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
171 lines (131 loc) · 4.89 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import numpy as np
import tkinter as tk
from tkinter import filedialog
import soundfile as sf
from scipy.signal import fftconvolve
import matplotlib.pyplot as plt
def get_audio_file_directory():
"""Open a file dialog to select an audio file and return the directory."""
# Create a hidden root window
root = tk.Tk()
root.withdraw() # Hide the main window
file_path = filedialog.askopenfilename(
title="Select an audio file",
filetypes=[("Audio Files", "*.mp3 *.wav *.aac *.ogg *.flac")],
)
if file_path:
# Get the directory name
return file_path
else:
print("No file was selected.")
return None
def load_audio(filename):
"""Load and audio file."""
signal, sample_rate = sf.read(filename)
return signal, sample_rate
def normalize_audio(signal):
"""Normalize the signal to range [-1, 1]."""
return signal / np.max(np.abs(signal))
def clip_signal(signal):
"""Clip the signal to the range [-1, 1]."""
return np.clip(signal, -1.0, 1.0)
def convolve_signals(signal, impulse_response):
"""Convolve the audio signal with the impulse_response."""
return fftconvolve(signal, impulse_response, mode='full')
def save_audio(filename, signal, sample_rate):
"""Save the convolved signal to a file."""
sf.write(filename, signal, sample_rate)
def plot_signals(original, convolved):
"""Plot the original and convolved signals."""
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.title("Original Signal")
plt.plot(original)
plt.xlabel('Sample Number')
plt.ylabel('Amplitude')
plt.subplot(2, 1, 2)
plt.title("Convolved Signal")
plt.plot(convolved)
plt.xlabel('Sample Number')
plt.ylabel('Amplitude')
plt.tight_layout()
plt.show()
# Impulse Response Generators
def sinc_filter(length, cutoff):
"""Generate a sinc filter as an impulse response."""
# Cutoff 0.1 - 1.0
t = np.linspace(-length // 2, length // 2, length)
impulse_response = cutoff * np.sinc(cutoff * t)
# Normalize
return impulse_response
def gaussian_impulse_response(length, std_dev):
# Generate a Gaussian impulse response
t = np.linspace(-length // 2, length // 2, length)
impulse_response = np.exp(-0.5 * (t / std_dev) ** 2)
return impulse_response
def exponential_decay(length, decay_rate):
# Generate an exponential decay impulse response.
t = np.linspace(0, length, length)
impulse_response = np.exp(-decay_rate * t)
return impulse_response
def impulse_selector():
# Select one from a list of impulse responses
length = 256
root = tk.Tk()
root.withdraw()
choice = tk.simpledialog.askstring(
"Impulse Response Choice",
"Type to choose: (s)sinc filter, (g)gaussian, (e)exponential decay"
).strip().lower()
if choice == 's':
cutoff = 1
impulse_response = sinc_filter(length, cutoff)
return impulse_response
elif choice == 'g':
std_dev = 50
impulse_response = gaussian_impulse_response(length, std_dev)
return impulse_response
elif choice == 'e':
decay_rate = 0.1
impulse_response = exponential_decay(length, decay_rate)
return impulse_response
def main():
# Load the audio signal and impulse response
audio_file = get_audio_file_directory()
print(f"Selected audio file: {audio_file}")
signal, sample_rate = load_audio(audio_file)
root = tk.Tk()
root.withdraw()
choice = tk.simpledialog.askstring(
"Impulse Response Choice",
"Type 'generate' or 'load':"
).strip().lower()
if choice == 'generate':
# Generate a custom impulse response
impulse_response = impulse_selector()
impulse_response = normalize_audio(impulse_response)
elif choice == 'load':
impulse_file = get_audio_file_directory()
print(f"Selected impulse file: {impulse_file}")
impulse_response, _ = load_audio(impulse_file)
impulse_response = normalize_audio(impulse_response)
else:
print("Invalid choice. Exiting.")
return
impulse_response = impulse_response.flatten()
print(f"Signal shape: {signal.shape}")
print(f"Impulse response shape: {impulse_response.shape}")
# Convolve the audio signal with the impulse response
convolved_signal = convolve_signals(signal, impulse_response)
# Clip the convolved signal
# convolved_signal = clip_signal(convolved_signal)
# Trim convolved signal to the length of the original signal
convolved_signal = convolved_signal[:len(signal)]
convolved_signal = normalize_audio(convolved_signal)
# Save the output
output_file = 'convolved_output.wav'
save_audio(output_file, convolved_signal, sample_rate)
# Plot the original and convolved_signal
plot_signals(signal, convolved_signal)
if __name__ == "__main__":
main()