示例#1
0
////////////////////////////////////////////////////////////
/// Clear the queue of any remaining buffers
////////////////////////////////////////////////////////////
void SoundStream::ClearQueue()
{
    // Get the number of buffers still in the queue
    ALint NbQueued;
    ALCheck(alGetSourcei(Sound::mySource, AL_BUFFERS_QUEUED, &NbQueued));

    // Unqueue them all
    ALuint Buffer;
    for (ALint i = 0; i < NbQueued; ++i)
        ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer));
}
示例#2
0
 void OpenALChannel::unqueueBuffers()
 {
    ALint processed = 0;
    alGetSourcei(mSourceID, AL_BUFFERS_PROCESSED, &processed);
    //LOG_SOUND("Recover buffers : %d (%d)", processed, mDynamicStackSize);
    if (processed)
    {
       alSourceUnqueueBuffers(mSourceID,processed,&mDynamicStack[mDynamicStackSize]);
       mDynamicStackSize += processed;
    }
 }
示例#3
0
void oggFree( SOggSound* o )
{
  // close ogg-vorbis stream
  ov_clear( &o->stream );

  // close file
  fclose( o->file );

  alSourceUnqueueBuffers( o->source, 1, &o->buffers[0] );
  alSourceUnqueueBuffers( o->source, 1, &o->buffers[1] );

  alDeleteBuffers( 2, o->buffers );

  alDeleteSources( 1, &o->source );
  
  alSourcei( o->source, AL_BUFFER, 0);

  soundCheckErrorAL( "free ogg" );
  soundCheckErrorALC( "free ogg" );
}
示例#4
0
	void SoundStreamBase::RetrieveQueuedBuffers() {
		ALuint buffer = 0;
		ALint queuedBuffers = 0;
		alGetSourcei(mOpenALSourceID, AL_BUFFERS_QUEUED, &queuedBuffers);
		for (ALint i = 0; i < queuedBuffers; i++) {
			alSourceUnqueueBuffers(mOpenALSourceID, 1, &buffer);
			for (Int j = 0; j < mBuffers.GetElementCount(); j++)
				if (buffer == mBuffers[j].mOpenALBufferID)
					mBuffers[j].mIsActive = false;
		}
	}
示例#5
0
文件: ogg.cpp 项目: pilkch/library
    void cOggStream::EmptyBuffer()
    {
      int queued;
      alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);

      while (queued--) {
        ALuint buffer;
        alSourceUnqueueBuffers(source, 1, &buffer);
        ReportError();
      }
    }
示例#6
0
static void play() {
	ALCdevice *dev;
	ALCcontext *ctx;
	unsigned src;
	unsigned bufs[2];
	unsigned buf;
	int v;
	float zero[3];

	memset(zero, 0, sizeof zero);

	dev = alcOpenDevice(NULL);
	ctx = alcCreateContext(dev, NULL);
	alcMakeContextCurrent(ctx);

	alGenSources(1, &src);
	alGenBuffers(2, bufs);

	alSourcef(src, AL_PITCH, 1.0f);
	alSourcef(src, AL_GAIN, 1.0f);
	alSourcefv(src, AL_POSITION, zero);
	alSourcefv(src, AL_VELOCITY, zero);
	alSourcei(src, AL_SOURCE_RELATIVE, AL_TRUE);
	alSourcei(src, AL_LOOPING, AL_FALSE);

	ima_init(&ima_state);

	queue(src, bufs[0]);
	queue(src, bufs[1]);

	alSourcePlay(src);

	for (;;) {
		alGetSourcei(src, AL_BUFFERS_PROCESSED, &v);

		while (v-- > 0) {
			alSourceUnqueueBuffers(src, 1, &buf);
			queue(src, buf);
		}

		alGetSourcei(src, AL_SOURCE_STATE, &v);

		if (v == AL_STOPPED)
			break;

		usleep(1000);
	}

        alDeleteSources(1, &src);
        alDeleteBuffers(2, bufs);
        alcMakeContextCurrent(NULL);
        alcDestroyContext(ctx);
        alcCloseDevice(dev);
}
 unsigned char *ChannelStream::GetBuffer( unsigned int *size )
 {
     if (this->startBuffer < NUM_BUFFERS){
         return GetStaticBuffer(size);
     }
     
     alSourceUnqueueBuffers(source, 1, &active_buffer);
     Check("getbuffer");
     
     return GetStaticBuffer(size);
 }
