-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoapyHifiBerryStreaming.cpp
224 lines (184 loc) · 5.32 KB
/
SoapyHifiBerryStreaming.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "SoapyHifiBerry.h"
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <queue>
IQSampleVector iqsamples;
void SoapyHifiBerry::setSampleRate(const int direction, const size_t channel, const double rate)
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::setSampleRate called");
int irate = floor(rate);
if (direction == SOAPY_SDR_TX)
{
}
if (direction == SOAPY_SDR_RX)
{
}
}
SoapySDR::RangeList SoapyHifiBerry::getSampleRateRange(const int direction, const size_t channel) const
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::getSampleRateRange called");
SoapySDR::RangeList rangeList;
if (direction == SOAPY_SDR_RX)
rangeList.push_back(SoapySDR::Range(48000.0, 192000.0));
if (direction == SOAPY_SDR_TX)
rangeList.push_back(SoapySDR::Range(48000.0, 384000.0));
return rangeList;
}
std::vector<std::string> SoapyHifiBerry::getStreamFormats(const int direction, const size_t channel) const
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::getStreamFormats called");
std::vector<std::string> formats;
formats.push_back(SOAPY_SDR_CF32);
//formats.push_back(SOAPY_SDR_CS16);
return formats;
}
std::string SoapyHifiBerry::getNativeStreamFormat(const int direction, const size_t channel, double &fullScale) const
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::getNativeStreamFormat called");
if (direction == SOAPY_SDR_RX)
{
fullScale = 16777216.0;
}
else if (direction == SOAPY_SDR_TX)
{
fullScale = 16777216.0;
}
return SOAPY_SDR_CF32;
}
SoapySDR::ArgInfoList SoapyHifiBerry::getStreamArgsInfo(const int direction, const size_t channel) const
{
SoapySDR::ArgInfoList streamArgs;
SoapySDR::ArgInfo bufflenArg;
bufflenArg.key = "bufflen";
bufflenArg.value = "2048";
bufflenArg.name = "Buffer Size";
bufflenArg.description = "Number of bytes per buffer, multiples of 512 only.";
bufflenArg.units = "bytes";
bufflenArg.type = SoapySDR::ArgInfo::INT;
streamArgs.push_back(bufflenArg);
return streamArgs;
}
auto startTime = std::chrono::high_resolution_clock::now();
SoapySDR::Stream *SoapyHifiBerry::setupStream(
const int direction,
const std::string &format,
const std::vector<size_t> &channels,
const SoapySDR::Kwargs &args)
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::setupStream called");
startTime = std::chrono::high_resolution_clock::now();
//check the format
sdr_stream *ptr;
ptr = new sdr_stream(direction);
if (format == SOAPY_SDR_CF32)
{
SoapySDR_log(SOAPY_SDR_INFO, "Using format CF32.");
ptr->set_stream_format(HIFIBERRY_SDR_CF32);
}
else
{
throw std::runtime_error(
"setupStream invalid format '" + format + "' -- Only CF32 is supported by SoapyHifiBerrySDR module.");
}
if (direction == SOAPY_SDR_TX)
{
iqsamples.clear();
}
setOutput(direction);
streams.push_back(ptr);
return (SoapySDR::Stream *)ptr;
}
int SoapyHifiBerry::deactivateStream(SoapySDR::Stream *stream, const int flags, const long long timeNs)
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::deactivateStream called ");
sdr_stream *ptr = (sdr_stream *)stream;
if (ptr != nullptr)
{
if (ptr->get_direction() == SOAPY_SDR_TX)
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::deactivateStream disable TX ");
setOutput(SOAPY_SDR_RX);
}
}
return 0;
}
void SoapyHifiBerry::closeStream(SoapySDR::Stream *stream)
{
int i = 0;
for (auto con : streams)
{
if ((sdr_stream *)stream == con)
{
SoapySDR_log(SOAPY_SDR_INFO, "SoapyHifiBerry::closeStream delete ");
delete ((sdr_stream *)stream);
streams.erase(streams.begin() + i);
}
i++;
}
}
std::queue<float> delay_queue;
const int delay = 0;
int SoapyHifiBerry::readStream(
SoapySDR::Stream *handle,
void *const *buffs,
const size_t numElems,
int &flags,
long long &timeNs,
const long timeoutUs)
{
int nr_samples = 0;
void *buff_base = buffs[0];
IQSample *target_buffer = (IQSample *)buff_base;
sdr_stream *ptr = (sdr_stream *)handle;
IQSampleVector iqsamples;
if (ptr->get_stream_format() == HIFIBERRY_SDR_CF32)
{
if (uptr_HifiBerryAudioInput->read(iqsamples))
{
for (auto con : iqsamples)
{
IQSample iq = con;
//printf("Iqsample %d nr_samples %d size %lu\n", ii, nr_samples, iqsamples.size());
if (delay > 0)
{
delay_queue.push(iq.real());
iq.real(0.0);
if (delay_queue.size() == delay)
{
iq.real(delay_queue.front());
delay_queue.pop();
}
}
target_buffer[nr_samples++] = iq;
//printf("I %f, Q %f\n", con.real(), con.imag());
}
}
}
//printf("SoapyHifiBerry samples overflow %d size %lu\n", nr_samples, iqsamples.size());
//printf("nr_samples %d sample: %d %d \n", nr_samples, left_sample, right_sample);
return (nr_samples); //return the number of IQ samples
}
int SoapyHifiBerry::writeStream(SoapySDR::Stream *handle, const void *const *buffs, const size_t numElems, int &flags, const long long timeNs, const long timeoutUs)
{
size_t ret;
int nr_samples;
void const *buff_base = buffs[0];
IQSample *target_buffer = (IQSample *)buff_base;
sdr_stream *ptr = (sdr_stream *)handle;
if (ptr->get_stream_format() == HIFIBERRY_SDR_CF32)
{
for (int i = 0; i < numElems; i++)
{
iqsamples.push_back(target_buffer[i]);
if (iqsamples.size() == hifiBerry_BufferSize)
uptr_HifiBerryAudioOutputput->write(iqsamples);
}
}
return numElems;
}