]> git.lizzy.rs Git - plan9front.git/blob - rc/bin/play
FLAC audio support
[plan9front.git] / rc / bin / play
1 #!/bin/rc
2
3 out=/dev/audio
4 typ=()
5 tmp=()
6 argv0=$0
7
8 fn cleanup {
9         if(! ~ $#tmp 0)
10                 rm -f $tmp
11         tmp=()
12 }
13
14 fn sigint {
15         cleanup
16         exit
17 }
18
19 fn sigexit {
20         cleanup
21 }
22
23 fn play1 {
24         if(~ $#* 0){
25                 tmp=/tmp/play.$pid.tmp
26                 dd -bs 64 -count 1 >$tmp >[2]/dev/null
27                 cat $tmp /fd/0 | play1 `{file -m $tmp} xxx
28                 cleanup
29         }
30         if not {
31                 switch($1){
32                 case *plain*
33                         sed 's/\r//g' | while(j=`{read}){
34                                 echo $"j >[1=2]
35                                 t=$typ
36                                 if(~ $"j http:* https:* HTTP:* HTTPS:*){
37                                         if(~ $#t 0) t=mp3
38                                         hget -r 'Icy-MetaData: 0' $j | play1 $t
39                                 }
40                                 if not {
41                                         if(test -r $"j){
42                                                 if(~ $#t 0 && ~ $"j *mp3*) t=mp3
43                                                 play1 $t <$"j
44                                         }
45                                         if not {
46                                                 echo $argv0: 'can''t open file:' $"j >[1=2]
47                                         }
48                                 }
49                         }
50                 case *ogg* *vorbis*
51                         audio/oggdec
52                 case *mp3* *mpeg*
53                         audio/mp3dec
54                 case *flac*
55                         audio/flacdec
56                 case *pls*
57                         awk 'BEGIN {FS="="} /^File/{print $2}' | play1 plain
58                 case *
59                         echo $argv0: unknown format: $1 >[1=2]
60                 }
61         }
62 }
63
64 fn usage {
65         echo usage: $argv0 [ -t type ] [-o file ] [ file ... ] >[1=2]
66         exit usage
67 }
68
69 while(~ $1 -*){
70         switch($1){
71         case -t
72                 typ=$2
73                 shift
74         case -o
75                 out=$2
76                 shift
77         case *
78                 usage
79         }
80         shift
81 }
82
83 >$out {
84         if(~ $#* 0){play1 $typ; exit}
85         for(i){echo $i} | play1 plain 
86