marquesafonso commited on
Commit
d0a9fdf
·
1 Parent(s): 2992a08

add color picker for waveform and bg

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -2,24 +2,30 @@ import gradio as gr
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  from matplotlib.animation import FuncAnimation
5
- import matplotlib.colors as mcolors
6
- import io
7
  import librosa
8
  import tempfile
9
 
10
- def extract_waveform_animation(audio_file, window_seconds=5):
 
 
 
 
 
 
 
 
 
 
 
11
  y, sr = librosa.load(audio_file, sr=None)
12
  duration = librosa.get_duration(y=y, sr=sr)
13
  FPS = 1
14
  fig, ax = plt.subplots()
15
- line, = ax.plot([], [], lw=2, color="#0021e4")
16
  window_length = int(window_seconds * sr)
17
-
18
- # Initialize with first window
19
- first_window = y[:window_length]
20
  x_vals = np.linspace(0, duration, num=len(y))
21
  ax.set_axis_off()
22
- bg_color ="#00FFFF00"
23
  fig.set_facecolor(bg_color)
24
 
25
  def init():
@@ -55,7 +61,9 @@ iface = gr.Interface(
55
  fn=extract_waveform_animation,
56
  inputs=[
57
  gr.Audio(type="filepath"),
58
- gr.Slider(1, 10, value=5, step=1, label="Window Size (seconds)")
 
 
59
  ],
60
  outputs=gr.Image(),
61
  description="Scroll through audio waveform with a moving window."
 
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  from matplotlib.animation import FuncAnimation
 
 
5
  import librosa
6
  import tempfile
7
 
8
+ def rgba_to_hex(r, g, b, a):
9
+ return f'#{r:02x}{g:02x}{b:02x}{a:02x}'
10
+
11
+ def check_rgba(string:str):
12
+ if '#' in string:
13
+ return string
14
+ else:
15
+ clean_str_list = string.split("(")[1].split(")")[0].split(",")
16
+ hex = rgba_to_hex(clean_str_list[0], clean_str_list[1], clean_str_list[2], clean_str_list[3])
17
+ return hex
18
+
19
+ def extract_waveform_animation(audio_file, window_seconds, waveform_color, background_color):
20
  y, sr = librosa.load(audio_file, sr=None)
21
  duration = librosa.get_duration(y=y, sr=sr)
22
  FPS = 1
23
  fig, ax = plt.subplots()
24
+ line, = ax.plot([], [], lw=2, color=waveform_color)
25
  window_length = int(window_seconds * sr)
 
 
 
26
  x_vals = np.linspace(0, duration, num=len(y))
27
  ax.set_axis_off()
28
+ bg_color = background_color
29
  fig.set_facecolor(bg_color)
30
 
31
  def init():
 
61
  fn=extract_waveform_animation,
62
  inputs=[
63
  gr.Audio(type="filepath"),
64
+ gr.Slider(1, 10, value=5, step=1, label="Window Size (seconds)"),
65
+ gr.ColorPicker(label="Waveform color", value="#0021e4"),
66
+ gr.ColorPicker(label="Background color", value="#00FFFF00")
67
  ],
68
  outputs=gr.Image(),
69
  description="Scroll through audio waveform with a moving window."