
# player.py

"""
This module is an integeral part of the program
MMA - Musical Midi Accompaniment.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Bob van der Poel <bob@mellowood.ca>

"""

import os
import time

from MMA.common import *
import MMA.gbl

# Just in case the player is NOT set in mma.py set it NULL to
# create the variable; then import the name from mma.py.

midiPlayer = ""
from __main__ import midiPlayer

def setMidiPlayer(ln):
    """ Set the MIDI file player (used with -P). """

    global midiPlayer
    
    if not ln:
        ln = ['']
    
    midiPlayer = [MMA.file.fixfname(a) for a in ln ]

    if gbl.debug:
        print "MidiPlayer set to '%s'." % ' '.join(midiPlayer)


def playMidi(file):
    """ Play a midi file. """

    pl = midiPlayer[0]
    opts = midiPlayer[1:]
    cmd = ' "%s" %s %s' % (pl, ' '.join(opts), file)

    if pl == '':
        if gbl.platform == 'Windows':
            print "Playing '%s' with default windows midi player." % file
        else:
            os.remove(file)
            error("No midi file player defined, temp file deleted.")
    else:
        print "PlayMidi: '%s'." % cmd

    t=time.time()
    os.system(cmd)
    os.remove(file)

    print "Play complete (%.2f min), file has been deleted." % ((time.time()-t)/60)