示例#8
0
void SoundStream::clearQueue()
{
    // Get the number of buffers still in the queue
    ALint nbQueued;
    alCheck(alGetSourcei(m_source, AL_BUFFERS_QUEUED, &nbQueued));

    // Dequeue them all
    ALuint buffer;
    for (ALint i = 0; i < nbQueued; ++i)
        alCheck(alSourceUnqueueBuffers(m_source, 1, &buffer));
}
示例#9
0
文件: openal2.cpp 项目: Grokafar/qTox
/**
 * @brief Handle audio output
 */
void OpenAL2::doOutput()
{
    alcMakeContextCurrent(alOutContext);
    ALuint bufids[PROXY_BUFFER_COUNT];
    ALint processed = 0, queued = 0;
    alGetSourcei(alProxySource, AL_BUFFERS_PROCESSED, &processed);
    alGetSourcei(alProxySource, AL_BUFFERS_QUEUED, &queued);

    if (processed > 0) {
        // unqueue all processed buffers
        alSourceUnqueueBuffers(alProxySource, processed, bufids);
        // delete all but the first buffer, reuse first for new data
        alDeleteBuffers(processed - 1, bufids + 1);
    } else if (queued < PROXY_BUFFER_COUNT) {
        // create new buffer until the maximum is reached
        alGenBuffers(1, bufids);
    } else {
        alcMakeContextCurrent(alProxyContext);
        return;
    }

    ALdouble latency[2] = {0};
    alGetSourcedvSOFT(alProxySource, AL_SEC_OFFSET_LATENCY_SOFT, latency);
    checkAlError();

    ALshort outBuf[AUDIO_FRAME_SAMPLE_COUNT] = {0};
    alcMakeContextCurrent(alProxyContext);
    alcRenderSamplesSOFT(alProxyDev, outBuf, AUDIO_FRAME_SAMPLE_COUNT);
    checkAlcError(alProxyDev);

    alcMakeContextCurrent(alOutContext);
    alBufferData(bufids[0], AL_FORMAT_MONO16, outBuf, AUDIO_FRAME_SAMPLE_COUNT * 2, AUDIO_SAMPLE_RATE);
    alSourceQueueBuffers(alProxySource, 1, bufids);

    // initialize echo canceler if supported
    if (!filterer) {
        filterer = new_filter_audio(AUDIO_SAMPLE_RATE);
        int16_t filterLatency = latency[1] * 1000 * 2 + AUDIO_FRAME_DURATION;
        qDebug() << "Setting filter delay to: " << filterLatency << "ms";
        set_echo_delay_ms(filterer, filterLatency);
        enable_disable_filters(filterer, 1, 1, 1, 0);
    }

    // do echo cancel
    pass_audio_output(filterer, outBuf, AUDIO_FRAME_SAMPLE_COUNT);

    ALint state;
    alGetSourcei(alProxySource, AL_SOURCE_STATE, &state);
    if (state != AL_PLAYING) {
        qDebug() << "Proxy source underflow detected";
        alSourcePlay(alProxySource);
    }
    alcMakeContextCurrent(alProxyContext);
}
示例#10
0
	//-----------------------------------------------------------------//
	void audio_io::test_stream(const char* filename)
	{
		static const int numbuff = 16;
		static const int bufsize = 4096;

		utils::fileio fin;
		if(!fin.open(filename, "rb")) {
			return;
		}
		wavio wav;
		audio_info info;
		wav.open_stream(fin, bufsize, info);
		const i_audio* aif = wav.get_stream();
		if(aif == 0) {
			wav.close_stream();
			fin.close();
			return;
		}

		int cnt = 0;

		ALuint source;
		alGenSources(1, &source);
		size_t pos = 0;
		while(pos < info.samples) {
			ALint n;
			alGetSourcei(source, AL_BUFFERS_QUEUED, &n);
			ALuint buffer;
			if(n < numbuff) {
				alGenBuffers(1, &buffer);
				++cnt;
			} else {
				ALint state;
				alGetSourcei(source, AL_SOURCE_STATE, &state);
				if(state != AL_PLAYING) {
					alSourcePlay(source);
				}
				while(alGetSourcei(source, AL_BUFFERS_PROCESSED, &n), n == 0) {
					useconds_t usec = 1000 * 10;	//[ms]
					usleep(usec);
				}
				alSourceUnqueueBuffers(source, 1, &buffer);
			}
			pos += wav.read_stream(fin, pos, bufsize);
			set_buffer(buffer, aif);
			alSourceQueueBuffers(source, 1, &buffer);
		}
		alDeleteSources(1, &source);

		wav.close_stream();
		fin.close();

		std::cout << boost::format("buffer: %d\n") % cnt;
	}
