@@ -13,8 +13,13 @@ extern std::vector<ALuint> global_al_buffers;
1313
1414ALuint 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
3035void 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
3845void 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
7077void 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
8488void 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
96107void 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 ;
0 commit comments