Welcome to mirror list, hosted at ThFree Co, Russian Federation.

al_audio_source.cpp « Sound « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62fb67def862e424fad256f51f42ccca923977c2 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//-----------------------------------------------------------------------------
//           Name: al_audio_source.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "al_audio_source.h"

#include <Sound/soundlogging.h>
#include <Utility/assert.h>

#include <cassert>

alAudioSource::alAudioSource( )
: src( INVALID_SRC )
{
}

alAudioSource::~alAudioSource()
{
    if( IsValid() )
    {
        alSourceStop(src);
        alDeleteSources(1, &src);        
    }
}

void alAudioSource::Allocate()
{
    alGenSources(1,&src); 

    if( !IsValid() )
    {
        ALenum e = alGetError();
        LOGW << "Unable to generate audio source: " << e << std::endl;
        src = INVALID_SRC;
    }
}

void alAudioSource::SetBuffer( rc_alAudioBuffer buf )
{
    LOG_ASSERT( queuedBuffers.size() == 0 ); //Verify that we aren't using this for buffered audio.

    boundBuffer = buf;

    if( boundBuffer->IsValid() )
    {
        alSourcei( src, AL_BUFFER, buf->buf );
    }
}

void alAudioSource::QueueBuffers( std::vector<rc_alAudioBuffer> d )
{
    LOG_ASSERT( !boundBuffer->IsValid() ); //Verify that we haven't bound anything else.

    if( d.size() > 0 )
    {
        ALuint* buffers = (ALuint*)alloca( sizeof( ALuint ) * d.size() );

        for( unsigned i = 0; i < d.size(); i++ )
        {
            buffers[i] = d[i]->buf;
        }

        alSourceQueueBuffers( src, d.size(), buffers );

        ALenum err = alGetError();

        if( err != AL_NO_ERROR )
        {
            LOGW << "Unable to queue sound buffer: " << alErrString(err) << std::endl;
            std::copy( d.begin(), d.end(), back_inserter(failedQueuedBuffers) );
        }
        else
        {
            std::copy( d.begin(), d.end(), back_inserter(queuedBuffers) );
        }
    }
}

std::vector<rc_alAudioBuffer> alAudioSource::DequeueBuffers()
{
    ALint finished_count = GetSourcei( AL_BUFFERS_PROCESSED );
    std::vector<rc_alAudioBuffer> retbuf;
    retbuf.reserve(finished_count);

    if( finished_count > 0 )
    {
        ALuint *finished_ids = (ALuint*)alloca( sizeof( ALuint ) * finished_count );
        alSourceUnqueueBuffers( src, finished_count, finished_ids );

        for( int i = 0; i < finished_count; i++ )
        {
            for( unsigned k = 0; k < queuedBuffers.size(); k++ )
            {
                if( queuedBuffers[k]->buf == finished_ids[i] )
                {
                    retbuf.push_back(queuedBuffers[k]);
                    queuedBuffers.erase(queuedBuffers.begin() + k);
                    break;
                }
            }
        }
    }

    if( failedQueuedBuffers.size() > 0 )
    {
        std::copy( failedQueuedBuffers.begin(), failedQueuedBuffers.end(), back_inserter(retbuf) );
        failedQueuedBuffers.clear();
    }

    ALenum err = alGetError();

    if( err != AL_NO_ERROR )
    {
        LOGE << alErrString(err)  << ":" << src << std::endl;
    }

    return retbuf;
}

void alAudioSource::Sourcef( ALenum param, ALfloat value )
{
    alSourcef( src, param, value );
}

void alAudioSource::Source3f( ALenum param, ALfloat v1, ALfloat v2, ALfloat v3 )
{
    alSource3f( src, param, v1, v2, v3 );
}

void alAudioSource::Sourcei( ALenum param, ALint value )
{
    LOG_ASSERT( param != AL_BUFFER );
    alSourcei( src, param, value );
}

ALint alAudioSource::GetSourcei( ALenum pname )
{
    ALint value;
    alGetSourcei( src, pname, &value );
    return value;
}

void alAudioSource::Play()
{
    alSourcePlay(src);

    ALenum err = alGetError();

    if( err != AL_NO_ERROR )
    {
        LOGE << alErrString(err) << std::endl;
    }
}

void alAudioSource::Stop()
{
    alSourceStop(src);
}

bool alAudioSource::IsValid()
{
    return src != INVALID_SRC && alIsSource(src);
}