Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

1.32

  • Loading branch information...
commit ea82f0ef5258411a2e9f4d1a1fcdac4dd860e089 1 parent f53edce
Ulf Betlehem authored holizz committed

Showing 1 changed file with 83 additions and 33 deletions. Show diff stats Hide diff stats

  1. +83 33 cplay
116 cplay
... ... @@ -1,7 +1,7 @@
1 1 #!/usr/bin/env python
2 2 # -*- python -*-
3 3
4   -__version__ = "$Id: cplay,v 1.31 tmp $"
  4 +__version__ = "$Id: cplay,v 1.32 2000/07/25 11:20:58 flu Exp $"
5 5
6 6 """
7 7 cplay - A curses front-end for mpg123 (version >= 0.59o)
@@ -44,9 +44,28 @@ from types import *
44 44
45 45 # ------------------------------------------
46 46 FPS = 38.28
47   -RE_SONG = re.compile(".*\.[Mm][Pp][123]$")
48   -RE_PLAYLIST = re.compile(".*\.[Mm]3[Uu]$")
49   -XTERM = re.search("rxvt|xterm", os.environ['TERM']) and 1 or 0
  47 +XTERM = re.search("rxvt|xterm", os.environ["TERM"]) and 1 or 0
  48 +
  49 +# ------------------------------------------
  50 +def error(msg):
  51 + msg and sys.stderr.write(msg)
  52 + sys.exit(1)
  53 +
  54 +# ------------------------------------------
  55 +def warning(msg):
  56 + msg and sys.stderr.write(msg)
  57 +
  58 +# ------------------------------------------
  59 +def which(program):
  60 + for path in string.split(os.environ["PATH"], ":"):
  61 + if os.path.exists(os.path.join(path, program)):
  62 + return os.path.join(path, program)
  63 + else:
  64 + warning("Could not find %s in your PATH.\n" % program)
  65 +
  66 +# ------------------------------------------
  67 +#def log(msg):
  68 +# f = open("log", "a"); f.write(msg); f.close()
50 69
51 70 # ------------------------------------------
52 71 class Stack:
@@ -553,9 +572,9 @@ class FilelistWindow(ListWindow):
553 572 for entry in os.listdir(self.cwd):
554 573 if os.path.isdir(self.cwd + entry):
555 574 self.dirs.append("%s/" % entry)
556   - elif RE_SONG.match(entry):
  575 + elif VALID_SONG(entry):
557 576 self.files.append("%s" % entry)
558   - elif RE_PLAYLIST.match(entry):
  577 + elif VALID_PLAYLIST(entry):
559 578 self.files.append("%s" % entry)
560 579 except os.error: pass
561 580 self.dirs.sort()
@@ -589,7 +608,7 @@ class FilelistWindow(ListWindow):
589 608 if os.path.isdir(self.cwd + self.current()):
590 609 self.chdir(self.cwd + self.current())
591 610 self.listdir()
592   - elif RE_SONG.match(self.current()):
  611 + elif VALID_SONG(self.current()):
593 612 app.player.play(self.cwd + self.current(), 0)
594 613
595 614 def command_chparentdir(self):
@@ -620,7 +639,7 @@ class FilelistWindow(ListWindow):
620 639
621 640 def command_add(self):
622 641 if (os.path.isfile(self.cwd + self.current()) and
623   - RE_SONG.match(self.current())):
  642 + VALID_SONG(self.current())):
624 643 app.win_playlist.append(self.cwd + self.current())
625 644 self.cursor_move(+1)
626 645
@@ -628,7 +647,7 @@ class FilelistWindow(ListWindow):
628 647 if os.path.isdir(self.cwd + self.current()):
629 648 self.add_dir(self.cwd + self.current())
630 649 self.cursor_move(+1)
631   - elif RE_PLAYLIST.match(self.current()):
  650 + elif VALID_PLAYLIST(self.current()):
632 651 app.win_playlist.append(self.cwd + self.current())
633 652 self.cursor_move(+1)
634 653
@@ -639,7 +658,7 @@ class FilelistWindow(ListWindow):
639 658 for entry in os.listdir(dir):
640 659 entries.append(os.path.join(dir, entry))
641 660 except os.error: return
642   - songs = filter(RE_SONG.match, entries)
  661 + songs = filter(VALID_SONG, entries)
643 662 dirs = filter(os.path.isdir, entries)
644 663 songs.sort()
645 664 for song in songs:
@@ -700,7 +719,7 @@ class PlaylistWindow(ListWindow):
700 719 self.random and "[random]" or " ")
701 720
702 721 def append(self, pathname):
703   - if RE_PLAYLIST.match(pathname):
  722 + if VALID_PLAYLIST(pathname):
704 723 try:
705 724 file = open(pathname)
706 725 for filename in map(string.strip, file.readlines()):
@@ -874,8 +893,8 @@ class PlaylistWindow(ListWindow):
874 893 if not filename: return
875 894 if filename[0] != '/':
876 895 filename = "%s%s" % (app.win_filelist.cwd, filename)
877   - if not RE_PLAYLIST.match(filename):
878   - filename = "%s%s" % (filename, '.m3u')
  896 + if not VALID_PLAYLIST(filename):
  897 + filename = "%s%s" % (filename, ".m3u")
879 898 try:
880 899 file = open(filename, "w")
881 900 for entry in self.buffer:
@@ -886,10 +905,36 @@ class PlaylistWindow(ListWindow):
886 905 app.status("Cannot write playlist!", 1)
887 906
888 907 # ------------------------------------------
889   -class Player:
890   - regex = re.compile("Frame#\s*(\d+)\s*\[\s*(\d+)")
  908 +class sox_Player:
  909 + def __init__(self):
  910 + self.format = re.compile(".*\.(aiff|au|cdr|wav)$", re.I)
  911 + self.program = which("sox")
  912 + self.command = "%s -t ossdsp /dev/dsp" % self.program
  913 +
  914 + def play(self, pathname, frame):
  915 + argv = string.split(self.command)
  916 + argv.insert(1, pathname)
  917 + try: os.execv(argv[0], argv)
  918 + except: os._exit(1)
  919 +
  920 +# ------------------------------------------
  921 +class mpg123_Player:
  922 + progress = re.compile("Frame#\s*(\d+)\s*\[\s*(\d+)")
891 923
892 924 def __init__(self):
  925 + self.format = re.compile(".*\.mp[123]$", re.I)
  926 + self.program = which("mpg123")
  927 + self.command = "%s -qv -k%%d" % self.program
  928 +
  929 + def play(self, pathname, frame):
  930 + argv = string.split(self.command % frame)
  931 + argv.append(pathname)
  932 + try: os.execv(argv[0], argv)
  933 + except: os._exit(1)
  934 +
  935 +# ------------------------------------------
  936 +class Player:
  937 + def __init__(self):
893 938 self.stopped = 1
894 939 self.paused = 0
895 940 self.pstdout = os.pipe()
@@ -934,10 +979,9 @@ class Player:
934 979 os.setpgrp()
935 980 os.dup2(self.pstdout[1], sys.stdout.fileno())
936 981 os.dup2(self.pstderr[1], sys.stderr.fileno())
937   - argv = string.split(app.command % frame)
938   - argv.append(pathname)
939   - try: os.execv(argv[0], argv)
940   - except: os._exit(1)
  982 + for player in PLAYERS:
  983 + if player.format.match(pathname):
  984 + player.play(pathname, frame)
941 985 self.seek_step = 1
942 986 self.seek_time = None
943 987 self.start_time = None
@@ -1093,7 +1137,7 @@ class Application:
1093 1137
1094 1138 # -- player input
1095 1139 if self.player.pstderr[0] in r:
1096   - m = Player.regex.search(os.read(self.player.pstderr[0], 256))
  1140 + m = mpg123_Player.progress.search(os.read(self.player.pstderr[0], 256))
1097 1141 if m: self.player.set_time(string.atoi(m.group(1)),
1098 1142 string.atoi(m.group(2)))
1099 1143
@@ -1146,26 +1190,20 @@ class Application:
1146 1190 self.quit()
1147 1191
1148 1192 # ------------------------------------------
1149   -def die(reason=None):
1150   - reason and sys.stderr.write(reason)
1151   - sys.exit(1)
1152   -
1153   -# ------------------------------------------
1154 1193 def main():
1155 1194 try:
1156 1195 opts, args = getopt.getopt(sys.argv[1:], "")
1157 1196 except:
1158   - die("Usage: %s [ file.mp3 | playlist.m3u ] ...\n" % sys.argv[0])
  1197 + error("Usage: %s [ file.mp3 | playlist.m3u ] ...\n" % sys.argv[0])
  1198 +
  1199 + for player in PLAYERS[:]:
  1200 + if not player.program:
  1201 + PLAYERS.remove(player)
  1202 + if not PLAYERS:
  1203 + error("No supported players found.. Sorry!\n")
1159 1204
1160 1205 global app
1161 1206 app = Application()
1162   - player = "mpg123"
1163   - for path in string.split(os.environ['PATH'], ":"):
1164   - if os.path.exists(os.path.join(path, player)):
1165   - app.command = "%s -qv -k%%d" % os.path.join(path, player)
1166   - break
1167   - else:
1168   - die("Error: Cannot find %s in your PATH\n" % player)
1169 1207
1170 1208 playlist = []
1171 1209 if not sys.stdin.isatty():
@@ -1189,4 +1227,16 @@ def main():
1189 1227 traceback.print_exc()
1190 1228
1191 1229 # ------------------------------------------
  1230 +PLAYERS = [mpg123_Player(), sox_Player()]
  1231 +
  1232 +def VALID_SONG(name):
  1233 + for player in PLAYERS:
  1234 + if player.format.match(name):
  1235 + return 1
  1236 +
  1237 +def VALID_PLAYLIST(name):
  1238 + if re.compile(".*\.m3u$", re.I).match(name):
  1239 + return 1
  1240 +
  1241 +# ------------------------------------------
1192 1242 if __name__ == "__main__": main()

0 comments on commit ea82f0e

Please sign in to comment.
Something went wrong with that request. Please try again.