/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+     Internet Radio Automation & Encoding Toolkit
#+
#+     Copyright (C) 2018, 2019, 2020, 2021 by Kevin C. O'Kane
#+
#+     Kevin C. O'Kane
#+     kc.okane@gmail.com
#+     https://www.cs.uni.edu/~okane
#+     https://threadsafebooks.com/
#+
#+ This program is free software; you can redistribute it and/or modify
#+ it under the terms of the GNU General Public License as published by
#+ the Free Software Foundation; either version 2 of the License, or
#+ (at your option) any later version.
#+
#+ This program is distributed in the hope that it will be useful,
#+ but WITHOUT ANY WARRANTY; without even the implied warranty of
#+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#+ GNU General Public License for more details.
#+
#+ You should have received a copy of the GNU General Public License
#+ along with this program; if not, write to the Free Software
#+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#+
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

//	Sept 8, 2021

#include "includes.h"
#include "common-externs.h"

#define SLASH '/'

void	agc_adjust() {

//-------------------------------
//      AGC 1 button is checked
//-------------------------------

	if (AGC) {

//----------------------------------------------------------------
//              Don't alter  with volume when mic is on or paused
//----------------------------------------------------------------

		if (! micmute &&
		        ! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pauseVideo)) ) {

//----------------------------------------------------------------
//                      If file not playing, reset preamp volume
//----------------------------------------------------------------

			if ( !(vlc_playing()) ) {

//------------------------------------------------------------------------------
//                              MaxAudioSample == 1 means we have clipped.
//                              MaxAudioSample == 0 means we have not clipped.
//                              An excessive volume has not been found yet.
//------------------------------------------------------------------------------

				MaxAudioSample = 0;
				if (AGC)  {
					clip_changed = 0;       // prevent multiple adjustments
					}

				if (SP->volumeLevel > INIT_VOLUME && AGC) { // reset only if AGC
					SP->volumeLevel = INIT_VOLUME;
					outVolume(INIT_VOLUME);
					gtk_adjustment_set_value (GTK_ADJUSTMENT(adjustment10),
					                          SP->volumeLevel - 100);
					}
				}

//----------------------------------------------
//                      A file is playing.
//----------------------------------------------

			else {

//-----------------------------------------------
//                              If not muted
//-----------------------------------------------

				if (! SP->auto_muted) {

					if (L_Clipping || R_Clipping) { // greater than clipping threshold ClipLevel


						SP->volumeLevel = SP->volumeLevel - RAMP_SPEED_DOWN; // reduce level

						gtk_adjustment_set_value (GTK_ADJUSTMENT(adjustment10),
						                          SP->volumeLevel - 100);
						outVolume(SP->volumeLevel);
						MaxAudioSample = 1; // max for this tune

						}

					else if ( (!MaxAudioSample || AGC1) && SP->volumeLevel < Topgain) {

						if (curLevel < 0.8 * ClipLevel) { // more than 20% below max

							SP->volumeLevel = SP->volumeLevel + RAMP_SPEED_UP; // raise level

							if (SP->volumeLevel > Topgain) SP->volumeLevel = Topgain;
							outVolume(SP->volumeLevel);
							gtk_adjustment_set_value (GTK_ADJUSTMENT(adjustment10),
							                          SP->volumeLevel - 100);

							if (AGC1) {
								MaxAudioSample = 0; // not at peak
								GdkColor color; // default background color
								color.red   = 0x0000;
								color.green = 0xffff;
								color.blue  = 0x0000;
								gtk_widget_modify_bg(agc_gain, GTK_STATE_NORMAL, &color);
								gtk_widget_modify_bg(agcLbl2, GTK_STATE_NORMAL, &color);
								}

							}
						}

					else if (SP->volumeLevel > Topgain) { // enforce max level if AGC

						SP->volumeLevel = Topgain;
						// printf("volume reset %d\n", SP->volumeLevel);
						outVolume(SP->volumeLevel);
						gtk_adjustment_set_value (GTK_ADJUSTMENT(adjustment10), SP->volumeLevel - 100);

						}
					}
				}
			}
		}

	}
