// Sound management
//
// Copyright (C) 2003-2019 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos 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 3 of the License, or
// (at your option) any later version.
//
// Vamos 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 Vamos. If not, see <http://www.gnu.org/licenses/>.
#ifndef _SOUNDS_H_
#define _SOUNDS_H_
#include "../media/Sample.hpp"
#include "../media/XML_Parser.hpp"
#include <memory>
#include <string>
namespace Vamos_World
{
class Souds_Reader;
class Sounds
{
friend class Sounds_Reader;
public:
Sounds(double volume);
// Set the overall volume level 0.0-1.0.
void set_volume(double volume);
// Read the sound definition file.
void read(std::string data_dir = "", std::string sounds_file = "");
using V3 = Vamos_Geometry::Three_Vector;
// The other sounds are adjusted by these functions.
void play_tire_squeal_sound(double slide, const V3& position);
void play_kerb_sound(double speed, const V3& position);
void play_grass_sound(double speed, const V3& position);
void play_gravel_sound(double speed, const V3& position);
void play_scrape_sound(double speed, const V3& position);
void play_wind_sound(double speed, const V3& position);
void play_hard_crash_sound(double speed, const V3& position);
void play_soft_crash_sound(double speed, const V3& position);
// Pause all sounds.
void pause();
private:
enum Sound_Type
{
TIRE_SQUEAL,
KERB,
GRASS,
GRAVEL,
SCRAPE,
WIND,
SOFT_CRASH,
HARD_CRASH,
NONE
};
void add_sample(std::string file, Sound_Type type, double volume, double pitch);
std::string m_data_dir;
std::string m_sounds_file;
using Sample_Ptr = std::unique_ptr<Vamos_Media::Sample>;
// The sound for tires slipping on a hard surface.
Sample_Ptr mp_tire_squeal_sound;
// The sound for driving on kerbs.
Sample_Ptr mp_kerb_sound;
// The sound for driving or sliding on grass.
Sample_Ptr mp_grass_sound;
// The sound for driving or sliding on gravel.
Sample_Ptr mp_gravel_sound;
// The sound for hard surfaces sliding.
Sample_Ptr mp_scrape_sound;
// Wind noise.
Sample_Ptr mp_wind_sound;
// The sound for a crash into a soft surface.
Sample_Ptr mp_soft_crash_sound;
// The sound for a crash into a hard surface.
Sample_Ptr mp_hard_crash_sound;
};
class Sounds_Reader : public Vamos_Media::XML_Parser
{
public:
Sounds_Reader(std::string file_name, Sounds* sounds);
// XML_Parser overrides
virtual void on_end_tag(const Vamos_Media::XML_Tag& tag) override;
virtual void on_data(std::string data_string) override;
Sounds* mp_sounds;
std::string m_file;
double m_pitch;
double m_volume;
int m_rate;
// The number of seconds of sound buffered for playing. Delay in responding to a
// change in a sound may be as long as this.
double m_buffer_duration;
};
} // namespace Vamos_World
#endif // not _SOUNDS_H_