diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioInfo.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioInfo.cpp index 3292882659..d37175dc68 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioInfo.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioInfo.cpp @@ -38,6 +38,23 @@ const char* AUDIO_FORMAT_LABELS[] = { "1 x 96,000 Hz (Interleaved Stereo R/L)", }; +size_t sample_size(AudioSampleFormat format){ + switch (format){ + case AudioSampleFormat::UINT8: + return sizeof(uint8_t); + case AudioSampleFormat::SINT16: + return sizeof(int16_t); + case AudioSampleFormat::SINT32: + return sizeof(int32_t); + case AudioSampleFormat::FLOAT32: + return sizeof(float); + default: + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid AudioSampleFormat: " + std::to_string((size_t)format)); + } +} + + + void set_format(QAudioFormat& native_format, AudioChannelFormat format){ switch (format){ case AudioChannelFormat::MONO_48000: diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioPassthroughPair.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioPassthroughPair.h index ac719a792f..0bfaddc4f3 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioPassthroughPair.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioPassthroughPair.h @@ -11,7 +11,6 @@ namespace PokemonAutomation{ -struct AudioFloatStreamListener; struct FFTListener; namespace Kernels{ @@ -32,9 +31,6 @@ class WorkingAudioStreamListener; // class AudioPassthroughPair{ public: - virtual void add_listener(AudioFloatStreamListener& listener) = 0; - virtual void remove_listener(AudioFloatStreamListener& listener) = 0; - virtual void add_listener(FFTListener& listener) = 0; virtual void remove_listener(FFTListener& listener) = 0; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioSession.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioSession.cpp index 7478d1ed4c..4f6fd0a2e9 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioSession.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioSession.cpp @@ -28,12 +28,6 @@ void AudioSession::add_state_listener(StateListener& listener){ void AudioSession::remove_state_listener(StateListener& listener){ m_listeners.remove(listener); } -void AudioSession::add_stream_listener(AudioFloatStreamListener& listener){ - m_devices->add_listener(listener); -} -void AudioSession::remove_stream_listener(AudioFloatStreamListener& listener){ - m_devices->remove_listener(listener); -} void AudioSession::add_working_stream_listener( const AudioFormat& format, Kernels::AudioPipeline::WorkingAudioStreamListener& listener ){ diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioSession.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioSession.h index da47606eff..8e5c836b78 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioSession.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioSession.h @@ -20,7 +20,6 @@ #include "Common/Cpp/Concurrency/Watchdog.h" #include "AudioFeed.h" #include "AudioPassthroughPair.h" -#include "AudioStream.h" #include "Spectrum/FFTStreamer.h" #include "Spectrum/AudioSpectrumHolder.h" #include "AudioOption.h" @@ -42,9 +41,6 @@ class AudioSession final : public AudioFeed, private FFTListener, private Watchd void add_state_listener(StateListener& listener); void remove_state_listener(StateListener& listener); - void add_stream_listener(AudioFloatStreamListener& listener); - void remove_stream_listener(AudioFloatStreamListener& listener); - // Attach a listener that receives the working audio stream (float) // converted to the requested format. See AudioPassthroughPair. void add_working_stream_listener(const AudioFormat& format, Kernels::AudioPipeline::WorkingAudioStreamListener& listener); diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioStream.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioStream.cpp deleted file mode 100644 index 0cd12c3b90..0000000000 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioStream.cpp +++ /dev/null @@ -1,147 +0,0 @@ -/* Audio Source Reader - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "Common/Cpp/Exceptions.h" -#include "Common/Cpp/Containers/AlignedVector.tpp" -#include "Kernels/AudioStreamConversion/AudioStreamConversion.h" -#include "Kernels/AbsFFT/Kernels_AbsFFT.h" -#include "AudioConstants.h" -#include "AudioStream.h" - -#include -using std::cout; -using std::endl; - -namespace PokemonAutomation{ - - -// Audio buffer size (measured in frames). -const size_t AUDIO_BUFFER_SIZE = 4096; - - - -size_t sample_size(AudioSampleFormat format){ - switch (format){ - case AudioSampleFormat::UINT8: - return sizeof(uint8_t); - case AudioSampleFormat::SINT16: - return sizeof(int16_t); - case AudioSampleFormat::SINT32: - return sizeof(int32_t); - case AudioSampleFormat::FLOAT32: - return sizeof(float); - default: - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid AudioSampleFormat: " + std::to_string((size_t)format)); - } -} - - - -void AudioStreamToFloat::add_listener(AudioFloatStreamListener& listener){ - auto scope_check = m_sanitizer.check_scope(); - if (listener.expected_samples_per_frame != m_samples_per_frame){ - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching frame size."); - } - m_listeners.insert(&listener); -} -void AudioStreamToFloat::remove_listener(AudioFloatStreamListener& listener){ - auto scope_check = m_sanitizer.check_scope(); - m_listeners.erase(&listener); -} - -AudioStreamToFloat::~AudioStreamToFloat(){ - MisalignedStreamConverter::remove_listener(*this); -} -AudioStreamToFloat::AudioStreamToFloat( - AudioSampleFormat input_format, - size_t samples_per_frame, - float volume_multiplier, - bool reverse_channels -) - : MisalignedStreamConverter( - sample_size(input_format) * samples_per_frame, - sizeof(float) * samples_per_frame, - AUDIO_BUFFER_SIZE - ) - , ObjectStreamListener(sizeof(float) * samples_per_frame) - , m_format(input_format) - , m_samples_per_frame(samples_per_frame) - , m_volume_multiplier(volume_multiplier) - , m_reverse_channels(reverse_channels) - , m_sample_size(sample_size(input_format)) - , m_frame_size(m_sample_size * samples_per_frame) -{ - if (samples_per_frame == 0){ - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Must have at least one sample."); - } - if (reverse_channels && samples_per_frame != 2){ - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Reverse channels only works with 2 samples/frame."); - } - MisalignedStreamConverter::add_listener(*this); -} -void AudioStreamToFloat::on_objects(const void* data, size_t objects){ - auto scope_check = m_sanitizer.check_scope(); - for (AudioFloatStreamListener* listener : m_listeners){ - listener->on_samples((const float*)data, objects); - } -} -void AudioStreamToFloat::convert(void* out, const void* in, size_t count){ - auto scope_check = m_sanitizer.check_scope(); - switch (m_format){ - case AudioSampleFormat::UINT8: - Kernels::AudioStreamConversion::convert_audio_uint8_to_float( - (float*)out, (const uint8_t*)in, count * m_samples_per_frame, m_volume_multiplier - ); - break; - case AudioSampleFormat::SINT16: - Kernels::AudioStreamConversion::convert_audio_sint16_to_float( - (float*)out, (const int16_t*)in, count * m_samples_per_frame, m_volume_multiplier - ); - break; - case AudioSampleFormat::SINT32: - Kernels::AudioStreamConversion::convert_audio_sint32_to_float( - (float*)out, (const int32_t*)in, count * m_samples_per_frame, m_volume_multiplier - ); - break; - case AudioSampleFormat::FLOAT32: - if (m_volume_multiplier == 1.0){ - memcpy(out, in, count * m_frame_size); - }else{ - float volume_multiplier = m_volume_multiplier; - float* o = (float*)out; - const float* i = (float*)in; - size_t stop = count * m_samples_per_frame; - for (size_t c = 0; c < stop; c++){ - o[c] = i[c] * volume_multiplier; - } - } - break; - case AudioSampleFormat::INVALID: - break; - } - if (m_reverse_channels){ - float* ptr = (float*)out; - for (size_t c = 0; c < count; c++){ - float r0 = ptr[2*c + 0]; - float r1 = ptr[2*c + 1]; - ptr[2*c + 0] = r1; - ptr[2*c + 1] = r0; - } - } -} - - - - - - - - - - - - -} diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioStream.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioStream.h deleted file mode 100644 index f9222b8773..0000000000 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/AudioStream.h +++ /dev/null @@ -1,68 +0,0 @@ -/* Audio Source Reader - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_AudioPipeline_AudioSourceReader_H -#define PokemonAutomation_AudioPipeline_AudioSourceReader_H - -#include "Common/Cpp/ListenerSet.h" -#include "Common/Cpp/LifetimeSanitizer.h" -#include "Common/Cpp/StreamConverters.h" -#include "Common/Cpp/Containers/AlignedVector.h" -#include "AudioInfo.h" - -namespace PokemonAutomation{ - - -struct AudioFloatStreamListener{ - AudioFloatStreamListener(size_t p_expected_samples_per_frame) - : expected_samples_per_frame(p_expected_samples_per_frame) - {} - virtual ~AudioFloatStreamListener() = default; - virtual void on_samples(const float* data, size_t frames) = 0; - - size_t expected_samples_per_frame; -}; - - - -// Convert a stream of raw audio and output to aligned frames of float samples. -class AudioStreamToFloat : private MisalignedStreamConverter, private ObjectStreamListener{ -public: - void add_listener(AudioFloatStreamListener& listener); - void remove_listener(AudioFloatStreamListener& listener); - -public: - AudioStreamToFloat( - AudioSampleFormat input_format, - size_t samples_per_frame, // Typically the # of channels. Can be higher if you want to group more into each frame. - float volume_multiplier, // Multiply every sample by this constant. - bool reverse_channels // Only valid if "samples_per_frame == 2". - ); - virtual ~AudioStreamToFloat(); - - using MisalignedStreamConverter::push_bytes; - -private: - virtual void on_objects(const void* data, size_t objects) override; - virtual void convert(void* out, const void* in, size_t count) override; - -private: - AudioSampleFormat m_format; - size_t m_samples_per_frame; - float m_volume_multiplier; - bool m_reverse_channels; - size_t m_sample_size; - size_t m_frame_size; - std::set m_listeners; - - LifetimeSanitizer m_sanitizer; -}; - - - - -} -#endif diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp index c3a2902d35..7865ec48e2 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp @@ -11,7 +11,6 @@ #include "CommonFramework/GlobalSettingsPanel.h" #include "CommonFramework/AudioPipeline/AudioPipelineOptions.h" #include "CommonFramework/AudioPipeline/Tools/AudioFormatUtils.h" -#include "CommonFramework/AudioPipeline/AudioStream.h" #include "CommonFramework/AudioPipeline/ChannelMixer.h" #include "CommonFramework/AudioPipeline/IO/AudioSource.h" #include "CommonFramework/AudioPipeline/IO/AudioOutputDevice.h" @@ -116,14 +115,6 @@ class AudioPassthroughPairQt::InternalWorkingListener final : public Kernels::Au }; -void AudioPassthroughPairQt::add_listener(AudioFloatStreamListener& listener){ - auto scope_check = m_sanitizer.check_scope(); - m_stream_listeners.add(listener); -} -void AudioPassthroughPairQt::remove_listener(AudioFloatStreamListener& listener){ - auto scope_check = m_sanitizer.check_scope(); - m_stream_listeners.remove(listener); -} void AudioPassthroughPairQt::add_listener(FFTListener& listener){ auto scope_check = m_sanitizer.check_scope(); m_fft_listeners.add(listener); @@ -154,28 +145,6 @@ void AudioPassthroughPairQt::remove_listener(WorkingAudioStreamListener& listene -class AudioPassthroughPairQt::SampleListener final : public AudioFloatStreamListener{ -public: - SampleListener(AudioPassthroughPairQt& parent, size_t samples_per_frame) - : AudioFloatStreamListener(samples_per_frame) - , m_parent(parent) - { - parent.m_reader->add_listener(*this); - } - ~SampleListener(){ - m_parent.m_reader->remove_listener(*this); - } - virtual void on_samples(const float* data, size_t frames) override{ - m_parent.m_stream_listeners.run_method( - &AudioFloatStreamListener::on_samples, - data, frames - ); - } - -private: - AudioPassthroughPairQt& m_parent; -}; - class AudioPassthroughPairQt::InternalFFTListener final : public FFTListener{ public: InternalFFTListener(AudioPassthroughPairQt& parent) @@ -454,22 +423,19 @@ void AudioPassthroughPairQt::reset( source.reset(new AudioSource( m_logger, file, - AudioChannelFormat::DUAL_48000, - m_file_input_multiplier + AudioChannelFormat::DUAL_48000 )); } WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); if (m_reader){ teardown_tree(); m_sink_device.reset(); - m_sample_listener.reset(); m_reader.reset(); m_input_format = AudioChannelFormat::NONE; } m_input_format = AudioChannelFormat::DUAL_48000; if (source){ m_reader = std::move(source); - m_sample_listener.reset(new SampleListener(*this, m_reader->samples_per_frame())); m_output_device = output; m_output_volume = output_volume; init_audio_sink(); @@ -491,15 +457,13 @@ void AudioPassthroughPairQt::reset( source.reset(new AudioSource( m_logger, input, - format, - m_device_input_multiplier + format )); } WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); if (m_reader){ teardown_tree(); m_sink_device.reset(); - m_sample_listener.reset(); m_reader.reset(); m_input_format = AudioChannelFormat::NONE; } @@ -508,7 +472,6 @@ void AudioPassthroughPairQt::reset( m_output_volume = output_volume; if (source){ m_reader = std::move(source); - m_sample_listener.reset(new SampleListener(*this, m_reader->samples_per_frame())); init_audio_sink(); build_tree(m_device_input_multiplier); m_fft_listener.reset(new InternalFFTListener(*this)); @@ -521,7 +484,6 @@ void AudioPassthroughPairQt::clear_audio_source(){ auto scope_check = m_sanitizer.check_scope(); WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); if (m_reader){ - m_sample_listener.reset(); teardown_tree(); m_reader.reset(); m_input_format = AudioChannelFormat::NONE; @@ -537,22 +499,19 @@ void AudioPassthroughPairQt::set_audio_source(const std::string& file){ source.reset(new AudioSource( m_logger, file, - AudioChannelFormat::DUAL_48000, - m_file_input_multiplier + AudioChannelFormat::DUAL_48000 )); } WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); if (m_reader){ teardown_tree(); m_sink_device.reset(); - m_sample_listener.reset(); m_reader.reset(); m_input_format = AudioChannelFormat::NONE; } m_input_format = AudioChannelFormat::DUAL_48000; if (source){ m_reader = std::move(source); - m_sample_listener.reset(new SampleListener(*this, m_reader->samples_per_frame())); init_audio_sink(); build_tree(m_file_input_multiplier); m_fft_listener.reset(new InternalFFTListener(*this)); @@ -568,22 +527,19 @@ void AudioPassthroughPairQt::set_audio_source(const AudioDeviceInfo& device, Aud source.reset(new AudioSource( m_logger, device, - format, - m_device_input_multiplier + format )); } WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); if (m_reader){ teardown_tree(); m_sink_device.reset(); - m_sample_listener.reset(); m_reader.reset(); m_input_format = AudioChannelFormat::NONE; } m_input_format = format; if (source){ m_reader = std::move(source); - m_sample_listener.reset(new SampleListener(*this, m_reader->samples_per_frame())); init_audio_sink(); build_tree(m_device_input_multiplier); m_fft_listener.reset(new InternalFFTListener(*this)); diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.h index 2035e27091..e84426495b 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.h @@ -40,9 +40,6 @@ class AudioPassthroughPairQt final : public QObject, public AudioPassthroughPair using WorkingAudio2WorkingAudio = Kernels::AudioPipeline::WorkingAudio2WorkingAudio; using WorkingAudioStreamListener = Kernels::AudioPipeline::WorkingAudioStreamListener; - virtual void add_listener(AudioFloatStreamListener& listener) override; - virtual void remove_listener(AudioFloatStreamListener& listener) override; - virtual void add_listener(FFTListener& listener) override; virtual void remove_listener(FFTListener& listener) override; @@ -74,7 +71,6 @@ class AudioPassthroughPairQt final : public QObject, public AudioPassthroughPair private: - class SampleListener; class InternalFFTListener; class InternalWorkingListener; @@ -119,7 +115,6 @@ class AudioPassthroughPairQt final : public QObject, public AudioPassthroughPair AudioChannelFormat m_input_format; std::unique_ptr m_reader; - std::unique_ptr m_sample_listener; // Attaches to "m_reader". // New pipeline tree. The decoder attaches to m_reader's raw stream and // the branch heads attach to the decoder (sink head before FFT head). @@ -148,7 +143,6 @@ class AudioPassthroughPairQt final : public QObject, public AudioPassthroughPair std::unique_ptr m_fft_listener; // Attaches to m_fft_node. - ListenerSet m_stream_listeners; ListenerSet m_fft_listeners; LifetimeSanitizer m_sanitizer; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQtThread.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQtThread.cpp index a42dd6fbe1..ab5f1688f5 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQtThread.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQtThread.cpp @@ -15,16 +15,6 @@ namespace PokemonAutomation{ -void AudioPassthroughPairQtThread::add_listener(AudioFloatStreamListener& listener){ - auto scope_check = m_sanitizer.check_scope(); - AudioPassthroughPairQt* body = m_body; - body->add_listener(listener); -} -void AudioPassthroughPairQtThread::remove_listener(AudioFloatStreamListener& listener){ - auto scope_check = m_sanitizer.check_scope(); - AudioPassthroughPairQt* body = m_body; - body->remove_listener(listener); -} void AudioPassthroughPairQtThread::add_listener(FFTListener& listener){ auto scope_check = m_sanitizer.check_scope(); AudioPassthroughPairQt* body = m_body; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQtThread.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQtThread.h index 24fc6ef29a..a443a654bb 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQtThread.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQtThread.h @@ -21,9 +21,6 @@ class AudioPassthroughPairQt; class AudioPassthroughPairQtThread : public AudioPassthroughPair{ public: - virtual void add_listener(AudioFloatStreamListener& listener) override; - virtual void remove_listener(AudioFloatStreamListener& listener) override; - virtual void add_listener(FFTListener& listener) override; virtual void remove_listener(FFTListener& listener) override; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp index 7fe2ba91bd..d500d3614d 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp @@ -9,7 +9,6 @@ #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/Time.h" -#include "CommonFramework/AudioPipeline/AudioStream.h" #include "CommonFramework/AudioPipeline/Tools/AudioFormatUtils.h" #include "AudioInputDevice.h" @@ -21,7 +20,6 @@ namespace{ // Frames of raw audio buffered before the raw listeners are notified. -// (Matches AudioStreamToFloat's AUDIO_BUFFER_SIZE.) const size_t RAW_BUFFER_CAPACITY = 4096; @@ -62,7 +60,7 @@ size_t raw_output_frame_bytes(const QAudioFormat& format, bool reinterpret_96k_m AudioInputDevice::AudioInputDevice( - Logger& logger, AudioStreamToFloat& reader, + Logger& logger, const NativeAudioInfo& device, const QAudioFormat& format, bool reinterpret_96k_mono_as_stereo ) @@ -72,7 +70,6 @@ AudioInputDevice::AudioInputDevice( RAW_BUFFER_CAPACITY ) , ObjectStreamListener(raw_output_frame_bytes(format, reinterpret_96k_mono_as_stereo)) - , m_reader(reader) , m_sample_type(sample_format_of(format)) , m_sample_rate(format.sampleRate()) , m_channels(format.channelCount()) @@ -124,7 +121,6 @@ void AudioInputDevice::remove_listener(Kernels::AudioPipeline::RawAudioStreamLis } qint64 AudioInputDevice::writeData(const char* data, qint64 len){ - m_reader.push_bytes(data, len); MisalignedStreamConverter::push_bytes(data, len); return len; } diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.h index 4192b35505..d88eb1f3b6 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.h @@ -31,19 +31,17 @@ class QAudioFormat; namespace PokemonAutomation{ class Logger; -class AudioStreamToFloat; // // Raw audio input from a capture device (QAudioSource / QAudioInput). -// -// Feeds the legacy AudioStreamToFloat reader and, in parallel, emits the raw -// stream to RawAudioStreamListener children (root of the new pipeline tree). +// Emits the raw stream to RawAudioStreamListener children (root of the new +// pipeline tree). // Qt makes no contract about the frame alignment of the chunks that the -// audio backend writes into the QIODevice, so the raw output goes through the -// same MisalignedStreamConverter / ObjectStreamListener byte-reassembly -// pattern that AudioStreamToFloat uses. In practice the backends emit -// whole-frame chunks, so no splicing happens in the common case. +// audio backend writes into the QIODevice, so the raw output goes through a +// MisalignedStreamConverter / ObjectStreamListener byte-reassembly step. +// In practice the backends emit whole-frame chunks, so no splicing happens +// in the common case. // // The INTERLEAVE_96000 presets open the device as a physical 1ch x 96000 // stream that is really the card's two 48 kHz channels (a misreporting @@ -64,7 +62,7 @@ class AudioInputDevice final ~AudioInputDevice(); AudioInputDevice( - Logger& logger, AudioStreamToFloat& reader, + Logger& logger, const NativeAudioInfo& device, const QAudioFormat& format, bool reinterpret_96k_mono_as_stereo ); @@ -97,7 +95,6 @@ class AudioInputDevice final private: - AudioStreamToFloat& m_reader; std::unique_ptr m_source; Kernels::AudioPipeline::SampleFormat m_sample_type; size_t m_sample_rate; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputFile.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputFile.cpp index 8cb52fa0a1..8ba1da8a24 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputFile.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputFile.cpp @@ -14,7 +14,6 @@ #include "3rdParty/QtWavFile/WavFile.h" #include "Common/Cpp/Exceptions.h" #include "CommonFramework/AudioPipeline/AudioInfo.h" -#include "CommonFramework/AudioPipeline/AudioStream.h" #include "CommonFramework/AudioPipeline/Tools/AudioFormatUtils.h" #include "AudioFileLoader.h" #include "AudioInputFile.h" @@ -142,11 +141,10 @@ bool probe_decoded_native_format(const std::string& filename, QAudioFormat& out_ AudioInputFile::AudioInputFile( - Logger& logger, AudioStreamToFloat& reader, + Logger& logger, const std::string& file, const QAudioFormat& format ) - : m_reader(reader) - , m_source(std::make_unique(nullptr, file, format)) + : m_source(std::make_unique(nullptr, file, format)) , m_sample_type(sample_format_of(format)) , m_sample_rate(format.sampleRate()) , m_channels(format.channelCount()) @@ -182,8 +180,6 @@ void AudioInputFile::remove_listener(Kernels::AudioPipeline::RawAudioStreamListe } void AudioInputFile::on_buffer_ready(const char* data, size_t len){ - m_reader.push_bytes(data, len); - // AudioFileLoader only emits whole multiples of the frame size, so the // raw listeners can take the data as-is. m_listeners.run_method( diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputFile.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputFile.h index 883dfef719..ffb5fe2326 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputFile.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputFile.h @@ -19,7 +19,6 @@ class QAudioFormat; namespace PokemonAutomation{ class Logger; -class AudioStreamToFloat; class AudioFileLoader; @@ -39,10 +38,8 @@ bool probe_decoded_native_format(const std::string& filename, QAudioFormat& out_ // // Raw audio input from a file (.wav via WavFile, compressed formats via -// QAudioDecoder). -// -// Feeds the legacy AudioStreamToFloat reader and, in parallel, emits the raw -// stream to RawAudioStreamListener children (root of the new pipeline tree). +// QAudioDecoder). Emits the raw stream to RawAudioStreamListener children +// (root of the new pipeline tree). // AudioFileLoader only ever emits chunks that are whole multiples of the // frame size, so the raw output is frame-aligned by construction and is // passed to the listeners as-is. @@ -52,7 +49,7 @@ class AudioInputFile final : public QObject{ ~AudioInputFile(); AudioInputFile( - Logger& logger, AudioStreamToFloat& reader, + Logger& logger, const std::string& file, const QAudioFormat& format ); @@ -75,7 +72,6 @@ class AudioInputFile final : public QObject{ private: - AudioStreamToFloat& m_reader; std::unique_ptr m_source; Kernels::AudioPipeline::SampleFormat m_sample_type; size_t m_sample_rate; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.cpp index a3baa4e3c4..504a023956 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.cpp @@ -6,7 +6,6 @@ #include "Common/Cpp/Exceptions.h" #include "Common/Qt/StringToolsQt.h" -#include "CommonFramework/AudioPipeline/AudioStream.h" #include "CommonFramework/AudioPipeline/Tools/AudioFormatUtils.h" #include "AudioInputDevice.h" #include "AudioInputFile.h" @@ -16,36 +15,6 @@ namespace PokemonAutomation{ -class AudioSource::InternalListener : public AudioFloatStreamListener{ -public: - InternalListener(AudioSource& parent) - : AudioFloatStreamListener(parent.m_channels * parent.m_multiplier) - , m_parent(parent) - {} - -private: - virtual void on_samples(const float* data, size_t objects) override{ -// cout << "objects = " << objects << endl; - m_parent.m_listeners.run_method( - &AudioFloatStreamListener::on_samples, - data, objects - ); - } - - AudioSource& m_parent; -}; - - - - -void AudioSource::add_listener(AudioFloatStreamListener& listener){ - auto scope_check = m_sanitizer.check_scope(); - m_listeners.add(listener); -} -void AudioSource::remove_listener(AudioFloatStreamListener& listener){ - auto scope_check = m_sanitizer.check_scope(); - m_listeners.remove(listener); -} size_t AudioSource::raw_sample_rate() const{ if (m_input_file){ return m_input_file->sample_rate(); @@ -86,13 +55,12 @@ void AudioSource::remove_raw_listener(Kernels::AudioPipeline::RawAudioStreamList } -AudioSource::~AudioSource(){} +AudioSource::~AudioSource() = default; AudioSource::AudioSource( Logger& logger, const std::string& file, - AudioChannelFormat format, - float volume_multiplier + AudioChannelFormat format ){ QAudioFormat native_format; if (has_extension(file, "wav") && probe_wav_native_format(file, native_format)){ @@ -117,19 +85,12 @@ AudioSource::AudioSource( m_stream_format = AudioSampleFormat::FLOAT32; } } - m_sample_rate = (size_t)native_format.sampleRate(); - m_channels = (size_t)native_format.channelCount(); - m_multiplier = 1; - m_reader.reset(new AudioStreamToFloat(m_stream_format, m_channels, volume_multiplier, false)); - m_internal_listener = std::make_unique(*this); - m_reader->add_listener(*m_internal_listener); - m_input_file = std::make_unique(logger, *m_reader, file, native_format); + m_input_file = std::make_unique(logger, file, native_format); } AudioSource::AudioSource( Logger& logger, const AudioDeviceInfo& device, - AudioChannelFormat format, - float volume_multiplier + AudioChannelFormat format ){ NativeAudioInfo native_info = device.native_info(); QAudioFormat native_format = native_info.preferredFormat(); @@ -138,11 +99,11 @@ AudioSource::AudioSource( AudioSampleFormat stream_format = get_sample_format(native_format); if (stream_format == AudioSampleFormat::INVALID){ - stream_format = AudioSampleFormat::FLOAT32; set_sample_format_to_float(native_format); + stream_format = AudioSampleFormat::FLOAT32; } + m_stream_format = stream_format; - init(format, stream_format, volume_multiplier); // Only the interleaved presets misreport the card's two 48 kHz // channels as a 1ch x 96000 stream; MONO_96000 is a true 96 kHz mono // stream and keeps that shape. This is a capture-card hardware quirk, @@ -151,58 +112,13 @@ AudioSource::AudioSource( const bool reinterpret_96k = format == AudioChannelFormat::INTERLEAVE_LR_96000 || format == AudioChannelFormat::INTERLEAVE_RL_96000; - m_input_device = std::make_unique(logger, *m_reader, native_info, native_format, reinterpret_96k); -} - -void AudioSource::init(AudioChannelFormat format, AudioSampleFormat stream_format, float volume_multiplier){ - auto scope_check = m_sanitizer.check_scope(); - m_stream_format = stream_format; - switch (format){ - case AudioChannelFormat::MONO_48000: - m_sample_rate = 48000; - m_channels = 1; - m_multiplier = 1; - m_reader.reset(new AudioStreamToFloat(stream_format, 1, volume_multiplier, false)); - break; - case AudioChannelFormat::DUAL_44100: - m_sample_rate = 44100; - m_channels = 2; - m_multiplier = 1; - m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, false)); - break; - case AudioChannelFormat::DUAL_48000: - m_sample_rate = 48000; - m_channels = 2; - m_multiplier = 1; - m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, false)); - break; - case AudioChannelFormat::MONO_96000: - // Treat mono-96000 as 2-sample frames. - // The FFT will then average each pair to produce 48000Hz. - // The output will push the same stream at the original 4 bytes * 96000Hz. - m_sample_rate = 96000; - m_channels = 1; - m_multiplier = 2; - m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, false)); - break; - case AudioChannelFormat::INTERLEAVE_LR_96000: - m_sample_rate = 48000; - m_channels = 2; - m_multiplier = 1; - m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, false)); - break; - case AudioChannelFormat::INTERLEAVE_RL_96000: - m_sample_rate = 48000; - m_channels = 2; - m_multiplier = 1; - m_reader.reset(new AudioStreamToFloat(stream_format, 2, volume_multiplier, true)); - break; - default: - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid AudioFormat: " + std::to_string((size_t)format)); - } - m_internal_listener = std::make_unique(*this); - m_reader->add_listener(*m_internal_listener); + m_input_device = std::make_unique( + logger, + native_info, + native_format, + reinterpret_96k + ); } diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.h index 042bc58133..377ec49d8e 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.h @@ -2,9 +2,9 @@ * * From: https://github.com/PokemonAutomation/ * - * AudioSource represents an audio input stream. Once constructed, - * it spits out the float samples. Listeners can attach themselves to this - * class to receive these audio frames. + * AudioSource represents an audio input stream. Once constructed, it + * emits the raw frame-aligned stream to its RawAudioStreamListener + * children (root of the pipeline tree). * */ @@ -12,17 +12,12 @@ #define PokemonAutomation_AudioPipeline_AudioSource_H #include -#include -#include "Common/Cpp/ListenerSet.h" #include "Common/Cpp/LifetimeSanitizer.h" -#include "Common/Cpp/Concurrency/SpinLock.h" #include "CommonFramework/AudioPipeline/AudioInfo.h" -#include "CommonFramework/AudioPipeline/AudioStream.h" namespace PokemonAutomation{ class Logger; -class AudioStreamToFloat; class AudioInputFile; class AudioInputDevice; @@ -31,12 +26,9 @@ namespace Kernels{namespace AudioPipeline{class RawAudioStreamListener;}} class AudioSource{ public: - void add_listener(AudioFloatStreamListener& listener); - void remove_listener(AudioFloatStreamListener& listener); - // Raw stream (pre-conversion) access. The raw stream is the source's - // native frame-aligned output, before AudioStreamToFloat applies the - // preset's rate/channels and the input volume. + // native frame-aligned output, before the pipeline tree applies any + // conversion (resample / mix / sample-type / volume). AudioSampleFormat raw_sample_format() const{ return m_stream_format; } size_t raw_sample_rate() const; size_t raw_channels() const; @@ -50,43 +42,22 @@ class AudioSource{ AudioSource( Logger& logger, const std::string& file, - AudioChannelFormat format, - float volume_multiplier + AudioChannelFormat format ); // Read from an audio input device. (i.e. capture card) AudioSource( Logger& logger, const AudioDeviceInfo& device, - AudioChannelFormat format, - float volume_multiplier + AudioChannelFormat format ); - size_t sample_rate() const{ return m_sample_rate; } - size_t channels() const{ return m_channels; } - size_t samples_per_frame() const{ return m_channels * m_multiplier; } - -private: - void init(AudioChannelFormat format, AudioSampleFormat stream_format, float volume_multiplier); - private: - class InternalListener; - - size_t m_sample_rate; - size_t m_channels; - size_t m_multiplier; AudioSampleFormat m_stream_format; - SpinLock m_lock; - - std::unique_ptr m_internal_listener; - std::unique_ptr m_reader; - std::unique_ptr m_input_file; std::unique_ptr m_input_device; - ListenerSet m_listeners; - LifetimeSanitizer m_sanitizer; }; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/SinkPipeline_Tests.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/SinkPipeline_Tests.h index 785d43f905..664e7a0655 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/SinkPipeline_Tests.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/SinkPipeline_Tests.h @@ -14,12 +14,11 @@ namespace PokemonAutomation{ // -// Registers the sink gate tests: raw-byte comparisons between the +// Registers the sink gate tests: self-contained invariants for the // pipeline tree's sink branch (RawAudio2WorkingAudio -> [resampler] -> -// [mixer] -> WorkingAudio2RawAudio) and the legacy converter chain -// (AudioStreamToFloat -> AudioFloatToStream), plus self-contained -// invariants for the resampled / mixed shapes the legacy path never -// produced (it opened the device in the input's format instead). +// [mixer] -> WorkingAudio2RawAudio) - no-op bit-exactness, 1-LSB sint16 +// against a plain-scalar reference, and the resampled / mixed shapes +// (output length, tone amplitudes, upmix / downmix channel behavior). // void add_tests_SinkPipeline(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/CommonFramework/Recording/StreamHistorySession.cpp b/SerialPrograms/Source/CommonFramework/Recording/StreamHistorySession.cpp index 93a6e72764..eed7d518c8 100644 --- a/SerialPrograms/Source/CommonFramework/Recording/StreamHistorySession.cpp +++ b/SerialPrograms/Source/CommonFramework/Recording/StreamHistorySession.cpp @@ -150,7 +150,6 @@ void StreamHistorySession::clear(){ Data& data = *m_data; data.m_logger.log("Clearing stream history...", COLOR_ORANGE); data.m_current.reset(); -// expected_samples_per_frame = 0; // data.m_audio_format = AudioChannelFormat::NONE; // data.m_has_video = false; } diff --git a/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp b/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp index 44b5bc9bf8..9a67034af1 100644 --- a/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp +++ b/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp @@ -59,7 +59,6 @@ #include "Kernels/AudioStreamConversion/AudioStreamConversion.h" #include "Common/Cpp/StreamConverters.h" #include "CommonFramework/AudioPipeline/AudioConstants.h" -#include "CommonFramework/AudioPipeline/AudioStream.h" #include "3rdParty-Core/nlohmann/json.hpp" #include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/Json/JsonArray.h" diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index e04611c3ef..fa2c5a3b48 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -504,8 +504,6 @@ file(GLOB LIBRARY_SOURCES Source/CommonFramework/AudioPipeline/AudioPipelineOptions.h Source/CommonFramework/AudioPipeline/AudioSession.cpp Source/CommonFramework/AudioPipeline/AudioSession.h - Source/CommonFramework/AudioPipeline/AudioStream.cpp - Source/CommonFramework/AudioPipeline/AudioStream.h Source/CommonFramework/AudioPipeline/AudioTemplate.cpp Source/CommonFramework/AudioPipeline/AudioTemplate.h Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp