#!/bin/sh

set -e
set -x

nice -20 ./midi_virtual_ticks.py &
TICKER_PID=$!

gst-launch-1.0 -e alsamidisrc ports=128:0 num-buffers=1000 ! fluiddec ! audioconvert ! audio/x-raw,channels=1 ! wavenc ! filesink location=alsamidisrc_before_sync_false.wav

gst-launch-1.0 -e alsamidisrc ports=128:0 num-buffers=1000 ! fluiddec ! audioconvert ! audio/x-raw,channels=1 ! wavenc ! filesink location=alsamidisrc_before_sync_true.wav sync=true


GST_PLUGINS_BASE_DIR=/home/ao2/WIP/gstreamer/gst-plugins-base

# repeat the tests with my local changes
if [ -d $GST_PLUGINS_BASE_DIR ];
then
  GST_PLUGIN_PATH="$GST_PLUGINS_BASE_DIR/build/ext/alsa/" \
    gst-launch-1.0 -e alsamidisrc ports=128:0 num-buffers=1000 ! fluiddec ! audioconvert ! audio/x-raw,channels=1 ! wavenc ! filesink location=alsamidisrc_after_sync_false.wav

  GST_PLUGIN_PATH="$GST_PLUGINS_BASE_DIR/build/ext/alsa/" \
    gst-launch-1.0 -e alsamidisrc ports=128:0 num-buffers=1000 ! fluiddec ! audioconvert ! audio/x-raw,channels=1 ! wavenc ! filesink location=alsamidisrc_after_sync_true.wav
fi

kill $TICKER_PID

# align output files by removing the silence at their beginning
find . -name "alsamidisrc_*.wav" -exec sh -c 'sox "{}" aligned_$(basename "{}") silence 1 0.1 1%i' \;
