Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 40b94179d1f801f3718388d8985b12ed575474ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
   client/soundext.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/


#include "audio/audio.h"
#include "audio/buffers.h"
#include "audio/sources.h"
#include "auxiliary/functions.h"
#include "core/gameinterface.h"
#include "core/entity.h"
#include "client/soundext.h"
#include "client/client.h"

#include <sstream>
#include <iomanip>

namespace client
{

SoundExt::SoundExt(core::Entity *entity) : core::Extension(core::Extension::Sound, entity)
{
	state_thusterloopbuffer = 0;

	state_impulseloopbuffer = 0;
	state_impulsestartbuffer = 0;
	state_impulsestopbuffer = 0;

	state_engineloopbuffer = 0;
	state_engineloopsource = 0;

	state_engineeventbuffer = 0;
	state_engineeventsource = 0;

	// load engine sound
	if (entity->type() == core::Entity::Controlable)
	{
		core::EntityControlable *entityco = static_cast<core::EntityControlable *>(entity);
		unsigned int enginesoundset = 0;
		unsigned int impulsesoundset = 0;
		
		if (entityco->model()) {
			enginesoundset = entityco->model()->enginesound();
			impulsesoundset = entityco->model()->impulsesound();
		}
		
			
		std::stringstream soundname;
		soundname << "engines/loop" << std::setfill('0') << std::setw(2) << enginesoundset;
		state_thusterloopbuffer = audio::Buffers::load(soundname.str());

		// load impulse sound
		// FIXME load impulse sound set
		state_impulseloopbuffer = audio::Buffers::load("engines/impulse_loop00");
		state_impulsestartbuffer = audio::Buffers::load("engines/impulse_start00");
		state_impulsestopbuffer = audio::Buffers::load("engines/impulse_stop00");

		state_engineloopsource = audio::Sources::get();
		state_engineeventsource = audio::Sources::get();
	}
}

SoundExt::~SoundExt()
{
	clear();
}

void SoundExt::clear()
{
	if (state_engineloopsource) {
		audio::Sources::remove(state_engineloopsource);
	}

	if (state_engineeventsource) {
		audio::Sources::remove(state_engineeventsource);
	}

	state_thusterloopbuffer = 0;
	state_impulseloopbuffer = 0;
	state_impulsestartbuffer = 0;
	state_impulsestopbuffer = 0;

	state_engineloopbuffer = 0;
	state_engineloopsource = 0;

	state_engineeventbuffer = 0;
	state_engineeventsource = 0;
}

void SoundExt::frame(float elapsed)
{
	if (entity()->type() == core::Entity::Controlable)
	{
		core::EntityControlable *entity = static_cast<core::EntityControlable *>(this->entity());
	
		float speed = entity->speed();
		float pitch = 1.0f;
		float gain = 0.0;
		float r = ( entity->model() ? entity->model()->maxbbox().x() : entity->radius());

		if (entity->state() == core::Entity::Impulse) {
			pitch = 1.0f;
			gain = 1.0f;
		} else if (entity->thrust() > 0 ) {
			pitch = 0.2f + entity->thrust() * 0.8f;
			gain = 0.8f;
		}	
	
		if (entity->state() == core::Entity::ImpulseInitiate ) {
	
			if (state_engineeventbuffer != state_impulsestartbuffer) {
				audio::update_source(state_engineeventsource,
					entity->location() - entity->axis().forward() * r ,
					entity->axis().forward() * speed);
				
				state_engineeventbuffer = audio::play(state_engineeventsource, state_impulsestartbuffer);
			}
		} else if (entity->state() == core::Entity::Impulse) {
	
			state_engineeventbuffer = state_impulseloopbuffer;
	
			if (state_engineloopbuffer != state_impulseloopbuffer) {
				state_engineloopbuffer = audio::loop(state_engineloopsource, state_impulseloopbuffer, pitch, 0);
			}
			pitch = 1.0f;
		} else {
	
			if (state_engineeventbuffer == state_impulseloopbuffer) {				
				audio::update_source(state_engineeventsource,
					entity->location() - entity->axis().forward() * r , 
					entity->axis().forward() * speed);
				state_engineeventbuffer = audio::play(state_engineeventsource, state_impulsestopbuffer);
			}
			state_engineeventbuffer = 0;
	
			if (state_engineloopbuffer != state_thusterloopbuffer) {
				state_engineloopbuffer = audio::loop(state_engineloopsource, state_thusterloopbuffer, pitch, 0);
			}
		}
	
		audio::update_source(state_engineloopsource,
		entity->location() - entity->axis().forward() * r , entity->axis().forward() * speed, pitch, gain);
	
		audio::update_source(state_engineeventsource,
		entity->location() - entity->axis().forward() * r , entity->axis().forward() * speed);
	}
}

} // namespace client