awacke1 commited on
Commit
c694a42
·
verified ·
1 Parent(s): 9cbbf1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -1,8 +1,7 @@
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
@@ -17,6 +16,8 @@ BLUES_PROGRESSIONS = [
17
  ['I', 'IV', 'I', 'V', 'IV', 'IV', 'I', 'V', 'IV', 'IV', 'I', 'V'],
18
  ]
19
 
 
 
20
  def note_to_freq(note):
21
  octave = int(note[-1])
22
  key_index = KEYS.index(note[:-1])
@@ -34,26 +35,22 @@ def get_chord_notes(root, chord_type):
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")
 
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
 
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])
 
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
+ duration = 60 / tempo / 4 # 16th notes
45
 
46
+ arpeggio = np.array([])
47
  for note in chord_notes:
48
  freq = note_to_freq(f"{note}4")
49
+ tone = create_tone(freq, duration)
50
+ arpeggio = np.concatenate((arpeggio, tone))
51
 
52
+ sd.play(arpeggio, SAMPLE_RATE)
53
+ sd.wait()
 
 
 
 
 
54
 
55
  def main():
56
  st.title("12-Bar Blues Arpeggio Game")