Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
-
import sounddevice as sd
|
4 |
-
from scipy import signal
|
5 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Constants
|
8 |
KEYS = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
|
@@ -16,12 +21,10 @@ BLUES_PROGRESSIONS = [
|
|
16 |
['I', 'IV', 'I', 'V', 'IV', 'IV', 'I', 'V', 'IV', 'IV', 'I', 'V'],
|
17 |
]
|
18 |
|
19 |
-
SAMPLE_RATE = 44100
|
20 |
-
|
21 |
def note_to_freq(note):
|
22 |
octave = int(note[-1])
|
23 |
key_index = KEYS.index(note[:-1])
|
24 |
-
return 440 * (2 ** ((key_index - 9) / 12 + (octave - 4)))
|
25 |
|
26 |
def get_chord_notes(root, chord_type):
|
27 |
root_index = KEYS.index(root)
|
@@ -35,22 +38,17 @@ def get_chord_notes(root, chord_type):
|
|
35 |
intervals = [0, 4, 7]
|
36 |
return [KEYS[(root_index + interval) % len(KEYS)] for interval in intervals]
|
37 |
|
38 |
-
def create_tone(freq, duration):
|
39 |
-
t = np.linspace(0, duration, int(SAMPLE_RATE * duration), False)
|
40 |
-
return 0.3 * signal.chirp(t, freq, duration, freq, method='linear')
|
41 |
-
|
42 |
def play_arpeggio(root, chord_type, tempo):
|
43 |
chord_notes = get_chord_notes(root, chord_type)
|
44 |
-
|
45 |
|
46 |
-
arpeggio = np.array([])
|
47 |
for note in chord_notes:
|
48 |
freq = note_to_freq(f"{note}4")
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
def main():
|
56 |
st.title("12-Bar Blues Arpeggio Game")
|
@@ -75,7 +73,7 @@ def main():
|
|
75 |
st.markdown(progression_display)
|
76 |
|
77 |
# Play/Stop button
|
78 |
-
if st.button("Play
|
79 |
for i, chord in enumerate(st.session_state.blues_progression):
|
80 |
st.session_state.current_bar = i
|
81 |
play_arpeggio(root_key, chord, tempo)
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
|
|
|
|
3 |
import time
|
4 |
+
import platform
|
5 |
+
|
6 |
+
# Check if running on Windows
|
7 |
+
is_windows = platform.system() == "Windows"
|
8 |
+
|
9 |
+
if is_windows:
|
10 |
+
import winsound
|
11 |
|
12 |
# Constants
|
13 |
KEYS = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
|
|
|
21 |
['I', 'IV', 'I', 'V', 'IV', 'IV', 'I', 'V', 'IV', 'IV', 'I', 'V'],
|
22 |
]
|
23 |
|
|
|
|
|
24 |
def note_to_freq(note):
|
25 |
octave = int(note[-1])
|
26 |
key_index = KEYS.index(note[:-1])
|
27 |
+
return int(440 * (2 ** ((key_index - 9) / 12 + (octave - 4))))
|
28 |
|
29 |
def get_chord_notes(root, chord_type):
|
30 |
root_index = KEYS.index(root)
|
|
|
38 |
intervals = [0, 4, 7]
|
39 |
return [KEYS[(root_index + interval) % len(KEYS)] for interval in intervals]
|
40 |
|
|
|
|
|
|
|
|
|
41 |
def play_arpeggio(root, chord_type, tempo):
|
42 |
chord_notes = get_chord_notes(root, chord_type)
|
43 |
+
duration_ms = int(60 / tempo * 250) # 16th notes
|
44 |
|
|
|
45 |
for note in chord_notes:
|
46 |
freq = note_to_freq(f"{note}4")
|
47 |
+
if is_windows:
|
48 |
+
winsound.Beep(freq, duration_ms)
|
49 |
+
else:
|
50 |
+
st.write(f"Playing {note}4 ({freq} Hz) for {duration_ms} ms")
|
51 |
+
time.sleep(duration_ms / 1000)
|
52 |
|
53 |
def main():
|
54 |
st.title("12-Bar Blues Arpeggio Game")
|
|
|
73 |
st.markdown(progression_display)
|
74 |
|
75 |
# Play/Stop button
|
76 |
+
if st.button("Play 12-Bar Blues"):
|
77 |
for i, chord in enumerate(st.session_state.blues_progression):
|
78 |
st.session_state.current_bar = i
|
79 |
play_arpeggio(root_key, chord, tempo)
|