mirror of
				https://github.com/PabloMK7/citra.git
				synced 2025-10-31 05:40:04 +00:00 
			
		
		
		
	Rest of the owl
This commit is contained in:
		
							parent
							
								
									5c9e327ff3
								
							
						
					
					
						commit
						c669aa8d55
					
				
					 7 changed files with 237 additions and 75 deletions
				
			
		|  | @ -10,21 +10,12 @@ | |||
| namespace AudioCore { | ||||
| 
 | ||||
| struct CubebInput::Impl { | ||||
|     // unsigned int sample_rate = 0;
 | ||||
|     // std::vector<std::string> device_list;
 | ||||
| 
 | ||||
|     cubeb* ctx = nullptr; | ||||
|     cubeb_stream* stream = nullptr; | ||||
| 
 | ||||
|     bool looped_buffer; | ||||
|     u8* buffer; | ||||
|     u32 buffer_size; | ||||
|     u32 initial_offset; | ||||
|     u32 offset; | ||||
|     u32 audio_buffer_size; | ||||
|     std::unique_ptr<Frontend::Mic::SampleQueue> sample_queue{}; | ||||
|     u8 sample_size_in_bytes = 0; | ||||
| 
 | ||||
|     void UpdateOffset(int new_offset, Impl* impl); | ||||
|     void ProcessSample(const u8* data, int current_sample_index, Impl* impl); | ||||
|     static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | ||||
|                              void* output_buffer, long num_frames); | ||||
|     static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state); | ||||
|  | @ -35,6 +26,7 @@ CubebInput::CubebInput() : impl(std::make_unique<Impl>()) { | |||
|         LOG_ERROR(Audio, "cubeb_init failed! Mic will not work properly"); | ||||
|         return; | ||||
|     } | ||||
|     impl->sample_queue = std::make_unique<Frontend::Mic::SampleQueue>(); | ||||
| } | ||||
| 
 | ||||
| CubebInput::~CubebInput() { | ||||
|  | @ -60,11 +52,10 @@ void CubebInput::StartSampling(Frontend::Mic::Parameters params) { | |||
|                   "Application requested unsupported 8 bit pcm format. Falling back to 16 bits"); | ||||
|     } | ||||
| 
 | ||||
|     impl->buffer = backing_memory; | ||||
|     impl->buffer_size = backing_memory_size; | ||||
|     impl->audio_buffer_size = params.buffer_size; | ||||
|     impl->offset = params.buffer_offset; | ||||
|     impl->looped_buffer = params.buffer_loop; | ||||
|     parameters = params; | ||||
|     is_sampling = true; | ||||
| 
 | ||||
|     impl->sample_size_in_bytes = 2; | ||||
| 
 | ||||
|     cubeb_devid input_device = nullptr; | ||||
|     cubeb_stream_params input_params; | ||||
|  | @ -92,6 +83,7 @@ void CubebInput::StopSampling() { | |||
|     if (impl->stream) { | ||||
|         cubeb_stream_stop(impl->stream); | ||||
|     } | ||||
|     is_sampling = false; | ||||
| } | ||||
| 
 | ||||
