<commit_before>// Filename: fmodAudioManager.cxx
// Created by:  cort (January 22, 2003)
// Prior system by: cary
// Rewrite [for new Version of FMOD-EX] by: <NAME> "Staque" - Spring 2006
//
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license.  You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://etc.cmu.edu/panda3d/docs/license/ .
//
// To contact the maintainers of this program write to
// <EMAIL> .
//
////////////////////////////////////////////////////////////////////

#include "pandabase.h"
#include "config_audio.h"
#include "dcast.h"

#ifdef HAVE_FMODEX //[

//Panda headers.
#include "config_audio.h"
#include "config_util.h"
#include "fmodAudioManager.h"
#include "fmodAudioSound.h"
#include "fmodAudioDSP.h"
//Needed so People use Panda's Generic UNIX Style Paths for Filename.
#include "filename.h"
#include "virtualFileSystem.h"

//FMOD Headers.
#include <fmod.hpp>
#include <fmod_errors.h>



TypeHandle FmodAudioManager::_type_handle;

FMOD::System *FmodAudioManager::_system; 
pset<FmodAudioManager *> FmodAudioManager::_all_managers;

bool FmodAudioManager::_system_is_valid = false;


//  This sets the distance factor for 3D audio to use feet. 
//  FMOD uses meters by default.
//  Since Panda use feet we need to compensate for that with a factor of 3.28
//
//  This can be overwritten.  You just need to call
//  audio_3d_set_distance_factor(float factor) and set your new factor.

float FmodAudioManager::_doppler_factor = 1;
float FmodAudioManager::_distance_factor = 3.28;
float FmodAudioManager::_drop_off_factor = 1;


////////////////////////////////////////////////////////////////////
// Central dispatcher for audio errors.
////////////////////////////////////////////////////////////////////

void fmod_audio_errcheck(const char *context, FMOD_RESULT result) {
  if (result != 0) {
    audio_error(context << ": " << FMOD_ErrorString(result) );
  }
}

////////////////////////////////////////////////////////////////////
//     Function: Create_AudioManager
//       Access: Private
//  Description: Factory Function
////////////////////////////////////////////////////////////////////
PT(AudioManager) Create_AudioManager() {
  return new FmodAudioManager;
}


////////////////////////////////////////////////////////////////////
//     Function: FmodAudioManager::FmodAudioManager()
//       Access: Public
//  Description: Constructor
////////////////////////////////////////////////////////////////////
FmodAudioManager::
FmodAudioManager() {
  FMOD_RESULT result;

  //We need a varible temporary to check the FMOD Version.
  unsigned int      version;

  _all_managers.insert(this);
  
  //Init 3D attributes
  _position.x = 0;
  _position.y = 0;
  _position.z = 0;

  _velocity.x = 0;
  _velocity.y = 0;
  _velocity.z = 0;

  _forward.x = 0;
  _forward.y = 0;
  _forward.z = 0;

  _up.x = 0;
  _up.y = 0;
  _up.z = 0;

  if (_system == (FMOD::System *)NULL) {
    // Create the global FMOD System object.  This one object must be
    // shared by all FmodAudioManagers (this is particularly true on
    // OSX, but the FMOD documentation is unclear as to whether this
    // is the intended design on all systems).

    result = FMOD::System_Create(&_system);
    fmod_audio_errcheck("FMOD::System_Create()", result);

    //  Let check the Version of FMOD to make sure the Headers and Libraries are correct.
    result = _system->getVersion(&version);
    fmod_audio_errcheck("_system->getVersion()", result);
  
    if (version < FMOD_VERSION){
      audio_error("You are using an old version of FMOD.  This program requires:" << FMOD_VERSION);
    }

    //Stick Surround Sound 5.1 thing Here.
    if (fmod_use_surround_sound) {
      audio_debug("Setting FMOD to use 5.1 Surround Sound.");
      result = _system->setSpeakerMode( FMOD_SPEAKERMODE_5POINT1 );
      fmod_audio_errcheck("_system->setSpeakerMode()", result);
    }

    //Now we Initialize the System.
    result = _system->init(fmod_number_of_sound_channels, FMOD_INIT_NORMAL, 0);
    fmod_audio_errcheck("_system->init()", result);

    _system_is_valid = (result == FMOD_OK);

    if (_system_is_valid) {
      result = _system->set3DSettings( _doppler_factor, _distance_factor, _drop_off_factor);
      fmod_audio_errcheck("_system->set3DSettings()", result);

      result = _system->setFileSystem(open_callback, close_callback, read_callback, seek_callback, -1);
      fmod_audio_errcheck("_system->setFileSystem()", result);
    }
  }

  _is_valid = _system_is_valid;

  memset(&_midi_info, 0, sizeof(_midi_info));
  _midi_info.cbsize = sizeof(_midi_info);

  Filename dls_pathname = get_dls_pathname();
  if (!dls_pathname.empty()) {
    _dlsname = dls_pathname.to_os_specific();
    _midi_info.dlsname = _dlsname.c_str();
  }
}

