diff options
Diffstat (limited to 'extern/audaspace/src/util')
-rw-r--r-- | extern/audaspace/src/util/Barrier.cpp | 42 | ||||
-rw-r--r-- | extern/audaspace/src/util/Buffer.cpp | 72 | ||||
-rw-r--r-- | extern/audaspace/src/util/BufferReader.cpp | 80 | ||||
-rw-r--r-- | extern/audaspace/src/util/FFTPlan.cpp | 66 | ||||
-rw-r--r-- | extern/audaspace/src/util/StreamBuffer.cpp | 83 | ||||
-rw-r--r-- | extern/audaspace/src/util/ThreadPool.cpp | 60 |
6 files changed, 403 insertions, 0 deletions
diff --git a/extern/audaspace/src/util/Barrier.cpp b/extern/audaspace/src/util/Barrier.cpp new file mode 100644 index 00000000000..3347a38092b --- /dev/null +++ b/extern/audaspace/src/util/Barrier.cpp @@ -0,0 +1,42 @@ +/******************************************************************************* +* Copyright 2015-2016 Juan Francisco Crespo Galán +* +* 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 "util/Barrier.h" + +AUD_NAMESPACE_BEGIN +Barrier::Barrier(unsigned int count) : + m_threshold(count), m_count(count), m_generation(0) +{ +} + +Barrier::~Barrier() +{ +} + +void Barrier::wait() +{ + std::unique_lock<std::mutex> lck(m_mutex); + int gen = m_generation; + if(!--m_count) + { + m_count = m_threshold; + m_generation++; + m_condition.notify_all(); + } + else + m_condition.wait(lck, [this, gen] { return gen != m_generation; }); +} +AUD_NAMESPACE_END
\ No newline at end of file diff --git a/extern/audaspace/src/util/Buffer.cpp b/extern/audaspace/src/util/Buffer.cpp new file mode 100644 index 00000000000..d212278cacc --- /dev/null +++ b/extern/audaspace/src/util/Buffer.cpp @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * 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 "util/Buffer.h" + +#include <algorithm> +#include <cstring> +#include <cstdlib> + +#define ALIGNMENT 32 +#define ALIGN(a) (a + ALIGNMENT - ((long long)a & (ALIGNMENT-1))) + +AUD_NAMESPACE_BEGIN + +Buffer::Buffer(int size) +{ + m_size = size; + m_buffer = (data_t*) std::malloc(size + ALIGNMENT); +} + +Buffer::~Buffer() +{ + std::free(m_buffer); +} + +sample_t* Buffer::getBuffer() const +{ + return (sample_t*) ALIGN(m_buffer); +} + +int Buffer::getSize() const +{ + return m_size; +} + +void Buffer::resize(int size, bool keep) +{ + if(keep) + { + data_t* buffer = (data_t*) std::malloc(size + ALIGNMENT); + + std::memcpy(ALIGN(buffer), ALIGN(m_buffer), std::min(size, m_size)); + + std::free(m_buffer); + m_buffer = buffer; + } + else + m_buffer = (data_t*) std::realloc(m_buffer, size + ALIGNMENT); + + m_size = size; +} + +void Buffer::assureSize(int size, bool keep) +{ + if(m_size < size) + resize(size, keep); +} + +AUD_NAMESPACE_END diff --git a/extern/audaspace/src/util/BufferReader.cpp b/extern/audaspace/src/util/BufferReader.cpp new file mode 100644 index 00000000000..37744420ab8 --- /dev/null +++ b/extern/audaspace/src/util/BufferReader.cpp @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * 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 "util/BufferReader.h" +#include "util/Buffer.h" + +#include <cstring> + +AUD_NAMESPACE_BEGIN + +BufferReader::BufferReader(std::shared_ptr<Buffer> buffer, + Specs specs) : + m_position(0), m_buffer(buffer), m_specs(specs) +{ +} + +bool BufferReader::isSeekable() const +{ + return true; +} + +void BufferReader::seek(int position) +{ + m_position = position; +} + +int BufferReader::getLength() const +{ + return m_buffer->getSize() / AUD_SAMPLE_SIZE(m_specs); +} + +int BufferReader::getPosition() const +{ + return m_position; +} + +Specs BufferReader::getSpecs() const +{ + return m_specs; +} + +void BufferReader::read(int& length, bool& eos, sample_t* buffer) +{ + eos = false; + + int sample_size = AUD_SAMPLE_SIZE(m_specs); + + sample_t* buf = m_buffer->getBuffer() + m_position * m_specs.channels; + + // in case the end of the buffer is reached + if(m_buffer->getSize() < (m_position + length) * sample_size) + { + length = m_buffer->getSize() / sample_size - m_position; + eos = true; + } + + if(length < 0) + { + length = 0; + return; + } + + m_position += length; + std::memcpy(buffer, buf, length * sample_size); +} + +AUD_NAMESPACE_END diff --git a/extern/audaspace/src/util/FFTPlan.cpp b/extern/audaspace/src/util/FFTPlan.cpp new file mode 100644 index 00000000000..5e99dbff247 --- /dev/null +++ b/extern/audaspace/src/util/FFTPlan.cpp @@ -0,0 +1,66 @@ +/******************************************************************************* +* Copyright 2015-2016 Juan Francisco Crespo Galán +* +* 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 "util/FFTPlan.h" + +AUD_NAMESPACE_BEGIN +FFTPlan::FFTPlan(double measureTime) : + FFTPlan(DEFAULT_N, measureTime) +{ +} + +FFTPlan::FFTPlan(int n, double measureTime) : + m_N(n), m_bufferSize(((n/2)+1)*2*sizeof(fftwf_complex)) +{ + fftwf_set_timelimit(measureTime); + void* buf = fftwf_malloc(m_bufferSize); + m_fftPlanR2C = fftwf_plan_dft_r2c_1d(m_N, (float*)buf, (fftwf_complex*)buf, FFTW_EXHAUSTIVE); + m_fftPlanC2R = fftwf_plan_dft_c2r_1d(m_N, (fftwf_complex*)buf, (float*)buf, FFTW_EXHAUSTIVE); + fftwf_free(buf); +} + +FFTPlan::~FFTPlan() +{ + fftwf_destroy_plan(m_fftPlanC2R); + fftwf_destroy_plan(m_fftPlanR2C); +} + +int FFTPlan::getSize() +{ + return m_N; +} + +void FFTPlan::FFT(void* buffer) +{ + fftwf_execute_dft_r2c(m_fftPlanR2C, (float*)buffer, (fftwf_complex*)buffer); +} + +void FFTPlan::IFFT(void* buffer) +{ + fftwf_execute_dft_c2r(m_fftPlanC2R, (fftwf_complex*)buffer, (float*)buffer); +} + +void* FFTPlan::getBuffer() +{ + return fftwf_malloc(m_bufferSize); +} + +void FFTPlan::freeBuffer(void* buffer) +{ + fftwf_free(buffer); +} + +AUD_NAMESPACE_END diff --git a/extern/audaspace/src/util/StreamBuffer.cpp b/extern/audaspace/src/util/StreamBuffer.cpp new file mode 100644 index 00000000000..b87b363377d --- /dev/null +++ b/extern/audaspace/src/util/StreamBuffer.cpp @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * 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 "util/StreamBuffer.h" +#include "util/BufferReader.h" +#include "util/Buffer.h" + +// 5 sec * 48000 samples/sec * 4 bytes/sample * 6 channels +#define BUFFER_RESIZE_BYTES 5760000 + +AUD_NAMESPACE_BEGIN + +StreamBuffer::StreamBuffer(std::shared_ptr<ISound> sound) : + m_buffer(new Buffer()) +{ + std::shared_ptr<IReader> reader = sound->createReader(); + + m_specs = reader->getSpecs(); + + int sample_size = AUD_SAMPLE_SIZE(m_specs); + int length; + int index = 0; + bool eos = false; + + // get an approximated size if possible + int size = reader->getLength(); + + if(size <= 0) + size = BUFFER_RESIZE_BYTES / sample_size; + else + size += m_specs.rate; + + // as long as the end of the stream is not reached + while(!eos) + { + // increase + m_buffer->resize(size*sample_size, true); + + // read more + length = size-index; + reader->read(length, eos, m_buffer->getBuffer() + index * m_specs.channels); + if(index == m_buffer->getSize() / sample_size) + size += BUFFER_RESIZE_BYTES / sample_size; + index += length; + } + + m_buffer->resize(index * sample_size, true); +} + +StreamBuffer::StreamBuffer(std::shared_ptr<Buffer> buffer, Specs specs) : + m_buffer(buffer), m_specs(specs) +{ +} + +std::shared_ptr<Buffer> StreamBuffer::getBuffer() +{ + return m_buffer; +} + +Specs StreamBuffer::getSpecs() +{ + return m_specs; +} + +std::shared_ptr<IReader> StreamBuffer::createReader() +{ + return std::shared_ptr<IReader>(new BufferReader(m_buffer, m_specs)); +} + +AUD_NAMESPACE_END diff --git a/extern/audaspace/src/util/ThreadPool.cpp b/extern/audaspace/src/util/ThreadPool.cpp new file mode 100644 index 00000000000..74051902c3b --- /dev/null +++ b/extern/audaspace/src/util/ThreadPool.cpp @@ -0,0 +1,60 @@ +/******************************************************************************* +* Copyright 2015-2016 Juan Francisco Crespo Galán +* +* 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 "util/ThreadPool.h" + +AUD_NAMESPACE_BEGIN + +ThreadPool::ThreadPool(unsigned int count) : + m_stopFlag(false), m_numThreads(count) +{ + for(unsigned int i = 0; i < count; i++) + m_threads.emplace_back(&ThreadPool::threadFunction, this); +} + +ThreadPool::~ThreadPool() +{ + m_mutex.lock(); + m_stopFlag = true; + m_mutex.unlock(); + m_condition.notify_all(); + for(unsigned int i = 0; i < m_threads.size(); i++) + m_threads[i].join(); +} + +unsigned int ThreadPool::getNumOfThreads() +{ + return m_numThreads; +} + +void ThreadPool::threadFunction() +{ + while(true) + { + std::function<void()> task; + { + std::unique_lock<std::mutex> lock(m_mutex); + m_condition.wait(lock, [this] { return m_stopFlag || !m_queue.empty(); }); + if(m_stopFlag && m_queue.empty()) + return; + task = std::move(m_queue.front()); + this->m_queue.pop(); + } + task(); + } +} + +AUD_NAMESPACE_END |