#!/bin/bash
#
#******************************************************************************
# startjack
#------------------------------------------------------------------------------
##
# \file        startjack
# \library     Home/Audio
# \author      Chris Ahlstrom
# \date        2020-12-08
# \update      2020-12-14
# \version     $Revision$
#
#   This script provides a way to make a fast JACK setup on Linux.  See:
#
#         https://wiki.archlinux.org/index.php/JACK_Audio_Connection_Kit
#
#   Requires the python-dbus package to be installed.  The HWPORT values
#   available are found in /proc/asound/cards:  PCH, HDMI, NVidia, CODEC, etc.
#   The "dps" rate can be 44100, 48000, and higher.  The "nperiods" value
#   should be 2 for motherboard, PCI, PCI-X audio, and 3 for USB audio.
#
#   However, we have had weird input issues, including missing input and
#   extremely slow response  with this setup, but not with qjackctrl running
#   with the macroed settings below.  Still experimenting.
#
#   With the current settings, latency = 21.3 msec.
#
#   Also note to be sure to set qjackctl to use the "seq" MIDI driver; the
#   "raw" driver seems to cause a lot of issues with the a2j bridge.
#
#------------------------------------------------------------------------------

HWPORT=PCH
JDRIVER=alsa
SAMPLERATE=48000
NPERIODS=4              # was 2
PFRAMES=256             # was 64
SLEEPTIME=10
START_YOSHIMI="no"
YOSHIMI_DIR="$HOME/.config/yoshimi"
YOSHIMI_GM="yoshimi-b4uacuse-gm.state"
YOSHIMI_STATE="$YOSHIMI_DIR/$YOSHIMI_GM"

# Not yet used:

REALTIME=yes
MDRIVER=seq             # raw can cause real issues with a2jmidid

if [ "$1" == "kill" ] ; then

    killall qjackctl
    a2j_control --stop
    a2j_control --exit
    jack_control exit

elif [ "$1" == "basic" ] ; then

    echo "Starting processes with 5-second wait for each..."
    echo "Starting QJackCtl/JACK: 'qjackctl --start'..."
    qjackctl --start &
    sleep 5
    echo "Starting ALSA-to-JACK bridge: 'a2jmidid --export-hw'..."
    a2jmidid --export-hw &

    if [ "$2" == "yoshimi" ] ; then
        START_YOSHIMI="yes"
        sleep 5
    fi

else

    jack_control start
    jack_control ds $JDRIVER
    jack_control dps device hw:$HWPORT
    jack_control dps rate $SAMPLERATE
    jack_control dps nperiods $NPERIODS
    jack_control dps period $PFRAMES
    echo "Waiting $SLEEPTIME seconds for jack_control to settle..."
    sleep $SLEEPTIME
    a2j_control --ehw
    a2j_control --start
    echo "Waiting $SLEEPTIME seconds for a2j_control to settle..."
    sleep $SLEEPTIME
    echo "Starting qjackctl..."
    qjackctl &

    if [ "$1" == "yoshimi" ] ; then
        START_YOSHIMI="yes"
        sleep 5
    fi

fi

if [ "$START_YOSHIMI" == "yes" ] ; then
    echo "Starting Yoshimi with JACK support and a GM state..."
    exec yoshimi -j -J --state=$YOSHIMI_STATE
fi

#------------------------------------------------------------------------------
# vim: ft=sh
#------------------------------------------------------------------------------
