#!/bin/bash

# record sound output to mp3 file

# requires: pulseaudio-utils, id3v2, sox

# argument: filename

# if no argument is given, output file is called record.wav and artist is set to username

# feel free to edit parameters if needed

# - - - - - - - - - - - - - - -

OUTPUT_FILENAME="record"

ALSA_DEVICE=`pactl list | grep monitor | grep Name  | awk '{print $2}'`
#ALSA_DEVICE="alsa_output.pci-0000_00_1b.0.analog-stereo.monitor"
CHANNELS=2
SAMPLING_RATE=44100
QUANTIZATION=16

# - - - - - - - - - - - - - - -

if [ $# -gt 0 ]
then
	OUTPUT_FILENAME=$1
fi

pacat --record -d $ALSA_DEVICE | sox -t raw -r $SAMPLING_RATE -e signed-integer -L -b $QUANTIZATION -c $CHANNELS - temp.wav &

read -p "Recording, press enter to stop" 

pkill pacat

echo "Normalizing..."

sox --norm temp.wav "$OUTPUT_FILENAME".wav silence 1 0 0%
rm temp.wav

echo "Saved on $OUTPUT_FILENAME.wav"

