32 ALboolean needsSource = AL_TRUE; |
32 ALboolean needsSource = AL_TRUE; |
33 ALfloat SourcePosition[] = { 0.0, 0.0, 0.0 }; |
33 ALfloat SourcePosition[] = { 0.0, 0.0, 0.0 }; |
34 ALfloat SourceVelocity[] = { 0.0, 0.0, 0.0 }; |
34 ALfloat SourceVelocity[] = { 0.0, 0.0, 0.0 }; |
35 ALint state; |
35 ALint state; |
36 int i, j; |
36 int i, j; |
37 |
37 |
38 if (openal_ready() == AL_TRUE && index < cache_size) { |
38 if (openal_ready() == AL_TRUE && index < cache_size) { |
39 // check if sound has already a source |
39 // check if sound has already a source |
40 if (the_sounds[index].source_index != -1) { |
40 if (the_sounds[index].source_index != -1) { |
41 // it has a source, check it's not playing |
41 // it has a source, check it's not playing |
42 alGetSourcei(Sources[the_sounds[index].source_index], AL_SOURCE_STATE, &state); |
42 alGetSourcei(Sources[the_sounds[index].source_index], AL_SOURCE_STATE, &state); |
43 if (state != AL_PLAYING && state != AL_PAUSED) { |
43 if (state != AL_PLAYING && state != AL_PAUSED) { |
44 // it is not being played, so we can use it safely |
44 // it is not being played, so we can use it safely |
45 needsSource = AL_FALSE; |
45 needsSource = AL_FALSE; |
46 } |
46 } |
47 // else it is being played, so we have to allocate a new source for this buffer |
47 // else it is being played, so we have to allocate a new source for this buffer |
48 } |
48 } |
49 |
49 |
50 if (needsSource) { |
50 if (needsSource) { |
51 #ifdef DEBUG |
51 #ifdef DEBUG |
52 fprintf(stderr,"(Bridge Debug) - looking for a source for sound %d\n", index); |
52 fprintf(stderr,"(Bridge Debug) - looking for a source for sound %d\n", index); |
53 #endif |
53 #endif |
54 for (i = 0; i < sources_number; i++) { |
54 for (i = 0; i < sources_number; i++) { |
55 // let's iterate on Sources until we find a source that is not playing |
55 // let's iterate on Sources until we find a source that is not playing |
56 alGetSourcei(Sources[i], AL_SOURCE_STATE, &state); |
56 alGetSourcei(Sources[i], AL_SOURCE_STATE, &state); |
57 if (state != AL_PLAYING && state != AL_PAUSED) { |
57 if (state != AL_PLAYING && state != AL_PAUSED) { |
58 // let's iterate on the_sounds until we find the sound using that source |
58 // let's iterate on the_sounds until we find the sound using that source |
59 for (j = 0; j < cache_size; j++) { |
59 for (j = 0; j < cache_size; j++) { |
60 if (the_sounds[j].source_index == i) { |
60 if (the_sounds[j].source_index == i) { |
61 the_sounds[j].source_index = -1; |
61 the_sounds[j].source_index = -1; |
64 } |
64 } |
65 // here we know that no-one is using that source so we can use it |
65 // here we know that no-one is using that source so we can use it |
66 break; |
66 break; |
67 } |
67 } |
68 } |
68 } |
69 |
69 |
70 if (i == sources_number) { |
70 if (i == sources_number) { |
71 // this means all sources are busy |
71 // this means all sources are busy |
72 } |
72 } |
73 |
73 |
74 // set source properties that it will use when it's in playback |
74 // set source properties that it will use when it's in playback |
75 alSourcei (Sources[i], AL_BUFFER, the_sounds[index].buffer); |
75 alSourcei (Sources[i], AL_BUFFER, the_sounds[index].buffer); |
76 alSourcef (Sources[i], AL_PITCH, 1.0f); |
76 alSourcef (Sources[i], AL_PITCH, 1.0f); |
77 alSourcef (Sources[i], AL_GAIN, 1.0f); |
77 alSourcef (Sources[i], AL_GAIN, 1.0f); |
78 alSourcefv(Sources[i], AL_POSITION, SourcePosition); |
78 alSourcefv(Sources[i], AL_POSITION, SourcePosition); |
79 alSourcefv(Sources[i], AL_VELOCITY, SourceVelocity); |
79 alSourcefv(Sources[i], AL_VELOCITY, SourceVelocity); |
80 alSourcei (Sources[i], AL_LOOPING, 0); |
80 alSourcei (Sources[i], AL_LOOPING, 0); |
81 |
81 |
82 if (AL_NO_ERROR != alGetError()) { |
82 if (AL_NO_ERROR != alGetError()) { |
83 fprintf(stderr,"(Bridge ERROR) - failed to set Source properties\n"); |
83 fprintf(stderr,"(Bridge ERROR) - failed to set Source properties\n"); |
84 return; |
84 return; |
85 } |
85 } |
86 the_sounds[index].source_index = i; |
86 the_sounds[index].source_index = i; |
87 } |
87 } |
88 |
88 |
89 alSourcePlay(Sources[the_sounds[index].source_index]); |
89 alSourcePlay(Sources[the_sounds[index].source_index]); |
90 |
90 |
91 if (AL_NO_ERROR != alGetError()) { |
91 if (AL_NO_ERROR != alGetError()) { |
92 fprintf(stderr,"(Bridge Warning) - failed to play sound %d\n", index); |
92 fprintf(stderr,"(Bridge Warning) - failed to play sound %d\n", index); |
93 return; |
93 return; |
94 } |
94 } |
95 } |
95 } |