naohiro701 commited on
Commit
f8b7e64
·
verified ·
1 Parent(s): 240bf4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -8,6 +8,28 @@ import tempfile
8
  import os
9
  import subprocess
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Streamlit App
12
  def main():
13
  st.title("MP3 Fourier Transform Visualizer: 科学未来館エディション")
@@ -46,7 +68,7 @@ def main():
46
 
47
  fft_frames = np.array(fft_frames)
48
 
49
- # Create animation with colorful design
50
  fig, ax = plt.subplots(facecolor="black")
51
  line, = ax.plot(freqs, fft_frames[0], lw=2, color="cyan")
52
  ax.set_xlim(0, np.max(freqs))
@@ -59,7 +81,8 @@ def main():
59
 
60
  def update(frame):
61
  line.set_ydata(fft_frames[frame])
62
- line.set_color(plt.cm.jet(frame / len(fft_frames))) # Dynamic color
 
63
  return line,
64
 
65
  ani = FuncAnimation(fig, update, frames=len(fft_frames), blit=True)
 
8
  import os
9
  import subprocess
10
 
11
+ # Define frequency ranges for musical notes (approximation)
12
+ note_frequencies = {
13
+ "C": 261.63,
14
+ "D": 293.66,
15
+ "E": 329.63,
16
+ "F": 349.23,
17
+ "G": 392.00,
18
+ "A": 440.00,
19
+ "B": 493.88,
20
+ }
21
+
22
+ # Function to map frequency to a color based on octave and note
23
+ def frequency_to_color(freq):
24
+ if freq < note_frequencies["C"] / 2 or freq > note_frequencies["B"] * 2:
25
+ return "gray" # Out of range
26
+
27
+ octave = int(np.log2(freq / note_frequencies["C"]))
28
+ for note, base_freq in note_frequencies.items():
29
+ if base_freq * (2 ** octave) <= freq < base_freq * (2 ** (octave + 1)):
30
+ return plt.cm.hsv((list(note_frequencies.keys()).index(note) + octave * len(note_frequencies)) / 12)
31
+ return "gray"
32
+
33
  # Streamlit App
34
  def main():
35
  st.title("MP3 Fourier Transform Visualizer: 科学未来館エディション")
 
68
 
69
  fft_frames = np.array(fft_frames)
70
 
71
+ # Create animation with note-based colors
72
  fig, ax = plt.subplots(facecolor="black")
73
  line, = ax.plot(freqs, fft_frames[0], lw=2, color="cyan")
74
  ax.set_xlim(0, np.max(freqs))
 
81
 
82
  def update(frame):
83
  line.set_ydata(fft_frames[frame])
84
+ colors = [frequency_to_color(freq) for freq in freqs]
85
+ line.set_color(colors[int(frame % len(colors))]) # Dynamic color per frequency range
86
  return line,
87
 
88
  ani = FuncAnimation(fig, update, frames=len(fft_frames), blit=True)