Showing
1 changed file
with
10 additions
and
9 deletions
... | @@ -8,11 +8,15 @@ from scipy.fft import fft, fftfreq | ... | @@ -8,11 +8,15 @@ from scipy.fft import fft, fftfreq |
8 | import math | 8 | import math |
9 | import time | 9 | import time |
10 | 10 | ||
11 | +TARGET = "frequency" # frequency, amplitude, decibel | ||
12 | +EFFECT = "pitch_shift" # echo, pitch_shift, low_filter | ||
13 | + | ||
11 | RATE = 44100 | 14 | RATE = 44100 |
12 | CHUNK = 1024 | 15 | CHUNK = 1024 |
13 | MAX_FREQ = 40 # max freq for pitch shifting | 16 | MAX_FREQ = 40 # max freq for pitch shifting |
14 | MAX_AMPLITUDE = 10000 # max amplitude for pitch shifting | 17 | MAX_AMPLITUDE = 10000 # max amplitude for pitch shifting |
15 | -MAX_DECIBEL = 50 # max decibel for decibel shifting | 18 | +MAX_DECIBEL = 150 # max decibel for decibel shifting |
19 | +MIN_DECIBEL = 50 | ||
16 | EFFECT_LEVEL = 20 # number of effect level | 20 | EFFECT_LEVEL = 20 # number of effect level |
17 | 21 | ||
18 | INPUT_DEVICE_INDEX = 0 | 22 | INPUT_DEVICE_INDEX = 0 |
... | @@ -69,8 +73,8 @@ class Effector: | ... | @@ -69,8 +73,8 @@ class Effector: |
69 | self.sound = np.concatenate((self.sound, np.zeros(CHUNK - len(sound)%CHUNK, dtype=np.int16))) | 73 | self.sound = np.concatenate((self.sound, np.zeros(CHUNK - len(sound)%CHUNK, dtype=np.int16))) |
70 | 74 | ||
71 | 75 | ||
72 | - self.target = "frequency" | 76 | + self.target = TARGET |
73 | - self.effect = "pitch_shift" | 77 | + self.effect = EFFECT |
74 | 78 | ||
75 | self.echoed_sounds = list() | 79 | self.echoed_sounds = list() |
76 | for i in range(EFFECT_LEVEL): | 80 | for i in range(EFFECT_LEVEL): |
... | @@ -109,7 +113,7 @@ class Effector: | ... | @@ -109,7 +113,7 @@ class Effector: |
109 | return np.array(echoed_audio, dtype=np.int16) | 113 | return np.array(echoed_audio, dtype=np.int16) |
110 | 114 | ||
111 | def pitch_shift(self, shift): | 115 | def pitch_shift(self, shift): |
112 | - sound = librosa.effects.pitch_shift(y=np.array(self.sound, np.float32), sr=RATE, n_steps=shift*3, bins_per_octave=1) | 116 | + sound = librosa.effects.pitch_shift(y=np.array(self.sound, np.float32), sr=RATE, n_steps=shift*3-1, bins_per_octave=1) |
113 | return np.array(sound, dtype=np.int16) | 117 | return np.array(sound, dtype=np.int16) |
114 | 118 | ||
115 | def low_filter(self, param): | 119 | def low_filter(self, param): |
... | @@ -133,13 +137,12 @@ class Effector: | ... | @@ -133,13 +137,12 @@ class Effector: |
133 | def get_distortion_rate(self, db, main_frequency, amplitude): | 137 | def get_distortion_rate(self, db, main_frequency, amplitude): |
134 | print("current target is ", self.target) | 138 | print("current target is ", self.target) |
135 | param = 0 | 139 | param = 0 |
136 | - print(MAX_FREQ, main_frequency) | ||
137 | if self.target == "frequency": | 140 | if self.target == "frequency": |
138 | param = min(MAX_FREQ-1, main_frequency) / MAX_FREQ | 141 | param = min(MAX_FREQ-1, main_frequency) / MAX_FREQ |
139 | elif self.target == "amplitude": | 142 | elif self.target == "amplitude": |
140 | param = min(MAX_AMPLITUDE-1, amplitude) / MAX_AMPLITUDE | 143 | param = min(MAX_AMPLITUDE-1, amplitude) / MAX_AMPLITUDE |
141 | elif self.target == "decibel": | 144 | elif self.target == "decibel": |
142 | - param = min(MAX_DECIBEL-1, db) / MAX_DECIBEL | 145 | + param = min(MAX_DECIBEL-MIN_DECIBEL, db-MIN_DECIBEL) / MAX_DECIBEL-MIN_DECIBEL |
143 | 146 | ||
144 | param = max(param, 0) | 147 | param = max(param, 0) |
145 | return param | 148 | return param |
... | @@ -170,7 +173,7 @@ class Effector: | ... | @@ -170,7 +173,7 @@ class Effector: |
170 | db, main_frequency, amplitude = self.get_decibel_freq_amplitude(active_sound) | 173 | db, main_frequency, amplitude = self.get_decibel_freq_amplitude(active_sound) |
171 | print("touched, db {}, main_frequency {}, amplitude {}".format(db, main_frequency, amplitude)) | 174 | print("touched, db {}, main_frequency {}, amplitude {}".format(db, main_frequency, amplitude)) |
172 | param = self.get_distortion_rate(db, main_frequency, amplitude) | 175 | param = self.get_distortion_rate(db, main_frequency, amplitude) |
173 | - print("param : ", param) | 176 | + print("effect 정도 :", param) |
174 | for i in range(0, len(self.sound), CHUNK): | 177 | for i in range(0, len(self.sound), CHUNK): |
175 | temp_chunk = self.augumented_sound[self.effect][int(param*EFFECT_LEVEL)][i:i+CHUNK] | 178 | temp_chunk = self.augumented_sound[self.effect][int(param*EFFECT_LEVEL)][i:i+CHUNK] |
176 | Q.put(temp_chunk) | 179 | Q.put(temp_chunk) |
... | @@ -218,8 +221,6 @@ def callback(in_data, frame_count, time_info, status): | ... | @@ -218,8 +221,6 @@ def callback(in_data, frame_count, time_info, status): |
218 | 221 | ||
219 | if Q.qsize() == 1: | 222 | if Q.qsize() == 1: |
220 | Q.put(np.zeros(CHUNK, dtype=np.int16)) | 223 | Q.put(np.zeros(CHUNK, dtype=np.int16)) |
221 | - else: | ||
222 | - print(Q.qsize()) | ||
223 | return (Q.get(), pyaudio.paContinue) | 224 | return (Q.get(), pyaudio.paContinue) |
224 | 225 | ||
225 | stream = pa.open(format=pa.get_format_from_width(2), | 226 | stream = pa.open(format=pa.get_format_from_width(2), | ... | ... |
-
Please register or login to post a comment