| void CubebInput::AdjustSampleRate(u32 sample_rate) { | ||||
|  | @ -99,47 +91,29 @@ void CubebInput::AdjustSampleRate(u32 sample_rate) { | |||
|     LOG_ERROR(Audio, "AdjustSampleRate unimplemented!"); | ||||
| } | ||||
| 
 | ||||
| void CubebInput::Impl::UpdateOffset(int new_offset, Impl* impl) { | ||||
|     impl->offset = new_offset; | ||||
|     std::memcpy(impl->buffer + impl->audio_buffer_size, reinterpret_cast<u8*>(&impl->offset), | ||||
|                 sizeof(u32)); | ||||
| } | ||||
| 
 | ||||
| void CubebInput::Impl::ProcessSample(const u8* data, int current_sample_index, Impl* impl) { | ||||
|     if (impl->offset >= impl->audio_buffer_size) { | ||||
|         if (impl->looped_buffer) | ||||
|             UpdateOffset(impl->initial_offset, impl); | ||||
|         else | ||||
|             return; | ||||
| Frontend::Mic::Samples CubebInput::Read() { | ||||
|     Frontend::Mic::Samples samples{}; | ||||
|     Frontend::Mic::Samples queue; | ||||
|     while (impl->sample_queue->Pop(queue)) { | ||||
|         samples.insert(samples.end(), queue.begin(), queue.end()); | ||||
|     } | ||||
| 
 | ||||
|     std::memcpy(impl->buffer + impl->offset, data + current_sample_index * sizeof(u16), | ||||
|                 sizeof(u16)); | ||||
|     UpdateOffset(impl->offset + sizeof(u16), impl); | ||||
|     return samples; | ||||
| } | ||||
| 
 | ||||
| long CubebInput::Impl::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | ||||
|                                     void* output_buffer, long num_frames) { | ||||
|     Impl* impl = static_cast<Impl*>(user_data); | ||||
|     const u8* data = reinterpret_cast<const u8*>(input_buffer); | ||||
|     u8 const* data = reinterpret_cast<u8 const*>(input_buffer); | ||||
| 
 | ||||
|     if (!impl) { | ||||
|         return 0; | ||||
|     } | ||||
| 
 | ||||
|     if (!impl->buffer) { | ||||
|         return 0; | ||||
|     } | ||||
| 
 | ||||
|     for (int i = 0; i < num_frames; i++) { | ||||
|         impl->ProcessSample(data, i, impl); | ||||
|     } | ||||
|     impl->sample_queue->Push(std::vector(data, data + num_frames * impl->sample_size_in_bytes)); | ||||
| 
 | ||||
|     // returning less than num_frames here signals cubeb to stop sampling
 | ||||
|     // return total_written;
 | ||||
|     // TODO:: Correct this
 | ||||
|     return num_frames; | ||||
| } // namespace AudioCore
 | ||||
| } | ||||
| 
 | ||||
| void CubebInput::Impl::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {} | ||||
| 
 | ||||
|  |  | |||
|  | @ -13,7 +13,7 @@ namespace AudioCore { | |||
| class CubebInput final : public Frontend::Mic::Interface { | ||||
| public: | ||||
|     CubebInput(); | ||||
|     ~CubebInput(); | ||||
|     ~CubebInput() override; | ||||
| 
 | ||||
|     void StartSampling(Frontend::Mic::Parameters params) override; | ||||
| 
 | ||||
|  | @ -21,6 +21,8 @@ public: | |||
| 
 | ||||
|     void AdjustSampleRate(u32 sample_rate) override; | ||||
| 
 | ||||
|     Frontend::Mic::Samples Read() override; | ||||
| 
 | ||||
| private: | ||||
|     struct Impl; | ||||
|     std::unique_ptr<Impl> impl; | ||||
|  |  | |||
|  | @ -164,6 +164,11 @@ | |||
|             <string>Real Device</string> | ||||
|            </property> | ||||
|           </item> | ||||
|           <item> | ||||
|            <property name="text"> | ||||
|             <string>Static Noise</string> | ||||
|            </property> | ||||
|           </item> | ||||
|          </widget> | ||||
|         </item> | ||||
|        </layout> | ||||
|  |  | |||
|  | @ -2,10 +2,73 @@ | |||
| // Licensed under GPLv2 or any later version
 | ||||
| // Refer to the license.txt file included.
 | ||||
| 
 | ||||
| #include <array> | ||||
| #include "core/frontend/mic.h" | ||||
| #include "core/hle/service/mic_u.h" | ||||
| 
 | ||||
| namespace Frontend { | ||||
| namespace Frontend::Mic { | ||||
| 
 | ||||
| constexpr std::array<u8, 32> NOISE_SAMPLE_8_BIT = { | ||||
|     0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF5, 0xFF, 0xFF, 0xFF, 0xFF, 0x8E, 0xFF, | ||||
|     0xF4, 0xE1, 0xBF, 0x9A, 0x71, 0x58, 0x5B, 0x5F, 0x62, 0xC2, 0x25, 0x05, 0x01, 0x01, 0x01, 0x01}; | ||||
| 
 | ||||
| constexpr std::array<u8, 512> NOISE_SAMPLE_16_BIT = { | ||||
|     0x64, 0x61, 0x74, 0x61, 0x56, 0xD7, 0x00, 0x00, 0x48, 0xF7, 0x86, 0x05, 0x77, 0x1A, 0xF4, 0x1F, | ||||
|     0x28, 0x0F, 0x6B, 0xEB, 0x1C, 0xC0, 0xCB, 0x9D, 0x46, 0x90, 0xDF, 0x98, 0xEA, 0xAE, 0xB5, 0xC4, | ||||
|     0x9D, 0xCE, 0xB6, 0xC9, 0xDF, 0xBD, 0x82, 0xBA, 0x83, 0xCD, 0x57, 0xF9, 0x96, 0x30, 0x2C, 0x5B, | ||||
|     0x29, 0x64, 0xD3, 0x46, 0x0D, 0x12, 0x3E, 0xDE, 0x00, 0xBD, 0x74, 0xAE, 0xF0, 0xA4, 0x91, 0x93, | ||||
|     0x02, 0x80, 0x00, 0x80, 0x03, 0x80, 0x6F, 0xAA, 0x8E, 0xE8, 0xEE, 0x1A, 0x2B, 0x2C, 0xC6, 0x18, | ||||
|     0xF8, 0xED, 0xC2, 0xBE, 0x00, 0x99, 0xC1, 0x82, 0x00, 0x80, 0xA6, 0x8A, 0x37, 0xA8, 0x6B, 0xCE, | ||||
|     0x20, 0xEF, 0xD0, 0xFD, 0x68, 0xF9, 0x50, 0xEF, 0x89, 0xF2, 0xDE, 0x0C, 0x24, 0x36, 0xDB, 0x58, | ||||
|     0xB2, 0x61, 0xD5, 0x4C, 0x0B, 0x27, 0x6D, 0x02, 0xDC, 0xE8, 0x13, 0xD8, 0x2F, 0xC9, 0x07, 0xBC, | ||||
|     0xAB, 0xBA, 0x70, 0xD1, 0xDE, 0x01, 0xF2, 0x3C, 0x64, 0x6A, 0xFF, 0x78, 0x47, 0x6B, 0x26, 0x56, | ||||
|     0x9B, 0x51, 0xF3, 0x65, 0xFF, 0x7F, 0xFF, 0x7F, 0x88, 0x6B, 0x46, 0x24, 0xBF, 0xD8, 0x8C, 0xB4, | ||||
|     0xD9, 0xCF, 0x77, 0x1C, 0x43, 0x6B, 0xFE, 0x7F, 0x15, 0x64, 0xA6, 0x13, 0x03, 0xCE, 0x51, 0xBF, | ||||
|     0xC7, 0xEB, 0xCB, 0x2E, 0x6C, 0x58, 0xA7, 0x51, 0x40, 0x2A, 0x24, 0x06, 0x45, 0xFB, 0xD3, 0xFE, | ||||
|     0x51, 0xF2, 0x1E, 0xC5, 0x79, 0x8A, 0x00, 0x80, 0x94, 0x8E, 0x7A, 0xDE, 0x83, 0x29, 0x8E, 0x3C, | ||||
|     0x4B, 0x0F, 0xD8, 0xCB, 0x41, 0xAB, 0xC1, 0xC5, 0xC6, 0xFE, 0x0F, 0x1F, 0x92, 0x05, 0x4B, 0xC4, | ||||
|     0xDC, 0x8F, 0xE1, 0x90, 0xE3, 0xC0, 0xDB, 0xF1, 0xCC, 0xF8, 0xC8, 0xD4, 0x2A, 0xAF, 0xB7, 0xB5, | ||||
|     0x7F, 0xF0, 0xB2, 0x39, 0xD4, 0x5C, 0xD6, 0x41, 0x69, 0xFD, 0xEA, 0xBB, 0x57, 0x9B, 0x59, 0x98, | ||||
|     0x8E, 0x9B, 0x8A, 0x97, 0x54, 0x98, 0xE6, 0xB5, 0x25, 0xF8, 0x77, 0x48, 0xFF, 0x7F, 0xFF, 0x7F, | ||||
|     0x49, 0x6B, 0xB4, 0x4B, 0xE9, 0x45, 0xE3, 0x57, 0x60, 0x64, 0x8C, 0x4D, 0x69, 0x10, 0x10, 0xCA, | ||||
|     0x13, 0xA4, 0xC5, 0xB7, 0xE4, 0xFD, 0x7C, 0x54, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xE0, 0x52, | ||||
|     0x5E, 0x22, 0x31, 0x0B, 0xB5, 0x12, 0xB1, 0x2E, 0xA7, 0x4D, 0x17, 0x61, 0x46, 0x65, 0xA5, 0x5F, | ||||
|     0x5C, 0x57, 0xB6, 0x4C, 0x3B, 0x38, 0xFD, 0x11, 0x70, 0xDC, 0x2A, 0xA8, 0xB9, 0x8C, 0xEA, 0x98, | ||||
|     0x07, 0xC6, 0xDF, 0xF7, 0x2D, 0x0C, 0xEC, 0xF0, 0xA8, 0xB2, 0x02, 0x80, 0x00, 0x80, 0xBA, 0x8A, | ||||
|     0x0E, 0xCB, 0x54, 0xF7, 0x2B, 0xEE, 0x4C, 0xB9, 0x48, 0x89, 0x8F, 0x90, 0x0E, 0xD8, 0x83, 0x32, | ||||
|     0xC5, 0x5C, 0x54, 0x34, 0x86, 0xD6, 0x78, 0x8C, 0x88, 0x90, 0x87, 0xDE, 0x75, 0x37, 0x55, 0x56, | ||||
|     0xF1, 0x28, 0xF0, 0xDA, 0xE2, 0xAB, 0xE6, 0xB6, 0xD6, 0xDF, 0x4D, 0xF4, 0xCF, 0xDE, 0x3E, 0xBA, | ||||
|     0xC6, 0xB3, 0x81, 0xDA, 0xFE, 0x0D, 0xE9, 0x1D, 0xCE, 0xFB, 0x59, 0xCD, 0x57, 0xCA, 0x77, 0x06, | ||||
|     0x63, 0x5A, 0xFE, 0x7F, 0x49, 0x63, 0x7F, 0x14, 0x0E, 0xDB, 0x2A, 0xE5, 0x3B, 0x27, 0xFF, 0x69, | ||||
|     0x0A, 0x7C, 0xC5, 0x56, 0x65, 0x19, 0xEC, 0xE5, 0x0E, 0xC6, 0xA6, 0xB0, 0x09, 0xA2, 0x06, 0xA8, | ||||
|     0xC7, 0xD1, 0xE9, 0x15, 0xD5, 0x4E, 0xBB, 0x56, 0x85, 0x2A, 0x25, 0xF0, 0x78, 0xD6, 0x70, 0xEB, | ||||
|     0xF2, 0x0F, 0xE1, 0x15, 0x66, 0xEC, 0xC7, 0xB0, 0xE7, 0x93, 0x9D, 0xAD, 0xD1, 0xE6, 0xD3, 0x0D, | ||||
|     0xAC, 0x00, 0xB2, 0xC7, 0x5D, 0x8B, 0x00, 0x80, 0xA1, 0x88, 0x14, 0xBE, 0xDF, 0xFB, 0xCF, 0x32, | ||||
|     0xD8, 0x5B, 0x0F, 0x6F, 0x6C, 0x62, 0xD9, 0x33, 0x76, 0xF3, 0xD6, 0xBF, 0x41, 0xB3, 0x5C, 0xD1}; | ||||
| 
 | ||||
| StaticMic::StaticMic() | ||||
|     : CACHE_8_BIT{NOISE_SAMPLE_8_BIT.begin(), NOISE_SAMPLE_8_BIT.end()}, | ||||
|       CACHE_16_BIT{NOISE_SAMPLE_16_BIT.begin(), NOISE_SAMPLE_16_BIT.end()} {} | ||||
| 
 | ||||
| StaticMic::~StaticMic() = default; | ||||
| 
 | ||||
| void StaticMic::StartSampling(Parameters params) { | ||||
|     sample_rate = params.sample_rate; | ||||
|     sample_size = params.sample_size; | ||||
| 
 | ||||
|     parameters = params; | ||||
|     is_sampling = true; | ||||
| } | ||||
| 
 | ||||
| void StaticMic::StopSampling() { | ||||
|     is_sampling = false; | ||||
| } | ||||
| 
 | ||||
| void StaticMic::AdjustSampleRate(u32 sample_rate) {} | ||||
| 
 | ||||
| Samples StaticMic::Read() { | ||||
|     return (sample_size == 8) ? CACHE_8_BIT : CACHE_16_BIT; | ||||
| } | ||||
| 
 | ||||
| static std::shared_ptr<Mic::Interface> current_mic; | ||||
| 
 | ||||
|  | @ -20,4 +83,4 @@ std::shared_ptr<Mic::Interface> GetCurrentMic() { | |||
|     return current_mic; | ||||
| } | ||||
| 
 | ||||
| } // namespace Frontend
 | ||||
| } // namespace Frontend::Mic
 | ||||
|  |  | |||
|  | @ -4,19 +4,21 @@ | |||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| #include <array> | ||||
| #include <memory> | ||||
| #include <vector> | ||||
| #include "common/swap.h" | ||||
| #include "common/threadsafe_queue.h" | ||||
| 
 | ||||
| namespace Frontend { | ||||
| 
 | ||||
| namespace Mic { | ||||
| namespace Frontend::Mic { | ||||
| 
 | ||||
| enum class Signedness : u8 { | ||||
|     Signed, | ||||
|     Unsigned, | ||||
| }; | ||||
| 
 | ||||
| using Samples = std::vector<u8>; | ||||
| using SampleQueue = Common::SPSCQueue<Samples>; | ||||
| 
 | ||||
| struct Parameters { | ||||
|     Signedness sign; | ||||
|     u8 sample_size; | ||||
|  | @ -28,23 +30,20 @@ struct Parameters { | |||
| 
 | ||||
| class Interface { | ||||
| public: | ||||
|     Interface() = default; | ||||
| 
 | ||||
|     virtual ~Interface() = default; | ||||
| 
 | ||||
|     /// Starts the microphone. Called by Core
 | ||||
|     virtual void StartSampling(Parameters params) = 0; | ||||
| 
 | ||||
|     /// Stops the microphone. Called by Core
 | ||||
|     virtual void StopSampling() = 0; | ||||
| 
 | ||||
|     Interface() = default; | ||||
| 
 | ||||
|     Interface(const Interface& other) | ||||
|         : gain(other.gain), powered(other.powered), backing_memory(other.backing_memory), | ||||
|           backing_memory_size(other.backing_memory_size), parameters(other.parameters) {} | ||||
| 
 | ||||
|     /// Sets the backing memory that the mic should write raw samples into. Called by Core
 | ||||
|     void SetBackingMemory(u8* pointer, u32 size) { | ||||
|         backing_memory = pointer; | ||||
|         backing_memory_size = size; | ||||
|     } | ||||
|     /// Called from the actual event timing read back. The frontend impl is responsible for wrapping
 | ||||
|     /// up any data and returning them to the core so the core can write them to the sharedmem. If
 | ||||
|     /// theres nothing to return just return an empty vector
 | ||||
|     virtual Samples Read() = 0; | ||||
| 
 | ||||
|     /// Adjusts the Parameters. Implementations should update the parameters field in addition to
 | ||||
|     /// changing the mic to sample according to the new parameters. Called by Core
 | ||||
|  | @ -76,9 +75,6 @@ public: | |||
|     } | ||||
| 
 | ||||
| protected: | ||||
|     u8* backing_memory; | ||||
|     u32 backing_memory_size; | ||||
| 
 | ||||
|     Parameters parameters; | ||||
|     u8 gain = 0; | ||||
|     bool is_sampling = false; | ||||
|  | @ -99,12 +95,32 @@ public: | |||
|     void AdjustSampleRate(u32 sample_rate) override { | ||||
|         parameters.sample_rate = sample_rate; | ||||
|     } | ||||
| 
 | ||||
|     Samples Read() override { | ||||
|         return {}; | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| } // namespace Mic
 | ||||
| class StaticMic final : public Interface { | ||||
| public: | ||||
|     StaticMic(); | ||||
|     ~StaticMic() override; | ||||
| 
 | ||||
|     void StartSampling(Parameters params) override; | ||||
|     void StopSampling() override; | ||||
|     void AdjustSampleRate(u32 sample_rate) override; | ||||
| 
 | ||||
|     Samples Read() override; | ||||
| 
 | ||||
| private: | ||||
|     u16 sample_rate = 0; | ||||
|     u8 sample_size = 0; | ||||
|     std::vector<u8> CACHE_8_BIT; | ||||
|     std::vector<u8> CACHE_16_BIT; | ||||
| }; | ||||
| 
 | ||||
| void RegisterMic(std::shared_ptr<Mic::Interface> mic); | ||||
| 
 | ||||
| std::shared_ptr<Mic::Interface> GetCurrentMic(); | ||||
| 
 | ||||
| } // namespace Frontend
 | ||||
| } // namespace Frontend::Mic
 | ||||
|  |  | |||
|  | @ -46,10 +46,75 @@ constexpr u32 GetSampleRateInHz(SampleRate sample_rate) { | |||
|     } | ||||
| } | ||||
| 
 | ||||
| // The following buffer write rates were found by hardware test on o3ds and n3ds.
 | ||||
| // The 3ds writes to the sharedmem roughly every 15 samples
 | ||||
| constexpr u64 BufferUpdateRate8180 = BASE_CLOCK_RATE_ARM11 / 511; | ||||
| constexpr u64 BufferUpdateRate10910 = BASE_CLOCK_RATE_ARM11 / 681; | ||||
| constexpr u64 BufferUpdateRate16360 = BASE_CLOCK_RATE_ARM11 / 1022; | ||||
| constexpr u64 BufferUpdateRate32730 = BASE_CLOCK_RATE_ARM11 / 2045; | ||||
| 
 | ||||
| constexpr u64 GetBufferUpdateRate(SampleRate sample_rate) { | ||||
|     switch (sample_rate) { | ||||
|     case SampleRate::Rate8180: | ||||
|         return BufferUpdateRate8180; | ||||
|     case SampleRate::Rate10910: | ||||
|         return BufferUpdateRate10910; | ||||
|     case SampleRate::Rate16360: | ||||
|         return BufferUpdateRate16360; | ||||
|     case SampleRate::Rate32730: | ||||
|         return BufferUpdateRate32730; | ||||
|     default: | ||||
|         UNREACHABLE(); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Variables holding the current mic buffer writing state
 | ||||
| struct State { | ||||
|     u8* sharedmem_buffer = nullptr; | ||||
|     u32 sharedmem_size = 0; | ||||
|     size_t size = 0; | ||||
|     u32 offset = 0; | ||||
|     u32 initial_offset = 0; | ||||
|     bool looped_buffer = false; | ||||
|     u8 sample_size = 0; | ||||
|     SampleRate sample_rate = SampleRate::Rate16360; | ||||
| 
 | ||||
|     void UpdateOffset() { | ||||
|         // The last 4 bytes of the shared memory contains the latest offset
 | ||||
|         // so update that as well https://www.3dbrew.org/wiki/MIC_Shared_Memory
 | ||||
|         std::memcpy(sharedmem_buffer + (sharedmem_size - sizeof(u32)), | ||||
|                     reinterpret_cast<u8*>(&offset), sizeof(u32)); | ||||
|     } | ||||
| 
 | ||||
|     void WriteSamples(const std::vector<u8>& samples) { | ||||
|         u32 bytes_total_written = 0; | ||||
|         const size_t remaining_space = size - offset; | ||||
|         size_t bytes_to_write = std::min(samples.size(), remaining_space); | ||||
| 
 | ||||
|         // Write as many samples as we can to the buffer.
 | ||||
|         // TODO if the sample size is 16bit, this could theoretically cut a sample
 | ||||
|         std::memcpy(sharedmem_buffer + offset, samples.data(), bytes_to_write); | ||||
|         offset += static_cast<u32>(bytes_to_write); | ||||
|         bytes_total_written += static_cast<u32>(bytes_to_write); | ||||
| 
 | ||||
|         // If theres any samples left to write after we looped, go ahead and write them now
 | ||||
|         if (looped_buffer && samples.size() > bytes_total_written) { | ||||
|             offset = initial_offset; | ||||
|             bytes_to_write = std::min(samples.size() - bytes_total_written, size); | ||||
|             std::memcpy(sharedmem_buffer + offset, samples.data() + bytes_total_written, | ||||
|                         bytes_to_write); | ||||
|             offset += static_cast<u32>(bytes_to_write); | ||||
|         } | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| struct MIC_U::Impl { | ||||
|     explicit Impl(Core::System& system) { | ||||
|     explicit Impl(Core::System& system) : timing(system.CoreTiming()) { | ||||
|         buffer_full_event = | ||||
|             system.Kernel().CreateEvent(Kernel::ResetType::OneShot, "MIC_U::buffer_full_event"); | ||||
|         buffer_write_event = timing.RegisterEvent( | ||||
|             "MIC_U::UpdateBuffer", std::bind(&Impl::UpdateSharedMemBuffer, this, | ||||
|                                              std::placeholders::_1, std::placeholders::_2)); | ||||
|     } | ||||
| 
 | ||||
|     void MapSharedMem(Kernel::HLERequestContext& ctx) { | ||||
|  | @ -59,10 +124,10 @@ struct MIC_U::Impl { | |||
| 
 | ||||
|         if (shared_memory) { | ||||
|             shared_memory->SetName("MIC_U:shared_memory"); | ||||
|             state.sharedmem_buffer = shared_memory->GetPointer(); | ||||
|             state.sharedmem_size = size; | ||||
|         } | ||||
| 
 | ||||
|         mic->SetBackingMemory(shared_memory->GetPointer(), size); | ||||
| 
 | ||||
|         IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||||
|         rb.Push(RESULT_SUCCESS); | ||||
| 
 | ||||
|  | @ -77,6 +142,25 @@ struct MIC_U::Impl { | |||
|         LOG_TRACE(Service_MIC, "MIC:U UnmapSharedMem called"); | ||||
|     } | ||||
| 
 | ||||
|     void UpdateSharedMemBuffer(u64 userdata, s64 cycles_late) { | ||||
|         // If the event was scheduled before the application requested the mic to stop sampling
 | ||||
|         if (!mic->IsSampling()) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         Frontend::Mic::Samples samples = mic->Read(); | ||||
|         if (!samples.empty()) { | ||||
|             // write the samples to sharedmem page
 | ||||
|             state.WriteSamples(samples); | ||||
|             // write the new offset to the last 4 bytes of the buffer
 | ||||
|             state.UpdateOffset(); | ||||
|         } | ||||
| 
 | ||||
|         // schedule next run
 | ||||
|         timing.ScheduleEvent(GetBufferUpdateRate(state.sample_rate) - cycles_late, | ||||
|                              buffer_write_event); | ||||
|     } | ||||
| 
 | ||||
|     void StartSampling(Kernel::HLERequestContext& ctx) { | ||||
|         IPC::RequestParser rp{ctx, 0x03, 5, 0}; | ||||
| 
 | ||||
|  | @ -86,14 +170,27 @@ struct MIC_U::Impl { | |||
|         u32 audio_buffer_size = rp.Pop<u32>(); | ||||
|         bool audio_buffer_loop = rp.Pop<bool>(); | ||||
| 
 | ||||
|         if (mic->IsSampling()) { | ||||
|             LOG_CRITICAL(Service_MIC, | ||||
|                          "Application started sampling again before stopping sampling"); | ||||
|             mic->StopSampling(); | ||||
|         } | ||||
| 
 | ||||
|         auto sign = encoding == Encoding::PCM8Signed || encoding == Encoding::PCM16Signed | ||||
|                         ? Frontend::Mic::Signedness::Signed | ||||
|                         : Frontend::Mic::Signedness::Unsigned; | ||||
|         u8 sample_size = encoding == Encoding::PCM8Signed || encoding == Encoding::PCM8 ? 8 : 16; | ||||
|         state.offset = state.initial_offset = audio_buffer_offset; | ||||
|         state.sample_rate = sample_rate; | ||||
|         state.sample_size = sample_size; | ||||
|         state.looped_buffer = audio_buffer_loop; | ||||
|         state.size = audio_buffer_size; | ||||
| 
 | ||||
|         mic->StartSampling({sign, sample_size, audio_buffer_loop, GetSampleRateInHz(sample_rate), | ||||
|                             audio_buffer_offset, audio_buffer_size}); | ||||
| 
 | ||||
|         timing.ScheduleEvent(GetBufferUpdateRate(state.sample_rate), buffer_write_event); | ||||
| 
 | ||||
|         IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||||
|         rb.Push(RESULT_SUCCESS); | ||||
|         LOG_TRACE(Service_MIC, | ||||
|  | @ -229,14 +326,16 @@ struct MIC_U::Impl { | |||
|         IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||||
|         rb.Push(RESULT_SUCCESS); | ||||
|     } | ||||
|     Kernel::SharedPtr<Kernel::Event> buffer_full_event = | ||||
|         Core::System::GetInstance().Kernel().CreateEvent(Kernel::ResetType::OneShot, | ||||
|                                                          "MIC_U::buffer_full_event"); | ||||
| 
 | ||||
|     Kernel::SharedPtr<Kernel::Event> buffer_full_event; | ||||
|     Core::TimingEventType* buffer_write_event = nullptr; | ||||
|     Kernel::SharedPtr<Kernel::SharedMemory> shared_memory; | ||||
|     u32 client_version = 0; | ||||
|     bool allow_shell_closed = false; | ||||
|     bool clamp = false; | ||||
|     std::shared_ptr<Frontend::Mic::Interface> mic; | ||||
|     Core::Timing& timing; | ||||
|     State state{}; | ||||
| }; | ||||
| 
 | ||||
| void MIC_U::MapSharedMem(Kernel::HLERequestContext& ctx) { | ||||
|  | @ -324,7 +423,7 @@ MIC_U::MIC_U(Core::System& system) | |||
|         {0x00100040, &MIC_U::SetClientVersion, "SetClientVersion"}, | ||||
|     }; | ||||
| 
 | ||||
|     impl->mic = Frontend::GetCurrentMic(); | ||||
|     impl->mic = Frontend::Mic::GetCurrentMic(); | ||||
|     RegisterHandlers(functions); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -63,10 +63,13 @@ void Apply() { | |||
|     // TODO support mic hotswapping by creating the new impl, and copying any parameters to it.
 | ||||
|     switch (Settings::values.mic_input_type) { | ||||
|     case 0: | ||||
|         Frontend::RegisterMic(std::make_shared<Frontend::Mic::NullMic>()); | ||||
|         Frontend::Mic::RegisterMic(std::make_shared<Frontend::Mic::NullMic>()); | ||||
|         break; | ||||
|     case 1: | ||||
|         Frontend::RegisterMic(std::make_shared<AudioCore::CubebInput>()); | ||||
|         Frontend::Mic::RegisterMic(std::make_shared<AudioCore::CubebInput>()); | ||||
|         break; | ||||
|     case 2: | ||||
|         Frontend::Mic::RegisterMic(std::make_shared<Frontend::Mic::StaticMic>()); | ||||
|         break; | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue