-
Notifications
You must be signed in to change notification settings - Fork 240
/
RspNativeBufferFactory.java
134 lines (115 loc) · 4.74 KB
/
RspNativeBufferFactory.java
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
/*
* *****************************************************************************
* Copyright (C) 2014-2023 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.source.tuner.sdrplay;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Native buffer factory for SDRPlay RSP tuners.
*
* The SDRPlay API automatically changes the length of sample buffers according to the sample rate. This causes
* problems for down-stream processing components that may be optimized for vector operations and depend on the
* sample arrays being a power-of-2 length. This class repackages the incoming sample stream into arrays of
* power-of-2 length.
*/
public class RspNativeBufferFactory
{
private RspSampleRate mRspSampleRate;
private short[] mIResidual = new short[0];
private short[] mQResidual = new short[0];
private long mResidualTimestamp = System.currentTimeMillis();
private int mIncomingBufferLength = 0;
private int mOptimalBufferLength = 128;
private float mSamplesPerMillisecond;
/**
* Constructs an instance.
* @param sampleRate of the tuner
*/
public RspNativeBufferFactory(RspSampleRate sampleRate)
{
setSampleRate(sampleRate);
}
/**
* Set or update the sample rate.
* @param sampleRate to set.
*/
public void setSampleRate(RspSampleRate sampleRate)
{
mRspSampleRate = sampleRate;
mSamplesPerMillisecond = sampleRate.getSamplesPerMillisecond();
}
/**
* Repackages the samples into optimal length buffers and returns zero or more RSP native buffers.
* @param i samples
* @param q samples
* @param timestamp of samples
* @return zero or more repackaged RSP native buffers
*/
public List<RspNativeBuffer> get(short[] i, short[] q, long timestamp)
{
updateBufferLength(i.length);
short[] iCombined = new short[mIResidual.length + i.length];
System.arraycopy(mIResidual, 0, iCombined, 0, mIResidual.length);
System.arraycopy(i, 0, iCombined, mIResidual.length, i.length);
short[] qCombined = new short[mQResidual.length + q.length];
System.arraycopy(mQResidual, 0, qCombined, 0, mQResidual.length);
System.arraycopy(q, 0, qCombined, mQResidual.length, q.length);
if(iCombined.length < mOptimalBufferLength)
{
mIResidual = iCombined;
mQResidual = qCombined;
return Collections.emptyList();
}
List<RspNativeBuffer> buffers = new ArrayList<>();
while(iCombined.length >= mOptimalBufferLength)
{
short[] iOptimal = Arrays.copyOf(iCombined, mOptimalBufferLength);
iCombined = Arrays.copyOfRange(iCombined, mOptimalBufferLength, iCombined.length);
short[] qOptimal = Arrays.copyOf(qCombined, mOptimalBufferLength);
qCombined = Arrays.copyOfRange(qCombined, mOptimalBufferLength, qCombined.length);
RspNativeBuffer buffer = new RspNativeBuffer(iOptimal, qOptimal, mResidualTimestamp, mSamplesPerMillisecond);
buffers.add(buffer);
mResidualTimestamp += (long)(mOptimalBufferLength / mSamplesPerMillisecond);
}
mIResidual = iCombined;
mQResidual = qCombined;
//For simplicity, just update the residual timestamp to be the timestamp for this latest update
mResidualTimestamp = timestamp;
return buffers;
}
/**
* Updates the optimal native buffer length based on the incoming buffer size. Optimal length is a power-of-2
* value that is closest to the buffer length to minimize the quantity of residual samples from each arriving buffer.
* @param length
*/
private void updateBufferLength(int length)
{
if(mIncomingBufferLength != length)
{
int optimal = 128;
while((optimal * 2) < length)
{
optimal *= 2;
}
mOptimalBufferLength = optimal;
mIncomingBufferLength = length;
}
}
}