# These are modules that Python needs to play sounds and to
# measure time.
import time
import pygame
import sys

# This sample program plays two tunes using the handbell sample files
# from the Science Buddies project "Sounds like RFID". To run this, you
# need Python 3, pyGame and the handbell WAV files from that project idea.

# Before we can use the pygame functions to play sounds, we have to
# let the module set itself up. You just do this once at the beginning.
pygame.init()
pygame.mixer.init()

# This is one way to play the sound. This doesn't allow you
# to play more than one sound at a time. It also makes it hard to
# use different sound files if you want to switch quickly between
# bell sounds and something else. We don't recommend this method, but
# it's here as an example of one way you could play sounds.
#
# Tell pygame the name of a file you want to play
pygame.mixer.music.load("329-E.wav")
# Tell pygame to play the file
pygame.mixer.music.play(0)
# The time.sleep(1) function tells Python to wait for 1 second before continuing.
# This is enough time to play the sound file.
time.sleep(1)
pygame.mixer.music.load("293-D.wav")
pygame.mixer.music.play(0)
time.sleep(1)
pygame.mixer.music.load("261-C.wav")
pygame.mixer.music.play(0)
time.sleep(1)
pygame.mixer.music.load("293-D.wav")
pygame.mixer.music.play(0)
time.sleep(1)
pygame.mixer.music.load("329-E.wav")
pygame.mixer.music.play(0)
time.sleep(1)
pygame.mixer.music.load("329-E.wav")
pygame.mixer.music.play(0)
time.sleep(1)
pygame.mixer.music.load("329-E.wav")
pygame.mixer.music.play(0)

# With the song complete, wait for 5 seconds.
time.sleep(5)

# ---------------------------------------------------------------

# This is a better way to make sounds. We preload the sound files
# in lists (we do that earlier in the program) and we can just call
# on pygame's player to play them. The 'play_a_bell()' routine
# just needs to know the short name we assigned the sound. It will find
# it and start the sound playing. It plays in the background and you
# can play multiple sounds at once.
#
# What would happen if you made multiple lists? Or could a list contain
# more than one kind of sound?
#
#
# Each of these lists is a sound, space you might use to store a RFID
# tag ID, and a friendlier name you can use to search. These tags are
# simply examples and they won't work with whatever RFID tags you might
# have. If you use this method, you need to read the data from your own
# RFID reader and tags, and substitute in your own data.
bells = [
	  #                    filename      tag data      friendly
      #                    for the       for the tag   name for
      #                    note          that plays    this note
      #                                  this note
      [pygame.mixer.Sound("220-A.wav"), "3501D5A69A", "A3"],
      [pygame.mixer.Sound("247-B.wav"), "3501D5A782", "B3"],
      [pygame.mixer.Sound("261-C.wav"), "3501D5B02A", "C3"],
      [pygame.mixer.Sound("293-D.wav"), "3501D5B15B", "D3"],
      [pygame.mixer.Sound("329-E.wav"), "3501D5B258", "E3"],
      [pygame.mixer.Sound("349-F.wav"), "3501D5BCE9", "F3"],
      [pygame.mixer.Sound("392-G.wav"), "3501D5BD0B", "G3"],
      [pygame.mixer.Sound("440-A.wav"), "3501D5BEF9", "A4"],
      [pygame.mixer.Sound("494-B.wav"), "3501D5C832", "B4"],
      [pygame.mixer.Sound("523-C.wav"), "3501D5CD2D", "C4"],
      [pygame.mixer.Sound("587-D.wav"), "840033CDA1", "D4"]
]

# given the tag OR the friendly name, start the bell sound. This
# routine searches by the friendly sound name, but if you searched
# the entry for the RFID tag, imagine the possibilities...
def play_a_bell(s):
	  # len(bells) tells us how many of those individual lists
	  # there are in the list 'bells'. Look at each one.
	  # If, for example, the user asks for "C3", look
	  # at each list for a "C3" in position 2 of the list.
	  # When we see one, start playing the sound file for
	  # that note.
      for i in range(0,len(bells)):
            #print (bells[i][2])
			# Does the friendly name in this list match what we're looking for?
            if s == bells[i][2]:
				  # Yes! Play the sound.
                  bells[i][0].play(loops=0, maxtime=0, fade_ms=0)
                  break;                  


# Let's make some music! Play a "C"
play_a_bell("C3")
# Wait one second
time.sleep(1)
# Play that same C again
play_a_bell("C3")
# Wait another second
time.sleep(1)
# Play a "G" above that previous C
play_a_bell("G3")
# You get the idea...
time.sleep(1)
play_a_bell("G3")
time.sleep(1)

play_a_bell("A4")
time.sleep(1)
play_a_bell("A4")
time.sleep(1)
play_a_bell("G3")
time.sleep(1)


