rem: state.ScaleModeSelected = max(state.ScaleModeSelected, 0); state.ScaleModeSelected = min(state.ScaleModeSelected, AHI_SCALE_FIXED_6_DB);
context: void NewMode(int selectedmode) {  struct UnitNode *unit = NULL;  struct ModeNode *mode = NULL;  ULONG id = AHI_INVALID_ID;  Fixed MinOutVol = 0, MaxOutVol = 0, MinMonVol = 0, MaxMonVol = 0;  Fixed MinGain = 0, MaxGain = 0;  double Min, Max, Current;  int offset;  state.ModeSelected = selectedmode;  unit = (struct UnitNode *) GetNode(state.UnitSelected, UnitList);  if( selectedmode != ~0 )  {    mode = (struct ModeNode *) GetNode(selectedmode, ModeList);  }  if( mode != NULL )  {    id = mode->ID;        AHI_GetAudioAttrs(id, NULL,		      AHIDB_IndexArg,         unit->prefs.ahiup_Frequency,		      AHIDB_Index,            (ULONG) &state.FreqSelected,		      AHIDB_Frequencies,      (ULONG) &state.Frequencies,		      AHIDB_MaxChannels,      (ULONG) &state.Channels,		      AHIDB_Inputs,           (ULONG) &state.Inputs,		      AHIDB_Outputs,          (ULONG) &state.Outputs,		      AHIDB_MinOutputVolume,  (ULONG) &MinOutVol,		      AHIDB_MaxOutputVolume,  (ULONG) &MaxOutVol,		      AHIDB_MinMonitorVolume, (ULONG) &MinMonVol,		      AHIDB_MaxMonitorVolume, (ULONG) &MaxMonVol,		      AHIDB_MinInputGain,     (ULONG) &MinGain,		      AHIDB_MaxInputGain,     (ULONG) &MaxGain,		      AHIDB_BufferLen,        128,		      AHIDB_Author,           (ULONG) authorBuffer,		      AHIDB_Copyright,        (ULONG) copyrightBuffer,		      AHIDB_Driver,           (ULONG) driverBuffer,		      AHIDB_Version,          (ULONG) versionBuffer,		      AHIDB_Annotation,       (ULONG) annotationBuffer,		      TAG_DONE);  }  state.ChannelsSelected = unit->prefs.ahiup_Channels;  state.ScaleModeSelected = unit->prefs.ahiup_ScaleMode;  state.InputSelected    = unit->prefs.ahiup_Input;  state.OutputSelected   = unit->prefs.ahiup_Output;  // Limit channels  state.Channels = min(state.Channels, 32);  if(unit->prefs.ahiup_Unit == AHI_NO_UNIT) {    state.ChannelsDisabled = TRUE;  }  else {    state.ChannelsDisabled = FALSE;  }  if(MinOutVol == 0) {    MinOutVol = 1;    state.OutVolMute = TRUE;    state.OutVols    = 1;  }  else {    state.OutVolMute = FALSE;    state.OutVols    = 0;  }  if(MinMonVol == 0) {    MinMonVol = 1;    state.MonVolMute = TRUE;    state.MonVols    = 1;  }  else {    state.MonVolMute = FALSE;    state.MonVols    = 0;  }  if(MinGain == 0) {    MinGain = 1;    state.GainMute = TRUE;    state.Gains    = 1;  }  else {    state.GainMute = FALSE;    state.Gains    = 0;  }  if(MaxOutVol == 0) {    state.OutVolSelected = 0;    state.OutVolOffset   = 0;  }  else {    Current = 20 * log10( unit->prefs.ahiup_OutputVolume / 65536.0 );    Min = floor(20 * log10( MinOutVol / 65536.0 ) / DBSTEP + 0.5) * DBSTEP;    Max = floor(20 * log10( MaxOutVol / 65536.0 ) / DBSTEP + 0.5) * DBSTEP;    state.OutVolSelected = (Current - Min) / DBSTEP + 0.5 + state.OutVols;    state.OutVols += ((Max - Min) / DBSTEP) + 1;    state.OutVolOffset = Min;  }  if(MaxMonVol == 0) {    state.MonVolSelected = 0;    state.MonVolOffset   = 0;  }  else {    Current = 20 * log10( unit->prefs.ahiup_MonitorVolume / 65536.0 );    Min = floor(20 * log10( MinMonVol / 65536.0 ) / DBSTEP + 0.5) * DBSTEP;    Max = floor(20 * log10( MaxMonVol / 65536.0 ) / DBSTEP + 0.5) * DBSTEP;    state.MonVolSelected = (Current - Min) / DBSTEP + 0.5 + state.MonVols;    state.MonVols += ((Max - Min) / DBSTEP) + 1;    state.MonVolOffset = Min;  }  if(MaxGain == 0) {    state.GainSelected = 0;    state.GainOffset   = 0;  }  else {    Current = 20 * log10( unit->prefs.ahiup_InputGain / 65536.0 );    Min = floor(20 * log10( MinGain / 65536.0 ) / DBSTEP + 0.5) * DBSTEP;    Max = floor(20 * log10( MaxGain / 65536.0 ) / DBSTEP + 0.5) * DBSTEP;    state.GainSelected = (Current - Min) / DBSTEP + 0.5 + state.Gains;    state.Gains += ((Max - Min) / DBSTEP) + 1;    state.GainOffset = Min;  }  // Make sure everything is within bounds!  state.FreqSelected = max(state.FreqSelected, 0);    state.FreqSelected = min(state.FreqSelected, state.Frequencies);  state.ChannelsSelected = max(state.ChannelsSelected, 1);  state.ChannelsSelected = min(state.ChannelsSelected, state.Channels);    state.ScaleModeSelected = max(state.ScaleModeSelected, 0);  state.ScaleModeSelected = min(state.ScaleModeSelected, AHI_SCALE_FIXED_6_DB);    state.OutVolSelected = max(state.OutVolSelected, 0);  state.OutVolSelected = min(state.OutVolSelected, state.OutVols);    state.MonVolSelected = max(state.MonVolSelected, 0);  state.MonVolSelected = min(state.MonVolSelected, state.MonVols);    state.GainSelected = max(state.GainSelected, 0);  state.GainSelected = min(state.GainSelected, state.Gains);  state.InputSelected = max(state.InputSelected, 0);  state.InputSelected = min(state.InputSelected, state.Inputs);  state.OutputSelected = max(state.OutputSelected, 0);  state.OutputSelected = min(state.OutputSelected, state.Outputs);  // Remove any \r's or \n's from version string  offset = strlen(versionBuffer);  while((offset > 0) &&        ((versionBuffer[offset-1] == '\r') ||         (versionBuffer[offset-1] == '\n'))) {    versionBuffer[offset-1] = '\0';    offset--;  }  FreeVec(Inputs);  FreeVec(Outputs);  Inputs   = GetInputs(id);  Outputs  = GetOutputs(id);}