VulcanoLE/headers/VulcanoLE/Audio/AudioGrabber.h

59 lines
1.8 KiB
C++

#pragma once
#include <pulse/error.h>
#include <pulse/pulseaudio.h>
#include <pulse/simple.h>
#include <unistd.h>
#include <string>
#include <fstream>
#include <memory>
#include <mutex>
#include <VUtils/Environment.h>
#include <VulcanoLE/Audio/FFT.h>
#include <VulcanoLE/Audio/Types.h>
static const char k_record_stream_name[] = "vulcanoLE";
static const char k_record_stream_description[] = "Keyboard Input Stream";
static const int32_t k_sample_rate = 44100;
static const int32_t k_channels = 2;
static const std::string k_default_monitor_postfix = ".monitor";
class AudioGrabber {
public:
enum class ReqMode {
FFT = 0,
RMS = 1,
PEAK = 2,
ALL = 3
};
AudioGrabber();
~AudioGrabber();
bool read(pcm_stereo_sample *buffer, uint32_t buffer_size);
static AudioGrabber* createAudioGrabber();
void init();
FFT fft;
ReqMode requestMode = ReqMode::FFT;
double loudness = 0.0;
float getLoudness();
bool doWork();
VUtils::Environment *env = nullptr;
private:
std::mutex m_mtx;
pcm_stereo_sample *m_pcm_buffer{};
pa_simple *m_pulseaudio_simple{};
pa_mainloop *m_pulseaudio_mainloop{};
std::string m_pulseaudio_default_source_name;
void populate_default_source_name();
bool open_pulseaudio_source(uint32_t max_buffer_size);
static void pulseaudio_context_state_callback(pa_context *c,
void *userdata);
static void pulseaudio_server_info_callback(pa_context *context,
const pa_server_info *i,
void *userdata);
void calculateRMS(pcm_stereo_sample *pFrame);
void calculatePEAK(pcm_stereo_sample *pFrame);
double m_scale = 1.0;
};