1#!/bin/sh
2# vsound - a wrapper script that allows you to record the output of OSS programs
3#
4# Copyright (C) 2004 Nathan Chantrell <nsc@zorg.org>
5# Copyright (C) 2003 Richard Taylor <r.taylor@bcs.org.uk>
6# Copyright (C) 2000,2001 Erik de Castro Lopo <erikd@zip.com.au>
7# Copyright (C) 1999 James Henstridge <james@daa.com.au>
8#
9#   Based on the esddsp utility that is part of esound.  Esddsp's copyright:
10#   Copyright (C) 1998, 1999 Manish Singh <yosh@gimp.org>
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU Lesser General Public License as published by
14# the Free Software Foundation; either version 2.1 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU Lesser General Public License for more details.
21#
22# You should have received a copy of the GNU Lesser General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26verbose=0
27keep_temps=0
28resamplerate=0
29outfile=./vsound.wav
30convert_to_wav=1
31
32if test $# -le 1; then
33	if [ "$1" = "-h" ] || [ "$1" = "--help" ] || \
34	   [ "$1" = "-V" ] || [ "$1" = "--version" ] ; then
35		echo -n
36	else
37		# No comand line args so recall ourself with -h option.
38		$0 -h
39		exit 0
40	fi
41fi
42
43while test $# -gt 0; do
44	case "$1" in
45	-h|--help)
46		echo "vsound - digitally record output of an OSS audio program"
47		echo ""
48		echo "vsound [options] program arguments"
49		echo ""
50		echo "options:"
51		echo "-f, --file=FILE    output file name"
52		echo "-v, --verbose      set to verbose output"
53		echo "-k, --keep-temps   don't delete temporary files"
54		echo "-h, --help         this help message"
55		echo "-V, --version      show program version"
56                echo "-r, --resample     resample the output file to the given sample rate"
57                echo "                     eg. vsound -r 44100 realplay file.rm"
58		echo "-d, --dspout       enable simulateous output to /dev/dsp and file"
59		echo "                     (may be required for some programs)"
60		echo "-s, --stdout       write the intermediate (Sun AU format) file to stdout"
61		echo "                     (no other output file is generated)"
62		echo "-n, --no-convert   do not convert the AU file to WAV"
63		echo "-t, --timing       add timing delays to allow recording of streaming data"
64		echo "-a, --autostop=SEC kill the player after 'SEC' seconds of inactivity."
65		echo
66		exit 0
67		;;
68
69	-V|--version)
70		echo "@PACKAGE@-@VERSION@"
71		exit 0
72		;;
73
74	--file=*)
75		outfile="`echo $1 | sed -e 's/^[^=]*=//g'`"
76		shift
77		;;
78
79	-f|--file)
80		shift
81		if test $# -gt 0; then
82			outfile="$1"
83		else
84			echo "no output file specified"
85			exit 1
86		fi
87		shift
88		;;
89
90	-d|--dspout)
91		export VSOUND_DSPOUT=1
92		shift
93		;;
94
95        -r|--resample)
96                shift
97                if test $# -gt 0; then
98                        resamplerate="$1"
99                else
100                        echo "no sample rate specified"
101                        exit 1
102                fi
103                shift
104                ;;
105
106	-s|--stdout)
107		export VSOUND_STDOUT=1
108		shift
109		;;
110
111	-v|--verbose)
112		verbose=1
113		shift
114		;;
115
116	-k|--keep-temps)
117		keep_temps=1
118		shift
119		;;
120
121	-n|--no-convert)
122		convert_to_wav=0
123		outfile=./vsound.au
124		shift
125		;;
126
127	-t|--timing)
128		export VSOUND_TIMING=1
129		shift
130		;;
131
132	-a|--autostop)
133		shift
134		if test $# -gt 0; then
135			export VSOUND_STOPDELAY="$1"
136		else
137			echo "no autostop delay specified"
138			exit 1
139			fi
140		shift
141		;;
142
143	--)
144		shift
145		break
146		;;
147
148	*)
149		break
150		;;
151	esac
152done
153
154export VSOUND_DATA=./vsound$$.au
155
156if [ "$verbose" ] && [ "$VSOUND_STDOUT" ]; then
157	echo "Error : cannot specify both --verbose and --stdout."
158	exit 1
159fi
160
161
162if [ "$verbose" = 1 ]; then
163	echo "Output file:   $outfile"
164	echo "Temp AU file:  $VSOUND_DATA"
165fi
166
167prefix=@prefix@
168exec_prefix=@exec_prefix@
169pkglibdir=@libdir@/@PACKAGE@
170
171if [ ! "$VSOUND_STDOUT" ]; then
172	echo "About to start the application. The output will not be available"
173	echo "until the application exits."
174fi
175
176# run the program with the vsound library preloaded
177LD_PRELOAD="$pkglibdir/libvsound.so" "$@"
178
179if test "$VSOUND_STDOUT" = 1; then
180	# Output went to stdout so there's no further processing to be done.
181	exit 0
182fi
183
184if [ ! -e "$VSOUND_DATA" ]; then
185	echo "Missing file $VSOUND_DATA."
186	echo "This means that the libvsound wrapper did not work correctlty."
187	echo "Here are some the possible reasons :"
188	echo " - You are trying to record a stream (RTSP or PNM protocol) from "
189	echo "   the internet. You will need to use the --timing option."
190	echo " - The program you are trying to run is setuid. You will need to "
191	echo "   run vsound as root."
192	echo " - Vsound was not properly installed and hence won't work at all."
193	exit
194fi
195
196if test "$resamplerate" != 0; then
197        # Use SoX to convert sample rate and file types simulatneously.
198        echo -n "Resampling file to $resamplerate Hz : "
199        sox $VSOUND_DATA -r $resamplerate $outfile resample
200        echo "Done."
201elif test "$convert_to_wav" = 1; then
202        # Use SoX to change from AU file to WAV file.
203        if test "$verbose" = 1; then
204                echo "Converting to WAV file."
205        fi
206        sox $VSOUND_DATA $outfile
207else
208        # Rename the file to remove pid portion of name.
209        mv $VSOUND_DATA $outfile
210        exit 0
211fi
212
213if test "$keep_temps" = 0; then
214        if test "$verbose" = 1; then
215                echo "Deleting temporary files."
216        fi
217        rm -f $VSOUND_DATA
218fi
219