Skip to content

Commit 933dcfb

Browse files
committed
MiniAL usage optimization for more optimal GUS performance.
1 parent bf4ebfd commit 933dcfb

6 files changed

Lines changed: 91 additions & 74 deletions

File tree

src/gamemng.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ void Gamemng::init_sound()
269269
{
270270
p_sound_crash = new Sound_crash;
271271
p_sound_crash->init(p_sound_game_static.p_hit_stream); // nulové *p_audiodevice je ošéfované uvnitř
272-
p_sound_crash->p_global_volume = &p_global_volume;
273272
}
274273

275274
void Gamemng::unset_scissor()

src/gamemng.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,11 @@ class Gamemng {
382382

383383
Sound_car* p_sound_car;
384384

385-
float p_global_volume;
386-
387385
int p_global_volume_i;
388386

389387
int get_global_volume() { return p_global_volume_i; }
390388
void set_global_volume(int volume) { if (volume < 0) volume = 0; else if (volume > 100) volume = 100;
391-
p_global_volume_i = volume; p_global_volume = p_global_volume_i*0.01f; }
389+
p_global_volume_i = volume; alListenerf(AL_GAIN, p_global_volume_i*0.01f); }
392390

393391

394392
Gamemenu p_gamemenu;

src/gamemng_load.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ bool Gamemng::load(int players_sel, const int* cars_sel, const int* cars_tex_sel
213213
p_sound_game_static.load(i, p_cars[cars_sel[i]].p_engine0_sample, p_cars[cars_sel[i]].p_engine1_sample);
214214

215215
// zvuky
216-
p_sound_car[i].p_global_volume = &p_global_volume; // je důležité, aby toto bylo první
217-
p_sound_car[i].init(p_sound_game_static.p_engine0_stream[i], p_sound_game_static.p_engine1_stream[i], p_cars[cars_sel[i]].engine1_pitch, p_sound_game_static.p_skid_stream[i], i, p_players);
216+
p_sound_car[i].init(p_sound_game_static.p_engine_stream[i], p_sound_game_static.p_engine0_sample[i], p_sound_game_static.p_engine1_sample[i], p_cars[cars_sel[i]].engine1_pitch, p_sound_game_static.p_skid_stream[i], i, p_players);
218217

219218
// částice
220219
if (p_particles)

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ int my_main (int argc, char** argv)
373373

374374
static const ALCint attribs[] = {
375375
ALC_FREQUENCY, ma_freq,
376-
ALC_MONO_SOURCES, 32,
376+
ALC_MONO_SOURCES, 14,
377377
0, 0
378378
};
379379

src/soundmng.cpp

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ extern std::vector<ALuint> global_al_buffers;
1313

1414
ALuint createSource(ALuint buffer)
1515
{
16-
ALuint source;
17-
alGenSources(1, &source); global_al_sources.push_back(source);
16+
ALuint source = 0;
17+
alGenSources(1, &source);
18+
if (alGetError() == AL_OUT_OF_MEMORY)
19+
{
20+
fprintf(stderr, "AL source allocation error\n");
21+
}
22+
global_al_sources.push_back(source);
1823
const ALfloat sourcePos[] = { 0.0, 0.0, 0.0 };
1924
const ALfloat sourceVel[] = { 0.0, 0.0, 0.0 };
2025
alSourcef (source, AL_PITCH, 1.0 );
@@ -29,10 +34,12 @@ ALuint createSource(ALuint buffer)
2934

3035
void Sound_game_static::playSoundTest(float gain)
3136
{
32-
alSourceStop(p_test_stream);
33-
alSourceRewind(p_test_stream);
34-
alSourcef(p_test_stream, AL_GAIN, gain);
35-
alSourcePlay(p_test_stream);
37+
alSourceStop(p_hit_stream[0]);
38+
alSourceRewind(p_hit_stream[0]);
39+
alSourcef(p_hit_stream[0], AL_GAIN, 1.0);
40+
alSourcef(p_hit_stream[0], AL_PITCH, 1.0);
41+
alListenerf(AL_GAIN, gain);
42+
alSourcePlay(p_hit_stream[0]);
3643
}
3744

3845
void Sound_game_static::init()
@@ -60,38 +67,42 @@ void Sound_game_static::init()
6067
p_skid_stream[i] = createSource(p_skid_sample);
6168
}
6269
for (int j = 0; j != 2; ++j)
63-
for (int i = 0; i != 5; ++i) {
64-
p_hit_stream[i+j*5] = createSource(p_hit_sample[j]);
70+
for (int i = 0; i != hitStreamCount/2; ++i) {
71+
p_hit_stream[i+j*(hitStreamCount/2)] = createSource(p_hit_sample[j]);
6572
}
6673

67-
p_test_stream = createSource(p_hit_sample[0]);
74+
//p_test_stream = createSource(p_hit_sample[0]);
6875
}
6976

7077
void Sound_game_static::load(unsigned int i, ALbuffer engine0_sample, ALbuffer engine1_sample)
7178
{
7279
if (i >= 4)
7380
return;
74-
if (p_engine0_stream[i] == 0)
75-
p_engine0_stream[i] = createSource(engine0_sample);
76-
else
77-
alSourcei(p_engine0_stream[i], AL_BUFFER, engine0_sample);
78-
if (p_engine1_stream[i] == 0)
79-
p_engine1_stream[i] = createSource(engine1_sample);
80-
else
81-
alSourcei(p_engine1_stream[i], AL_BUFFER, engine1_sample);
81+
if (p_engine_stream[i] == 0)
82+
p_engine_stream[i] = createSource(0);
83+
84+
p_engine0_sample[i] = engine0_sample;
85+
p_engine1_sample[i] = engine1_sample;
8286
}
8387

8488
void Sound_car::stop()
8589
{
86-
alSourceStop(p_engine0_stream);
90+
if (p_engine0_state)
91+
{
92+
alGetSourcef(p_engine_stream, AL_SAMPLE_OFFSET, &p_engine0_offset);
93+
}
94+
else if (p_engine1_state)
95+
{
96+
alGetSourcef(p_engine_stream, AL_SAMPLE_OFFSET, &p_engine1_offset);
97+
}
98+
alSourceStop(p_engine_stream);
8799
p_engine0_state = 0;
88-
alSourceStop(p_engine1_stream);
89100
p_engine1_state = 0;
90-
alSourceStop(p_skid_stream);
101+
alSourcePause(p_skid_stream);
91102
p_skid_state = 0;
92103
}
93104

94-
const float engine1_volume0 = 0.75f;
105+
static constexpr float engine1_volume0 = 0.75f;
95106

96107
void Sound_car::frame(float deltaT, int engine_state /*0 - nultý, 1 - první, 2 - první potichu*/, float engine_pitch, const float velocity[2])
97108
{
@@ -103,33 +114,41 @@ void Sound_car::frame(float deltaT, int engine_state /*0 - nultý, 1 - první, 2
103114
if (engine_state == 0)
104115
{
105116
p_engine_on = 0;
106-
alSourcef(p_engine0_stream, AL_GAIN, 0.5f**p_global_volume);
107-
if (!p_engine0_state) {
108-
alSourcePlay(p_engine0_stream);
109-
p_engine0_state = 1;
110-
}
117+
alSourcef(p_engine_stream, AL_GAIN, 0.5f);
111118
if (p_engine1_state) {
112-
alSourceStop(p_engine1_stream);
119+
alGetSourcef(p_engine_stream, AL_SAMPLE_OFFSET, &p_engine1_offset);
120+
alSourceStop(p_engine_stream);
113121
p_engine1_state = 0;
114122
}
123+
if (!p_engine0_state) {
124+
alSourcef(p_engine_stream, AL_GAIN, 0.5f);
125+
alSourcef(p_engine_stream, AL_PITCH, 1.f);
126+
alSourcei(p_engine_stream, AL_BUFFER, p_engine0_sample);
127+
alSourcef(p_engine_stream, AL_SAMPLE_OFFSET, p_engine0_offset);
128+
alSourcePlay(p_engine_stream);
129+
p_engine0_state = 1;
130+
}
115131
} else {
116132
if (engine_state == 1) {
117133
p_engine_on = 1;
118-
alSourcef(p_engine1_stream, AL_GAIN, engine1_volume0**p_global_volume);
134+
alSourcef(p_engine_stream, AL_GAIN, engine1_volume0);
119135
} else {
120-
alSourcef(p_engine1_stream, AL_GAIN, 0.5f**p_global_volume);
136+
alSourcef(p_engine_stream, AL_GAIN, 0.5f);
121137
}
122138

123-
alSourcef(p_engine1_stream, AL_PITCH, engine_pitch*p_running_pitch);
139+
alSourcef(p_engine_stream, AL_PITCH, engine_pitch*p_running_pitch);
124140

125-
if (!p_engine1_state) {
126-
alSourcePlay(p_engine1_stream);
127-
p_engine1_state = 1;
128-
}
129141
if (p_engine0_state) {
130-
alSourceStop(p_engine0_stream);
142+
alGetSourcef(p_engine_stream, AL_SAMPLE_OFFSET, &p_engine0_offset);
143+
alSourceStop(p_engine_stream);
131144
p_engine0_state = 0;
132145
}
146+
if (!p_engine1_state) {
147+
alSourcei(p_engine_stream, AL_BUFFER, p_engine1_sample);
148+
alSourcef(p_engine_stream, AL_SAMPLE_OFFSET, p_engine1_offset);
149+
alSourcePlay(p_engine_stream);
150+
p_engine1_state = 1;
151+
}
133152
}
134153

135154
if (p_brake_volume <= 0.f)
@@ -139,7 +158,7 @@ void Sound_car::frame(float deltaT, int engine_state /*0 - nultý, 1 - první, 2
139158
p_skid_state = 0;
140159
}
141160
} else {
142-
alSourcef(p_skid_stream, AL_GAIN, p_brake_volume**p_global_volume);
161+
alSourcef(p_skid_stream, AL_GAIN, p_brake_volume);
143162

144163
float speed = std::sqrt(velocity[0]*velocity[0] + velocity[1]*velocity[1]);
145164
float skidPitch = 0.8f+(speed - 6.f)/24.f*0.4f;
@@ -155,7 +174,7 @@ void Sound_car::frame(float deltaT, int engine_state /*0 - nultý, 1 - první, 2
155174
}
156175
}
157176

158-
void Sound_car::init(ALsource stream_idle, ALsource stream_running, float running_pitch, ALsource stream_skid, int player, int players_n)
177+
void Sound_car::init(ALsource stream_engine, ALbuffer sample_idle, ALbuffer sample_running, float running_pitch, ALsource stream_skid, int player, int players_n)
159178
{
160179
p_brake_volume = 0.f;
161180
// toto zásadně změnit
@@ -173,25 +192,25 @@ void Sound_car::init(ALsource stream_idle, ALsource stream_running, float runnin
173192
default: // 3, 4
174193
if (player < 2) p_pan = -1; else p_pan = 1;
175194
}
176-
p_engine0_stream = stream_idle;
177-
p_engine1_stream = stream_running;
195+
p_engine_stream = stream_engine;
196+
p_engine0_sample = sample_idle;
197+
p_engine1_sample = sample_running;
178198
p_skid_stream = stream_skid;
179199
p_time = 0.f;
180200
p_T = 0.01f;
181201

182-
alSourcei(p_engine0_stream, AL_LOOPING, 1);
183-
alSourcei(p_engine1_stream, AL_LOOPING, 1);
202+
alSourcei(p_engine_stream, AL_LOOPING, 1);
184203
alSourcei(p_skid_stream, AL_LOOPING, 1);
185204
// nejmenší délka v samplech je 32000
186-
alSourcef(p_engine0_stream, AL_SAMPLE_OFFSET, (smallSampleRam() ? 16000.0 : 32000.0)*double(player)/double(players_n));
187-
alSourcef(p_engine0_stream, AL_SAMPLE_OFFSET, (smallSampleRam() ? 16000.0 : 32000.0)*double(player)/double(players_n));
188-
alSourcef(p_engine0_stream, AL_SAMPLE_OFFSET, (smallSampleRam() ? 16000.0 : 32000.0)*double(player)/double(players_n));
189205

190-
alSourcef(p_engine0_stream, AL_GAIN, 0.5**p_global_volume);
191-
alSourcef(p_engine1_stream, AL_GAIN, engine1_volume0**p_global_volume);
206+
p_engine0_offset = (smallSampleRam() ? 16000.0 : 32000.0)*double(player)/double(players_n);
207+
p_engine1_offset = (smallSampleRam() ? 16000.0 : 32000.0)*double(player)/double(players_n);
208+
209+
alSourcef(p_skid_stream, AL_SAMPLE_OFFSET, (smallSampleRam() ? 16000.0 : 32000.0)*double(player)/double(players_n));
192210

193211
p_engine0_state = 0; // 0 - nehraje, 1 - hraje
194212
p_engine1_state = 0; // 0 - nehraje, 1 - hraje
213+
195214
p_skid_state = 0;
196215

197216
p_engine_on = 0;
@@ -216,7 +235,7 @@ void Sound_crash::play(float c_j) // přehraje zvuk nárazu
216235

217236
alSourceStop(p_hit_stream[sel]);
218237
alSourceRewind(p_hit_stream[sel]);
219-
alSourcef(p_hit_stream[sel], AL_GAIN, volume**p_global_volume);
238+
alSourcef(p_hit_stream[sel], AL_GAIN, volume);
220239

221240
float pitch_min = 0.88;
222241
float pitch = pitch_min+randn1(int((1.f-pitch_min)*2.f*1000.f))*0.001;

src/soundmng.h

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ typedef ALuint ALsource;
3131

3232
class Sound_game_static {
3333
public:
34+
static constexpr int hitStreamCount = 6;
3435
Sound_game_static() {
3536
for (int i = 0; i != 4; ++i)
3637
{
3738
p_skid_stream[i] = 0;
38-
p_engine0_stream[i] = 0;
39-
p_engine1_stream[i] = 0;
39+
p_engine0_sample[i] = 0;
40+
p_engine1_sample[i] = 0;
41+
p_engine_stream[i] = 0;
4042
}
41-
for (int i = 0; i != 10; ++i)
43+
for (int i = 0; i != hitStreamCount; ++i)
4244
p_hit_stream[i] = 0;
43-
p_test_stream = 0;
4445
p_skid_sample = 0;
4546
p_hit_sample[0] = 0;
4647
p_hit_sample[1] = 0;
@@ -49,10 +50,11 @@ class Sound_game_static {
4950
for (int i = 0; i != 4; ++i)
5051
{
5152
p_skid_stream[i] = 0;
52-
p_engine0_stream[i] = 0;
53-
p_engine1_stream[i] = 0;
53+
p_engine0_sample[i] = 0;
54+
p_engine1_sample[i] = 0;
55+
p_engine_stream[i] = 0;
5456
}
55-
for (int i = 0; i != 10; ++i)
57+
for (int i = 0; i != hitStreamCount; ++i)
5658
p_hit_stream[i] = 0;
5759
p_skid_sample = 0;
5860
p_hit_sample[0] = 0;
@@ -63,11 +65,10 @@ class Sound_game_static {
6365
void load(unsigned int i, ALbuffer p_engine0_sample, ALbuffer p_engine1_sample);
6466

6567
ALsource p_skid_stream[4];
66-
ALsource p_engine0_stream[4];
67-
ALsource p_engine1_stream[4];
68-
ALsource p_hit_stream[10];
69-
70-
ALsource p_test_stream;
68+
ALsource p_engine_stream[4];
69+
ALbuffer p_engine0_sample[4];
70+
ALbuffer p_engine1_sample[4];
71+
ALsource p_hit_stream[hitStreamCount];
7172

7273
ALbuffer p_skid_sample;
7374
ALbuffer p_hit_sample[2];
@@ -82,16 +83,19 @@ class Sound_game_static {
8283

8384
class Sound_car {
8485
public:
85-
Sound_car() : p_skid_stream(0), p_engine0_stream(0), p_engine1_stream(0), p_engine0_state(0), p_engine1_state(0), p_skid_state(0), p_time(0),
86-
p_T(0), p_running_pitch(0), p_pan(0), p_player(0), p_brake_volume(0), p_engine_on(0), p_global_volume(0) { }
86+
Sound_car() : p_skid_stream(0), p_engine_stream(0), p_engine0_sample(0), p_engine1_sample(0), p_engine0_state(0), p_engine1_state(0), p_skid_state(0), p_time(0),
87+
p_T(0), p_running_pitch(0), p_pan(0), p_player(0), p_brake_volume(0), p_engine_on(0) { }
8788
~Sound_car() {
8889
}
89-
void init(ALsource stream_idle, ALsource stream_running, float running_pitch, ALsource stream_skid, int player, int players_n);
90+
void init(ALsource stream_engine, ALbuffer sample_idle, ALbuffer sample_running, float running_pitch, ALsource stream_skid, int player, int players_n);
9091
void frame(float deltaT, int engine_state/*0 - nultý, 1 - první, 2 - první potichu*/, float engine_pitch, const float velocity[2]);
9192
void stop();
9293
ALsource p_skid_stream;
93-
ALsource p_engine0_stream;
94-
ALsource p_engine1_stream;
94+
ALsource p_engine_stream;
95+
ALbuffer p_engine0_sample;
96+
ALbuffer p_engine1_sample;
97+
ALfloat p_engine0_offset = 0;
98+
ALfloat p_engine1_offset = 0;
9599
int p_engine0_state; // 0 - nehraje, 1 - hraje
96100
int p_engine1_state; // 0 - nehraje, 1 - hraje
97101
int p_skid_state; // 0 - nehraje, 1 - hraje
@@ -102,12 +106,11 @@ class Sound_car {
102106
int p_player;
103107
float p_brake_volume;
104108
int p_engine_on;
105-
const float* p_global_volume;
106109
};
107110

108111
class Sound_crash {
109112
public:
110-
Sound_crash() { p_sz_samples = 2; p_width = 5; p_global_volume = 0; p_hit_stream = 0; p_fronta_pos[0] = 0; p_fronta_pos[1] = 0; }
113+
Sound_crash() { p_sz_samples = 2; p_width = 3; p_hit_stream = 0; p_fronta_pos[0] = 0; p_fronta_pos[1] = 0; }
111114
~Sound_crash() {
112115
}
113116
void init(ALsource* stream_hit); // load zvuků
@@ -117,7 +120,6 @@ class Sound_crash {
117120
ALsource* p_hit_stream;
118121
int p_fronta_pos[10]; // stačí 2
119122

120-
const float* p_global_volume;
121123
// n = 3
122124
// f = 5
123125
// samply[n]

0 commit comments

Comments
 (0)