]> git.lizzy.rs Git - plan9front.git/blob - rc/bin/play
torrent(1): update url in examples section
[plan9front.git] / rc / bin / play
1 #!/bin/rc
2 rfork e
3
4 nl='
5 '
6 out=/dev/audio
7 typ=()
8 tmp=()
9 argv0=$0
10
11 fn cleanup {
12         if(! ~ $#tmp 0)
13                 rm -f $tmp
14         tmp=()
15 }
16
17 fn sigint {
18         cleanup
19         exit
20 }
21
22 fn sigexit {
23         cleanup
24 }
25
26 fn play1 {
27         if(! ~ $#* 2){
28                 tmp=(/tmp/play.$pid.$#tmp.tmp $tmp)
29                 dd -bs 8192 -count 1 >$tmp(1) >[2]/dev/null
30                 cat $tmp(1) /fd/0 | play1 $1 `{file -m $tmp(1)}
31                 cleanup
32         }
33         if not {
34                 switch($2){
35                 case *plain*
36                         sed 's/\r//g' | while(j=`$nl{read}){
37                                 echo $"j >[1=2]
38                                 if(~ $"j http:* https:* HTTP:* HTTPS:*){
39                                         hget -r 'User-Agent: play' -r 'Icy-MetaData: 0' $"j | play1 $"j
40                                 }
41                                 if not {
42                                         if(test -r $"j)
43                                                 play1 <$"j $"j
44                                         if not {
45                                                 echo $argv0: 'can''t open file:' $"j >[1=2]
46                                         }
47                                 }
48                         }
49                 case *ogg* *vorbis*
50                         audio/oggdec
51                 case *mp3* *mpeg*
52                         audio/mp3dec
53                 case *wave*
54                         audio/wavdec
55                 case *flac*
56                         audio/flacdec
57                 case *audio/basic*
58                         audio/sundec
59                 case *audio/midi*
60                         games/midi -c
61                 case *audio/mus*
62                         games/mus | games/midi -c
63                 case *pls*
64                         awk 'BEGIN {FS="="} /^File/{print $2}' | play1 list plain
65                 case *
66                         switch($1){
67                         case *.mp3 *.MP3
68                                 audio/mp3dec
69                         case *.ogg *.OGG
70                                 audio/oggdec
71                         case *.wav *.WAV
72                                 audio/wavdec
73                         case *.flac *.FLAC
74                                 audio/flacdec
75                         case *.au *.AU
76                                 audio/sundec
77                         case *.mid *.MID
78                                 games/midi -c
79                         case *.mus *.MUS
80                                 games/mus | games/midi -c
81                         case *
82                                 echo $argv0: $1: unknown format: $2 >[1=2]
83                         }
84                 }
85         }
86 }
87
88 fn usage {
89         echo usage: $argv0 '[ -o file ] [ file ... ]' >[1=2]
90         exit usage
91 }
92
93 while(~ $1 -*){
94         switch($1){
95         case -o
96                 out=$2
97                 shift
98         case *
99                 usage
100         }
101         shift
102 }
103
104 # boost priority
105 @{echo pri 13 >/proc/$pid/ctl} >/dev/null >[2=1] 
106
107 >$out {
108         if(~ $#* 0){play1 stdin; exit}
109         for(i){echo $"i} | play1 args plain
110