void SoundSDL::initialize()

in atari_py/ale_interface/src/common/SoundSDL.cxx [75:173]


void SoundSDL::initialize()
{
  // Check whether to start the sound subsystem
  if(!myIsEnabled)
  {
    close();
    if(myOSystem->settings().getBool("showinfo"))
      cerr << "Sound disabled." << endl << endl;
    return;
  }

  // Make sure the sound queue is clear
  myRegWriteQueue.clear();
  myTIASound.reset();

  if(!((SDL_WasInit(SDL_INIT_AUDIO) & SDL_INIT_AUDIO) > 0))
  {
    myIsInitializedFlag = false;
    myIsMuted = false;
    myLastRegisterSetCycle = 0;

    if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
    {
      Logger::Warning << "WARNING: Couldn't initialize SDL audio system! " << endl;
      Logger::Warning << "         " << SDL_GetError() << endl;
      return;
    }
    else
    {
      uInt32 fragsize = myOSystem->settings().getInt("fragsize");
      Int32 frequency = myOSystem->settings().getInt("freq");
      Int32 tiafreq   = myOSystem->settings().getInt("tiafreq");

      SDL_AudioSpec desired;
      desired.freq   = frequency;
    #ifndef GP2X
      desired.format = AUDIO_U8;
    #else
      desired.format = AUDIO_U16;
    #endif
      desired.channels = myNumChannels;
      desired.samples  = fragsize;
      desired.callback = callback;
      desired.userdata = (void*)this;

      if(SDL_OpenAudio(&desired, &myHardwareSpec) < 0)
      {
        Logger::Warning << "WARNING: Couldn't open SDL audio system! " << endl;
        Logger::Warning << "         " << SDL_GetError() << endl;
        return;
      }

      // Make sure the sample buffer isn't to big (if it is the sound code
      // will not work so we'll need to disable the audio support)
      if(((float)myHardwareSpec.samples / (float)myHardwareSpec.freq) >= 0.25)
      {
        Logger::Warning << "WARNING: Sound device doesn't support realtime audio! Make ";
        Logger::Warning << "sure a sound" << endl;
        Logger::Warning << "         server isn't running.  Audio is disabled." << endl;

        SDL_CloseAudio();
        return;
      }

      myIsInitializedFlag = true;
      myIsMuted = false;
      myFragmentSizeLogBase2 = log((double)myHardwareSpec.samples) / log(2.0);

      // Now initialize the TIASound object which will actually generate sound
      myTIASound.outputFrequency(myHardwareSpec.freq);
      myTIASound.tiaFrequency(tiafreq);
      myTIASound.channels(myHardwareSpec.channels);

      bool clipvol = myOSystem->settings().getBool("clipvol");
      myTIASound.clipVolume(clipvol);

      // Adjust volume to that defined in settings
      myVolume = myOSystem->settings().getInt("volume");
      setVolume(myVolume);

      // Show some info
      if(myOSystem->settings().getBool("showinfo"))
        cerr << "Sound enabled:"  << endl
             << "  Volume     : " << myVolume << endl
             << "  Frag size  : " << fragsize << endl
             << "  Frequency  : " << myHardwareSpec.freq << endl
             << "  Format     : " << myHardwareSpec.format << endl
             << "  TIA Freq.  : " << tiafreq << endl
             << "  Channels   : " << myNumChannels << endl
             << "  Clip volume: " << (int)clipvol << endl << endl;
    }
  }

  // And start the SDL sound subsystem ...
  if(myIsInitializedFlag)
  {
    SDL_PauseAudio(0);
  }
}