1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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;
}
}
|