void CCAudioPlayer::update() {
	if(m_source == 0)
		return;
    
	if(!m_stream->isSingleBuffer()) {
		bool end = false;
		ALint buffers;
		alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &buffers);
		while(buffers-- > 0) {
			// unqueue a buffer
			ALuint buffer;
			alSourceUnqueueBuffers(m_source, 1, &buffer);
			if(buffer == AL_INVALID_VALUE)
				break;
            
			// adjust rendered seconds
			m_renderedSeconds += m_secondsPerBuffer;
            
			// try to fill buffer, if failed, let it end
			if(fill(buffer))
				alSourceQueueBuffers(m_source, 1, &buffer);
			else {
				end = true;
				break;
			}
		}
        
		// if no queued buffer or end flag is set
		alGetSourcei(m_source, AL_BUFFERS_QUEUED, &buffers);
		if(end && buffers == 0) {
			stop();
			return;
		}
        
		// A buffer underflow will cause the source to stop.
		ALint state;
		alGetSourcei(m_source, AL_SOURCE_STATE, &state);
		if(m_playing && !m_paused && state != AL_PLAYING)
			alSourcePlay(m_source);
	} else {
		if(m_playing) {
			ALint state;
			alGetSourcei(m_source, AL_SOURCE_STATE, &state);
			if(state != AL_PLAYING)
				stop();
		}
	}
    
    // check error
    int error = alGetError();
    if(error != AL_NO_ERROR) {
        CCLOGWARN("CCAudioPlayer::update: AL error occured: 0x%X", error);
    }
}
示例#12
0
static void unqueue_buffers(void) {
  ALint p;
  int s, i;
  for (s = 0;  s < ao_data.channels; s++) {
    alGetSourcei(sources[s], AL_BUFFERS_PROCESSED, &p);
    for (i = 0; i < p; i++) {
      alSourceUnqueueBuffers(sources[s], 1, &buffers[s][unqueue_buf[s]]);
      unqueue_buf[s] = (unqueue_buf[s] + 1) % NUM_BUF;
    }
  }
}
示例#13
0
void CSH_OpenAL::RecycleBuffers()
{
	unsigned int bufferCount = m_source.GetBuffersProcessed();
	CHECK_AL_ERROR();
	if(bufferCount != 0)
	{
		ALuint* bufferNames = reinterpret_cast<ALuint*>(alloca(sizeof(ALuint) * bufferCount));
		alSourceUnqueueBuffers(m_source, bufferCount, bufferNames);
		CHECK_AL_ERROR();
		m_availableBuffers.insert(m_availableBuffers.begin(), bufferNames, bufferNames + bufferCount);
	}
}
static void
openal_unqueue_buffers(struct openal_data *od)
{
	ALint num;
	ALuint buffer;

	alGetSourcei(od->source, AL_BUFFERS_QUEUED, &num);

	while (num--) {
		alSourceUnqueueBuffers(od->source, 1, &buffer);
	}
}
示例#15
0
文件: alSource.c 项目: Aye1/RVProject
ALAPI ALvoid ALAPIENTRY alSourcei (ALuint source, ALenum pname, ALint value)
{
	ALenum error;
	ALuint buffer;
	
	if (alIsSource(source))
	{
		switch(pname) 
		{
			case AL_LOOPING:
				gSource[source].looping = value;
				break;
			case AL_BUFFER:
				if ((gSource[source].state == AL_STOPPED) || (gSource[source].state == AL_INITIAL))
				{
					if ((alIsBuffer(value)) || (value == NULL))
					{
						gSource[source].srcBufferNum = value;
				
						// reset queue
						alGetError(); // clear error code
						error = AL_NO_ERROR;
						while (error == AL_NO_ERROR)
						{
							alSourceUnqueueBuffers(source, 1, &buffer);
							error = alGetError();
						}
					
					} else
					{
						alSetError(AL_INVALID_VALUE);
					}
				} else
				{
					alSetError(AL_INVALID_OPERATION);
				}
				break;
			case AL_SOURCE_RELATIVE:
				if ((value==AL_FALSE)||(value==AL_TRUE))
				{
					gSource[source].srcRelative = value;
				} else
				{
					alSetError(AL_INVALID_VALUE);
				}
				break;
			default:
				alSetError(AL_INVALID_ENUM);
				break;
	
		}
	}
}
示例#16
0
void audio_output::data(const audio_blob &blob)
{
    assert(blob.data);
    ALenum format = get_al_format(blob);
    msg::dbg(std::string("Buffering ") + str::from(blob.size) + " bytes of audio data.");
    if (_state == 0)
    {
        set_source_parameters();
        // Initial buffering
        assert(blob.size == _num_buffers * _buffer_size);
        char *data = static_cast<char *>(blob.data);
        for (size_t j = 0; j < _num_buffers; j++)
        {
            _buffer_channels.push_back(blob.channels);
            _buffer_sample_bits.push_back(blob.sample_bits());
            _buffer_rates.push_back(blob.rate);
            alBufferData(_buffers[j], format, data, _buffer_size, blob.rate);
            alSourceQueueBuffers(_source, 1, &(_buffers[j]));
            data += _buffer_size;
        }
        if (alGetError() != AL_NO_ERROR)
        {
            throw exc(_("Cannot buffer initial OpenAL data."));
        }
    }
    else if (blob.size > 0)
    {
        // Replace one buffer
        assert(blob.size == _buffer_size);
        ALuint buf = 0;
        alSourceUnqueueBuffers(_source, 1, &buf);
        assert(buf != 0);
        alBufferData(buf, format, blob.data, _buffer_size, blob.rate);
        alSourceQueueBuffers(_source, 1, &buf);
        if (alGetError() != AL_NO_ERROR)
        {
            throw exc(_("Cannot buffer OpenAL data."));
        }
        // Update the time spent on all past buffers
        int64_t current_buffer_samples = _buffer_size / _buffer_channels[0] * 8 / _buffer_sample_bits[0];
        int64_t current_buffer_time = current_buffer_samples * 1000000 / _buffer_rates[0];
        _past_time += current_buffer_time;
        // Remove current buffer entries
        _buffer_channels.erase(_buffer_channels.begin());
        _buffer_sample_bits.erase(_buffer_sample_bits.begin());
        _buffer_rates.erase(_buffer_rates.begin());
        // Add entries for the new buffer
        _buffer_channels.push_back(blob.channels);
        _buffer_sample_bits.push_back(blob.sample_bits());
        _buffer_rates.push_back(blob.rate);
    }
}
示例#17
0
    void AudioWorld::musicRenderFunction()
    {
        ALenum error;
        std::int16_t buf[RE_MUSIC_BUFFER_LEN];
        for (int i = 0; i < RE_MUSIC_BUFFER_LEN; i++) buf[i] = 0;

        for (int i = 0; i < RE_NUM_MUSIC_BUFFERS; i++)
        {
            alBufferData(m_musicBuffers[i], AL_FORMAT_STEREO16, buf, RE_MUSIC_BUFFER_LEN * 2, 44100);
        }

        alSourceQueueBuffers(m_musicSource, RE_NUM_MUSIC_BUFFERS, m_musicBuffers);
        alSourcePlay(m_musicSource);
        error = alGetError();
        if (error != AL_NO_ERROR)
        {
            LogError() << "Cannot start playing music: " << AudioEngine::getErrorString(error);
            return;
        }

        m_musicContext->renderBlock(buf, RE_MUSIC_BUFFER_LEN);

        while (!m_exiting)
        {
            ALint val;
            int n = 0;
            alGetSourcei(m_musicSource, AL_BUFFERS_PROCESSED, &val);
            if (val <= 0)
            {
                continue;
            }

            for(int i = 0; i < val; i++)
            {
                ALuint buffer;
                alSourceUnqueueBuffers(m_musicSource, 1, &buffer);
                alBufferData(buffer, AL_FORMAT_STEREO16, buf, RE_MUSIC_BUFFER_LEN * 2, 44100);
                alSourceQueueBuffers(m_musicSource, 1, &buffer);
                error = alGetError();
                if (error != AL_NO_ERROR)
                {
                    LogError() << "Error while buffering: " << AudioEngine::getErrorString(error);
                    return;
                }
                m_musicContext->renderBlock(buf, RE_MUSIC_BUFFER_LEN);
            }

            alGetSourcei(m_musicSource, AL_SOURCE_STATE, &val);
            if (val != AL_PLAYING)
                alSourcePlay(m_musicSource);
        }
    }