////////////////////////////////////////////////////////////////////
//     Function: FmodAudioManager::~FmodAudioManager
//       Access: Public
//  Description: DESTRCUTOR !!!
////////////////////////////////////////////////////////////////////
FmodAudioManager::
~FmodAudioManager() {
  // Be sure to delete associated sounds before deleting the manager!
  FMOD_RESULT result;

  //Release DSPs First
  _system_dsp.clear();

  //Release Sounds Next
  _all_sounds.clear();

  // Remove me from the managers list.
  _all_managers.erase(this);

  if (_all_managers.empty()) {<commit_msg>hack to protect FMOD from gs_instruments.dls<commit_after>// Filename: fmodAudioManager.cxx
// Created by:  cort (January 22, 2003)
// Prior system by: cary
// Rewrite [for new Version of FMOD-EX] by: <NAME> "Staque" - Spring 2006
//
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license.  You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://etc.cmu.edu/panda3d/docs/license/ .
//
// To contact the maintainers of this program write to
// <EMAIL> .
//
////////////////////////////////////////////////////////////////////

#include "pandabase.h"
#include "config_audio.h"
#include "dcast.h"

#ifdef HAVE_FMODEX //[

//Panda headers.
#include "config_audio.h"
#include "config_util.h"
#include "fmodAudioManager.h"
#include "fmodAudioSound.h"
#include "fmodAudioDSP.h"
//Needed so People use Panda's Generic UNIX Style Paths for Filename.
#include "filename.h"
#include "virtualFileSystem.h"

//FMOD Headers.
#include <fmod.hpp>
#include <fmod_errors.h>



TypeHandle FmodAudioManager::_type_handle;

FMOD::System *FmodAudioManager::_system; 
pset<FmodAudioManager *> FmodAudioManager::_all_managers;

bool FmodAudioManager::_system_is_valid = false;


//  This sets the distance factor for 3D audio to use feet. 
//  FMOD uses meters by default.
//  Since Panda use feet we need to compensate for that with a factor of 3.28
//
//  This can be overwritten.  You just need to call
//  audio_3d_set_distance_factor(float factor) and set your new factor.

float FmodAudioManager::_doppler_factor = 1;
float FmodAudioManager::_distance_factor = 3.28;
float FmodAudioManager::_drop_off_factor = 1;


////////////////////////////////////////////////////////////////////
// Central dispatcher for audio errors.
////////////////////////////////////////////////////////////////////

void fmod_audio_errcheck(const char *context, FMOD_RESULT result) {
  if (result != 0) {
    audio_error(context << ": " << FMOD_ErrorString(result) );
  }
}

////////////////////////////////////////////////////////////////////
//     Function: Create_AudioManager
//       Access: Private
//  Description: Factory Function
////////////////////////////////////////////////////////////////////
PT(AudioManager) Create_AudioManager() {
  return new FmodAudioManager;
}


////////////////////////////////////////////////////////////////////
//     Function: FmodAudioManager::FmodAudioManager()
//       Access: Public
//  Description: Constructor
////////////////////////////////////////////////////////////////////
FmodAudioManager::
FmodAudioManager() {
  FMOD_RESULT result;

  //We need a varible temporary to check the FMOD Version.
  unsigned int      version;

  _all_managers.insert(this);
  
  //Init 3D attributes
  _position.x = 0;
  _position.y = 0;
  _position.z = 0;

  _velocity.x = 0;
  _velocity.y = 0;
  _velocity.z = 0;

  _forward.x = 0;
  _forward.y = 0;
  _forward.z = 0;

  _up.x = 0;
  _up.y = 0;
  _up.z = 0;

  if (_system == (FMOD::System *)NULL) {
    // Create the global FMOD System object.  This one object must be
    // shared by all FmodAudioManagers (this is particularly true on
    // OSX, but the FMOD documentation is unclear as to whether this
    // is the intended design on all systems).

    result = FMOD::System_Create(&_system);
    fmod_audio_errcheck("FMOD::System_Create()", result);

    //  Let check the Version of FMOD to make sure the Headers and Libraries are correct.
    result = _system->getVersion(&version);
    fmod_audio_errcheck("_system->getVersion()", result);
  
    if (version < FMOD_VERSION){
      audio_error("You are using an old version of FMOD.  This program requires:" << FMOD_VERSION);
    }

    //Stick Surround Sound 5.1 thing Here.
    if (fmod_use_surround_sound) {
      audio_debug("Setting FMOD to use 5.1 Surround Sound.");
      result = _system->setSpeakerMode( FMOD_SPEAKERMODE_5POINT1 );
      fmod_audio_errcheck("_system->setSpeakerMode()", result);
    }

    //Now we Initialize the System.
    result = _system->init(fmod_number_of_sound_channels, FMOD_INIT_NORMAL, 0);
    fmod_audio_errcheck("_system->init()", result);

    _system_is_valid = (result == FMOD_OK);

    if (_system_is_valid) {
      result = _system->set3DSettings( _doppler_factor, _distance_factor, _drop_off_factor);
      fmod_audio_errcheck("_system->set3DSettings()", result);

      result = _system->setFileSystem(open_callback, close_callback, read_callback, seek_callback, -1);
      fmod_audio_errcheck("_system->setFileSystem()", result);
    }
  }

  _is_valid = _system_is_valid;

  memset(&_midi_info, 0, sizeof(_midi_info));
  _midi_info.cbsize = sizeof(_midi_info);

  Filename dls_pathname = get_dls_pathname();

#ifdef IS_OSX
  // Here's a big kludge.  Don't ever let FMOD try to load this
  // OSX-provided file; it crashes messily if you do.
  if (dls_pathname == "/System/Library/Components/CoreAudio.component/Contents/Resources/gs_instruments.dls") {
    dls_pathname = "";
  }
#endif  // IS_OSX

  if (!dls_pathname.empty()) {
    _dlsname = dls_pathname.to_os_specific();
    _midi_info.dlsname = _dlsname.c_str();
  }
}

////////////////////////////////////////////////////////////////////
//     Function: FmodAudioManager::~FmodAudioManager
//       Access: Public
//  Description: DESTRCUTOR !!!
////////////////////////////////////////////////////////////////////
FmodAudioManager::
~FmodAudioManager() {
  // Be sure to delete associated sounds before deleting the manager!
  FMOD_RESULT result;

  //Release DSPs First
  _system_dsp.clear();

  //Release Sounds Next
  _all_sounds.clear();

  // Remove me from the managers list.
  _all_managers.erase(this);

  if (_all_managers.empty()) {
{'fraction_non_alphanumeric': 0.21742881794650562, 'fraction_numerical': 0.009663503019844694, 'mean_word_length': 2.245869504340521, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2967707', 'n_tokens_mistral': 3600, 'n_tokens_neox': 3448, 'n_words': 949}