diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBuffer.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBuffer.cpp deleted file mode 100644 index af4df31dd8..0000000000 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBuffer.cpp +++ /dev/null @@ -1,184 +0,0 @@ -/* Time Sample Buffer - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_CommonFramework_AudioPipeline_TimeSampleBuffer_TPP -#define PokemonAutomation_CommonFramework_AudioPipeline_TimeSampleBuffer_TPP - -#include "Common/Cpp/Exceptions.h" -#include "TimeSampleWriter.h" -#include "TimeSampleBuffer.h" - -namespace PokemonAutomation{ - - -template -TimeSampleBuffer::TimeSampleBuffer( - size_t samples_per_second, - Duration history, - Duration gap_threshold -) - : m_samples_per_second(samples_per_second) - , m_sample_period(Duration(std::chrono::seconds(1)) / samples_per_second) - , m_samples_to_buffer(samples_per_second * std::chrono::duration_cast(history).count() / 1000) - , m_duration_gap_threshold(gap_threshold) - , m_sample_gap_threshold(samples_per_second * std::chrono::duration_cast(gap_threshold).count() / 1000) - , m_samples_stored(0) -{ - if (gap_threshold < m_sample_period){ - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Gap threshold cannot be smaller than sample period."); - } -} - -template -void TimeSampleBuffer::push_samples( - const Type* samples, size_t count, - WallClock timestamp -){ - std::vector block(count); - memcpy(block.data(), samples, count * sizeof(Type)); - - WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); - -#if 0 - auto iter = m_samples.find(timestamp); - if (iter != m_samples.end()){ - timestamp++; - } -#endif - - m_samples[timestamp] = std::move(block); - m_samples_stored += count; - - // Drop samples that are too old. - while (!m_samples.empty()){ - auto iter = m_samples.begin(); - size_t samples_to_drop = iter->second.size(); - if (m_samples_stored < m_samples_to_buffer + samples_to_drop){ - break; - } - m_samples.erase(iter); - m_samples_stored -= samples_to_drop; - } -} - -template -std::string TimeSampleBuffer::dump() const{ - ReadSpinLock lg(m_lock, PA_CURRENT_FUNCTION); - - std::string str; - if (m_samples.empty()){ - str += "(buffer is empty)"; - return str; - } - auto iter = m_samples.rbegin(); - WallClock latest = iter->first; - for (; iter != m_samples.rend(); ++iter){ - Duration last = iter->first - latest; - Duration first = last - m_sample_period * iter->second.size(); - str += std::to_string(std::chrono::duration_cast(last).count() / 1000.); - str += " - "; - str += std::to_string(std::chrono::duration_cast(first).count() / 1000.); - str += " : "; - str += std::to_string(iter->second.size()); - str += "\n"; - } - return str; -} - - -template -void TimeSampleBuffer::read_samples( - Type* samples, size_t count, - WallClock timestamp -) const{ - ReadSpinLock lg(m_lock, PA_CURRENT_FUNCTION); - - if (m_samples.empty()){ - memset(samples, 0, count * sizeof(Type)); - return; - } - - // Setup output state. - WallClock requested_time = timestamp; - TimeSampleWriterReverse output_buffer(samples, count); - - // Jump to the latest block that's relevant to this request. -// cout << requested - reference << endl; -// cout << timestamp - reference << endl; - auto current_block = m_samples.lower_bound(requested_time); - if (current_block == m_samples.end()){ -// cout << "front gap" << endl; - --current_block; - } - - // Setup input state. - WallClock current_time = current_block->first; - size_t current_index = current_block->second.size(); - - // State machine loop. Look at the current input and output states to - // decide on the next action. Stop when output is filled or we run out of - // blocks. - while (output_buffer.samples_left() > 0){ -// cout << "Requested: " << requested_time - reference << endl; -// cout << "Buffer : " << current_time - reference << endl; - - // Current block is empty. Move to previous block. - if (current_index == 0){ - if (current_block == m_samples.begin()){ - output_buffer.fill_rest_with_zeros(); - return; - } - --current_block; - current_time = current_block->first; - current_index = current_block->second.size(); - } - - Duration output_ahead = requested_time - current_time; - - // Requested is far ahead of what's next. Fill the gap with zeros. - if (output_ahead > m_duration_gap_threshold){ -// cout << "Output Ahead" << endl; - size_t block = output_ahead.count() / m_sample_period.count(); - output_buffer.push_zeros(block); - requested_time -= block * m_sample_period; - continue; - } - - Duration input_ahead = current_time - requested_time; - - // Requested is far behind what's next. Skip ahead. - if (input_ahead > m_duration_gap_threshold){ -// cout << "Input Ahead" << endl; - size_t block = input_ahead.count() / m_sample_period.count(); - block = std::min(block, current_index); - current_index -= block; - current_time -= block * m_sample_period; - continue; - } - - size_t block = output_buffer.push_block(current_block->second.data(), current_index); -// cout << "Push block: " << block << endl; - Duration block_time = block * m_sample_period; - current_index -= block; - current_time -= block_time; - requested_time -= block_time; - } -} - - - -template class TimeSampleBuffer; -template class TimeSampleBuffer; -template class TimeSampleBuffer; -template class TimeSampleBuffer; -template class TimeSampleBuffer; -template class TimeSampleBuffer; -template class TimeSampleBuffer; - - - -} -#endif diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBuffer.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBuffer.h deleted file mode 100644 index a453dd77b8..0000000000 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBuffer.h +++ /dev/null @@ -1,94 +0,0 @@ -/* Time Sample Buffer - * - * From: https://github.com/PokemonAutomation/ - * - * - * This is a buffer for raw audio samples taken from an audio input. - * - * It will store at least "history" worth of samples before dropping old data. - * - * This class isn't just a trivial circular buffer. It allows random write - * and random read access. Reads will intelligently try to construct a - * contiguous audio stream. If it can't, it will insert zeros or drop samples - * where appropriate to correct it. - * - * This class in effect, is fault-tolerant to interruptions by both the writer - * and reader(s). Gaps or delays will self-correct to avoid a situation where - * the reader(s) perpetually fall behind or get ahead of the writers. Drifts - * where the writer/reader(s) are of slightly different speeds will also be - * corrected. - * - * - * Both read and write access to the buffer is done by blocks with a timestamp - * indicating the approximate time of the latest sample in the block. - * - * If you need to read data contiguously, you need to use TimeSampleBufferReader - * to read it as it will ensure that the samples are contiguous across - * successive read calls. - * - */ - -#ifndef PokemonAutomation_CommonFramework_AudioPipeline_TimeSampleBuffer_H -#define PokemonAutomation_CommonFramework_AudioPipeline_TimeSampleBuffer_H - -#include -#include -#include -#include "Common/Cpp/Time.h" -#include "Common/Cpp/Concurrency/SpinLock.h" - -namespace PokemonAutomation{ - - -template -class TimeSampleBufferReader; - - -template -class TimeSampleBuffer{ - using Duration = std::chrono::system_clock::duration; - -public: - TimeSampleBuffer( - size_t samples_per_second, - Duration history, - Duration gap_threshold = std::chrono::milliseconds(100) - ); - - // Write "count" samples ending on "timestamp". - void push_samples( - const Type* samples, size_t count, - WallClock timestamp = current_time() - ); - - // Read "count" samples ending on "timestamp". - void read_samples( - Type* samples, size_t count, - WallClock timestamp = current_time() - ) const; - - std::string dump() const; - -private: - friend class TimeSampleBufferReader; - using MapType = std::map>; - - const size_t m_samples_per_second; - const Duration m_sample_period; // Time between adjacent samples. - - // Minimum # of samples to keep. - const size_t m_samples_to_buffer; - - // # of samples in a gap or an overlap before we can no longer stretch. - const Duration m_duration_gap_threshold; - const size_t m_sample_gap_threshold; - - mutable SpinLock m_lock; - MapType m_samples; - size_t m_samples_stored; -}; - - - -} -#endif diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBufferReader.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBufferReader.cpp deleted file mode 100644 index 3c094c5f20..0000000000 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBufferReader.cpp +++ /dev/null @@ -1,180 +0,0 @@ -/* Time Sample Buffer Reader - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "TimeSampleWriter.h" -#include "TimeSampleBufferReader.h" - -namespace PokemonAutomation{ - - - -template -TimeSampleBufferReader::TimeSampleBufferReader(TimeSampleBuffer& buffer) - : m_buffer(buffer) -// , m_last_timestamp(TimePoint::min()) - , m_current_block(WallClock::min()) - , m_current_index(0) -{} - -template -void TimeSampleBufferReader::set_to_timestamp(WallClock timestamp){ - WriteSpinLock lg(m_buffer.m_lock); - set_to_timestamp_unprotected(timestamp); -} - -template -void TimeSampleBufferReader::set_to_timestamp_unprotected(WallClock timestamp){ - m_current_block = WallClock::min(); - m_current_index = 0; - - const typename TimeSampleBuffer::MapType& buffer = m_buffer.m_samples; - if (buffer.empty()){ - return; - } - - auto current_block = buffer.upper_bound(timestamp); - if (current_block == buffer.end()){ -// cout << "front gap" << endl; - --current_block; - } - - WallClock end = current_block->first; - WallClock start = end - current_block->second.size() * m_buffer.m_sample_period; - -// cout << start - REFERENCE << " - " << end - REFERENCE << endl; - - // Way ahead of the latest sample. - if (timestamp - end > m_buffer.m_duration_gap_threshold){ -// cout << "way ahead" << endl; - return; - } - - // Way before the earliest sample. - if (start - timestamp > m_buffer.m_duration_gap_threshold){ -// cout << "way behind" << endl; - return; - } - - size_t block_size = current_block->second.size(); - m_current_block = current_block->first; - - // Slightly ahead of latest sample. Clip to latest. - if (timestamp >= end){ -// cout << "slightly ahead" << endl; - m_current_index = block_size; - return; - } - - // Slightly behind oldest sample. Clip to oldest. - if (timestamp <= start){ -// cout << "slightly behind" << endl; - m_current_index = 0; - return; - } - - // Somewhere inside the block. - size_t block = (end - timestamp).count() / m_buffer.m_sample_period.count(); - block = std::min(block, block_size); - m_current_index = block_size - block; -} - -template -void TimeSampleBufferReader::read_samples( - Type* samples, size_t count, - WallClock timestamp -){ - const typename TimeSampleBuffer::MapType& buffer = m_buffer.m_samples; - - WriteSpinLock lg(m_buffer.m_lock); - - if (buffer.empty()){ - memset(samples, 0, count * sizeof(Type)); - return; - } - - // Setup output state. - WallClock requested_time = timestamp - count * m_buffer.m_sample_period; - TimeSampleWriterForward output_buffer(samples, count); - - auto current_block = buffer.lower_bound(m_current_block); - if (current_block == buffer.end()){ -// cout << "front gap" << endl; - --current_block; - } - - // If the block no longer exists, jump to whatever is best block for the requested timestamp. - if (current_block->first != m_current_block || current_block->second.size() <= m_current_index){ -// cout << "resetting state" << endl; - current_block = buffer.lower_bound(requested_time); - if (current_block == buffer.end()){ - --current_block; - } - m_current_block = current_block->first; - m_current_index = 0; - } - - // Setup input state. - WallClock current_time = current_block->first - current_block->second.size() * m_buffer.m_sample_period; - - while (output_buffer.samples_left() > 0){ - // Current block is empty. Move to next block. - if (m_current_index >= current_block->second.size()){ - ++current_block; - if (current_block == buffer.end()){ - output_buffer.fill_rest_with_zeros(); - return; - } - m_current_block = current_block->first; - m_current_index = 0; - current_time = current_block->first - current_block->second.size() * m_buffer.m_sample_period; - } - - const std::vector& data = current_block->second; - size_t samples_remaining_in_block = data.size() - m_current_index; - - // Requested is far ahead of what's next. Skip ahead. - Duration output_ahead = requested_time - current_time; - if (output_ahead > m_buffer.m_duration_gap_threshold){ -// cout << "Output Ahead" << endl; - size_t block = output_ahead.count() / m_buffer.m_sample_period.count(); - block = std::min(block, samples_remaining_in_block); - m_current_index += block; - current_time += block * m_buffer.m_sample_period; - continue; - } - - // Requested is far behind what's next. Fill the gap with zeros. - Duration input_ahead = current_time - requested_time; - if (input_ahead > m_buffer.m_duration_gap_threshold){ -// cout << "Input Ahead" << endl; - size_t block = input_ahead.count() / m_buffer.m_sample_period.count(); - output_buffer.push_zeros(block); - requested_time += block * m_buffer.m_sample_period; - continue; - } - - size_t block = output_buffer.push_block(data.data() + m_current_index, samples_remaining_in_block); - Duration block_time = block * m_buffer.m_sample_period; - m_current_index += block; - current_time += block_time; - requested_time += block_time; - } -} - - - -template class TimeSampleBufferReader; -template class TimeSampleBufferReader; -template class TimeSampleBufferReader; -template class TimeSampleBufferReader; -template class TimeSampleBufferReader; -template class TimeSampleBufferReader; -template class TimeSampleBufferReader; - - - - -} diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBufferReader.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBufferReader.h deleted file mode 100644 index e895a43c79..0000000000 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleBufferReader.h +++ /dev/null @@ -1,45 +0,0 @@ -/* Time Sample Buffer Reader - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_CommonFramework_AudioPipeline_TimeSampleBufferReader_H -#define PokemonAutomation_CommonFramework_AudioPipeline_TimeSampleBufferReader_H - -#include "TimeSampleBuffer.h" - -namespace PokemonAutomation{ - - -template -class TimeSampleBufferReader{ - using Duration = std::chrono::system_clock::duration; - -public: - TimeSampleBufferReader(TimeSampleBuffer& buffer); - - void set_to_timestamp(WallClock timestamp = current_time()); - - void read_samples( - Type* samples, size_t count, - WallClock timestamp = current_time() - ); - -private: - void set_to_timestamp_unprotected(WallClock timestamp = current_time()); - -public: - TimeSampleBuffer& m_buffer; - -// TimePoint m_last_timestamp; - - // Last read sample. - WallClock m_current_block; - size_t m_current_index; -}; - - - -} -#endif diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleWriter.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleWriter.h deleted file mode 100644 index 1b45c0135d..0000000000 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Tools/TimeSampleWriter.h +++ /dev/null @@ -1,107 +0,0 @@ -/* Time Sample Writer - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_CommonFramework_AudioPipeline_TimeSampleWriter_H -#define PokemonAutomation_CommonFramework_AudioPipeline_TimeSampleWriter_H - -#include -#include - -namespace PokemonAutomation{ - - -template -class TimeSampleWriterForward{ -public: - TimeSampleWriterForward(Type* output, size_t count) - : m_output(output) - , m_samples_left(count) - {} - - // # of samples still left to fill before the output buffer is full. - size_t samples_left() const{ return m_samples_left; } - - // Push the current block of samples into the output buffer. - // Returns the # of samples actually pushed. - size_t push_block(const Type* samples, size_t count){ - size_t block = std::min(m_samples_left, count); - memcpy(m_output, samples, block * sizeof(Type)); - m_output += block; - m_samples_left -= block; - return block; - } - - // Push zeros into the output buffer. - // Returns the # of samples actually pushed. - size_t push_zeros(size_t count){ - size_t block = std::min(m_samples_left, count); - memset(m_output, 0, block * sizeof(Type)); - m_output += block; - m_samples_left -= block; - return block; - } - - void fill_rest_with_zeros(){ - memset(m_output, 0, m_samples_left * sizeof(Type)); - m_output += m_samples_left; - m_samples_left = 0; - } - - -private: - Type* m_output; - size_t m_samples_left; -}; - - - -template -class TimeSampleWriterReverse{ -public: - TimeSampleWriterReverse(Type* output, size_t count) - : m_output(output + count) - , m_samples_left(count) - {} - - // # of samples still left to fill before the output buffer is full. - size_t samples_left() const{ return m_samples_left; } - - // Push the current block of samples into the output buffer. - // Returns the # of samples actually pushed. - size_t push_block(const Type* samples, size_t count){ - size_t block = std::min(m_samples_left, count); - memcpy(m_output - block, samples + count - block, block * sizeof(Type)); - m_output -= block; - m_samples_left -= block; - return block; - } - - // Push zeros into the output buffer. - // Returns the # of samples actually pushed. - size_t push_zeros(size_t count){ - size_t block = std::min(m_samples_left, count); - memset(m_output - block, 0, block * sizeof(Type)); - m_output -= block; - m_samples_left -= block; - return block; - } - - void fill_rest_with_zeros(){ - m_output -= m_samples_left; - memset(m_output, 0, m_samples_left * sizeof(Type)); - m_samples_left = 0; - } - - -private: - Type* m_output; - size_t m_samples_left; -}; - - - -} -#endif diff --git a/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion.cpp b/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion.cpp deleted file mode 100644 index 4e5c2d7fe9..0000000000 --- a/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* Audio Stream Conversion - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "Common/Cpp/CpuId/CpuId.h" -#include "AudioStreamConversion.h" - -namespace PokemonAutomation{ -namespace Kernels{ -namespace AudioStreamConversion{ - - - -void convert_audio_uint8_to_float_Default(float* f, const uint8_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_uint8_Default(uint8_t* i, const float* f, size_t length); -void convert_audio_sint16_to_float_Default(float* f, const int16_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_sint16_Default(int16_t* i, const float* f, size_t length); -void convert_audio_sint32_to_float_Default(float* f, const int32_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_sint32_Default(int32_t* i, const float* f, size_t length); - -void convert_audio_uint8_to_float_x86_SSE41(float* f, const uint8_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_uint8_x86_SSE41(uint8_t* i, const float* f, size_t length); -void convert_audio_sint16_to_float_x86_SSE41(float* f, const int16_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_sint16_x86_SSE41(int16_t* i, const float* f, size_t length); -void convert_audio_sint32_to_float_x86_SSE2(float* f, const int32_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_sint32_x86_SSE2(int32_t* i, const float* f, size_t length); - - - - -void convert_audio_uint8_to_float(float* f, const uint8_t* i, size_t length, float output_multiplier){ -#ifdef PA_AutoDispatch_x64_08_Nehalem - if (CPU_CAPABILITY_CURRENT.OK_08_Nehalem){ - convert_audio_uint8_to_float_x86_SSE41(f, i, length, output_multiplier); - return; - } -#endif - convert_audio_uint8_to_float_Default(f, i, length, output_multiplier); -} -void convert_audio_float_to_uint8(uint8_t* i, const float* f, size_t length){ -#ifdef PA_AutoDispatch_x64_08_Nehalem - if (CPU_CAPABILITY_CURRENT.OK_08_Nehalem){ - convert_audio_float_to_uint8_x86_SSE41(i, f, length); - return; - } -#endif - convert_audio_float_to_uint8_Default(i, f, length); -} -void convert_audio_sint16_to_float(float* f, const int16_t* i, size_t length, float output_multiplier){ -#ifdef PA_AutoDispatch_x64_08_Nehalem - if (CPU_CAPABILITY_CURRENT.OK_08_Nehalem){ - convert_audio_sint16_to_float_x86_SSE41(f, i, length, output_multiplier); - return; - } -#endif - convert_audio_sint16_to_float_Default(f, i, length, output_multiplier); -} -void convert_audio_float_to_sint16(int16_t* i, const float* f, size_t length){ -#ifdef PA_AutoDispatch_x64_08_Nehalem - if (CPU_CAPABILITY_CURRENT.OK_08_Nehalem){ - convert_audio_float_to_sint16_x86_SSE41(i, f, length); - return; - } -#endif - convert_audio_float_to_sint16_Default(i, f, length); -} -void convert_audio_sint32_to_float(float* f, const int32_t* i, size_t length, float output_multiplier){ -#ifdef PA_AutoDispatch_x64_08_Nehalem - if (CPU_CAPABILITY_CURRENT.OK_08_Nehalem){ - convert_audio_sint32_to_float_x86_SSE2(f, i, length, output_multiplier); - return; - } -#endif - convert_audio_sint32_to_float_Default(f, i, length, output_multiplier); -} -void convert_audio_float_to_sint32(int32_t* i, const float* f, size_t length){ -#ifdef PA_AutoDispatch_x64_08_Nehalem - if (CPU_CAPABILITY_CURRENT.OK_08_Nehalem){ - convert_audio_float_to_sint32_x86_SSE2(i, f, length); - return; - } -#endif - convert_audio_float_to_sint32_Default(i, f, length); -} - - - -} -} -} diff --git a/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion.h b/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion.h deleted file mode 100644 index 410abe41b5..0000000000 --- a/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion.h +++ /dev/null @@ -1,33 +0,0 @@ -/* Audio Stream Conversion - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_Kernels_AudioStreamConversion_H -#define PokemonAutomation_Kernels_AudioStreamConversion_H - -#include -#include - -namespace PokemonAutomation{ -namespace Kernels{ -namespace AudioStreamConversion{ - - -void convert_audio_uint8_to_float(float* f, const uint8_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_uint8(uint8_t* i, const float* f, size_t length); - -void convert_audio_sint16_to_float(float* f, const int16_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_sint16(int16_t* i, const float* f, size_t length); - -void convert_audio_sint32_to_float(float* f, const int32_t* i, size_t length, float output_multiplier); -void convert_audio_float_to_sint32(int32_t* i, const float* f, size_t length); - - - - -} -} -} -#endif diff --git a/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion_Core_Default.cpp b/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion_Core_Default.cpp deleted file mode 100644 index b5e6e1e383..0000000000 --- a/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion_Core_Default.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* Audio Stream Conversion (Default) - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include "AudioStreamConversion.h" - -namespace PokemonAutomation{ -namespace Kernels{ -namespace AudioStreamConversion{ - - - -void convert_audio_uint8_to_float_Default(float* f, const uint8_t* i, size_t length, float output_multiplier){ - const float SCALE = output_multiplier / 127.f; - for (size_t c = 0; c < length; c++){ - float x = (float)i[c] * SCALE - output_multiplier; - x = std::max(x, -1.0f); - x = std::min(x, 1.0f); - f[c] = x; - } -} -void convert_audio_float_to_uint8_Default(uint8_t* i, const float* f, size_t length){ - for (size_t c = 0; c < length; c++){ - float r = (f[c] + 1.0f) * 127.f; - r = std::min(r, 255.f); - r = std::max(r, 0.f); - r += 0.5; - i[c] = (uint8_t)r; - } -} - -void convert_audio_sint16_to_float_Default(float* f, const int16_t* i, size_t length, float output_multiplier){ - const float SCALE = output_multiplier / 32767.f; - for (size_t c = 0; c < length; c++){ - float x = (float)i[c] * SCALE; - x = std::max(x, -1.0f); - x = std::min(x, 1.0f); - f[c] = x; - } -} -void convert_audio_float_to_sint16_Default(int16_t* i, const float* f, size_t length){ - for (size_t c = 0; c < length; c++){ - float r = f[c] * 32767.f; - r = std::min(r, 32767.f); - r = std::max(r, -32768.f); - r += r > 0 ? 0.5f : -0.5f; - i[c] = (int16_t)r; - } -} - -void convert_audio_sint32_to_float_Default(float* f, const int32_t* i, size_t length, float output_multiplier){ - const float SCALE = output_multiplier / 2147483647.f; - for (size_t c = 0; c < length; c++){ - float x = (float)i[c] * SCALE; - x = std::max(x, -1.0f); - x = std::min(x, 1.0f); - f[c] = x; - } -} -void convert_audio_float_to_sint32_Default(int32_t* i, const float* f, size_t length){ - for (size_t c = 0; c < length; c++){ - double r = f[c] * 2147483647.; - r = std::min(r, 2147483647.); - r = std::max(r, -2147483648.); - r += r > 0 ? 0.5 : -0.5; - i[c] = (int32_t)r; - } -} - - - - -} -} -} diff --git a/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion_Core_x86_SSE41.cpp b/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion_Core_x86_SSE41.cpp deleted file mode 100644 index e96c1ea7aa..0000000000 --- a/SerialPrograms/Source/Kernels/AudioStreamConversion/AudioStreamConversion_Core_x86_SSE41.cpp +++ /dev/null @@ -1,190 +0,0 @@ -/* Audio Stream Conversion (x86 SSE4.1) - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifdef PA_AutoDispatch_x64_08_Nehalem - -#include -#include -#include "AudioStreamConversion.h" - -namespace PokemonAutomation{ -namespace Kernels{ -namespace AudioStreamConversion{ - - - -void convert_audio_uint8_to_float_x86_SSE41(float* f, const uint8_t* i, size_t length, float output_multiplier){ - const __m128 SCALE = _mm_set1_ps(output_multiplier / 127.f); - const __m128 SUB = _mm_set1_ps(output_multiplier); - size_t lc = length / 4; - while (lc--){ -#if __GNUC__ - __m128i i0 = _mm_cvtepu8_epi32(_mm_cvtsi32_si128(*(int32_t*)i)); -#else - __m128i i0 = _mm_cvtepu8_epi32(_mm_loadu_si32(i)); -#endif - __m128 f0 = _mm_cvtepi32_ps(i0); - f0 = _mm_mul_ps(f0, SCALE); - f0 = _mm_sub_ps(f0, SUB); - f0 = _mm_max_ps(f0, _mm_set1_ps(-1.0f)); - f0 = _mm_min_ps(f0, _mm_set1_ps(1.0f)); - _mm_storeu_ps(f, f0); - f += 4; - i += 4; - } - - length %= 4; - while (length--){ - __m128 f0 = _mm_cvtsi32_ss(_mm_setzero_ps(), i[0]); - f0 = _mm_mul_ss(f0, SCALE); - f0 = _mm_sub_ss(f0, SUB); - f0 = _mm_max_ss(f0, _mm_set1_ps(-1.0f)); - f0 = _mm_min_ss(f0, _mm_set1_ps(1.0f)); - _mm_store_ss(f, f0); - f += 1; - i += 1; - } -} -void convert_audio_float_to_uint8_x86_SSE41(uint8_t* i, const float* f, size_t length){ - size_t lc = length / 4; - while (lc--){ - __m128 f0 = _mm_loadu_ps(f); - f0 = _mm_add_ps(f0, _mm_set1_ps(1.0f)); - f0 = _mm_mul_ps(f0, _mm_set1_ps(127.f)); - f0 = _mm_min_ps(f0, _mm_set1_ps(255.f)); - f0 = _mm_max_ps(f0, _mm_set1_ps(0.f)); - __m128i i0 = _mm_cvtps_epi32(f0); - i0 = _mm_shuffle_epi8(i0, _mm_setr_epi8(0, 4, 8, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); -#if __GNUC__ && __GNUC__ < 9 - *(int32_t*)i = _mm_cvtsi128_si32(i0); -#else - _mm_storeu_si32((__m128i*)i, i0); -#endif - f += 4; - i += 4; - } - - length %= 4; - while (length--){ - __m128 f0 = _mm_load_ss(f); - f0 = _mm_add_ss(f0, _mm_set1_ps(1.0f)); - f0 = _mm_mul_ss(f0, _mm_set1_ps(127.f)); - f0 = _mm_min_ss(f0, _mm_set1_ps(255.f)); - f0 = _mm_max_ss(f0, _mm_set1_ps(0.f)); - i[0] = (uint8_t)_mm_cvt_ss2si(f0); - f += 1; - i += 1; - } -} - -void convert_audio_sint16_to_float_x86_SSE41(float* f, const int16_t* i, size_t length, float output_multiplier){ - const __m128 SCALE = _mm_set1_ps(output_multiplier / 32767.f); - size_t lc = length / 4; - while (lc--){ - __m128i i0 = _mm_cvtepi16_epi32(_mm_loadl_epi64((const __m128i*)i)); - __m128 f0 = _mm_cvtepi32_ps(i0); - f0 = _mm_mul_ps(f0, SCALE); - f0 = _mm_max_ps(f0, _mm_set1_ps(-1.0f)); - f0 = _mm_min_ps(f0, _mm_set1_ps(1.0f)); - _mm_storeu_ps(f, f0); - f += 4; - i += 4; - } - - length %= 4; - while (length--){ - __m128 f0 = _mm_cvtsi32_ss(_mm_setzero_ps(), i[0]); - f0 = _mm_mul_ss(f0, SCALE); - f0 = _mm_max_ss(f0, _mm_set1_ps(-1.0f)); - f0 = _mm_min_ss(f0, _mm_set1_ps(1.0f)); - _mm_store_ss(f, f0); - f += 1; - i += 1; - } -} -void convert_audio_float_to_sint16_x86_SSE41(int16_t* i, const float* f, size_t length){ - size_t lc = length / 4; - while (lc--){ - __m128 f0 = _mm_loadu_ps(f); - f0 = _mm_mul_ps(f0, _mm_set1_ps(32767.f)); - f0 = _mm_min_ps(f0, _mm_set1_ps(32767.f)); - f0 = _mm_max_ps(f0, _mm_set1_ps(-32768.f)); - __m128i i0 = _mm_cvtps_epi32(f0); - i0 = _mm_shuffle_epi8(i0, _mm_setr_epi8(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1)); - _mm_storel_epi64((__m128i*)i, i0); - f += 4; - i += 4; - } - - length %= 4; - while (length--){ - __m128 f0 = _mm_load_ss(f); - f0 = _mm_mul_ss(f0, _mm_set1_ps(32767.f)); - f0 = _mm_min_ss(f0, _mm_set1_ps(32767.f)); - f0 = _mm_max_ss(f0, _mm_set1_ps(-32768.f)); - i[0] = (int16_t)_mm_cvt_ss2si(f0); - f += 1; - i += 1; - } -} - -void convert_audio_sint32_to_float_x86_SSE2(float* f, const int32_t* i, size_t length, float output_multiplier){ - const __m128 SCALE = _mm_set1_ps(output_multiplier / 2147483647.f); - size_t lc = length / 4; - while (lc--){ - __m128i i0 = _mm_loadu_si128((const __m128i*)i); - __m128 f0 = _mm_cvtepi32_ps(i0); - f0 = _mm_mul_ps(f0, SCALE); - f0 = _mm_max_ps(f0, _mm_set1_ps(-1.0f)); - f0 = _mm_min_ps(f0, _mm_set1_ps(1.0f)); - _mm_storeu_ps(f, f0); - f += 4; - i += 4; - } - - length %= 4; - while (length--){ - __m128 f0 = _mm_cvtsi32_ss(_mm_setzero_ps(), i[0]); - f0 = _mm_mul_ss(f0, SCALE); - f0 = _mm_min_ss(f0, _mm_set1_ps(32767.f)); - f0 = _mm_max_ss(f0, _mm_set1_ps(-32768.f)); - _mm_store_ss(f, f0); - f += 1; - i += 1; - } -} -void convert_audio_float_to_sint32_x86_SSE2(int32_t* i, const float* f, size_t length){ - size_t lc = length / 4; - while (lc--){ - __m128 f0 = _mm_loadu_ps(f); - f0 = _mm_mul_ps(f0, _mm_set1_ps(2147483647.f)); - f0 = _mm_min_ps(f0, _mm_set1_ps(2147483520.f)); // 2^31 - 2^6 - f0 = _mm_max_ps(f0, _mm_set1_ps(-2147483648.f)); - __m128i i0 = _mm_cvtps_epi32(f0); - _mm_storeu_si128((__m128i*)i, i0); - f += 4; - i += 4; - } - - length %= 4; - while (length--){ - __m128 f0 = _mm_load_ss(f); - f0 = _mm_mul_ss(f0, _mm_set1_ps(2147483647.f)); - f0 = _mm_min_ss(f0, _mm_set1_ps(2147483520.f)); // 2^31 - 2^6 - f0 = _mm_max_ss(f0, _mm_set1_ps(-2147483648.f)); - i[0] = _mm_cvt_ss2si(f0); - f += 1; - i += 1; - } -} - - - - -} -} -} -#endif diff --git a/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp b/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp index 9a67034af1..a9ef349522 100644 --- a/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp +++ b/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp @@ -56,9 +56,6 @@ #include "Kernels/ScaleInvariantMatrixMatch/Kernels_ScaleInvariantMatrixMatch.h" #include "Kernels/SpikeConvolution/Kernels_SpikeConvolution.h" #include "VideoGames/PokemonSwSh/MaxLair/AI/PokemonSwSh_MaxLair_AI.h" -#include "Kernels/AudioStreamConversion/AudioStreamConversion.h" -#include "Common/Cpp/StreamConverters.h" -#include "CommonFramework/AudioPipeline/AudioConstants.h" #include "3rdParty-Core/nlohmann/json.hpp" #include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/Json/JsonArray.h" @@ -1985,25 +1982,6 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco -#if 0 - int16_t in[4] = {1, 1, 2, -2}; - print_u8((uint8_t*)in, 8); - - - - AudioSourceReader reader(AudioSampleFormat::SINT16, 1, false); - AudioListener listener(1); - reader += listener; - - -// reader.push_bytes(in, 8); - reader.push_bytes((char*)in + 0, 5); - reader.push_bytes((char*)in + 5, 3); -#endif - - - -// AudioStreamReader2 reader(2, AudioSampleFormat::SINT16); @@ -2029,23 +2007,6 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco // cout << reader.dump() << endl; #endif -#if 0 - float f[10]; - uint8_t i[10] = {}; - i[0] = 1; - i[1] = 255; - i[2] = 0; - i[3] = 123; - i[9] = 123; - - Kernels::AudioStreamConversion::convert_audio_uint8_to_float(f, i, 10); - print(f, 10); - memset(i, 0, sizeof(i)); - Kernels::AudioStreamConversion::convert_audio_float_to_uint8(i, f, 10); - print_u8(i, 10); -#endif - - #if 0 union{ float f32; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index fa2c5a3b48..7e424ec601 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -536,11 +536,6 @@ file(GLOB LIBRARY_SOURCES Source/CommonFramework/AudioPipeline/Tools/AudioFormatUtils.cpp Source/CommonFramework/AudioPipeline/Tools/AudioFormatUtils.h Source/CommonFramework/AudioPipeline/Tools/AudioNormalization.h - Source/CommonFramework/AudioPipeline/Tools/TimeSampleBuffer.cpp - Source/CommonFramework/AudioPipeline/Tools/TimeSampleBuffer.h - Source/CommonFramework/AudioPipeline/Tools/TimeSampleBufferReader.cpp - Source/CommonFramework/AudioPipeline/Tools/TimeSampleBufferReader.h - Source/CommonFramework/AudioPipeline/Tools/TimeSampleWriter.h Source/CommonFramework/AudioPipeline/UI/AudioDisplayWidget.cpp Source/CommonFramework/AudioPipeline/UI/AudioDisplayWidget.h Source/CommonFramework/AudioPipeline/UI/AudioSelectorWidget.cpp @@ -1067,10 +1062,6 @@ file(GLOB LIBRARY_SOURCES Source/Kernels/AudioResampler/Kernels_AudioResampler_Tests.h Source/Kernels/AudioResampler/Kernels_AudioResampler_WeightTable.cpp Source/Kernels/AudioResampler/Kernels_AudioResampler_WeightTable.h - Source/Kernels/AudioStreamConversion/AudioStreamConversion.cpp - Source/Kernels/AudioStreamConversion/AudioStreamConversion.h - Source/Kernels/AudioStreamConversion/AudioStreamConversion_Core_Default.cpp - Source/Kernels/AudioStreamConversion/AudioStreamConversion_Core_x86_SSE41.cpp Source/Kernels/AudioStreamConverter/Kernels_AudioStreamConverter.cpp Source/Kernels/AudioStreamConverter/Kernels_AudioStreamConverter.h Source/Kernels/AudioStreamConverter/Kernels_AudioStreamConverter_Kernels_ARM64_NEON.cpp