shithub: riscv

ref: eca2dbed8b9872f8071b0e8ddc2d1da3f097f283
dir: /rc/bin/play/

View raw version
#!/bin/rc
rfork e

out=/dev/audio
typ=()
tmp=()
argv0=$0

fn cleanup {
	if(! ~ $#tmp 0)
		rm -f $tmp
	tmp=()
}

fn sigint {
	cleanup
	exit
}

fn sigexit {
	cleanup
}

fn play1 {
	if(~ $#* 0){
		tmp=(/tmp/play.$pid.$#tmp.tmp $tmp)
		dd -bs 8192 -count 1 >$tmp(1) >[2]/dev/null
		cat $tmp(1) /fd/0 | play1 `{file -m $tmp(1)} xxx
		cleanup
	}
	if not {
		switch($1){
		case *plain*
			sed 's/
//g' | while(j=`{read}){
				echo $"j >[1=2]
				if(~ $"j http:* https:* HTTP:* HTTPS:*){
					hget -r 'Icy-MetaData: 0' $j | play1
				}
				if not {
					if(test -r $"j)
						play1 <$"j
					if not {
						echo $argv0: 'can''t open file:' $"j >[1=2]
					}
				}
			}
		case *ogg* *vorbis*
			audio/oggdec
		case *mp3* *mpeg*
			audio/mp3dec
		case *wave*
			audio/wavdec
		case *flac*
			audio/flacdec
		case *audio/basic*
			audio/sundec
		case *pls*
			awk 'BEGIN {FS="="} /^File/{print $2}' | play1 plain
		case *
			echo $argv0: unknown format: $1 >[1=2]
		}
	}
}

fn usage {
	echo usage: $argv0 [ -o file ] [ file ... ] >[1=2]
	exit usage
}

while(~ $1 -*){
	switch($1){
	case -o
		out=$2
		shift
	case *
		usage
	}
	shift
}

# boost priority
@{echo pri 13 >/proc/$pid/ctl} >/dev/null >[2=1] 

>$out {
	if(~ $#* 0){play1; exit}
	for(i){echo $i} | play1 plain 
}