Spaces:
Sleeping
Sleeping
Upload pitcher.py
Browse files
src/modules/Pitcher/pitcher.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Pitcher module"""
|
| 2 |
+
|
| 3 |
+
import crepe
|
| 4 |
+
from scipy.io import wavfile
|
| 5 |
+
|
| 6 |
+
from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted, red_highlighted
|
| 7 |
+
from modules.Pitcher.pitched_data import PitchedData
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_pitch_with_crepe_file(
|
| 11 |
+
filename: str, model_capacity: str, step_size: int = 10, device: str = "cpu"
|
| 12 |
+
) -> PitchedData:
|
| 13 |
+
"""Pitch with crepe"""
|
| 14 |
+
|
| 15 |
+
print(
|
| 16 |
+
f"{ULTRASINGER_HEAD} Pitching with {blue_highlighted('crepe')} and model {blue_highlighted(model_capacity)} and {red_highlighted(device)} as worker"
|
| 17 |
+
)
|
| 18 |
+
sample_rate, audio = wavfile.read(filename)
|
| 19 |
+
|
| 20 |
+
return get_pitch_with_crepe(audio, sample_rate, model_capacity, step_size)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def get_pitch_with_crepe(
|
| 24 |
+
audio, sample_rate: int, model_capacity: str, step_size: int = 10
|
| 25 |
+
) -> PitchedData:
|
| 26 |
+
"""Pitch with crepe"""
|
| 27 |
+
|
| 28 |
+
# Info: The model is trained on 16 kHz audio, so if the input audio has a different sample rate, it will be first resampled to 16 kHz using resampy inside crepe.
|
| 29 |
+
|
| 30 |
+
times, frequencies, confidence, activation = crepe.predict(
|
| 31 |
+
audio, sample_rate, model_capacity, step_size=step_size, viterbi=True
|
| 32 |
+
)
|
| 33 |
+
return PitchedData(times, frequencies, confidence)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def get_pitched_data_with_high_confidence(
|
| 37 |
+
pitched_data: PitchedData, threshold=0.4
|
| 38 |
+
) -> PitchedData:
|
| 39 |
+
"""Get frequency with high confidence"""
|
| 40 |
+
new_pitched_data = PitchedData([], [], [])
|
| 41 |
+
for i, conf in enumerate(pitched_data.confidence):
|
| 42 |
+
if conf > threshold:
|
| 43 |
+
new_pitched_data.times.append(pitched_data.times[i])
|
| 44 |
+
new_pitched_data.frequencies.append(pitched_data.frequencies[i])
|
| 45 |
+
new_pitched_data.confidence.append(pitched_data.confidence[i])
|
| 46 |
+
|
| 47 |
+
return new_pitched_data
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def get_frequencies_with_high_confidence(
|
| 51 |
+
frequencies: list[float], confidences: list[float], threshold=0.4
|
| 52 |
+
) -> list[float]:
|
| 53 |
+
"""Get frequency with high confidence"""
|
| 54 |
+
conf_f = []
|
| 55 |
+
for i, conf in enumerate(confidences):
|
| 56 |
+
if conf > threshold:
|
| 57 |
+
conf_f.append(frequencies[i])
|
| 58 |
+
if not conf_f:
|
| 59 |
+
conf_f = frequencies
|
| 60 |
+
return conf_f
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class Pitcher:
|
| 64 |
+
"""Docstring"""
|