示例#18
0
CMusic::~CMusic()
{
    ALenum error = alGetError();

    // Fermeture du fichier
    if (m_file)
    {
        sf_close(m_file);
    }

    stop();

    // On purge la file de tampons de la source
    if (m_source != 0)
    {
        ALint nbr_queued;
        ALuint buffer;

        alGetSourcei(m_source, AL_BUFFERS_QUEUED /*AL_BUFFERS_PROCESSED*/, &nbr_queued);

        // Traitement des erreurs
        if ((error = alGetError()) != AL_NO_ERROR)
        {
            CSoundEngine::displayOpenALError(error, "alSourcei", __LINE__);
            nbr_queued = 0;
        }

        for (ALint i = 0; i < nbr_queued; ++i)
        {
            alSourceUnqueueBuffers(m_source, 1, &buffer);

            // Traitement des erreurs
            if ((error = alGetError()) != AL_NO_ERROR)
            {
                CSoundEngine::displayOpenALError(error, "alSourceUnqueueBuffers", __LINE__);
            }
        }
    }

    if (m_buffer[0] != 0 || m_buffer[1] != 0)
    {
        alDeleteBuffers(2, m_buffer);

        // Traitement des erreurs
        if ((error = alGetError()) != AL_NO_ERROR)
        {
            CSoundEngine::displayOpenALError(error, "alDeleteBuffers", __LINE__);
        }
    }

    //CSoundEngine::Instance().RemoveMusic(this);
}
示例#19
0
//-----------------------------------------------------------------------------
void MusicOggStream::update()
{

    if (m_pausedMusic || m_soundSource == ALuint(-1))
    {
        // nothing todo
        return;
    }

    int processed= 0;
    bool active= true;

    alGetSourcei(m_soundSource, AL_BUFFERS_PROCESSED, &processed);

    while(processed--)
    {
        ALuint buffer;

        alSourceUnqueueBuffers(m_soundSource, 1, &buffer);
        if(!check("alSourceUnqueueBuffers")) return;

        active = streamIntoBuffer(buffer);
        if(!active)
        {
            // no more data. Seek to beginning (causes the sound to loop)
            ov_time_seek(&m_oggStream, 0);
            active = streamIntoBuffer(buffer);//now there really should be data
        }

        alSourceQueueBuffers(m_soundSource, 1, &buffer);
        if (!check("alSourceQueueBuffers")) return;
    }

    if (active)
    {
        // we have data, so we should be playing...
        ALenum state;
        alGetSourcei(m_soundSource, AL_SOURCE_STATE, &state);
        if (state != AL_PLAYING)
        {
            Log::warn("MusicOgg", "Music not playing when it should be. "
                      "Source state: %d\n", state);
            alGetSourcei(m_soundSource, AL_BUFFERS_PROCESSED, &processed);
            alSourcePlay(m_soundSource);
        }
    }
    else
    {
        Log::warn("MusicOgg", "Attempt to stream music into buffer failed "
                              "twice in a row.\n");
    }
}   // update
示例#20
0
	// 更新缓冲区
	/////////////////////////////////////////////////////////////////////////////////
	void OALMusicBuffer::LoopUpdateBuffer()
	{
		std::unique_lock<std::mutex> lock(play_mutex_);
		while (!played_)
		{
			play_cond_.wait(lock);
		}
		played_ = false;

		while (!stopped_)
		{
			ALint processed;
			alGetSourcei(source_, AL_BUFFERS_PROCESSED, &processed);
			if (processed > 0)
			{
				while (processed > 0)
				{
					-- processed;

					ALuint buf;
					alSourceUnqueueBuffers(source_, 1, &buf);

					std::vector<uint8_t> data(READSIZE);
					data.resize(dataSource_->Read(&data[0], data.size()));
					if (data.empty())
					{
						if (loop_)
						{
							stopped_ = false;
							alSourceStopv(1, &source_);
							this->DoReset();
							alSourcePlay(source_);
						}
						else
						{
							stopped_ = true;
						}
					}
					else
					{
						alBufferData(buf, Convert(format_), &data[0],
							static_cast<ALsizei>(data.size()), freq_);
						alSourceQueueBuffers(source_, 1, &buf);
					}
				}
			}
			else
			{
				Sleep(1000 / PreSecond);
			}
		}
	}
