shh_algo_PY

Synthesizer

Mar 27th, 2022 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. import play
  2. import pygame
  3.  
  4. play.set_backdrop('light blue')
  5. introduce1 = play.new_text(words='Piano for fun!', x=0, y=200)
  6. introduce2 = play.new_text(words='Create your melody by pressing the keys', x=0, y=150)
  7.  
  8. p = play.new_box(color='green', x=-100, y=-170, width=160, height=50)
  9. p_text = play.new_text(words='play melody', x=-100, y=-170, font_size=20)
  10.  
  11. c = play.new_box(color='yellow',  x=100, y=-170, width=160, height=50)
  12. c_text = play.new_text(words='clear melody', x=100, y=-170, font_size=20)
  13.  
  14. b_p = play.new_circle(x=-180, y=-100, radius=10)
  15. txt_p = play.new_text(words='piano', x=-145, y=-100, font_size=20)
  16.  
  17. b_g = play.new_circle(color='blue', x=-80, y=-100, radius=10)
  18. txt_g = play.new_text(words='guitar', x=-45, y=-100, font_size=20)
  19.  
  20. b_v = play.new_circle(color='blue', x=20, y=-100, radius=10)
  21. txt_v = play.new_text(words='violin', x=55, y=-100, font_size=20)
  22.  
  23. b_f = play.new_circle(color='blue', x=120, y=-100, radius=10)
  24. txt_f = play.new_text(words='flute', x=155, y=-100, font_size=20)
  25.  
  26. instrument = 0
  27. instrument_buttons = [b_p, b_g, b_v, b_f]
  28.  
  29. @play.repeat_forever
  30. def switch():
  31.     for i in instrument_buttons:
  32.         if i.is_clicked:
  33.             global instrument
  34.             instrument = instrument_buttons.index(i)
  35.             for j in instrument_buttons:
  36.                 j.color = "blue"
  37.             i.color = "black"
  38.  
  39. keys = []
  40. sounds = [[], [], [], []]
  41. colors = ["red", "orange", "yellow", "green", "dark green", "blue", "indigo", "purple"]
  42. buttons = ["a", "s", "d", "f", "g", "h", "j", "k"]
  43.  
  44. for i in range(8):
  45.     key_x = -180 + i * 50
  46.     key = play.new_box(color=colors[i], x=key_x, width=40, height=100)
  47.     keys.append(key)
  48.     p_sound = pygame.mixer.Sound("pia" + str(i+1)+'.ogg')
  49.     g_sound = pygame.mixer.Sound("git" + str(i+1)+'.ogg')
  50.     v_sound = pygame.mixer.Sound("vio" + str(i+1)+'.ogg')
  51.     f_sound = pygame.mixer.Sound("fl" + str(i+1)+'.ogg')
  52.     sounds[0].append(p_sound)
  53.     sounds[1].append(g_sound)
  54.     sounds[2].append(v_sound)
  55.     sounds[3].append(f_sound)
  56.  
  57. @play.when_program_starts
  58. def start():
  59.     pygame.mixer_music.load('hi-1.mp3')
  60.     pygame.mixer_music.play()
  61.  
  62. melody = []
  63.  
  64. @play.repeat_forever
  65. async def play_instrument():
  66.     for i in range(len(keys)):
  67.         if keys[i].is_clicked or play.key_is_pressed(buttons[i]):
  68.             keys[i].color = 'light grey'
  69.             sounds[instrument][i].play()
  70.             await play.timer(seconds=0.1)
  71.             keys[i].color = colors[i] #alternately, "white"
  72.             melody.append(i)
  73.  
  74. @p.when_clicked
  75. async def play_m():
  76.     for i in range(len(melody)):
  77.         await play.timer(seconds=0.5)
  78.         sounds[instrument][melody[i]].play()
  79.  
  80. @c.when_clicked
  81. def clear():
  82.     melody.clear()
  83.  
  84. play.start_program()
  85.  
Add Comment
Please, Sign In to add comment