# Additional functions to help with reading and understanding the
# LOSC Event Tutorial, https://losc.ligo.org/s/events/LOSC_Event_tutorial.py
# Usage:
#           import tutorial_extras as *
# 
# Eric Myers <EricMyers47@gmail.com> - 2 August 2018
# @(#) Last revised: 25APR2020 -EAM  / Use pyglet for audio with Python 3
###############################################################################

# So we can use plt.show()
import matplotlib.pyplot as plt

# So we can detect Python 2
import sys

print("Python version %s" % sys.version[0])


## TRACING
#
# Use these to trace the tutorial, step by step, to see what's happening
# First use halt() to stop the tutorial at a particular place and
# show all graphs produced so far.  Then change that to nohalt() to
# continue on, or use pause() or shown(), as you wish.

# Display a checkpoint message and stop the script

def halt(message):
    print("\n* "+message)
    plt.show()      # Shows all graphs created so far
    exit()          # stop Everything, not just exit function

    
# After you've seen a step, change halt() to nohalt() and you'll know
# you've passed a step, but it will no longer stop, and you won't see
# any of those graphs.

def nohalt(message):
    print ("* "+message)
    ##    plt.gcf().clear()   # This discards plots created so far
    plt.close()         # This closes the window


# Display a checkpoint message and plot all graphs created up to now.
# Script will continue when all graphs exit.

def shown(message):
    print("\n* "+message)
    plt.show()


# Just pause until user hits Enter  (as between sound files)
     
def pause(message):
    print("\n* "+message)
    if int(sys.version[0]) >= 3: 
       input("Press ENTER to continue...")
    else: 
       raw_input("Press ENTER to continue..")
    print("\n")




## AUDIO
#
# The LOSC tutorial uses the IPython.Audio module, which is not avaiable
# when using "regular" python from python.org.   The pygame module
# makes simple audio available for playing the .wav files.   See
# https://raspberrypi.stackexchange.com/questions/7088/playing-audio-files-with-python
# (The simpleaudio module won't play at the desired sampling rates, and
# no other modules are as easy to use as pygame.)

##import pygame

def Audio_pygame(fileName) :
    pygame.mixer.init()
    pygame.mixer.music.load(fileName)
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy() == True:
        continue

# 2020 - The pygame module only works with Python 2.7, so switch to using
# the playsound module, which is easier anyway.   Note that on a Mac you
# may also need `pip3 install PyObjC`

import playsound

def Audio (fileName) :
    playsound.playsound(fileName)


##EOF##