示例#21
0
bool Source::update()
{
	if (!valid)
		return false;

	if (type == TYPE_STATIC)
	{
		// Looping mode could have changed.
		alSourcei(source, AL_LOOPING, isLooping() ? AL_TRUE : AL_FALSE);
		return !isStopped();
	}
	else if (type == TYPE_STREAM && (isLooping() || !isFinished()))
	{
		// Number of processed buffers.
		ALint processed = 0;

		alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);

		while (processed--)
		{
			ALuint buffer;

			float curOffsetSamples, curOffsetSecs;

			alGetSourcef(source, AL_SAMPLE_OFFSET, &curOffsetSamples);

			int freq = decoder->getSampleRate();
			curOffsetSecs = curOffsetSamples / freq;

			// Get a free buffer.
			alSourceUnqueueBuffers(source, 1, &buffer);

			float newOffsetSamples, newOffsetSecs;

			alGetSourcef(source, AL_SAMPLE_OFFSET, &newOffsetSamples);
			newOffsetSecs = newOffsetSamples / freq;

			offsetSamples += (curOffsetSamples - newOffsetSamples);
			offsetSeconds += (curOffsetSecs - newOffsetSecs);

			// FIXME: We should put freed buffers into a list that we later
			// consume here, so we can keep track of all free buffers even if we
			// tried to stream data to one but the decoder didn't have data for it.
			if (streamAtomic(buffer, decoder.get()) > 0)
				alSourceQueueBuffers(source, 1, &buffer);
		}

		return true;
	}

	return false;
}
示例#22
0
void
AudioManager::Player::Update()
{
    // try to fill processed buffers
    {
        int processed;
        alGetSourcei( m_Source, AL_BUFFERS_PROCESSED, &processed );

        for( int i = 0; i < processed; ++i )
        {
            // try fill buffer
            ALsizei buffer_size = FillBuffer();

            if( buffer_size )
            {
                ALuint buffer_id;
                alSourceUnqueueBuffers( m_Source, 1, &buffer_id );
                alBufferData( buffer_id, m_VorbisInfo->channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, ( const ALvoid* )AudioManager::getSingleton().m_Buffer, ( ALsizei )buffer_size, ( ALsizei )m_VorbisInfo->rate );
                alSourceQueueBuffers( m_Source, 1, &buffer_id );
            }
            // finished reading stream
            else
            {
                m_StreamFinished = true;
                break;
            }
        }
    }

    // manage source state
    {
        int processed;
        alGetSourcei( m_Source, AL_BUFFERS_PROCESSED, &processed );

        int queued;
        alGetSourcei( m_Source, AL_BUFFERS_QUEUED, &queued );

        if( m_StreamFinished && processed == queued )
        {
            Stop();
        }
        else
        {
            ALenum source_state;
            alGetSourcei( m_Source, AL_SOURCE_STATE, &source_state );
            if( source_state == AL_STOPPED )
            {
                alSourcePlay( m_Source );
            }
        }
    }
}
示例#23
0
文件: openal.c 项目: neil4/RetroArch
static bool al_unqueue_buffers(al_t *al)
{
   ALint val;

   alGetSourcei(al->source, AL_BUFFERS_PROCESSED, &val);

   if (val <= 0)
      return false;

   alSourceUnqueueBuffers(al->source, val, &al->res_buf[al->res_ptr]);
   al->res_ptr += val;
   return true;
}
示例#24
0
void TStreamBuffer::end(ALuint source)
{
    int count = 0;
    ALuint buffer;
    
    alGetSourcei(source, AL_BUFFERS_QUEUED, &count);
    for(; count > 0; --count)
        alSourceUnqueueBuffers(source, 1, &buffer);
    
    is_reading = false;
    ov_pcm_seek(&_v_file, 0);
    return;
}
示例#25
0
void AudioPlayer::freebuffers()
{
	// free all queued buffers
	ALint queued;
	alGetSourcei(source_, AL_BUFFERS_QUEUED, &queued);
	for (int i = 0; i < queued; ++i)
	{
		ALuint buffer;
		alSourceUnqueueBuffers(source_, 1, &buffer);
		positions_.pop_front();
		alDeleteBuffers(1, &buffer);
	}
}
示例#26
0
文件: OpenAL.cpp 项目: hallor/ufo
ALuint OpenAL::PopBufferQueue(ALuint source)
{
    if(!GetProcessedBuffersCount(source))
        return 0;
    
    GetLastError();

    ALuint buffer = 0;

    alSourceUnqueueBuffers(source, 1, &buffer);

    return GetLastError() == AL_NO_ERROR ? buffer : 0;
};
 AUDIO_BUFFER_HANDLER OpenALAudioContext::SourceDequeueBuffer( AUDIO_SOURCE_HANDLER source )
 {
     OpenALSource* pSource = (OpenALSource*)source;
     if ( pSource->IsEmpty() )
     {
         return NULL;
     }
     OpenALBuffer* pBuffer = pSource->FirstBuffer();
     UINT32 bufferID = pBuffer->GetID();
     alSourceUnqueueBuffers( pSource->GetID(), 1, &bufferID );
     pSource->DequeueBuffer();
     return pBuffer;
 }
