add audio::InputDevice

This commit is contained in:
MihailRis 2025-10-14 12:43:51 +03:00
parent 2b72f87c64
commit 27416ab0cd
4 changed files with 96 additions and 0 deletions

View File

@ -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;

View File

@ -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,

View File

@ -71,10 +71,17 @@ 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 {
return nullptr;
}
void setListener(
glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
) override {

View File

@ -108,6 +108,20 @@ namespace audio {
}
};
class InputDevice {
public:
virtual ~InputDevice() {};
virtual void startCapture() = 0;
virtual void stopCapture() = 0;
/// @brief Get number of audio channels
/// @return 1 if mono, 2 if stereo
virtual uint getChannels() const = 0;
virtual size_t read(char* buffer, size_t bufferSize) = 0;
};
/// @brief audio::PCMStream is a data source for audio::Stream
class PCMStream {
public:
@ -345,6 +359,9 @@ namespace audio {
virtual std::unique_ptr<Stream> openStream(
std::shared_ptr<PCMStream> stream, bool keepSource
) = 0;
virtual std::unique_ptr<InputDevice> openInputDevice(
uint sampleRate, uint channels, uint bitsPerSample
) = 0;
virtual void setListener(
glm::vec3 position,
glm::vec3 velocity,