source: flacrip/trunk/playflac @ 8

Last change on this file since 8 was 1, checked in by peter, 13 years ago

initial import of current flacrip files into the trunk

  • Property svn:executable set to *
File size: 3.6 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use IPC::Open2;
5use IO::Prompt;
6use Term::ReadKey;
7
8my $FLAC_FILE = shift;
9my $TRACK = shift || 1;
10
11$|++;
12
13# get the track start positions from the embedded cuesheet
14open my $CUESHEET, "metaflac --export-cuesheet-to - $FLAC_FILE |";
15
16my @position_for_track;
17my $i = 1;
18while (<$CUESHEET>) {
19    if (my ($m,$s,$f) = /INDEX 01 (\d\d):(\d\d):(\d\d)/) {
20        $position_for_track[$i] = ($m * 60) + $s + ($f / 75);
21        $i++;
22    }
23}
24
25close $CUESHEET;
26
27
28my $track_count = scalar @position_for_track - 1;
29print "$track_count tracks\n";
30
31die "There is no track $TRACK" if $TRACK < 1 or $TRACK > $track_count;
32
33   
34# launch the flac123 player as a daemon
35my $flac123_pid = open2(my $PLAYER_OUT, my $PLAYER_IN, qw{flac123 -R});
36
37print "flac123 PID = $flac123_pid\n";
38
39my $current_track = $TRACK;
40my $next_track_start = $position_for_track[$current_track + 1];
41
42print "Track $current_track\n";
43
44my %commands = (
45    z => sub {
46        return if $current_track <= 1;
47        $current_track--;
48        $next_track_start = $position_for_track[$current_track + 1];
49        my $position = $position_for_track[$current_track];
50        print $PLAYER_IN "JUMP $position\n";
51        print "Track $current_track\n";
52    },
53    x => sub {},
54    c => sub {},
55    v => sub {},
56    b => sub {
57        return if $current_track >= $track_count;
58        $current_track++;
59        $next_track_start = $position_for_track[$current_track + 1];
60        my $position = $position_for_track[$current_track];
61        print $PLAYER_IN "JUMP $position\n";
62        print "Track $current_track\n";
63    },
64    q => sub {
65        print $PLAYER_IN "QUIT\n";
66        ReadMode 'restore';
67        exit;
68    },
69);
70
71
72print $PLAYER_IN "LOAD $FLAC_FILE\n";
73print $PLAYER_IN "JUMP $position_for_track[$TRACK]\n";
74
75ReadMode 4;
76
77while (<$PLAYER_OUT>) {
78    # do a non-blocking read to see if the user pressed a key
79    my $cmd = ReadKey -1;
80    if (defined $cmd && exists $commands{$cmd}) {
81        $commands{$cmd}->();
82    }
83
84    if (/\@F (\d+) (\d+) (\d+\.\d\d) (\d+\.\d\d)/) {
85        my ($frame, $frames_left, $time, $time_left) = ($1, $2, $3, $4);
86        if (defined $next_track_start && $time >= $next_track_start) {
87            $current_track++;
88            $next_track_start = $position_for_track[$current_track + 1];
89            print "Track $current_track\n";
90        }
91
92=begin
93
94        for (my $track_number = $track_count; $track_number > 0; $track_number--) {
95            #warn $track_number;
96            if ($position_for_track[$track_number] <= $time) {
97                $current_track = $track_number;
98                #print "Track $current_track\n";
99                last;
100            }
101        }       
102
103=cut
104
105    }
106}
107   
108waitpid $flac123_pid, 0;
109
110ReadMode 'restore';
111
112=begin flac123 -R outputs
113
114@R FLAC123
115flac123 tagline. Output at startup.
116
117@I ID3:<a><b><c><d><e><f>
118Prints out the metadata information after loading the flac file.
119a = title (30 chars)
120b = artist (30 chars)
121c = album (30 chars)
122d = year (4 chars)
123e = comment (30 chars)
124f = genre (30 chars)
125
126@I filename
127Prints out the filename of the flac file, minus the extension. Happens after
128a flac file has been loaded and there is no metadata available.
129
130@F <current-frame> <frames-remaining> <current-time> <time-remaining>
131Frame decoding status updates (once per frame).
132Current-frame and frames-remaining are integers; current-time and
133time-remaining floating point numbers with two decimal places.
134
135@P {0, 1, 2}
136Stop/pause status.
1370 - playing has stopped. When 'STOP' is entered, or the flac file is finished.
1381 - Playing is paused. Enter 'PAUSE' or 'P' to continue.
1392 - Playing has begun again.
140
141=cut
Note: See TracBrowser for help on using the repository browser.