Ivan000 commited on
Commit
0fb1e5a
·
verified ·
1 Parent(s): d48dfdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -20
app.py CHANGED
@@ -4,9 +4,7 @@
4
 
5
  import gradio as gr
6
  import numpy as np
7
- import matplotlib.pyplot as plt
8
  import librosa
9
- import librosa.display
10
  import os
11
  import cv2
12
 
@@ -24,29 +22,21 @@ def generate_frequency_visualization(audio_path, fps, num_bars):
24
  # Perform Short-Time Fourier Transform (STFT)
25
  hop_length = int(sr / fps) # Hop length to match the desired fps
26
  S = np.abs(librosa.stft(y, n_fft=2048, hop_length=hop_length))
27
- frequencies = librosa.fft_frequencies(sr=sr)
28
 
29
- # Create frequency bins for the bars
30
- bins = np.linspace(0, len(frequencies), num_bars + 1, dtype=int)
31
- bar_heights = []
32
-
33
- # Aggregate power for each bar
34
- for i in range(len(S[0])):
35
- frame = S[:, i]
36
- bar_frame = [np.mean(frame[bins[j]:bins[j+1]]) for j in range(num_bars)]
37
- bar_heights.append(bar_frame)
38
 
39
  # Create a directory to save the frames
40
  os.makedirs('frames', exist_ok=True)
41
 
42
  # Generate and save each frame
43
- for i, heights in enumerate(bar_heights):
44
  # Create black background
45
  img = np.zeros((720, 1280, 3), dtype=np.uint8)
46
 
47
- # Normalize heights to fit the frame
48
- heights = np.array(heights)
49
- heights = (heights / np.max(heights) * 600).astype(int)
50
 
51
  # Calculate bar positions
52
  bar_width = 80
@@ -61,14 +51,14 @@ def generate_frequency_visualization(audio_path, fps, num_bars):
61
  frame_path = f'frames/frame_{i:04d}.png'
62
  cv2.imwrite(frame_path, img)
63
 
64
- print(f"Generated {len(bar_heights)} frames for visualization.")
65
  return 'frames', duration
66
  except Exception as e:
67
  print(f"Error generating frequency visualization: {e}")
68
  return None, None
69
 
70
  # Function to create a video from the generated frames
71
- def create_video_from_frames(frames_directory, audio_path, fps, duration):
72
  try:
73
  # Get the list of frame files
74
  frame_files = [os.path.join(frames_directory, f) for f in os.listdir(frames_directory) if f.endswith('.png')]
@@ -109,7 +99,7 @@ def process_audio(audio):
109
  num_bars = 12
110
  frames_directory, duration = generate_frequency_visualization(audio_path, fps, num_bars)
111
  if frames_directory:
112
- video_path = create_video_from_frames(frames_directory, audio_path, fps, duration)
113
  return video_path
114
  else:
115
  return None
@@ -135,6 +125,6 @@ if __name__ == "__main__":
135
  # The following dependencies are required to run this app:
136
  # - librosa
137
  # - numpy
138
- # - matplotlib
139
  # - opencv-python
 
140
  # - ffmpeg (installed separately)
 
4
 
5
  import gradio as gr
6
  import numpy as np
 
7
  import librosa
 
8
  import os
9
  import cv2
10
 
 
22
  # Perform Short-Time Fourier Transform (STFT)
23
  hop_length = int(sr / fps) # Hop length to match the desired fps
24
  S = np.abs(librosa.stft(y, n_fft=2048, hop_length=hop_length))
25
+ S = S[:num_bars, :] # Limit the frequency bands to match the number of bars
26
 
27
+ # Normalize frequency power
28
+ S = S / np.max(S)
 
 
 
 
 
 
 
29
 
30
  # Create a directory to save the frames
31
  os.makedirs('frames', exist_ok=True)
32
 
33
  # Generate and save each frame
34
+ for i in range(S.shape[1]):
35
  # Create black background
36
  img = np.zeros((720, 1280, 3), dtype=np.uint8)
37
 
38
+ # Get the bar heights for the current frame
39
+ heights = (S[:, i] * 600).astype(int)
 
40
 
41
  # Calculate bar positions
42
  bar_width = 80
 
51
  frame_path = f'frames/frame_{i:04d}.png'
52
  cv2.imwrite(frame_path, img)
53
 
54
+ print(f"Generated {S.shape[1]} frames for visualization.")
55
  return 'frames', duration
56
  except Exception as e:
57
  print(f"Error generating frequency visualization: {e}")
58
  return None, None
59
 
60
  # Function to create a video from the generated frames
61
+ def create_video_from_frames(frames_directory, audio_path, fps):
62
  try:
63
  # Get the list of frame files
64
  frame_files = [os.path.join(frames_directory, f) for f in os.listdir(frames_directory) if f.endswith('.png')]
 
99
  num_bars = 12
100
  frames_directory, duration = generate_frequency_visualization(audio_path, fps, num_bars)
101
  if frames_directory:
102
+ video_path = create_video_from_frames(frames_directory, audio_path, fps)
103
  return video_path
104
  else:
105
  return None
 
125
  # The following dependencies are required to run this app:
126
  # - librosa
127
  # - numpy
 
128
  # - opencv-python
129
+ # - matplotlib
130
  # - ffmpeg (installed separately)