add audio::InputDevice
This commit is contained in:
@@ -37,6 +37,38 @@ std::unique_ptr<Speaker> ALSound::newInstance(int priority, int channel) const {
|
||||
return speaker;
|
||||
}
|
||||
|
||||
ALInputDevice::ALInputDevice(
|
||||
ALAudio* al, ALCdevice* device, uint channels, uint bitsPerSample
|
||||
)
|
||||
: al(al), device(device), channels(channels), bitsPerSample(bitsPerSample) {
|
||||
}
|
||||
|
||||
ALInputDevice::~ALInputDevice() {
|
||||
alcCaptureCloseDevice(device);
|
||||
}
|
||||
|
||||
void ALInputDevice::startCapture() {
|
||||
AL_CHECK(alcCaptureStart(device));
|
||||
}
|
||||
|
||||
void ALInputDevice::stopCapture() {
|
||||
AL_CHECK(alcCaptureStop(device));
|
||||
}
|
||||
|
||||
uint ALInputDevice::getChannels() const {
|
||||
return channels;
|
||||
}
|
||||
|
||||
size_t ALInputDevice::read(char* buffer, size_t bufferSize) {
|
||||
ALCint samplesCount;
|
||||
AL_CHECK(alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, 1, &samplesCount));
|
||||
size_t samplesRead = std::min<ALCsizei>(
|
||||
samplesCount, bufferSize / channels / (bitsPerSample >> 3)
|
||||
);
|
||||
AL_CHECK(alcCaptureSamples(device, buffer, samplesRead));
|
||||
return samplesRead;
|
||||
}
|
||||
|
||||
ALStream::ALStream(
|
||||
ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSource
|
||||
)
|
||||
@@ -411,6 +443,21 @@ std::unique_ptr<Stream> ALAudio::openStream(
|
||||
return std::make_unique<ALStream>(this, stream, keepSource);
|
||||
}
|
||||
|
||||
std::unique_ptr<InputDevice> ALAudio::openInputDevice(
|
||||
uint sampleRate, uint channels, uint bitsPerSample
|
||||
) {
|
||||
uint bps = bitsPerSample >> 3;
|
||||
AL_CHECK(
|
||||
ALCdevice* device = alcCaptureOpenDevice(
|
||||
nullptr,
|
||||
sampleRate,
|
||||
AL::to_al_format(channels, bps),
|
||||
sampleRate * channels * bps
|
||||
)
|
||||
);
|
||||
return std::make_unique<ALInputDevice>(this, device, channels, bps);
|
||||
}
|
||||
|
||||
std::unique_ptr<ALAudio> ALAudio::create() {
|
||||
ALCdevice* device = alcOpenDevice(nullptr);
|
||||
if (device == nullptr) return nullptr;
|
||||
|
||||
@@ -82,6 +82,26 @@ namespace audio {
|
||||
static inline constexpr uint STREAM_BUFFERS = 3;
|
||||
};
|
||||
|
||||
class ALInputDevice : public InputDevice {
|
||||
public:
|
||||
ALInputDevice(
|
||||
ALAudio* al, ALCdevice* device, uint channels, uint bitsPerSample
|
||||
);
|
||||
~ALInputDevice() override;
|
||||
|
||||
void startCapture() override;
|
||||
void stopCapture() override;
|
||||
|
||||
uint getChannels() const override;
|
||||
|
||||
size_t read(char* buffer, size_t bufferSize) override;
|
||||
private:
|
||||
ALAudio* al;
|
||||
ALCdevice* device;
|
||||
uint channels;
|
||||
uint bitsPerSample;
|
||||
};
|
||||
|
||||
/// @brief AL source adapter
|
||||
class ALSpeaker : public Speaker {
|
||||
ALAudio* al;
|
||||
@@ -157,10 +177,15 @@ namespace audio {
|
||||
std::unique_ptr<Sound> createSound(
|
||||
std::shared_ptr<PCM> pcm, bool keepPCM
|
||||
) override;
|
||||
|
||||
std::unique_ptr<Stream> openStream(
|
||||
std::shared_ptr<PCMStream> stream, bool keepSource
|
||||
) override;
|
||||
|
||||
std::unique_ptr<InputDevice> openInputDevice(
|
||||
uint sampleRate, uint channels, uint bitsPerSample
|
||||
) override;
|
||||
|
||||
void setListener(
|
||||
glm::vec3 position,
|
||||
glm::vec3 velocity,
|
||||
|
||||
Reference in New Issue
Block a user