citra/src/core/frontend/mic.h

117 lines
2.6 KiB
C++
Raw Normal View History

2019-03-06 18:16:43 +00:00
// Copyright 2019 Citra Emulator Project
2019-02-27 22:13:51 +00:00
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <memory>
2019-03-05 05:47:24 +00:00
#include <vector>
2019-02-27 22:13:51 +00:00
#include "common/swap.h"
2019-03-05 05:47:24 +00:00
#include "common/threadsafe_queue.h"
2019-02-27 22:13:51 +00:00
2019-03-05 05:47:24 +00:00
namespace Frontend::Mic {
2019-02-27 22:13:51 +00:00
enum class Signedness : u8 {
Signed,
Unsigned,
};
2019-03-05 05:47:24 +00:00
using Samples = std::vector<u8>;
2019-02-27 22:13:51 +00:00
struct Parameters {
Signedness sign;
u8 sample_size;
bool buffer_loop;
u32 sample_rate;
u32 buffer_offset;
u32 buffer_size;
};
class Interface {
public:
2019-03-05 05:47:24 +00:00
Interface() = default;
2019-03-06 18:16:43 +00:00
virtual ~Interface();
2019-03-05 05:47:24 +00:00
2019-02-27 22:13:51 +00:00
/// Starts the microphone. Called by Core
2019-03-06 18:16:43 +00:00
virtual void StartSampling(const Parameters& params) = 0;
2019-02-27 22:13:51 +00:00
/// Stops the microphone. Called by Core
virtual void StopSampling() = 0;
2019-03-06 18:16:43 +00:00
/**
* 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.
*/
2019-03-05 05:47:24 +00:00
virtual Samples Read() = 0;
2019-02-27 22:13:51 +00:00
/**
* 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
*/
2019-02-27 22:13:51 +00:00
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;
}
2019-03-06 18:16:43 +00:00
const Parameters& GetParameters() const {
2019-02-27 22:13:51 +00:00
return parameters;
}
protected:
Parameters parameters;
u8 gain = 0;
bool is_sampling = false;
bool powered = false;
};
class NullMic final : public Interface {
public:
2019-03-06 18:16:43 +00:00
void StartSampling(const Parameters& params) override;
2019-02-27 22:13:51 +00:00
2019-03-06 18:16:43 +00:00
void StopSampling() override;
2019-02-27 22:13:51 +00:00
2019-03-06 18:16:43 +00:00
void AdjustSampleRate(u32 sample_rate) override;
2019-03-05 05:47:24 +00:00
2019-03-06 18:16:43 +00:00
Samples Read() override;
2019-02-27 22:13:51 +00:00
};
2019-03-05 05:47:24 +00:00
class StaticMic final : public Interface {
public:
StaticMic();
~StaticMic() override;
2019-03-06 18:16:43 +00:00
void StartSampling(const Parameters& params) override;
2019-03-05 05:47:24 +00:00
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;
};
2019-02-27 22:13:51 +00:00
2019-03-05 05:47:24 +00:00
} // namespace Frontend::Mic