File: player.py

package info (click to toggle)
songwrite 0.12b-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 984 kB
  • ctags: 989
  • sloc: python: 6,321; xml: 850; makefile: 10
file content (68 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Songwrite
# Copyright (C) 2001-2002 Jean-Baptiste LAMY
#
# This program is free software. See README or LICENSE for the license terms.

from __future__ import nested_scopes
import sys, popen2, os, thread, globdef

_p = None

def is_playing(): return _p and (_p.poll() == -1)
def stop():
  global _p
  if not _p is None:
    try: os.kill(_p.pid, 9)
    except OSError: pass
    _p  = None
    
def play(midi, loop = 0):
  global _p
  
  while 1:
    if globdef.config.MIDI_USE_TEMP_FILE:
      import tempfile
      file = tempfile.mktemp(".midi")
      open(file, "w").write(midi)
      _p = p = popen2.Popen4(globdef.config.MIDI_COMMAND % file)
    else:
      _p = p = popen2.Popen4(globdef.config.MIDI_COMMAND)
      _p.tochild.write(midi)
      _p.tochild.flush()
      
    p.wait()
    
    if (globdef.config.MIDI_COMMAND.find("timidity") != -1) and (p.fromchild.read().find("Couldn't open Arts device") != -1):
      # We are using the KDE / aRts version of Timidity
      # This fucking version can only play with aRts,
      # until we provide another config file for it.
      #
      # BTW am I alone to think that aRts is a pain ?
      
      # Get the default config file
      import re, tempfile
      timidity_verbose = os.popen4("timidity -idvvv")[1].read()
      timidity_cfg = re.findall(r"\S*\.cfg", timidity_verbose)
      if timidity_cfg:
        timidity_cfg = timidity_cfg[0]
        
        cfg = open(timidity_cfg).read()
        cfg = re.sub("-O\s+A", "# No longer use fucking aRts", cfg)
        
        temp_cfg = tempfile.mktemp(".timidity.noarts.cfg")
        open(temp_cfg, "w").write(cfg)
        
        globdef.config.MIDI_COMMAND += " -c %s" % temp_cfg
        
        import tkMessageBox
        tkMessageBox.showwarning(_("aRts is getting in the way"), _("__fucking-arts__") % temp_cfg)
        
    if (p.poll() != 0) or (not loop):
      break # Stop if _p has been killed (by stop()) or if not loop !
    

def play_async(midi, loop = 0):
  stop()
  thread.start_new_thread(play, (midi, loop))