示例#28
0
void
StreamSoundSource::update()
{
  ALint processed = 0;
  alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
  for(ALint i = 0; i < processed; ++i) {
    ALuint buffer;
    alSourceUnqueueBuffers(source, 1, &buffer);
    try
    {
      SoundManager::check_al_error("Couldn't unqueue audio buffer: ");
    }
    catch(std::exception& e)
    {
      log_warning << e.what() << std::endl;
    }

    if(fillBufferAndQueue(buffer) == false)
      break;
  }

  if(!playing()) {
    if(processed == 0 || !looping)
      return;

    // we might have to restart the source if we had a buffer underrun
    log_info << "Restarting audio source because of buffer underrun" << std::endl;
    play();
  }

  if(fade_state == FadingOn || fade_state == FadingResume) {
    float time = real_time - fade_start_time;
    if(time >= fade_time) {
      set_gain(1.0);
      fade_state = NoFading;
    } else {
      set_gain(time / fade_time);
    }
  } else if(fade_state == FadingOff || fade_state == FadingPause) {
    float time = real_time - fade_start_time;
    if(time >= fade_time) {
      if(fade_state == FadingOff)
        stop();
      else
        pause();
      fade_state = NoFading;
    } else {
      set_gain( (fade_time-time) / fade_time);
    }
  }
}
示例#29
0
	//------------------------------------------------------------------------------
	bool CGUIMusicData_openal::Update()
	{
		// Request the number of OpenAL Buffers have been processed (played) on the Source
		ALint iBuffersProcessed = 0;
		alGetSourcei(m_nSourceId, AL_BUFFERS_PROCESSED, &iBuffersProcessed);

		// For each processed buffer, remove it from the Source Queue, read next chunk of audio
		// data from disk, fill buffer with new data, and add it to the Source Queue
		while (iBuffersProcessed)
		{
			// Remove the Buffer from the Queue.  (uiBuffer contains the Buffer ID for the unqueued Buffer)
			ALuint uiBuffer = 0;
			alSourceUnqueueBuffers(m_nSourceId, 1, &uiBuffer);

			// Read more audio data (if there is any)
			unsigned long ulBytesWritten = DecodeOggVorbis( &m_aVorbisFile, m_pDecodeBuffer, m_nBufferSize, m_nChannels );
			if (ulBytesWritten)
			{
				alBufferData(uiBuffer, m_nFormat, m_pDecodeBuffer, ulBytesWritten, m_nFrequency);
				alSourceQueueBuffers(m_nSourceId, 1, &uiBuffer);
			}

			iBuffersProcessed--;
		}

		// Check the status of the Source.  If it is not playing, then playback was completed,
		// or the Source was starved of audio data, and needs to be restarted.
		ALint iState = 0;
		alGetSourcei(m_nSourceId, AL_SOURCE_STATE, &iState);
		if (iState == AL_PAUSED)
		{
		}
		else if (iState != AL_PLAYING)
		{
			// If there are Buffers in the Source Queue then the Source was starved of audio
			// data, so needs to be restarted (because there is more audio data to play)
			ALint iQueuedBuffers = 0;
			alGetSourcei(m_nSourceId, AL_BUFFERS_QUEUED, &iQueuedBuffers);
			if (iQueuedBuffers)
			{
				alSourcePlay(m_nSourceId);
			}
			else
			{
				// Finished playing
				return false;
			}
		}

		return true;
	}
示例#30
0
void StreamSoundResource::empty(ALuint source)
{
	int queued;

	alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);

	while(queued--)
	{
		ALuint buffer;

		alSourceUnqueueBuffers(source, 1, &buffer);
		check();
	}
}