File: player.py

package info (click to toggle)
songwrite 0.14-10
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,536 kB
  • ctags: 1,193
  • sloc: python: 7,059; xml: 850; makefile: 11
file content (80 lines) | stat: -rw-r--r-- 2,691 bytes parent folder | download | duplicates (6)
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
69
70
71
72
73
74
75
76
77
78
79
80
# SongWrite
# Copyright (C) 2001-2004 Jean-Baptiste LAMY -- jibalamy@free.fr
#
# 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

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))