コード例 #1
0
ファイル: Music.cpp プロジェクト: BigFax/NazaraEngine
void NzMusic::MusicThread()
{
	ALuint buffers[NAZARA_AUDIO_STREAMED_BUFFER_COUNT];
	alGenBuffers(NAZARA_AUDIO_STREAMED_BUFFER_COUNT, buffers);

	for (unsigned int i = 0; i < NAZARA_AUDIO_STREAMED_BUFFER_COUNT; ++i)
	{
		if (FillAndQueueBuffer(buffers[i])) // Fin du fichier ?
			break; // Nous avons atteint la fin du fichier, inutile de rajouter des buffers
	}

	alSourcePlay(m_source);

	while (m_impl->streaming)
	{
		nzSoundStatus status = GetInternalStatus();
		if (status == nzSoundStatus_Stopped)
		{
			m_impl->streaming = false;
			break;
		}

		ALint processedCount = 0;
		alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);

		ALuint buffer;
		while (processedCount--)
		{
			alSourceUnqueueBuffers(m_source, 1, &buffer);
			if (FillAndQueueBuffer(buffer))
				break;
		}

		NzThread::Sleep(50);
	}

	alSourceStop(m_source);

	ALint queuedBufferCount;
	alGetSourcei(m_source, AL_BUFFERS_QUEUED, &queuedBufferCount);

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

	alDeleteBuffers(NAZARA_AUDIO_STREAMED_BUFFER_COUNT, buffers);
}
コード例 #2
0
	void Music::MusicThread()
	{
		// Allocation of streaming buffers
		ALuint buffers[NAZARA_AUDIO_STREAMED_BUFFER_COUNT];
		alGenBuffers(NAZARA_AUDIO_STREAMED_BUFFER_COUNT, buffers);

		for (unsigned int buffer : buffers)
		{
			if (FillAndQueueBuffer(buffer))
				break; // We have reached the end of the stream, there is no use to add new buffers
		}

		alSourcePlay(m_source);

		// Reading loop (Filling new buffers as playing)
		while (m_impl->streaming)
		{
			// The reading has stopped, we have reached the end of the stream
			SoundStatus status = GetInternalStatus();
			if (status == SoundStatus_Stopped)
			{
				m_impl->streaming = false;
				break;
			}

			Nz::LockGuard lock(m_impl->bufferLock);

			// We treat read buffers
			ALint processedCount = 0;
			alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);
			while (processedCount--)
			{
				ALuint buffer;
				alSourceUnqueueBuffers(m_source, 1, &buffer);

				ALint bits, size;
				alGetBufferi(buffer, AL_BITS, &bits);
				alGetBufferi(buffer, AL_SIZE, &size);

				if (bits != 0)
					m_impl->processedSamples += (8 * size) / bits;

				if (FillAndQueueBuffer(buffer))
					break;
			}

			lock.Unlock();

			// We go back to sleep
			Thread::Sleep(50);
		}

		// Stop playing of the sound (in the case where it has not been already done)
		alSourceStop(m_source);

		// We delete buffers from the stream
		ALint queuedBufferCount;
		alGetSourcei(m_source, AL_BUFFERS_QUEUED, &queuedBufferCount);

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

		alDeleteBuffers(NAZARA_AUDIO_STREAMED_BUFFER_COUNT, buffers);
	}
コード例 #3
0
	void Music::MusicThread()
	{
		// Allocation des buffers de streaming
		ALuint buffers[NAZARA_AUDIO_STREAMED_BUFFER_COUNT];
		alGenBuffers(NAZARA_AUDIO_STREAMED_BUFFER_COUNT, buffers);

		for (unsigned int i = 0; i < NAZARA_AUDIO_STREAMED_BUFFER_COUNT; ++i)
		{
			if (FillAndQueueBuffer(buffers[i]))
				break; // Nous avons atteint la fin du stream, inutile de rajouter des buffers
		}

		alSourcePlay(m_source);

		// Boucle de lecture (remplissage de nouveaux buffers au fur et à mesure)
		while (m_impl->streaming)
		{
			// La lecture s'est arrêtée, nous avons atteint la fin du stream
			SoundStatus status = GetInternalStatus();
			if (status == SoundStatus_Stopped)
			{
				m_impl->streaming = false;
				break;
			}

			Nz::LockGuard lock(m_impl->bufferLock);

			// On traite les buffers lus
			ALint processedCount = 0;
			alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);
			while (processedCount--)
			{
				ALuint buffer;
				alSourceUnqueueBuffers(m_source, 1, &buffer);
				
				ALint bits, size;
				alGetBufferi(buffer, AL_BITS, &bits);
				alGetBufferi(buffer, AL_SIZE, &size);
				
				if (bits != 0)
					m_impl->processedSamples += (8 * size) / bits;

				if (FillAndQueueBuffer(buffer))
					break;
			}

			lock.Unlock();

			// On retourne dormir un peu
			Thread::Sleep(50);
		}

		// Arrêt de la lecture du son (dans le cas où ça ne serait pas déjà fait)
		alSourceStop(m_source);

		// On supprime les buffers du stream
		ALint queuedBufferCount;
		alGetSourcei(m_source, AL_BUFFERS_QUEUED, &queuedBufferCount);

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

		alDeleteBuffers(NAZARA_AUDIO_STREAMED_BUFFER_COUNT, buffers);
	}