Merge pull request #4671 from jroweboy/mic4

Microphone support
This commit is contained in:
Vamsi Krishna 2019-03-20 23:12:38 +05:30 committed by GitHub
commit aedf5a84b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 714 additions and 60 deletions

60
src/core/frontend/mic.cpp Normal file
View file

@ -0,0 +1,60 @@
// Copyright 2019 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <array>
#include "core/frontend/mic.h"
namespace Frontend::Mic {
constexpr std::array<u8, 16> NOISE_SAMPLE_8_BIT = {0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xF5, 0xFF, 0xFF, 0xFF, 0xFF, 0x8E, 0xFF};
constexpr std::array<u8, 32> 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};
Interface::~Interface() = default;
void NullMic::StartSampling(const Parameters& params) {
parameters = params;
is_sampling = true;
}
void NullMic::StopSampling() {
is_sampling = false;
}
void NullMic::AdjustSampleRate(u32 sample_rate) {
parameters.sample_rate = sample_rate;
}
Samples NullMic::Read() {
return {};
}
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(const 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;
}
} // namespace Frontend::Mic

116
src/core/frontend/mic.h Normal file
View file

@ -0,0 +1,116 @@
// Copyright 2019 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include <vector>
#include "common/swap.h"
#include "common/threadsafe_queue.h"
namespace Frontend::Mic {
enum class Signedness : u8 {
Signed,
Unsigned,
};
using Samples = std::vector<u8>;
struct Parameters {
Signedness sign;
u8 sample_size;
bool buffer_loop;
u32 sample_rate;
u32 buffer_offset;
u32 buffer_size;
};
class Interface {
public:
Interface() = default;
virtual ~Interface();
/// Starts the microphone. Called by Core
virtual void StartSampling(const Parameters& params) = 0;
/// Stops the microphone. Called by Core
virtual void StopSampling() = 0;
/**
* Called from the actual event timing at a constant period under a given sample rate.
* When sampling is enabled this function is expected to return a buffer of 16 samples in ideal
* conditions, but can be lax if the data is coming in from another source like a real mic.
*/
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
*/
virtual void AdjustSampleRate(u32 sample_rate) = 0;
/// Value from 0 - 100 to adjust the mic gain setting. Called by Core
virtual void SetGain(u8 mic_gain) {
gain = mic_gain;
}
u8 GetGain() const {
return gain;
}
void SetPower(bool power) {
powered = power;
}
bool GetPower() const {
return powered;
}
bool IsSampling() const {
return is_sampling;
}
const Parameters& GetParameters() const {
return parameters;
}
protected:
Parameters parameters;
u8 gain = 0;
bool is_sampling = false;
bool powered = false;
};
class NullMic final : public Interface {
public:
void StartSampling(const Parameters& params) override;
void StopSampling() override;
void AdjustSampleRate(u32 sample_rate) override;
Samples Read() override;
};
class StaticMic final : public Interface {
public:
StaticMic();
~StaticMic() override;
void StartSampling(const 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;
};
} // namespace Frontend::Mic