aboutsummaryrefslogtreecommitdiff
path: root/src/audio.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio.cpp')
-rw-r--r--src/audio.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/audio.cpp b/src/audio.cpp
new file mode 100644
index 0000000..e5aed56
--- /dev/null
+++ b/src/audio.cpp
@@ -0,0 +1,82 @@
+#include "audio.hpp"
+#include <bits/stdc++.h>
+
+using namespace std;
+
+AudioData::AudioData()
+{
+ this->device_num = 0;
+ this->channel_cnt = 0;
+ this->channels = NULL;
+ this->stream = NULL;
+}
+
+int AudioData::get_channel_cnt()
+{
+ return this->channel_cnt;
+}
+
+float* AudioData::get_channels()
+{
+ return this->channels;
+}
+
+void AudioData::set_device_num(int device_num)
+{
+ this->device_num = device_num;
+}
+
+int AudioData::patestCallback(const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void* userData)
+{
+ AudioData* data = (AudioData*)userData;
+ float* in = (float*)inputBuffer;
+
+ for (int i = 0; i < data->channel_cnt; i++) {
+ if (data->channels[i] > 0) {
+ data->channels[i] -= 0.005;
+ }
+ }
+
+ for (unsigned long i = 0; i < framesPerBuffer * data->channel_cnt; i += data->channel_cnt) {
+ for (int j = 0; j < data->channel_cnt; j++) {
+ data->channels[j] = max(data->channels[j], fabs(in[i + j]));
+ }
+ }
+
+ return 0;
+}
+
+void AudioData::start_stream()
+{
+ close_stream();
+ Pa_Initialize();
+
+ PaStreamParameters inputParameters;
+ memset(&inputParameters, 0, sizeof(inputParameters));
+ inputParameters.channelCount = Pa_GetDeviceInfo(device_num)->maxInputChannels;
+ inputParameters.device = device_num;
+ inputParameters.hostApiSpecificStreamInfo = NULL;
+ inputParameters.sampleFormat = paFloat32;
+ inputParameters.suggestedLatency = Pa_GetDeviceInfo(device_num)->defaultLowInputLatency;
+
+ this->channel_cnt = inputParameters.channelCount;
+ this->channels = (float*)calloc(this->channel_cnt, sizeof(float));
+
+ Pa_OpenStream(&stream, &inputParameters, NULL, Pa_GetDeviceInfo(device_num)->defaultSampleRate, FRAMES_PER_BUFFER, paNoFlag, &this->patestCallback, this);
+ Pa_StartStream(stream);
+}
+
+void AudioData::close_stream()
+{
+ if (this->stream != NULL) {
+ Pa_StopStream(this->stream);
+ Pa_CloseStream(this->stream);
+ Pa_Terminate();
+ this->stream = NULL;
+ }
+
+ if (this->channels) {
+ free(this->channels);
+ this->channels = NULL;
+ }
+}