Spaces:
Runtime error
Runtime error
Emanuele Lapponi
commited on
Commit
Β·
a0a7a38
1
Parent(s):
5284dba
:sparkles: more hackathon messing about
Browse files
app.py
CHANGED
@@ -1,51 +1,75 @@
|
|
1 |
import numpy as np
|
2 |
import io
|
|
|
3 |
import soundfile as sf
|
4 |
import streamlit as st
|
5 |
from audio_recorder_streamlit import audio_recorder
|
6 |
|
7 |
import pedalboard as pb
|
8 |
|
9 |
-
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
cols = st.columns(5)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
import io
|
3 |
+
import pydub
|
4 |
import soundfile as sf
|
5 |
import streamlit as st
|
6 |
from audio_recorder_streamlit import audio_recorder
|
7 |
|
8 |
import pedalboard as pb
|
9 |
|
10 |
+
|
11 |
+
def stretch(audio, factor, buffer=[]):
|
12 |
+
for s in audio:
|
13 |
+
for _ in range(factor):
|
14 |
+
buffer.append(s)
|
15 |
+
return buffer
|
16 |
+
|
17 |
+
|
18 |
+
def destretcher(audio, buffer=[]):
|
19 |
+
for i, s in enumerate(audio):
|
20 |
+
if i % 100 == 0:
|
21 |
+
for _ in range(10):
|
22 |
+
buffer.append([0, 0])
|
23 |
+
return buffer
|
24 |
+
|
25 |
+
st.title("πΉ Sound Refuckulator")
|
26 |
+
|
27 |
+
audio = np.array([])
|
28 |
+
|
29 |
+
source = st.radio("Choose source", ["Microphone", "File upload"])
|
30 |
+
if source == "Microphone":
|
31 |
+
audio_bytes = audio_recorder()
|
32 |
+
if audio_bytes:
|
33 |
+
audio, sr = sf.read(io.BytesIO(audio_bytes))
|
34 |
+
else:
|
35 |
+
audio_bytes = st.file_uploader("Upload file")
|
36 |
+
if audio_bytes:
|
37 |
+
if audio_bytes.name.endswith("mp3"):
|
38 |
+
audio = pydub.AudioSegment.from_mp3(audio_bytes)
|
39 |
+
sr = audio.frame_rate
|
40 |
+
audio = np.float32(audio.get_array_of_samples()) / 2**15
|
41 |
+
else:
|
42 |
+
audio, sr = sf.read(audio_bytes)
|
43 |
+
|
44 |
+
|
45 |
+
if audio.any():
|
46 |
cols = st.columns(5)
|
47 |
+
chunk_dividend = cols[0].slider("π« Chunkizer", 16, 128, step=16)
|
48 |
+
stretch_factor = cols[1].slider("π§ Trollizer", 1, 10, 1)
|
49 |
+
prob = cols[2].slider("π€‘ Impredictidiblize", 0, 100, 0)
|
50 |
+
sr_factor = cols[3].slider("πββοΈ Chirpidize", 1, 16, 1)
|
51 |
+
rev_size = cols[4].slider("π Hugermaker", 0.0, 0.99, 0.0)
|
52 |
+
delay = pb.Pedalboard([pb.Delay(delay_seconds=0.01, feedback=0.6)])
|
53 |
+
reverb = pb.Pedalboard([pb.Reverb(room_size=rev_size)])
|
54 |
+
chorus = pb.Pedalboard([pb.Chorus()])
|
55 |
+
pshifter = pb.Pedalboard([pb.PitchShift(-12)])
|
56 |
+
processed = []
|
57 |
+
chunk_dividend = sorted(np.random.randint(audio.shape[0], size=chunk_dividend))
|
58 |
+
for i, chunk in enumerate(np.array_split(audio, chunk_dividend)):
|
59 |
+
if np.random.randint(100) < prob*2:
|
60 |
+
chunk = reverb(chunk, sample_rate=sr, reset=False) if np.random.randint(100) < prob else chunk
|
61 |
+
chunk = delay(chunk, sample_rate=sr, reset=False) if np.random.randint(100) < prob else chunk
|
62 |
+
chunk = chorus(chunk, sample_rate=sr, reset=False) if np.random.randint(100) < prob else chunk
|
63 |
+
chunk = pshifter(chunk, sample_rate=sr, reset=False) if np.random.randint(100) < prob else chunk
|
64 |
+
chunk = chunk + chunk if np.random.randint(100) < prob/2 else chunk
|
65 |
+
chunk = stretch(chunk, stretch_factor) if np.random.randint(100) < prob else chunk
|
66 |
+
chunk = destretcher(chunk) if np.random.randint(100) < prob else chunk
|
67 |
+
processed += [s for s in chunk]
|
68 |
+
|
69 |
+
processed = np.array(processed)
|
70 |
+
reverb = pb.Pedalboard([pb.Reverb(room_size=0.3)])
|
71 |
+
reverbered = reverb(processed, sr, reset=False)
|
72 |
+
compressor = pb.Pedalboard([pb.Limiter(threshold_db=-1)])
|
73 |
+
compressed = compressor(processed, sr, reset=False)
|
74 |
+
|
75 |
+
st.audio(compressed.T, sample_rate=sr * sr_factor)
|