"Fossies" - the Fresh Open Source Software Archive

Member "stella-6.0.2/src/common/AudioSettings.cxx" (11 Oct 2019, 9691 Bytes) of package /linux/privat/stella-6.0.2-src.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AudioSettings.cxx" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 6.0.1_vs_6.0.2.

    1 //============================================================================
    2 //
    3 //   SSSS    tt          lll  lll
    4 //  SS  SS   tt           ll   ll
    5 //  SS     tttttt  eeee   ll   ll   aaaa
    6 //   SSSS    tt   ee  ee  ll   ll      aa
    7 //      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator"
    8 //  SS  SS   tt   ee      ll   ll  aa  aa
    9 //   SSSS     ttt  eeeee llll llll  aaaaa
   10 //
   11 // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
   12 // and the Stella Team
   13 //
   14 // See the file "License.txt" for information on usage and redistribution of
   15 // this file, and for a DISCLAIMER OF ALL WARRANTIES.
   16 //============================================================================
   17 
   18 #include "AudioSettings.hxx"
   19 #include "Settings.hxx"
   20 
   21 namespace {
   22   uInt32 lboundInt(int x, int defaultValue)
   23   {
   24     return x <= 0 ? defaultValue : x;
   25   }
   26 
   27   AudioSettings::Preset normalizedPreset(int numericPreset)
   28   {
   29     return (
   30       numericPreset >= static_cast<int>(AudioSettings::Preset::custom) &&
   31       numericPreset <= static_cast<int>(AudioSettings::Preset::veryHighQualityVeryLowLag)
   32     ) ? static_cast<AudioSettings::Preset>(numericPreset) : AudioSettings::DEFAULT_PRESET;
   33   }
   34 
   35   AudioSettings::ResamplingQuality normalizeResamplingQuality(int numericResamplingQuality)
   36   {
   37     return (
   38       numericResamplingQuality >= static_cast<int>(AudioSettings::ResamplingQuality::nearestNeightbour) &&
   39       numericResamplingQuality <= static_cast<int>(AudioSettings::ResamplingQuality::lanczos_3)
   40     ) ? static_cast<AudioSettings::ResamplingQuality>(numericResamplingQuality) : AudioSettings::DEFAULT_RESAMPLING_QUALITY;
   41   }
   42 }
   43 
   44 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   45 AudioSettings::AudioSettings(Settings& settings)
   46   : mySettings(settings),
   47     myIsPersistent(true)
   48 {
   49   setPreset(normalizedPreset(mySettings.getInt(SETTING_PRESET)));
   50 }
   51 
   52 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   53 void AudioSettings::normalize(Settings& settings)
   54 {
   55   int settingPreset = settings.getInt(SETTING_PRESET);
   56   Preset preset = normalizedPreset(settingPreset);
   57   if (static_cast<int>(preset) != settingPreset) settings.setValue(SETTING_PRESET, static_cast<int>(DEFAULT_PRESET));
   58 
   59   switch (settings.getInt(SETTING_SAMPLE_RATE)) {
   60     case 44100:
   61     case 48000:
   62     case 96000:
   63       break;
   64 
   65     default:
   66       settings.setValue(SETTING_SAMPLE_RATE, DEFAULT_SAMPLE_RATE);
   67       break;
   68   }
   69 
   70   switch (settings.getInt(SETTING_FRAGMENT_SIZE)) {
   71     case 128:
   72     case 256:
   73     case 512:
   74     case 1024:
   75     case 2048:
   76     case 4096:
   77       break;
   78 
   79     default:
   80       settings.setValue(SETTING_FRAGMENT_SIZE, DEFAULT_FRAGMENT_SIZE);
   81       break;
   82   }
   83 
   84   int settingBufferSize = settings.getInt(SETTING_BUFFER_SIZE);
   85   if (settingBufferSize < 0 || settingBufferSize > MAX_BUFFER_SIZE) settings.setValue(SETTING_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
   86 
   87   int settingHeadroom = settings.getInt(SETTING_HEADROOM);
   88   if (settingHeadroom < 0 || settingHeadroom > MAX_HEADROOM) settings.setValue(SETTING_HEADROOM, DEFAULT_HEADROOM);
   89 
   90   int settingResamplingQuality = settings.getInt(SETTING_RESAMPLING_QUALITY);
   91   ResamplingQuality resamplingQuality = normalizeResamplingQuality(settingResamplingQuality);
   92   if (static_cast<int>(resamplingQuality) != settingResamplingQuality)
   93     settings.setValue(SETTING_RESAMPLING_QUALITY, static_cast<int>(DEFAULT_RESAMPLING_QUALITY));
   94 
   95   int settingVolume = settings.getInt(SETTING_VOLUME);
   96   if (settingVolume < 0 || settingVolume > 100) settings.setValue(SETTING_VOLUME, DEFAULT_VOLUME);
   97 }
   98 
   99 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  100 AudioSettings::Preset AudioSettings::preset()
  101 {
  102   updatePresetFromSettings();
  103   return myPreset;
  104 }
  105 
  106 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  107 uInt32 AudioSettings::sampleRate()
  108 {
  109   updatePresetFromSettings();
  110   return customSettings() ? lboundInt(mySettings.getInt(SETTING_SAMPLE_RATE), DEFAULT_SAMPLE_RATE) : myPresetSampleRate;
  111 }
  112 
  113 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  114 uInt32 AudioSettings::fragmentSize()
  115 {
  116   updatePresetFromSettings();
  117   return customSettings() ? lboundInt(mySettings.getInt(SETTING_FRAGMENT_SIZE), DEFAULT_FRAGMENT_SIZE) : myPresetFragmentSize;
  118 }
  119 
  120 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  121 uInt32 AudioSettings::bufferSize()
  122 {
  123   updatePresetFromSettings();
  124   // 0 is a valid value -> keep it
  125   return customSettings() ? lboundInt(mySettings.getInt(SETTING_BUFFER_SIZE), 0) : myPresetBufferSize;
  126 }
  127 
  128 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  129 uInt32 AudioSettings::headroom()
  130 {
  131   updatePresetFromSettings();
  132   // 0 is a valid value -> keep it
  133   return customSettings() ? lboundInt(mySettings.getInt(SETTING_HEADROOM), 0) : myPresetHeadroom;
  134 }
  135 
  136 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  137 AudioSettings::ResamplingQuality AudioSettings::resamplingQuality()
  138 {
  139   updatePresetFromSettings();
  140   return customSettings() ? normalizeResamplingQuality(mySettings.getInt(SETTING_RESAMPLING_QUALITY)) : myPresetResamplingQuality;
  141 }
  142 
  143 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  144 bool AudioSettings::stereo() const
  145 {
  146   // 0 is a valid value -> keep it
  147   return mySettings.getBool(SETTING_STEREO);
  148 }
  149 
  150 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  151 uInt32 AudioSettings::volume() const
  152 {
  153   // 0 is a valid value -> keep it
  154   return lboundInt(mySettings.getInt(SETTING_VOLUME), 0);
  155 }
  156 
  157 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  158 bool AudioSettings::enabled() const
  159 {
  160   return mySettings.getBool(SETTING_ENABLED);
  161 }
  162 
  163 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  164 void AudioSettings::setPreset(AudioSettings::Preset preset)
  165 {
  166   if (preset == myPreset) return;
  167   myPreset = preset;
  168 
  169   switch (myPreset) {
  170     case Preset::custom:
  171       break;
  172 
  173     case Preset::lowQualityMediumLag:
  174       myPresetSampleRate = 44100;
  175       myPresetFragmentSize = 1024;
  176       myPresetBufferSize = 6;
  177       myPresetHeadroom = 5;
  178       myPresetResamplingQuality = ResamplingQuality::nearestNeightbour;
  179       break;
  180 
  181     case Preset::highQualityMediumLag:
  182       myPresetSampleRate = 44100;
  183       myPresetFragmentSize = 1024;
  184       myPresetBufferSize = 6;
  185       myPresetHeadroom = 5;
  186       myPresetResamplingQuality = ResamplingQuality::lanczos_2;
  187       break;
  188 
  189     case Preset::highQualityLowLag:
  190       myPresetSampleRate = 48000;
  191       myPresetFragmentSize = 512;
  192       myPresetBufferSize = 3;
  193       myPresetHeadroom = 2;
  194       myPresetResamplingQuality = ResamplingQuality::lanczos_2;
  195       break;
  196 
  197     case Preset::veryHighQualityVeryLowLag:
  198       myPresetSampleRate = 96000;
  199       myPresetFragmentSize = 128;
  200       myPresetBufferSize = 0;
  201       myPresetHeadroom = 0;
  202       myPresetResamplingQuality = ResamplingQuality::lanczos_3;
  203       break;
  204 
  205     default:
  206       throw runtime_error("invalid preset");
  207   }
  208 
  209   if (myIsPersistent) mySettings.setValue(SETTING_PRESET, static_cast<int>(myPreset));
  210 }
  211 
  212 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  213 void AudioSettings::setSampleRate(uInt32 sampleRate)
  214 {
  215   if (!myIsPersistent) return;
  216 
  217   mySettings.setValue(SETTING_SAMPLE_RATE, sampleRate);
  218   normalize(mySettings);
  219 }
  220 
  221 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  222 void AudioSettings::setFragmentSize(uInt32 fragmentSize)
  223 {
  224   if (!myIsPersistent) return;
  225 
  226   mySettings.setValue(SETTING_FRAGMENT_SIZE, fragmentSize);
  227   normalize(mySettings);
  228 }
  229 
  230 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  231 void AudioSettings::setBufferSize(uInt32 bufferSize)
  232 {
  233   if (!myIsPersistent) return;
  234 
  235   mySettings.setValue(SETTING_BUFFER_SIZE, bufferSize);
  236   normalize(mySettings);
  237 }
  238 
  239 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  240 void AudioSettings::setHeadroom(uInt32 headroom)
  241 {
  242   if (!myIsPersistent) return;
  243 
  244   mySettings.setValue(SETTING_HEADROOM, headroom);
  245   normalize(mySettings);
  246 }
  247 
  248 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  249 void AudioSettings::setResamplingQuality(AudioSettings::ResamplingQuality resamplingQuality)
  250 {
  251   if (!myIsPersistent) return;
  252 
  253   mySettings.setValue(SETTING_RESAMPLING_QUALITY, static_cast<int>(resamplingQuality));
  254   normalize(mySettings);
  255 }
  256 
  257 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  258 void AudioSettings::setStereo(bool allROMs)
  259 {
  260   if(!myIsPersistent) return;
  261 
  262   mySettings.setValue(SETTING_STEREO, allROMs);
  263 }
  264 
  265 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  266 void AudioSettings::setVolume(uInt32 volume)
  267 {
  268   if (!myIsPersistent) return;
  269 
  270   mySettings.setValue(SETTING_VOLUME, volume);
  271   normalize(mySettings);
  272 }
  273 
  274 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  275 void AudioSettings::setEnabled(bool isEnabled)
  276 {
  277   if (!myIsPersistent) return;
  278 
  279   mySettings.setValue(SETTING_ENABLED, isEnabled);
  280 }
  281 
  282 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  283 void AudioSettings::setPersistent(bool isPersistent)
  284 {
  285   myIsPersistent = isPersistent;
  286 }
  287 
  288 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  289 bool AudioSettings::customSettings() const
  290 {
  291   return myPreset == Preset::custom;
  292 }
  293 
  294 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  295 void AudioSettings::updatePresetFromSettings()
  296 {
  297   if (!myIsPersistent) return;
  298 
  299   setPreset(normalizedPreset(mySettings.getInt(SETTING_PRESET)));
  300 }