1#!/bin/sh
2#
3# Diverse Bristol audio routines.
4# Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2011
5#
6#  This program is free software; you can redistribute it and/or modify
7#  it under the terms of the GNU General Public License as published by
8#  the Free Software Foundation; either version 3 of the License, or
9#  (at your option) any later version.
10#
11#  This program is distributed in the hope that it will be useful,
12#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#  GNU General Public License for more details.
15#
16#  You should have received a copy of the GNU General Public License
17#  along with this program; if not, see <http://www.gnu.org/licenses/>.
18#
19#set -x
20
21args=$*
22
23vers=@VERSION@
24
25cli=0
26engine=1
27gui=1
28quiet=0
29verbose=0
30jack=0
31export JACK_START_SERVER=1
32midi=seq
33HELP=0
34README=0
35
36EMULATE=hammondB3
37FREQ=0
38
39#PORT=5028
40# Randomise the port numbers, can be overridden by -port which would be a
41# requirement for multitimbral
42UNAME=`uname`
43if [ "${UNAME}x" = "DragonFlyx" ]; then
44	PORT=`jot -r 1 1025 65536`
45else
46	PORT=`date +%N`
47	PORT=`expr $PORT % 65536` >/dev/null 2>&1
48	if [ -z $PORT ]; then
49		PORT=`date +%s`
50		PORT=`expr $PORT % 65536` >/dev/null 2>&1
51	fi
52	if [ $PORT -lt 1024 ]; then
53		PORT=`expr $PORT + 5028`
54	fi
55fi
56
57valgrind=0
58execme=1
59liblist=0
60
61host=net
62
63engstatus=0
64guistatus=0
65
66JS=1
67RATE=44100
68DRIVER=@DEFAULT_AUDIO_FLAG@
69COUNT=256
70
71LOGO=no
72
73#
74# The following is configured by the autotools process.
75#
76BRISTOL_DIR=@BRISTOL_DIR@
77
78#
79# Whatever directory was called for startBristol we also neede in the PATH
80#
81PATH=${PATH}:`dirname $0`
82
83showhelp()
84{
85	if [ $HELP -eq 3 ]; then
86		brighton -V
87		exit 0
88	fi
89	if [ $HELP -eq 2 ]; then
90		brighton -h | less
91		exit 0
92	fi
93	if [ $verbose = 1 ]; then
94		# let the bristol gui give a verbose help text.
95		engine=0
96	else
97		brighton -V
98		echo
99		echo "startBristol [-explorer|-mini|-memory|-hammond|-b3|-dx|-axxe|-odyssey|-2600|-mono|-poly|-juno|-prophet|-pro10|-pro52|-mixer|-vox|-rhodes|-rhodesbass|-obx|-obxa|-aks|<others>] [-oss|-alsa|-jack] [[-help|-h] [-verbose|-v]]"
100		echo
101		echo "Try 'startBristol -v|-h' for more verbose help output on device configuration."
102		echo
103		echo "Try 'startBristol -readme' for very verbose output"
104		echo "Try 'startBristol -readme -mini' for info on specific emulator"
105		echo
106		echo "Try 'startBristol -b3' or 'startBristol -jack', for example, if you are unsure."
107		echo
108		echo "Q&A to nickycopeland@hotmail.com"
109		echo "bugs, features, enhancements to http://sourceforge.net/projects/bristol"
110		echo
111		exit 0
112	fi
113}
114
115if [ $# = 0 ]; then
116	HELP=1
117fi
118
119# Nokey: Don't override BRISTOL if it's not already set
120if [ -z "$BRISTOL" ]; then
121	if [ -d "${BRISTOL_DIR}/memory" ]; then
122		export BRISTOL=${BRISTOL_DIR}
123	else
124		# We should first take our basename and look for a binary
125		BRISTOL=`dirname $0`
126		BRISTOL=`dirname $BRISTOL`
127
128		if [ ! -f $BRISTOL/bin/bristol ]; then
129			#
130			# Take a default
131			#
132			export BRISTOL=/opt/audio/bristol
133		fi
134	fi
135fi
136export BRISTOL
137
138for index in $*; do
139	if [ $index = "-stop" ]; then
140		pkill bristol
141		if [ $? != 0 ]; then
142			pkill brighton
143		fi
144		exit 0
145	fi
146	if [ $index = "-exit" ]; then
147		pkill bristol
148		pkill brighton
149		exit 0
150	fi
151	if [ $index = "-kill" ]; then
152		pkill -f "\\$2"
153		exit 0
154	fi
155	if [ $index = "-host" ]; then
156		host=`echo $2 | awk -F: '{print $1}'`
157		if [ $host = "unix" ]; then
158			localport=`echo $2 | awk -F: '{print $2}'`
159			if [ -z $localport ]; then
160				localport=$PORT
161			fi
162		fi
163	fi
164	if [ $index = "-freqtweak" ]; then
165		FREQ=$2
166		if [ $FREQ != 0 -a -x /usr/bin/cpufreq-set -a $execme -eq 0 ]; then
167			echo Setting min CPU speed to $FREQ
168			cpufreq-set -d ${FREQ}
169		fi
170	fi
171	if [ $index = "-count" ]; then
172		COUNT=$2
173	fi
174	if [ $index = "-rc" ]; then
175		BRISTOL_RC=/dev/null
176	fi
177	if [ $index = "-sleep" ]; then
178		sleep $2
179	fi
180	if [ $index = "-emulate" ]; then
181		EMULATE=$2
182	fi
183	if [ $index = "-port" ]; then
184		PORT=$2
185	fi
186	if [ $index = "-cache" ]; then
187		export BRISTOL_CACHE=$2
188	fi
189	if [ $index = "-memdump" ]; then
190		export BRISTOL_DUMP=$2
191	fi
192	if [ $index = "--summary" ]; then
193		brighton --summary
194		exit 0
195	fi
196	if [ $index = "-summary" ]; then
197		brighton -summary
198		exit 0
199	fi
200	if [ $index = "-console" ]; then
201		export BRISTOL_LOG_CONSOLE=true
202	fi
203	if [ $index = "-cli" ]; then
204		cli=1
205		execme=0
206	fi
207	if [ $index = "-exec" ]; then
208		execme=0
209	fi
210	if [ $index = "-ladi" ]; then
211		execme=1
212	fi
213	if [ $index = "-valgrind" ]; then
214		valgrind=1
215	fi
216	if [ $index = "-debug" ]; then
217		liblist=1
218	fi
219	if [ $index = "-liblist" ]; then
220		liblist=1
221	fi
222	if [ $index = "-master" ]; then
223		engine=0
224	fi
225	if [ $index = "-engine" ]; then
226		engine=0
227	fi
228	if [ $index = "-gui" ]; then
229		gui=0
230	fi
231	if [ $index = "-readme" ]; then
232		README=1
233	fi
234	if [ $index = "-v" ]; then
235		HELP=2
236	fi
237	if [ $index = "--v" ]; then
238		HELP=2
239	fi
240	if [ $index = "-verbose" ]; then
241		verbose=1
242	fi
243	if [ $index = "--verbose" ]; then
244		verbose=1
245	fi
246	if [ $index = "-version" ]; then
247		HELP=3
248	fi
249	if [ $index = "-V" ]; then
250		HELP=3
251	fi
252	if [ $index = "-logo" ]; then
253		LOGO=yes
254	fi
255	if [ $index = "-h" ]; then
256		HELP=2
257	fi
258	if [ $index = "--h" ]; then
259		HELP=2
260	fi
261	if [ $index = "-help" ]; then
262		HELP=2
263	fi
264	if [ $index = "--help" ]; then
265		HELP=2
266	fi
267	if [ $index = "-libtest" ]; then
268		engine=0
269	fi
270	if [ $index = "-quiet" ]; then
271		quiet=1
272	fi
273	if [ $index = "-q" ]; then
274		quiet=1
275	fi
276	if [ $index = "-jsmuuid" ]; then
277		export BRISTOL_AUTOCONN=false
278	fi
279	if [ $index = "-jackstats" ]; then
280		JS=0
281	fi
282	if [ $index = "-jack" ]; then
283		jack=1
284		COUNT=1024
285		DRIVER=-jack
286	fi
287	if [ $index = "-audio" ]; then
288		DRIVER=-$2
289	fi
290	if [ $index = "-alsa" ]; then
291		jack=0
292		DRIVER=-alsa
293	fi
294	if [ $index = "-jackstart" ]; then
295		unset JACK_START_SERVER
296	fi
297	shift
298done
299
300#
301# As a note for anybody interested in reading this script, brighton also has
302# a BRISTOL_CACHE directory for private memories, sequences and controller maps.
303# Default is in $HOME/.bristol and we create this if it is not already made
304#
305if [ -z "$BRISTOL_CACHE" ]; then
306	export BRISTOL_CACHE=${HOME}/.bristol
307fi
308
309if [ -z "$BRISTOL_DUMP" ]; then
310	mkdir -p ${BRISTOL_CACHE}/memory
311else
312	if [ ! -d $BRISTOL_DUMP/memory/$EMULATE ]; then
313		mkdir -p $BRISTOL_DUMP/memory/$EMULATE
314		cp -f $BRISTOL_DIR/memory/$EMULATE/* $BRISTOL_DUMP/memory/$EMULATE
315		cp -f $BRISTOL_CACHE/memory/$EMULATE/* $BRISTOL_DUMP/memory/$EMULATE
316		echo created memory shadow in $BRISTOL_DUMP
317	fi
318	export BRISTOL_CACHE=$BRISTOL_DUMP
319fi
320
321PREARGS=
322POSTARGS=
323#
324# Changed some of the processing to have tokens in the PREARGS file. We are
325# looking for PREARGS and POSTARGS. If we don't find either of them then take
326# the whole file which was the previous method and is equivalent to PREARGS
327#
328if [ -z "${BRISTOL_RC}" -a -f ${BRISTOL_CACHE}/bristolrc ]; then
329	BRISTOL_RC=${BRISTOL_CACHE}/bristolrc
330fi
331if [ -f "${BRISTOL_RC}" ]; then
332	egrep '(PREARGS|POSTARGS)' ${BRISTOL_RC} >/dev/null
333
334	if [ $? -gt 0 ]; then
335		PREARGS=`cat ${BRISTOL_RC}`
336	else
337		PREARGS=`awk -F= '/PREARGS/ {print $2}'  ${BRISTOL_RC}`
338		POSTARGS=`awk -F= '/POSTARGS/ {print $2}'  ${BRISTOL_RC}`
339	fi
340fi
341
342# Nokey: Does the BRISTOL directory actually exist?
343if [ ! -d $BRISTOL ]; then
344	# Apparently not. Let's try to make a guess as to where it is.
345
346	# Try to find out how we were started. If this script was in
347	#  the PATH of the user, the $0 should have a leading '/'.
348	#  This leading '/' would also be there if this script was
349	#  executed as /full/path/name/startBristol
350	STARTUP=$0
351	# If this script was started as ./"*whatever*" then we need
352	#  to strip off the './' in order to accurately determine
353	#  the BRISTOL directory
354	if [ "$(echo $STARTUP | cut -c 1-2)" = "./" ]; then
355		STARTUP=$(echo $STARTUP | cut -c 3-)
356	fi
357
358	if [ "$(echo $STARTUP | cut -c 1)" = "/" ]; then
359		BIN_DIR=$(dirname $STARTUP)
360	else
361		BIN_DIR=$(dirname $PWD/$STARTUP)
362	fi
363
364	export BRISTOL=$(dirname $BIN_DIR)
365	unset BIN_DIR STARTUP
366fi
367
368if [ ${README} -ge 1 ]; then
369	brighton $args | less
370	exit 0
371fi
372
373if [ ${HELP} -ge 1 ]; then
374	showhelp
375fi
376
377#
378# Try and get Jack driver information. If the daemon is running, use it. If
379# user has a .jackdrc use that, otherwise use /etc/jackdrc
380#
381if [ ${DRIVER} = "-jack" ]; then
382	jack=1
383
384	if [ $JS -ne 0 ]; then
385		JP=`bristoljackstats 2>&1`
386		if [ $? -eq 0 ]; then
387			JACKPARAMS=`echo $JP | awk '/JACKSTATS/'`
388			RATE=`echo ${JACKPARAMS} | awk '{print $2}'`
389			COUNT=`echo ${JACKPARAMS} | awk '{print $3}'`
390
391			echo jackstats found -rate ${RATE} -count ${COUNT}
392		fi
393	else
394		#
395		# We should not really get here. If jack was requested but the daemon
396		# could not be reached (we did not get feedback from bristoljackstats)
397		# then larger issues will occur presently
398		#
399		# Thanks to Sylvain Robitaille for the next code.
400		#
401		# See if the process is running
402		#
403		# We should replace this with pgrep and do it for this user - if the
404		# daemon is running with other rights we may not be able to connect.
405		#
406		JACKD=`ps ax | grep -w jackd | grep -vw grep`
407
408		#
409		# Otherwise see what would be started
410		#
411		if [ -z "${JACKD}" ]; then
412			echo "jack driver requested, jackd not running"
413
414			if [ -f /etc/jackdrc ]; then
415				echo "jack configuration found in /etc/jackdrc"
416				JACKD=`cat /etc/jackdrc`
417			fi
418
419			if [ -f ~/.jackdrc ]; then
420				echo "private jack configuration found in ~/.jackdrc"
421				JACKD=`cat ~/.jackdrc`
422			fi
423		fi
424
425		if [ -z "${JACKD}" ]; then
426			RATE=48000
427			COUNT=1024
428		else
429			RATE=`echo ${JACKD} |grep -o -- '-r[0-9][0-9]*' |sed 's/^-r//'`
430
431			if [ -z "${RATE}" ]; then
432				# assume Jackd's default rate:
433				RATE=48000
434			fi
435
436			#COUNT=`echo ${JACKD} |grep -o -- '-p[0-9][0-9]*' |sed 's/^-p//'`
437			COUNT=`echo ${JACKD} |grep -o -- '-p[0-9][0-9]*' |tail -n1 |sed 's/^-p//'`
438
439			if [ -z "${COUNT}" ]; then
440				# assume Jackd's default count:
441				COUNT=1024
442			fi
443		fi
444	fi
445fi
446
447#
448# If slabhome already exists, we should take it rather than this definition.
449#
450export SLAB_HOME=$BRISTOL
451export BRIGHTON=$BRISTOL
452
453export LD_LIBRARY_PATH=@BRISTOL_DIR@/lib:/usr/local/lib:/usr/lib:/lib
454
455export PATH=$BRISTOL/bin:/usr/local/bin:${PATH}
456
457if [ ${liblist} != 0 ]; then
458	echo
459	echo '*** bristol libraries ***'
460	echo
461	ldd `which bristol`
462	echo '*** brighton libraries ***'
463	echo
464	ldd `which brighton`
465	echo
466fi
467
468if [ $jack -eq 1 ]; then
469	ldd `which bristol` | grep jack > /dev/null 2>&1
470	if [ $? -ne 0 ]; then
471		echo Requested Jack drivers, not compiled into bristol
472		exit -1
473	fi
474fi
475
476if [ ${LOGO} = "yes" ]; then
477	#
478	# Just to make sure
479	#
480	echo
481	echo "Bristol is about to start."
482	echo
483	echo "This program is totally unrelated to ANY of the companies that produced"
484	echo "the original instruments emulated by bristol, and none of the original"
485	echo "manufacturers in ANY way endorse this product."
486	echo
487
488	sleep 1
489fi
490
491#
492# Requesting an engine. This means we need a free TCP port. Our default is
493# 5028 and if it is not free then search for one from there
494#
495if [ $engine = 1 ]; then
496	if [ $host = "unix" ]; then
497		echo checking availability of host port $localport
498		ls /tmp/br.$localport >/dev/null 2>&1
499		if [ $? -eq 0 ]; then
500			echo "host port looked busy, unlinking"
501			rm -f /tmp/br.$localport
502		fi
503	else
504		echo checking availability of TCP port $PORT
505		netstat -a -f inet -p tcp -ln | grep $PORT > /dev/null
506		while [ $? -eq 0 ]; do
507			echo -n "port looked busy, trying "
508			PORT=`expr $PORT + 1`
509			echo $PORT
510			netstat -a -f inet -p tcp -ln | grep $PORT > /dev/null
511		done
512		if [ ${PORT} -lt 1024 -a ${USER} != "root" ]; then
513				echo you may not have permissions for ports less than 1024
514		else
515			echo using port $PORT
516		fi
517	fi
518fi
519
520#
521# Start up the GUI
522#
523if [ $cli = 1 ]; then
524	if [ $engine = 1 ]; then
525		bristol -rate ${RATE} -count ${COUNT} ${PREARGS} $args -port $PORT ${POSTARGS} &
526	fi
527	export BRISTOL_LOG_CONSOLE=true
528	brighton ${PREARGS} $args -port ${PORT} ${POSTARGS}
529else
530	if [ $gui = 1 ]; then
531		if [ $engine = 1 ]; then
532			if [ $quiet = 0 ]; then
533				brighton ${PREARGS} $args -port ${PORT} ${POSTARGS} &
534			else
535				brighton ${PREARGS} $args -port ${PORT} ${POSTARGS} > /dev/null 2>&1 &
536			fi
537		else
538			if [ $quiet = 0 ]; then
539				if [ $execme = 0 ]; then
540					if [ $valgrind = 0 ]; then
541						brighton ${PREARGS} $args -port ${PORT} ${POSTARGS}
542					else
543						valgrind --leak-check=full -v --show-reachable=yes --track-origins=yes brighton $args -port ${PORT} ${POSTARGS}
544					fi
545				else
546					exec brighton ${PREARGS} $args -port ${PORT} ${POSTARGS}
547				fi
548				guistatus=$?
549			else
550				if [ $execme = 0 ]; then
551					brighton ${PREARGS} $args -port ${PORT} ${POSTARGS} > /dev/null 2>&1
552				else
553					exec brighton ${PREARGS} $args -port ${PORT} ${POSTARGS} > /dev/null 2>&1
554				fi
555				guistatus=$?
556			fi
557		fi
558	fi
559
560	#
561	# the bristol engine has flags for -oss or -alsa drivers, -preload buffers of
562	# -count samples:
563	#	bristol -preload 4 -count 256 [-oss]
564	#
565	if [ $engine = 1 ]; then
566		if [ $quiet = 0 ]; then
567			if [ $valgrind = 0 ]; then
568				if [ $execme = 0 ]; then
569					bristol -rate ${RATE} -count ${COUNT} ${PREARGS} $args -port $PORT ${POSTARGS}
570					engstatus=$?
571				else
572					exec bristol -rate ${RATE} -count ${COUNT} ${PREARGS} $args -port $PORT ${POSTARGS}
573				fi
574			else
575				valgrind --leak-check=full -v --show-reachable=yes --track-origins=yes bristol -rate ${RATE} -count ${COUNT} ${PREARGS} $args -port $PORT ${POSTARGS}
576				engstatus=$?
577			fi
578		else
579			if [ $execme = 0 ]; then
580				bristol -rate ${RATE} -count ${COUNT} ${PREARGS} $args -port $PORT ${POSTARGS} > /dev/null 2>&1
581			else
582				exec bristol -rate ${RATE} -count ${COUNT} ${PREARGS} $args -port $PORT ${POSTARGS} > /dev/null 2>&1
583			fi
584			engstatus=$?
585		fi
586	fi
587fi
588
589# Make sure we have the children returned, prevents ugly delayed output
590if [ $engine = 1 ]; then
591	wait % >/dev/null 2>&1
592fi
593if [ $gui = 1 ]; then
594	wait % >/dev/null 2>&1
595fi
596
597sleep 1
598
599if [ $FREQ != 0 -a -x /usr/bin/cpufreq-set ]; then
600	cpufreq-set -d 250MHz
601fi
602# This could be a lot more intelligent but it will work. We need to remove any
603# dangling sockets. Alternatively just delete the files, the check for their
604# existance is superfluous
605rm -f /tmp/br.* >/dev/null 2>&1
606
607 exit `expr $engstatus + $guistatus`
608
609