bool OpenALBackend::AddData(const void* src, u32 num_samples) { AUDIT(alIsSource(m_source)); // Unqueue processed buffers, if any unqueue_processed(); // Fail if there are no free buffers remaining if (m_num_unqueued == 0) { LOG_WARNING(GENERAL, "XAudio2Backend : no unqueued buffers remaining"); return false; } // Copy data to the next available buffer alBufferData(m_buffers[m_next_buffer], m_format, src, num_samples * m_sample_size, m_sampling_rate); checkForAlError("AddData->alBufferData"); // Enqueue buffer alSourceQueueBuffers(m_source, 1, &m_buffers[m_next_buffer]); checkForAlError("AddData->alSourceQueueBuffers"); m_num_unqueued--; m_next_buffer = (m_next_buffer + 1) % m_num_buffers; return true; }
void OpenALThread::AddData(const void* src, ALsizei size) { const char* bsrc = (const char*)src; ALuint buffer; ALint buffers_count; alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &buffers_count); checkForAlError("alGetSourcei"); while(size) { if(buffers_count-- <= 0) { Play(); alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &buffers_count); checkForAlError("alGetSourcei"); continue; } alSourceUnqueueBuffers(m_source, 1, &buffer); checkForAlError("alSourceUnqueueBuffers"); int bsize = size < m_buffer_size ? size : m_buffer_size; AddBlock(buffer, bsize, bsrc); alSourceQueueBuffers(m_source, 1, &buffer); checkForAlError("alSourceQueueBuffers"); size -= bsize; bsrc += bsize; } Play(); }
void OpenALThread::Close() { alSourceStop(m_source); checkForAlError("alSourceStop"); if (alIsSource(m_source)) alDeleteSources(1, &m_source); alDeleteBuffers(g_al_buffers_count, m_buffers); checkForAlError("alDeleteBuffers"); }
void OpenALThread::Play() { ALint state; alGetSourcei(m_source, AL_SOURCE_STATE, &state); checkForAlError("OpenALThread::Play -> alGetSourcei"); if (state != AL_PLAYING) { alSourcePlay(m_source); checkForAlError("alSourcePlay"); } }
void OpenALBackend::Play() { AUDIT(alIsSource(m_source)); ALint state; alGetSourcei(m_source, AL_SOURCE_STATE, &state); checkForAlError("Play->alGetSourcei(AL_SOURCE_STATE)"); if (state != AL_PLAYING) { alSourcePlay(m_source); checkForAlError("Play->alSourcePlay"); } }
void OpenALBackend::Pause() { AUDIT(alIsSource(m_source)); alSourcePause(m_source); checkForAlError("Pause->alSourcePause"); }
void OpenALBackend::Open(u32 num_buffers) { AUDIT(!alIsSource(m_source)); // Initialize Source alGenSources(1, &m_source); checkForAlError("Open->alGenSources"); alSourcei(m_source, AL_LOOPING, AL_FALSE); checkForAlError("Open->alSourcei"); // Initialize Buffers alGenBuffers(num_buffers, m_buffers); checkForAlError("Open->alGenBuffers"); m_num_buffers = num_buffers; m_num_unqueued = num_buffers; }
bool OpenALThread::AddBlock(const ALuint buffer_id, const ALsizei size, const void* src) { if (size < 1) return false; alBufferData(buffer_id, g_audio_format, src, size, 48000); checkForAlError("alBufferData"); return true; }
bool OpenALThread::AddBlock(const ALuint buffer_id, ALsizei size, const void* src) { if (size < 1) return false; alBufferData(buffer_id, AL_FORMAT_STEREO16, src, size, 48000); checkForAlError("alBufferData"); return true; }
f32 OpenALBackend::SetFrequencyRatio(f32 new_ratio) { new_ratio = std::clamp(new_ratio, 0.5f, 2.0f); alSourcef(m_source, AL_PITCH, new_ratio); checkForAlError("SetFrequencyRatio->alSourcei(AL_PITCH)"); return new_ratio; }
u64 OpenALBackend::GetNumEnqueuedSamples() { AUDIT(alIsSource(m_source)); // Get number of buffers queued ALint num_queued; alGetSourcei(m_source, AL_BUFFERS_QUEUED, &num_queued); checkForAlError("GetNumEnqueuedSamples->alGetSourcei(AL_BUFFERS_QUEUED)"); AUDIT(static_cast<u32>(num_queued) <= m_num_buffers - m_num_unqueued); // Get sample position ALint sample_pos; alGetSourcei(m_source, AL_SAMPLE_OFFSET, &sample_pos); checkForAlError("GetNumEnqueuedSamples->alGetSourcei(AL_SAMPLE_OFFSET)"); // Return return (num_queued * AUDIO_BUFFER_SAMPLES) + (sample_pos % AUDIO_BUFFER_SAMPLES); }
bool OpenALBackend::IsPlaying() { AUDIT(alIsSource(m_source)); ALint state; alGetSourcei(m_source, AL_SOURCE_STATE, &state); checkForAlError("IsPlaying->alGetSourcei(AL_SOURCE_STATE)"); return state == AL_PLAYING; }
void OpenALBackend::unqueue_processed() { AUDIT(alIsSource(m_source)); // Get number of buffers ALint num_processed; alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &num_processed); checkForAlError("Flush->alGetSourcei(AL_BUFFERS_PROCESSED)"); if (num_processed > 0) { // Unqueue all buffers ALuint x[MAX_AUDIO_BUFFERS]; alSourceUnqueueBuffers(m_source, num_processed, x); checkForAlError("Flush->alSourceUnqueueBuffers"); m_num_unqueued += num_processed; } }
void OpenALBackend::Flush() { AUDIT(alIsSource(m_source)); // Stop source first alSourceStop(m_source); checkForAlError("Flush->alSourceStop"); // Unqueue processed buffers (should now be all of them) unqueue_processed(); }
void OpenALBackend::Close() { if (alIsSource(m_source)) { // Stop & Kill Source Pause(); alDeleteSources(1, &m_source); // Free Buffers alDeleteBuffers(m_num_buffers, m_buffers); checkForAlError("alDeleteBuffers"); } }
void OpenALThread::Open(const void* src, ALsizei size) { alGenSources(1, &m_source); checkForAlError("alGenSources"); alGenBuffers(g_al_buffers_count, m_buffers); checkForAlError("alGenBuffers"); alSourcei(m_source, AL_LOOPING, AL_FALSE); checkForAlError("OpenALThread::Open ->alSourcei"); m_buffer_size = size; for(uint i=0; i<g_al_buffers_count; ++i) { AddBlock(m_buffers[i], m_buffer_size, src); } alSourceQueueBuffers(m_source, g_al_buffers_count, m_buffers); checkForAlError("alSourceQueueBuffers"); Play(); }
void OpenALThread::AddData(const void* src, int size) { const char* bsrc = (const char*)src; ALuint buffer; ALint buffers_count; alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &buffers_count); checkForAlError("OpenALThread::AddData -> alGetSourcei"); while (size) { if (buffers_count-- <= 0) { Play(); alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &buffers_count); checkForAlError("OpenALThread::AddData(in loop) -> alGetSourcei"); continue; } alSourceUnqueueBuffers(m_source, 1, &buffer); checkForAlError("alSourceUnqueueBuffers"); int bsize = size < m_buffer_size ? size : m_buffer_size; alBufferData(buffer, Ini.AudioConvertToU16.GetValue() ? AL_FORMAT_71CHN16 : AL_FORMAT_71CHN32, bsrc, bsize, 48000); checkForAlError("alBufferData"); alSourceQueueBuffers(m_source, 1, &buffer); checkForAlError("alSourceQueueBuffers"); size -= bsize; bsrc += bsize; } Play(); }
void OpenALThread::Open(const void* src, int size) { alGenSources(1, &m_source); checkForAlError("alGenSources"); alGenBuffers(g_al_buffers_count, m_buffers); checkForAlError("alGenBuffers"); alSourcei(m_source, AL_LOOPING, AL_FALSE); checkForAlError("OpenALThread::Open ->alSourcei"); m_buffer_size = size; for (uint i = 0; i<g_al_buffers_count; ++i) { alBufferData(m_buffers[i], Ini.AudioConvertToU16.GetValue() ? AL_FORMAT_71CHN16 : AL_FORMAT_71CHN32, src, m_buffer_size, 48000); checkForAlError("alBufferData"); } alSourceQueueBuffers(m_source, g_al_buffers_count, m_buffers); checkForAlError("alSourceQueueBuffers"); Play(); }
void OpenALThread::Stop() { alSourceStop(m_source); checkForAlError("alSourceStop"); }