add audio::InputDevice
This commit is contained in:
parent
2b72f87c64
commit
27416ab0cd
@ -37,6 +37,38 @@ std::unique_ptr<Speaker> ALSound::newInstance(int priority, int channel) const {
|
|||||||
return speaker;
|
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(
|
ALStream::ALStream(
|
||||||
ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSource
|
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);
|
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() {
|
std::unique_ptr<ALAudio> ALAudio::create() {
|
||||||
ALCdevice* device = alcOpenDevice(nullptr);
|
ALCdevice* device = alcOpenDevice(nullptr);
|
||||||
if (device == nullptr) return nullptr;
|
if (device == nullptr) return nullptr;
|
||||||
|
|||||||
@ -82,6 +82,26 @@ namespace audio {
|
|||||||
static inline constexpr uint STREAM_BUFFERS = 3;
|
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
|
/// @brief AL source adapter
|
||||||
class ALSpeaker : public Speaker {
|
class ALSpeaker : public Speaker {
|
||||||
ALAudio* al;
|
ALAudio* al;
|
||||||
@ -157,10 +177,15 @@ namespace audio {
|
|||||||
std::unique_ptr<Sound> createSound(
|
std::unique_ptr<Sound> createSound(
|
||||||
std::shared_ptr<PCM> pcm, bool keepPCM
|
std::shared_ptr<PCM> pcm, bool keepPCM
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
std::unique_ptr<Stream> openStream(
|
std::unique_ptr<Stream> openStream(
|
||||||
std::shared_ptr<PCMStream> stream, bool keepSource
|
std::shared_ptr<PCMStream> stream, bool keepSource
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
|
std::unique_ptr<InputDevice> openInputDevice(
|
||||||
|
uint sampleRate, uint channels, uint bitsPerSample
|
||||||
|
) override;
|
||||||
|
|
||||||
void setListener(
|
void setListener(
|
||||||
glm::vec3 position,
|
glm::vec3 position,
|
||||||
glm::vec3 velocity,
|
glm::vec3 velocity,
|
||||||
|
|||||||
@ -71,10 +71,17 @@ namespace audio {
|
|||||||
std::unique_ptr<Sound> createSound(
|
std::unique_ptr<Sound> createSound(
|
||||||
std::shared_ptr<PCM> pcm, bool keepPCM
|
std::shared_ptr<PCM> pcm, bool keepPCM
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
std::unique_ptr<Stream> openStream(
|
std::unique_ptr<Stream> openStream(
|
||||||
std::shared_ptr<PCMStream> stream, bool keepSource
|
std::shared_ptr<PCMStream> stream, bool keepSource
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
|
std::unique_ptr<InputDevice> openInputDevice(
|
||||||
|
uint sampleRate, uint channels, uint bitsPerSample
|
||||||
|
) override {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
void setListener(
|
void setListener(
|
||||||
glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
|
glm::vec3 position, glm::vec3 velocity, glm::vec3 at, glm::vec3 up
|
||||||
) override {
|
) override {
|
||||||
|
|||||||
@ -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
|
/// @brief audio::PCMStream is a data source for audio::Stream
|
||||||
class PCMStream {
|
class PCMStream {
|
||||||
public:
|
public:
|
||||||
@ -345,6 +359,9 @@ namespace audio {
|
|||||||
virtual std::unique_ptr<Stream> openStream(
|
virtual std::unique_ptr<Stream> openStream(
|
||||||
std::shared_ptr<PCMStream> stream, bool keepSource
|
std::shared_ptr<PCMStream> stream, bool keepSource
|
||||||
) = 0;
|
) = 0;
|
||||||
|
virtual std::unique_ptr<InputDevice> openInputDevice(
|
||||||
|
uint sampleRate, uint channels, uint bitsPerSample
|
||||||
|
) = 0;
|
||||||
virtual void setListener(
|
virtual void setListener(
|
||||||
glm::vec3 position,
|
glm::vec3 position,
|
||||||
glm::vec3 velocity,
|
glm::vec3 velocity,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user