-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioInput.cpp
172 lines (147 loc) · 4.36 KB
/
AudioInput.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "SoapyHifiBerry.h"
#include "AudioInput.h"
#define dB2mag(x) pow(10.0, (x) / 20.0)
int HifiBerryAudioInput_record(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData)
{
HifiBerryAudioInput *hifiberryAudioInput = (HifiBerryAudioInput *)userData;
SoapyHifiBerryDataBuffer<IQSample> *databuffer = hifiberryAudioInput->get_databuffer();
if (status)
std::cout << "Stream overflow detected!" << std::endl;
IQSampleVector buf;
for (int i = 0; i < nBufferFrames; i++)
{
IQSample f = ((IQSample *)inputBuffer)[i];
buf.push_back(f);
}
databuffer->clear();
databuffer->push(move(buf));
return 0;
}
void HifiBerryAudioInput::listDevices(std::vector<std::string> &devices)
{
int noDevices = this->getDeviceCount();
struct DeviceInfo dev;
if (noDevices < 1) {
std::cout << "\nNo audio devices found!\n";
return ;
}
for (int i = 0; i < noDevices; i++)
{
dev = getDeviceInfo(i);
if (dev.outputChannels > 0 || dev.inputChannels > 0)
devices.push_back(dev.name);
}
}
int HifiBerryAudioInput::getDevices(std::string device)
{
int noDevices = this->getDeviceCount();
if (noDevices < 1) {
std::cout << "\nNo audio devices found!\n";
return -1;
}
for (int i = 0; i < noDevices; i++)
{
info = getDeviceInfo(i);
printf("%d device: %s input %d output %d\n", i, info.name.c_str(), info.inputChannels, info.outputChannels);
if (info.name.find(device) != std::string::npos && info.inputChannels > 0)
{
if (info.outputChannels < parameters.nChannels)
parameters.nChannels = info.outputChannels;
return i;
}
}
return 0; // return default device
}
HifiBerryAudioInput::HifiBerryAudioInput(unsigned int pcmrate, bool stereo, SoapyHifiBerryDataBuffer<IQSample> *AudioBuffer, RtAudio::Api api)
: RtAudio(api), parameters{}, m_volume{0.5}, asteps{}, tune_tone{0}
{
m_stereo = stereo;
databuffer = AudioBuffer;
parameters.nChannels = 2;
parameters.firstChannel = 0;
sampleRate = pcmrate;
bufferFrames = hifiBerry_BufferSize;
gaindb = 0;
databuffer->clear();
}
std::vector<RtAudio::Api> HifiBerryAudioInput::listApis()
{
std::vector<RtAudio::Api> apis;
RtAudio ::getCompiledApi(apis);
std::cout << "\nCompiled APIs:\n";
for (size_t i = 0; i < apis.size(); i++)
std::cout << i << ". " << RtAudio::getApiDisplayName(apis[i])
<< " (" << RtAudio::getApiName(apis[i]) << ")" << std::endl;
return apis;
}
bool HifiBerryAudioInput::open(std::string device)
{
RtAudioErrorType err;
if (this->getDeviceCount() < 1)
{
std::cout << "\nNo audio devices found!\n";
return false;
}
parameters.nChannels = 2; // info.inputChannels;
err = this->openStream(NULL, ¶meters, RTAUDIO_FLOAT64, sampleRate, &bufferFrames, &HifiBerryAudioInput_record, (void *)this);
if (err != RTAUDIO_NO_ERROR)
{
printf("HifiBerry Cannot open audio input stream\n");
return false;
}
this->startStream();
printf("HifiBerry audio input device = %d %s samplerate %d bufferFrames %d\n", parameters.deviceId, device.c_str(), sampleRate, bufferFrames);
return true;
}
bool HifiBerryAudioInput::open(unsigned int device)
{
RtAudioErrorType err;
parameters.deviceId = device;
info = getDeviceInfo(device);
parameters.nChannels = 2; // info.inputChannels;
err = this->openStream(NULL, ¶meters, RTAUDIO_FLOAT32, sampleRate, &bufferFrames, &HifiBerryAudioInput_record, (void *)this);
if (err != RTAUDIO_NO_ERROR)
{
printf("HifiBerry Cannot open audio input stream\n");
return false;
}
this->startStream();
printf("HifiBerry audio input device = %d samplerate %d bufferFrames %d\n", parameters.deviceId, sampleRate, bufferFrames);
return true;
}
void HifiBerryAudioInput::set_volume(int vol)
{
// log volume
m_volume = exp(((double)vol * 6.908) / 100.0) / 5.0;
printf("mic vol %f\n", (float)m_volume);
}
void HifiBerryAudioInput::adjust_gain(IQSampleVector& samples)
{
for (unsigned int i = 0, n = samples.size(); i < n; i++) {
samples[i] *= m_volume * dB2mag(gaindb);
}
}
bool HifiBerryAudioInput::read(IQSampleVector& samples)
{
if (databuffer == nullptr)
return false;
if (!isStreamOpen())
{
printf("Stream Closed \n");
return false;
}
samples = databuffer->pull();
if (samples.empty())
return false;
adjust_gain(samples);
return true;
}
void HifiBerryAudioInput::close()
{
if (isStreamOpen())
closeStream();
}
HifiBerryAudioInput::~HifiBerryAudioInput()
{
close();
}