forked from paulh002/sdrberry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioInput.cpp
More file actions
144 lines (124 loc) · 2.95 KB
/
AudioInput.cpp
File metadata and controls
144 lines (124 loc) · 2.95 KB
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
#include "AudioInput.h"
int record(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData)
{
AudioInput *audioinput = (AudioInput *)userData ;
DataBuffer<Sample> *databuffer = audioinput->get_databuffer();
if (status)
std::cout << "Stream overflow detected!" << std::endl;
// Do something with the data in the "inputBuffer" buffer.
//printf("frames %u \n", nBufferFrames);
SampleVector buf;
Sample l = 0.0;
for (int i = 0; i < nBufferFrames; i++)
{
Sample f = ((double *)inputBuffer)[i];
buf.push_back(f);
if (audioinput->get_stereo())
buf.push_back(f);
l += f*f;
}
audioinput->set_level(l);
databuffer->push(move(buf));
return 0;
}
bool AudioInput::init(std::string device, int pcmrate, bool stereo ,DataBuffer<Sample> *AudioBuffer)
{
if (this->getDeviceCount() < 1) {
std::cout << "\nNo audio devices found!\n";
m_zombie = true;
return false;
}
parameters.deviceId = this->getDefaultInputDevice();
/* if (parameters.deviceId)
{
m_zombie = true;
return false;
}*/
m_stereo = stereo;
databuffer = AudioBuffer;
parameters.nChannels = 1;
parameters.firstChannel = 0;
sampleRate = pcmrate;
bufferFrames = 1024; // 256 sample frames
return true;
}
bool AudioInput::open()
{
try {
this->openStream(NULL, ¶meters, RTAUDIO_FLOAT64, sampleRate, &bufferFrames, &record, (void *)this);
this->startStream();
}
catch (RtAudioError& e) {
e.printMessage();
return false;
}
return true;
}
void AudioInput::adjust_gain(SampleVector& samples)
{
for (unsigned int i = 0, n = samples.size(); i < n; i++) {
samples[i] *= m_volume;
}
}
bool AudioInput::read(SampleVector& samples)
{
if (databuffer == nullptr)
return false;
samples = databuffer->pull();
if (samples.empty())
return false;
return true;
}
void AudioInput::close()
{
if (isStreamOpen())
closeStream();
}
AudioInput::~AudioInput()
{
close();
}
#define TWOPIOVERSAMPLERATE 0.0001308996938995747; // 2 Pi / 48000
const double cw_keyer_sidetone_frequency {750.0};
const double cw_keyer_sidetone_frequency2 {1500.0};
double AudioInput::Nexttone()
{
double angle = (asteps*cw_keyer_sidetone_frequency)*TWOPIOVERSAMPLERATE;
if (++asteps >= 48000) asteps = 0;
return sin(angle);
}
void AudioInput::ToneBuffer(int twotone)
{
SampleVector buf;
for (int i = 0; i < bufferFrames; i++)
{
Sample f;
if (twotone == 2)
{
f = (Sample) NextTwotone();
}
else
{
f = (Sample) Nexttone();
}
buf.push_back(f);
if (m_stereo)
buf.push_back(f);
}
databuffer->push(move(buf));
}
double AudioInput::NextTwotone()
{
double angle = (asteps*cw_keyer_sidetone_frequency)*TWOPIOVERSAMPLERATE;
double angle2 = (asteps*cw_keyer_sidetone_frequency2)*TWOPIOVERSAMPLERATE;
if (++asteps >= 48000) asteps = 0;
return (sin(angle) + sin(angle)) /2.0;
}
float AudioInput::get_rms_level()
{
return m_level / (float)bufferFrames;
}
void AudioInput::set_level(float f)
{
m_level = f;
}