awacke1 commited on
Commit
8519f44
·
verified ·
1 Parent(s): b77a318

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -1,10 +1,9 @@
1
  import streamlit as st
2
  import numpy as np
 
 
 
3
  import time
4
- import pygame
5
-
6
- # Initialize Pygame mixer
7
- pygame.mixer.init()
8
 
9
  # Constants
10
  KEYS = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
@@ -35,15 +34,26 @@ 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 play_arpeggio(root, chord_type, tempo):
39
  chord_notes = get_chord_notes(root, chord_type)
40
- duration = 60 / tempo / 4 # 16th notes
 
 
41
  for note in chord_notes:
42
  freq = note_to_freq(f"{note}4")
43
- pygame.mixer.Sound(pygame.sndarray.make_sound(
44
- (np.sin(2 * np.pi * np.arange(44100) * freq / 44100) * 32767).astype(np.int16)
45
- )).play()
46
- time.sleep(duration)
 
 
 
 
 
 
47
 
48
  def main():
49
  st.title("12-Bar Blues Arpeggio Game")
@@ -70,8 +80,6 @@ def main():
70
  # Play/Stop button
71
  if st.button("Play/Stop 12-Bar Blues"):
72
  for i, chord in enumerate(st.session_state.blues_progression):
73
- if st.stop_button:
74
- break
75
  st.session_state.current_bar = i
76
  play_arpeggio(root_key, chord, tempo)
77
  st.session_state.chord_history.append(f"{root_key}{chord}")
 
1
  import streamlit as st
2
  import numpy as np
3
+ from pydub import AudioSegment
4
+ from pydub.generators import Sine
5
+ import simpleaudio as sa
6
  import time
 
 
 
 
7
 
8
  # Constants
9
  KEYS = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
 
34
  intervals = [0, 4, 7]
35
  return [KEYS[(root_index + interval) % len(KEYS)] for interval in intervals]
36
 
37
+ def create_tone(freq, duration_ms):
38
+ return Sine(freq).to_audio_segment(duration=duration_ms)
39
+
40
  def play_arpeggio(root, chord_type, tempo):
41
  chord_notes = get_chord_notes(root, chord_type)
42
+ duration_ms = int(60 / tempo * 250) # 16th notes
43
+
44
+ arpeggio = AudioSegment.silent(duration=1)
45
  for note in chord_notes:
46
  freq = note_to_freq(f"{note}4")
47
+ tone = create_tone(freq, duration_ms)
48
+ arpeggio = arpeggio.overlay(tone)
49
+
50
+ play_obj = sa.play_buffer(
51
+ arpeggio.raw_data,
52
+ num_channels=arpeggio.channels,
53
+ bytes_per_sample=arpeggio.sample_width,
54
+ sample_rate=arpeggio.frame_rate
55
+ )
56
+ play_obj.wait_done()
57
 
58
  def main():
59
  st.title("12-Bar Blues Arpeggio Game")
 
80
  # Play/Stop button
81
  if st.button("Play/Stop 12-Bar Blues"):
82
  for i, chord in enumerate(st.session_state.blues_progression):
 
 
83
  st.session_state.current_bar = i
84
  play_arpeggio(root_key, chord, tempo)
85
  st.session_state.chord_history.append(f"{root_key}{chord}")