diff options
Diffstat (limited to 'extern/audaspace/include/util')
-rw-r--r-- | extern/audaspace/include/util/Barrier.h | 78 | ||||
-rw-r--r-- | extern/audaspace/include/util/Buffer.h | 87 | ||||
-rw-r--r-- | extern/audaspace/include/util/BufferReader.h | 76 | ||||
-rw-r--r-- | extern/audaspace/include/util/FFTPlan.h | 120 | ||||
-rw-r--r-- | extern/audaspace/include/util/ILockable.h | 46 | ||||
-rw-r--r-- | extern/audaspace/include/util/Math3D.h | 324 | ||||
-rw-r--r-- | extern/audaspace/include/util/StreamBuffer.h | 85 | ||||
-rw-r--r-- | extern/audaspace/include/util/ThreadPool.h | 119 |
8 files changed, 935 insertions, 0 deletions
diff --git a/extern/audaspace/include/util/Barrier.h b/extern/audaspace/include/util/Barrier.h new file mode 100644 index 00000000000..d9e8bf07f82 --- /dev/null +++ b/extern/audaspace/include/util/Barrier.h @@ -0,0 +1,78 @@ +/******************************************************************************* +* 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. +******************************************************************************/ + +#pragma once + +/** +* @file Barrier.h +* @ingroup util +* The Barrier class. +*/ + +#include "Audaspace.h" + +#include <mutex> +#include <condition_variable> + +AUD_NAMESPACE_BEGIN +/** +* This represents a barrier mechanism for thread sychronization. +*/ +class Barrier +{ +private: + /** + * A mutex needed to use a condition variable. + */ + std::mutex m_mutex; + + /** + * Condition varieble used to sync threads. + */ + std::condition_variable m_condition; + + /** + * Number of threads that need to reach the barrier for it to lift. + */ + unsigned int m_threshold; + + /** + * Conter that count from threshold to 0. + */ + unsigned int m_count; + + /** + * Variable used for predicate check in the condition variable wait. + */ + unsigned int m_generation; + + // delete copy constructor and operator= + Barrier(const Barrier&) = delete; + Barrier& operator=(const Barrier&) = delete; +public: + /** + * Creates a new Barrier object. + * \param count the number of threads that need to reach the barrier for it to lift. + */ + Barrier(unsigned int count); + virtual ~Barrier(); + + /** + * Makes the caller thread wait until enough threads are stopped by this method. + */ + void wait(); +}; +AUD_NAMESPACE_END
\ No newline at end of file diff --git a/extern/audaspace/include/util/Buffer.h b/extern/audaspace/include/util/Buffer.h new file mode 100644 index 00000000000..9934e53625e --- /dev/null +++ b/extern/audaspace/include/util/Buffer.h @@ -0,0 +1,87 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ + +#pragma once + +/** + * @file Buffer.h + * @ingroup util + * The Buffer class. + */ + +#include "Audaspace.h" + +AUD_NAMESPACE_BEGIN + +/** + * This class is a simple buffer in RAM which is 32 Byte aligned and provides + * resize functionality. + */ +class AUD_API Buffer +{ +private: + /// The size of the buffer in bytes. + int m_size; + + /// The pointer to the buffer memory. + data_t* m_buffer; + + // delete copy constructor and operator= + Buffer(const Buffer&) = delete; + Buffer& operator=(const Buffer&) = delete; + +public: + /** + * Creates a new buffer. + * \param size The size of the buffer in bytes. + */ + Buffer(int size = 0); + + /** + * Destroys the buffer. + */ + ~Buffer(); + + /** + * Returns the pointer to the buffer in memory. + */ + sample_t* getBuffer() const; + + /** + * Returns the size of the buffer in bytes. + */ + int getSize() const; + + /** + * Resizes the buffer. + * \param size The new size of the buffer, measured in bytes. + * \param keep Whether to keep the old data. If the new buffer is smaller, + * the data at the end will be lost. + */ + void resize(int size, bool keep = false); + + /** + * Makes sure the buffer has a minimum size. + * If size is >= current size, nothing will happen. + * Otherwise the buffer is resized with keep as parameter. + * \param size The new minimum size of the buffer, measured in bytes. + * \param keep Whether to keep the old data. If the new buffer is smaller, + * the data at the end will be lost. + */ + void assureSize(int size, bool keep = false); +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/include/util/BufferReader.h b/extern/audaspace/include/util/BufferReader.h new file mode 100644 index 00000000000..b98313726bc --- /dev/null +++ b/extern/audaspace/include/util/BufferReader.h @@ -0,0 +1,76 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ + +#pragma once + +/** + * @file BufferReader.h + * @ingroup util + * The BufferReader class. + */ + +#include "IReader.h" + +#include <memory> + +AUD_NAMESPACE_BEGIN + +class Buffer; + +/** + * This class represents a simple reader from a buffer that exists in memory. + * \warning Notice that the buffer is not multi-threading ready, so changing the + * buffer while the reader is reading is potentially dangerous. + */ +class AUD_API BufferReader : public IReader +{ +private: + /** + * The current position in the buffer. + */ + int m_position; + + /** + * The buffer that is read. + */ + std::shared_ptr<Buffer> m_buffer; + + /** + * The specification of the sample data in the buffer. + */ + Specs m_specs; + + // delete copy constructor and operator= + BufferReader(const BufferReader&) = delete; + BufferReader& operator=(const BufferReader&) = delete; + +public: + /** + * Creates a new buffer reader. + * \param buffer The buffer to read from. + * \param specs The specification of the sample data in the buffer. + */ + BufferReader(std::shared_ptr<Buffer> buffer, Specs specs); + + virtual bool isSeekable() const; + virtual void seek(int position); + virtual int getLength() const; + virtual int getPosition() const; + virtual Specs getSpecs() const; + virtual void read(int& length, bool& eos, sample_t* buffer); +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/include/util/FFTPlan.h b/extern/audaspace/include/util/FFTPlan.h new file mode 100644 index 00000000000..7d533ca8773 --- /dev/null +++ b/extern/audaspace/include/util/FFTPlan.h @@ -0,0 +1,120 @@ +/******************************************************************************* +* 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. +******************************************************************************/ + +#pragma once + +/** +* @file FFTPlan.h +* @ingroup util +* The FFTPlan class. +*/ + +#include <complex> +#include <fftw3.h> +#include "Audaspace.h" + +#include <memory> +#include <vector> + +/**Default FFT size.*/ +#define DEFAULT_N 4096 + +AUD_NAMESPACE_BEGIN + +/** +* Thas class represents an plan object that allows to calculate FFTs and IFFTs. +*/ +class AUD_API FFTPlan +{ +private: + /** + * The size of the FFT plan. + */ + int m_N; + + /** + * The plan to transform the input to the frequency domain. + */ + fftwf_plan m_fftPlanR2C; + + /** + * The plan to transform the input to the time domain again. + */ + fftwf_plan m_fftPlanC2R; + + /** + * The size of a buffer for its use with the FFT plan (in bytes). + */ + unsigned int m_bufferSize; + + // delete copy constructor and operator= + FFTPlan(const FFTPlan&) = delete; + FFTPlan& operator=(const FFTPlan&) = delete; + +public: + /** + * Creates a new FFTPlan object with DEFAULT_N size (4096). + * \param measureTime The aproximate amount of seconds that FFTW will spend searching for the optimal plan, + * which means faster FFTs and IFFTs while using this plan. If measureTime is negative, it will take all the time it needs. + */ + FFTPlan(double measureTime = 0); + + /** + * Creates a new FFTPlan object with a custom size. + * \param n The size of the FFT plan. Values that are a power of two are faster. + * The useful range usually is between 2048 and 8192, but bigger values can be useful + * in certain situations (when using the StreamBuffer class per example). + * Generally, low values use more CPU power and are a bit faster than large ones, + * there is also a huge decrease in efficiency when n is lower than 2048. + * \param measureTime The aproximate amount of seconds that FFTW will spend searching for the optimal plan, + * which means faster FFTs while using this plan. If measureTime is negative, it will take all the time it needs. + */ + FFTPlan(int n, double measureTime = 0); + ~FFTPlan(); + + /** + * Retrieves the size of the FFT plan. + * \return The size of the plan. + */ + int getSize(); + + /** + * Calculates the FFT of an input buffer with the current plan. + * \param[in,out] buffer A buffer with the input data an in which the output data will be written. + */ + void FFT(void* buffer); + + /** + * Calculates the IFFT of an input buffer with the current plan. + * \param[in,out] buffer A buffer with the input data an in which the output data will be written. + */ + void IFFT(void* buffer); + + /** + * Reserves memory for a buffer that can be used for inplace transformations with this plan. + * \return A pointer to a buffer of size ((N/2)+1)*2*sizeof(fftwf_complex). + * \warning The returned buffer must be freed with the freeBuffer method of this class. + */ + void* getBuffer(); + + /** + * Frees one of the buffers reserved with the getRealOnlyBuffer(), getComplexOnlyBuffer() or getInplaceBuffer() method. + * \param buffer A pointer to the buufer taht must be freed. + */ + void freeBuffer(void* buffer); +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/include/util/ILockable.h b/extern/audaspace/include/util/ILockable.h new file mode 100644 index 00000000000..8300fbd2845 --- /dev/null +++ b/extern/audaspace/include/util/ILockable.h @@ -0,0 +1,46 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ + +#pragma once + +/** + * @file ILockable.h + * @ingroup util + * The ILockable interface. + */ + +#include "Audaspace.h" + +AUD_NAMESPACE_BEGIN + +/** + * @interface ILockable + * This class provides an interface for lockable objects. + */ +class AUD_API ILockable +{ +public: + /** + * Locks the object. + */ + virtual void lock()=0; + /** + * Unlocks the previously locked object. + */ + virtual void unlock()=0; +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/include/util/Math3D.h b/extern/audaspace/include/util/Math3D.h new file mode 100644 index 00000000000..7ded12c1ba8 --- /dev/null +++ b/extern/audaspace/include/util/Math3D.h @@ -0,0 +1,324 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ + +#pragma once + +/** + * @file Math3D.h + * @ingroup util + * Defines the Vector3 and Quaternion classes. + */ + +#include "Audaspace.h" + +#include <cmath> +#include <cstring> + +AUD_NAMESPACE_BEGIN + +/** + * This class represents a 3 dimensional vector. + */ +class AUD_API Vector3 +{ +private: + /** + * The vector components. + */ + union + { + float m_v[3]; + struct + { + float m_x; + float m_y; + float m_z; + }; + }; + +public: + /** + * Creates a new 3 dimensional vector. + * \param x The x component. + * \param y The y component. + * \param z The z component. + */ + inline Vector3(float x = 0, float y = 0, float z = 0) : + m_x(x), m_y(y), m_z(z) + { + } + + /** + * Retrieves the x component of the vector. + * \return The x component. + */ + inline const float& x() const + { + return m_x; + } + + /** + * Retrieves the y component of the vector. + * \return The y component. + */ + inline const float& y() const + { + return m_y; + } + + /** + * Retrieves the z component of the vector. + * \return The z component. + */ + inline const float& z() const + { + return m_z; + } + + /** + * Retrieves the components of the vector. + * \param destination Where the 3 float values should be saved to. + */ + inline void get(float* destination) const + { + std::memcpy(destination, m_v, sizeof(m_v)); + } + + /** + * Retrieves the components of the vector. + * \return The components as float[3]. + */ + inline float* get() + { + return m_v; + } + + /** + * Retrieves the components of the vector. + * \return The components as float[3]. + */ + inline const float* get() const + { + return m_v; + } + + /** + * Retrieves the length of the vector. + * \return The length of the vector. + */ + inline float length() const + { + return std::sqrt(m_x*m_x + m_y*m_y + m_z*m_z); + } + + /** + * Retrieves the cross product. + * \param op The second operand. + * \return The cross product of the two vectors. + */ + inline Vector3 cross(const Vector3& op) const + { + return Vector3(m_y * op.m_z - m_z * op.m_y, + m_z * op.m_x - m_x * op.m_z, + m_x * op.m_y - m_y * op.m_x); + } + + /** + * Retrieves the dot product. + * \param op The second operand. + * \return The dot product of the two vectors. + */ + inline float operator*(const Vector3& op) const + { + return m_x * op.m_x + m_y * op.m_y + m_z * op.m_z; + } + + /** + * Retrieves the product with a scalar. + * \param op The second operand. + * \return The scaled vector. + */ + inline Vector3 operator*(const float& op) const + { + return Vector3(m_x * op, m_y * op, m_z * op); + } + + /** + * Adds two vectors. + * \param op The second operand. + * \return The sum vector. + */ + inline Vector3 operator+(const Vector3& op) const + { + return Vector3(m_x + op.m_x, m_y + op.m_y, m_z + op.m_z); + } + + /** + * Subtracts two vectors. + * \param op The second operand. + * \return The difference vector. + */ + inline Vector3 operator-(const Vector3& op) const + { + return Vector3(m_x - op.m_x, m_y - op.m_y, m_z - op.m_z); + } + + /** + * Negates the vector. + * \return The vector facing in the opposite direction. + */ + inline Vector3 operator-() const + { + return Vector3(-m_x, -m_y, -m_z); + } + + /** + * Subtracts the second vector. + * \param op The second operand. + * \return The difference vector. + */ + inline Vector3& operator-=(const Vector3& op) + { + m_x -= op.m_x; + m_y -= op.m_y; + m_z -= op.m_z; + return *this; + } +}; + +/** + * This class represents a quaternion used for 3D rotations. + */ +class AUD_API Quaternion +{ +private: + /** + * The quaternion components. + */ + union + { + float m_v[4]; + struct + { + float m_w; + float m_x; + float m_y; + float m_z; + }; + }; + +public: + /** + * Creates a new quaternion. + * \param w The w component. + * \param x The x component. + * \param y The y component. + * \param z The z component. + */ + inline Quaternion(float w = 1, float x = 0, float y = 0, float z = 0) : + m_w(w), m_x(x), m_y(y), m_z(z) + { + } + + /** + * Retrieves the w component of the quarternion. + * \return The w component. + */ + inline const float& w() const + { + return m_w; + } + + /** + * Retrieves the x component of the quarternion. + * \return The x component. + */ + inline const float& x() const + { + return m_x; + } + + /** + * Retrieves the y component of the quarternion. + * \return The y component. + */ + inline const float& y() const + { + return m_y; + } + + /** + * Retrieves the z component of the quarternion. + * \return The z component. + */ + inline const float& z() const + { + return m_z; + } + + /** + * Retrieves the components of the vector. + * \param destination Where the 4 float values should be saved to. + */ + inline void get(float* destination) const + { + std::memcpy(destination, m_v, sizeof(m_v)); + } + + /** + * Retrieves the components of the vector. + * \return The components as float[4]. + */ + inline float* get() + { + return m_v; + } + + /** + * Retrieves the components of the vector. + * \return The components as float[4]. + */ + inline const float* get() const + { + return m_v; + } + + /** + * When the quaternion represents an orientation, this returns the negative + * z axis vector. + * \return The negative z axis vector. + */ + inline Vector3 getLookAt() const + { + return Vector3(-2 * (m_w * m_y + m_x * m_z), + 2 * (m_x * m_w - m_z * m_y), + 2 * (m_x * m_x + m_y * m_y) - 1); + } + + /** + * When the quaternion represents an orientation, this returns the y axis + * vector. + * \return The y axis vector. + */ + inline Vector3 getUp() const + { + return Vector3(2 * (m_x * m_y - m_w * m_z), + 1 - 2 * (m_x * m_x + m_z * m_z), + 2 * (m_w * m_x + m_y * m_z)); + } +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/include/util/StreamBuffer.h b/extern/audaspace/include/util/StreamBuffer.h new file mode 100644 index 00000000000..8ec4cfcf46d --- /dev/null +++ b/extern/audaspace/include/util/StreamBuffer.h @@ -0,0 +1,85 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ + +#pragma once + +/** + * @file StreamBuffer.h + * @ingroup util + * The StreamBuffer class. + */ + +#include "ISound.h" +#include "respec/Specification.h" + +AUD_NAMESPACE_BEGIN + +class Buffer; + +/** + * This sound creates a buffer out of a reader. This way normally streamed + * sound sources can be loaded into memory for buffered playback. + */ +class AUD_API StreamBuffer : public ISound +{ +private: + /** + * The buffer that holds the audio data. + */ + std::shared_ptr<Buffer> m_buffer; + + /** + * The specification of the samples. + */ + Specs m_specs; + + // delete copy constructor and operator= + StreamBuffer(const StreamBuffer&) = delete; + StreamBuffer& operator=(const StreamBuffer&) = delete; + +public: + /** + * Creates the sound and reads the reader created by the sound supplied + * to the buffer. + * \param sound The sound that creates the reader for buffering. + * \exception Exception Thrown if the reader cannot be created. + */ + StreamBuffer(std::shared_ptr<ISound> sound); + + /** + * Creates the sound from an preexisting buffer. + * \param buffer The buffer to stream from. + * \param specs The specification of the data in the buffer. + * \exception Exception Thrown if the reader cannot be created. + */ + StreamBuffer(std::shared_ptr<Buffer> buffer, Specs specs); + + /** + * Returns the buffer to be streamed. + * @return The buffer to stream. + */ + std::shared_ptr<Buffer> getBuffer(); + + /** + * Returns the specification of the buffer. + * @return The specification of the buffer. + */ + Specs getSpecs(); + + virtual std::shared_ptr<IReader> createReader(); +}; + +AUD_NAMESPACE_END diff --git a/extern/audaspace/include/util/ThreadPool.h b/extern/audaspace/include/util/ThreadPool.h new file mode 100644 index 00000000000..24ec089d52c --- /dev/null +++ b/extern/audaspace/include/util/ThreadPool.h @@ -0,0 +1,119 @@ +/******************************************************************************* +* 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. +******************************************************************************/ + +#pragma once + +/** +* @file ThreadPool.h +* @ingroup util +* The ThreadPool class. +*/ + +#include "Audaspace.h" + +#include <mutex> +#include <condition_variable> +#include <vector> +#include <thread> +#include <queue> +#include <future> +#include <functional> + +AUD_NAMESPACE_BEGIN +/** +* This represents pool of threads. +*/ +class AUD_API ThreadPool +{ +private: + /** + * A queue of tasks. + */ + std::queue<std::function<void()>> m_queue; + + /** + * A vector of thread objects. + */ + std::vector<std::thread> m_threads; + + /** + * A mutex for synchronization. + */ + std::mutex m_mutex; + + /** + * A condition variable used to stop the threads when there are no tasks. + */ + std::condition_variable m_condition; + + /** + * Stop flag. + */ + bool m_stopFlag; + + /** + * The number fo threads. + */ + unsigned int m_numThreads; + + // delete copy constructor and operator= + ThreadPool(const ThreadPool&) = delete; + ThreadPool& operator=(const ThreadPool&) = delete; +public: + /** + * Creates a new ThreadPool object. + * \param count The number of threads of the pool. It must not be 0. + */ + ThreadPool(unsigned int count); + + virtual ~ThreadPool(); + + /** + * Enqueues a new task for the threads to realize. + * \param t A function that realices a task. + * \param args The arguments of the task. + * \return A future of the same type as the return type of the task. + */ + template<class T, class... Args> + std::future<typename std::result_of<T(Args...)>::type> enqueue(T&& t, Args&&... args) + { + using pkgdTask = std::packaged_task<typename std::result_of<T(Args...)>::type()>; + + std::shared_ptr<pkgdTask> task = std::make_shared<pkgdTask>(std::bind(std::forward<T>(t), std::forward<Args>(args)...)); + auto result = task->get_future(); + + m_mutex.lock(); + m_queue.emplace([task]() { (*task)(); }); + m_mutex.unlock(); + + m_condition.notify_one(); + return result; + } + + /** + * Retrieves the number of threads of the pool. + * \return The number of threads. + */ + unsigned int getNumOfThreads(); + +private: + + /** + * Worker thread function. + */ + void threadFunction(); +}; +AUD_NAMESPACE_END |