-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
FskModulator.java
72 lines (58 loc) · 1.75 KB
/
FskModulator.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
package ru.r2cloud.jradio.blocks;
import java.io.IOException;
import ru.r2cloud.jradio.ByteInput;
import ru.r2cloud.jradio.ComplexFloatInput;
import ru.r2cloud.jradio.Context;
import ru.r2cloud.jradio.FloatValueSource;
import ru.r2cloud.jradio.util.NumericallyControlledOscillator;
public class FskModulator extends ComplexFloatInput implements FloatValueSource {
private final ByteInput source;
private final int sps;
private final float[] complex = new float[2];
private final Context context;
private final NumericallyControlledOscillator nco;
private final float markFreq;
private final float spaceFreq;
private float currentFreq;
private int currentSymbol = 0;
public FskModulator(ByteInput source, int samplesPerSymbol, float centerFreq, float shift) {
this.source = source;
this.nco = new NumericallyControlledOscillator(this, 1.0);
this.sps = samplesPerSymbol;
this.currentSymbol = sps;
this.context = new Context(source.getContext());
context.setSampleRate(context.getSampleRate() * sps);
if (context.getTotalSamples() != null) {
context.setTotalSamples(context.getTotalSamples() * sps);
}
context.setChannels(2);
markFreq = (centerFreq + shift / 2) / context.getSampleRate();
spaceFreq = (centerFreq - shift / 2) / context.getSampleRate();
}
@Override
public float[] readComplex() throws IOException {
if (currentSymbol >= sps) {
if (source.readByte() == 1) {
currentFreq = markFreq;
} else {
currentFreq = spaceFreq;
}
currentSymbol = 0;
}
nco.sincos(complex);
currentSymbol++;
return complex;
}
@Override
public float getValue() {
return currentFreq;
}
@Override
public void close() throws IOException {
source.close();
}
@Override
public Context getContext() {
return context;
}
}