Removed OpenAL and now using PortAudio

This commit is contained in:
Charles J. Cliffe
2014-11-09 04:38:33 -05:00
parent c3c204bc04
commit b70b659a5f
47 changed files with 5635 additions and 4332 deletions
-698
View File
@@ -1,698 +0,0 @@
#include "Demodulate.h"
#include "CubicSDRDefs.h"
#include <math.h>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#define DEFAULT_SAMPLE_RATE 24000
#define DEFAULT_BUF_LENGTH (1 * 16384)
#define MAXIMUM_OVERSAMPLE 16
#define MAXIMUM_BUF_LENGTH (MAXIMUM_OVERSAMPLE * DEFAULT_BUF_LENGTH)
#define AUTO_GAIN -100
#define BUFFER_DUMP 4096
//#define MAXIMUM_RATE 2400000
#define PI_INT (1<<14)
#define ONE_INT (1<<14)
static int *atan_lut = NULL;
static int atan_lut_size = 131072; /* 512 KB */
static int atan_lut_coef = 8;
//static uint32_t MINIMUM_RATE = 1000000;
/* {length, coef, coef, coef} and scaled by 2^15
for now, only length 9, optimal way to get +85% bandwidth */
#define CIC_TABLE_MAX 10
int cic_9_tables[][10] = { { 0, }, { 9, -156, -97, 2798, -15489, 61019, -15489, 2798, -97, -156 }, { 9, -128, -568, 5593, -24125, 74126, -24125, 5593,
-568, -128 }, { 9, -129, -639, 6187, -26281, 77511, -26281, 6187, -639, -129 },
{ 9, -122, -612, 6082, -26353, 77818, -26353, 6082, -612, -122 }, { 9, -120, -602, 6015, -26269, 77757, -26269, 6015, -602, -120 }, { 9, -120,
-582, 5951, -26128, 77542, -26128, 5951, -582, -120 }, { 9, -119, -580, 5931, -26094, 77505, -26094, 5931, -580, -119 }, { 9, -119,
-578, 5921, -26077, 77484, -26077, 5921, -578, -119 }, { 9, -119, -577, 5917, -26067, 77473, -26067, 5917, -577, -119 }, { 9, -199,
-362, 5303, -25505, 77489, -25505, 5303, -362, -199 }, };
#ifdef _MSC_VER
double log2(double n)
{
return log(n) / log(2.0);
}
#endif
void rotate_90(unsigned char *buf, uint32_t len)
/* 90 rotation is 1+0j, 0+1j, -1+0j, 0-1j
or [0, 1, -3, 2, -4, -5, 7, -6] */
{
uint32_t i;
unsigned char tmp;
for (i = 0; i < len; i += 8) {
/* uint8_t negation = 255 - x */
tmp = 255 - buf[i + 3];
buf[i + 3] = buf[i + 2];
buf[i + 2] = tmp;
buf[i + 4] = 255 - buf[i + 4];
buf[i + 5] = 255 - buf[i + 5];
tmp = 255 - buf[i + 6];
buf[i + 6] = buf[i + 7];
buf[i + 7] = tmp;
}
}
int translate_init(struct translate_state *ts)
/* two pass: first to find optimal length, second to alloc/fill */
{
int max_length = 100000;
int i, s, c, best_i;
double a, a2, err, best_360;
if (fabs(ts->angle) < 2 * M_PI / max_length) {
std::cout << "angle too small or array too short\n" << std::endl;
return 1;
}
ts->i = 0;
ts->sincos = NULL;
if (ts->len) {
max_length = ts->len;
ts->sincos = (int16_t *) malloc(max_length * sizeof(int16_t));
}
a = 0.0;
err = 0.0;
best_i = 0;
best_360 = 4.0;
for (i = 0; i < max_length; i += 2) {
s = (int) round(sin(a + err) * (1 << 14));
c = (int) round(cos(a + err) * (1 << 14));
a2 = atan2(s, c);
err = fmod(a, 2 * M_PI) - a2;
a += ts->angle;
while (a > M_PI) {
a -= 2 * M_PI;
}
while (a < -M_PI) {
a += 2 * M_PI;
}
if (i != 0 && fabs(a) < best_360) {
best_i = i;
best_360 = fabs(a);
}
if (i != 0 && fabs(a) < 0.0000001) {
break;
}
if (ts->sincos) {
ts->sincos[i] = s;
ts->sincos[i + 1] = c;
//fprintf(stderr, "%i %i %i\n", i, s, c);
}
}
if (ts->sincos) {
return 0;
}
ts->len = best_i + 2;
return translate_init(ts);
}
void translate(Demodulate *d) {
int i, len, sc_i, sc_len;
int32_t tmp, ar, aj, br, bj;
int16_t *buf = d->lowpassed;
int16_t *sincos = d->rotate.sincos;
len = d->lp_len;
sc_i = d->rotate.i;
sc_len = d->rotate.len;
for (i = 0; i < len; i += 2, sc_i += 2) {
sc_i = sc_i % sc_len;
ar = (int32_t) buf[i];
aj = (int32_t) buf[i + 1];
br = (int32_t) sincos[sc_i];
bj = (int32_t) sincos[sc_i + 1];
tmp = ar * br - aj * bj;
buf[i] = (int16_t) (tmp >> 14);
tmp = aj * br + ar * bj;
buf[i + 1] = (int16_t) (tmp >> 14);
}
d->rotate.i = sc_i;
}
void low_pass(Demodulate *d)
/* simple square window FIR */
{
int i = 0, i2 = 0;
while (i < d->lp_len) {
d->now_r += d->lowpassed[i];
d->now_j += d->lowpassed[i + 1];
i += 2;
d->prev_index++;
if (d->prev_index < d->downsample) {
continue;
}
d->lowpassed[i2] = d->now_r; // * d->output_scale;
d->lowpassed[i2 + 1] = d->now_j; // * d->output_scale;
d->prev_index = 0;
d->now_r = 0;
d->now_j = 0;
i2 += 2;
}
d->lp_len = i2;
}
int low_pass_simple(int16_t *signal2, int len, int step)
// no wrap around, length must be multiple of step
{
int i, i2, sum;
for (i = 0; i < len; i += step) {
sum = 0;
for (i2 = 0; i2 < step; i2++) {
sum += (int) signal2[i + i2];
}
//signal2[i/step] = (int16_t)(sum / step);
signal2[i / step] = (int16_t) (sum);
}
signal2[i / step + 1] = signal2[i / step];
return len / step;
}
void low_pass_real(Demodulate *s)
/* simple square window FIR */
// add support for upsampling?
{
int16_t *lp = s->lowpassed;
int i = 0, i2 = 0;
int fast = (int) s->rate_out;
int slow = s->rate_out2;
while (i < s->lp_len) {
s->now_lpr += lp[i];
i++;
s->prev_lpr_index += slow;
if (s->prev_lpr_index < fast) {
continue;
}
lp[i2] = (int16_t) (s->now_lpr / (fast / slow));
s->prev_lpr_index -= fast;
s->now_lpr = 0;
i2 += 1;
}
s->lp_len = i2;
}
void fifth_order(int16_t *data, int length, int16_t *hist)
/* for half of interleaved data */
{
int i;
int16_t a, b, c, d, e, f;
a = hist[1];
b = hist[2];
c = hist[3];
d = hist[4];
e = hist[5];
f = data[0];
/* a downsample should improve resolution, so don't fully shift */
data[0] = (a + (b + e) * 5 + (c + d) * 10 + f) >> 4;
for (i = 4; i < length; i += 4) {
a = c;
b = d;
c = e;
d = f;
e = data[i - 2];
f = data[i];
data[i / 2] = (a + (b + e) * 5 + (c + d) * 10 + f) >> 4;
}
/* archive */
hist[0] = a;
hist[1] = b;
hist[2] = c;
hist[3] = d;
hist[4] = e;
hist[5] = f;
}
void generic_fir(int16_t *data, int length, int *fir, int16_t *hist)
/* Okay, not at all generic. Assumes length 9, fix that eventually. */
{
int d, temp, sum;
for (d = 0; d < length; d += 2) {
temp = data[d];
sum = 0;
sum += (hist[0] + hist[8]) * fir[1];
sum += (hist[1] + hist[7]) * fir[2];
sum += (hist[2] + hist[6]) * fir[3];
sum += (hist[3] + hist[5]) * fir[4];
sum += hist[4] * fir[5];
data[d] = sum >> 15;
hist[0] = hist[1];
hist[1] = hist[2];
hist[2] = hist[3];
hist[3] = hist[4];
hist[4] = hist[5];
hist[5] = hist[6];
hist[6] = hist[7];
hist[7] = hist[8];
hist[8] = temp;
}
}
/* define our own complex math ops
because ARMv5 has no hardware float */
void multiply(int ar, int aj, int br, int bj, int *cr, int *cj) {
*cr = ar * br - aj * bj;
*cj = aj * br + ar * bj;
}
int polar_discriminant(int ar, int aj, int br, int bj) {
int cr, cj;
double angle;
multiply(ar, aj, br, -bj, &cr, &cj);
angle = atan2((double) cj, (double) cr);
return (int) (angle / M_PI * (1 << 14));
}
int fast_atan2(int y, int x)
/* pre scaled for int16 */
{
int yabs, angle;
int pi4 = (1 << 12), pi34 = 3 * (1 << 12); // note pi = 1<<14
if (x == 0 && y == 0) {
return 0;
}
yabs = y;
if (yabs < 0) {
yabs = -yabs;
}
if (x >= 0) {
angle = pi4 - pi4 * (x - yabs) / (x + yabs);
} else {
angle = pi34 - pi4 * (x + yabs) / (yabs - x);
}
if (y < 0) {
return -angle;
}
return angle;
}
int polar_disc_fast(int ar, int aj, int br, int bj) {
int cr, cj;
multiply(ar, aj, br, -bj, &cr, &cj);
return fast_atan2(cj, cr);
}
int atan_lut_init(void) {
int i = 0;
atan_lut = (int *) malloc(atan_lut_size * sizeof(int));
for (i = 0; i < atan_lut_size; i++) {
atan_lut[i] = (int) (atan((double) i / (1 << atan_lut_coef)) / M_PI * (1 << 14));
}
return 0;
}
int polar_disc_lut(int ar, int aj, int br, int bj) {
int cr, cj, x, x_abs;
multiply(ar, aj, br, -bj, &cr, &cj);
/* special cases */
if (cr == 0 || cj == 0) {
if (cr == 0 && cj == 0) {
return 0;
}
if (cr == 0 && cj > 0) {
return 1 << 13;
}
if (cr == 0 && cj < 0) {
return -(1 << 13);
}
if (cj == 0 && cr > 0) {
return 0;
}
if (cj == 0 && cr < 0) {
return 1 << 14;
}
}
/* real range -32768 - 32768 use 64x range -> absolute maximum: 2097152 */
x = (cj << atan_lut_coef) / cr;
x_abs = abs(x);
if (x_abs >= atan_lut_size) {
/* we can use linear range, but it is not necessary */
return (cj > 0) ? 1 << 13 : -1 << 13;
}
if (x > 0) {
return (cj > 0) ? atan_lut[x] : atan_lut[x] - (1 << 14);
} else {
return (cj > 0) ? (1 << 14) - atan_lut[-x] : -atan_lut[-x];
}
return 0;
}
int esbensen(int ar, int aj, int br, int bj)
/*
input signal: s(t) = a*exp(-i*w*t+p)
a = amplitude, w = angular freq, p = phase difference
solve w
s' = -i(w)*a*exp(-i*w*t+p)
s'*conj(s) = -i*w*a*a
s'*conj(s) / |s|^2 = -i*w
*/
{
int cj, dr, dj;
int scaled_pi = 2608; /* 1<<14 / (2*pi) */
dr = (br - ar) * 2;
dj = (bj - aj) * 2;
cj = bj * dr - br * dj; /* imag(ds*conj(s)) */
return (scaled_pi * cj / (ar * ar + aj * aj + 1));
}
void fm_demod(Demodulate *fm) {
int i, pcm = 0;
int16_t *lp = fm->lowpassed;
int16_t pr = fm->pre_r;
int16_t pj = fm->pre_j;
for (i = 0; i < (fm->lp_len - 1); i += 2) {
switch (fm->custom_atan) {
case 0:
pcm = polar_discriminant(lp[i], lp[i + 1], pr, pj);
break;
case 1:
pcm = polar_disc_fast(lp[i], lp[i + 1], pr, pj);
break;
case 2:
pcm = polar_disc_lut(lp[i], lp[i + 1], pr, pj);
break;
case 3:
pcm = esbensen(lp[i], lp[i + 1], pr, pj);
break;
}
pr = lp[i];
pj = lp[i + 1];
fm->lowpassed[i / 2] = (int16_t) pcm;
}
fm->pre_r = pr;
fm->pre_j = pj;
fm->lp_len = fm->lp_len / 2;
}
void am_demod(Demodulate *fm)
// todo, fix this extreme laziness
{
int32_t i, pcm;
int16_t *lp = fm->lowpassed;
for (i = 0; i < fm->lp_len; i += 2) {
// hypot uses floats but won't overflow
//r[i/2] = (int16_t)hypot(lp[i], lp[i+1]);
pcm = lp[i] * lp[i];
pcm += lp[i + 1] * lp[i + 1];
lp[i / 2] = (int16_t) sqrt(pcm) * fm->output_scale;
}
fm->lp_len = fm->lp_len / 2;
// lowpass? (3khz)
}
void usb_demod(Demodulate *fm) {
int i, pcm;
int16_t *lp = fm->lowpassed;
for (i = 0; i < fm->lp_len; i += 2) {
pcm = lp[i] + lp[i + 1];
lp[i / 2] = (int16_t) pcm * fm->output_scale;
}
fm->lp_len = fm->lp_len / 2;
}
void lsb_demod(Demodulate *fm) {
int i, pcm;
int16_t *lp = fm->lowpassed;
for (i = 0; i < fm->lp_len; i += 2) {
pcm = lp[i] - lp[i + 1];
lp[i / 2] = (int16_t) pcm * fm->output_scale;
}
fm->lp_len = fm->lp_len / 2;
}
void raw_demod(Demodulate *fm) {
return;
}
void deemph_filter(Demodulate *fm) {
static int avg; // cheating, not threadsafe
int i, d;
int16_t *lp = fm->lowpassed;
// de-emph IIR
// avg = avg * (1 - alpha) + sample * alpha;
for (i = 0; i < fm->lp_len; i++) {
d = lp[i] - avg;
if (d > 0) {
avg += (d + fm->deemph_a / 2) / fm->deemph_a;
} else {
avg += (d - fm->deemph_a / 2) / fm->deemph_a;
}
lp[i] = (int16_t) avg;
}
}
void dc_block_filter(Demodulate *fm) {
int i, avg;
int64_t sum = 0;
int16_t *lp = fm->lowpassed;
for (i = 0; i < fm->lp_len; i++) {
sum += lp[i];
}
avg = sum / fm->lp_len;
avg = (avg + fm->dc_avg * 9) / 10;
for (i = 0; i < fm->lp_len; i++) {
lp[i] -= avg;
}
fm->dc_avg = avg;
}
int mad(int16_t *samples, int len, int step)
/* mean average deviation */
{
int i = 0, sum = 0, ave = 0;
if (len == 0) {
return 0;
}
for (i = 0; i < len; i += step) {
sum += samples[i];
}
ave = sum / (len * step);
sum = 0;
for (i = 0; i < len; i += step) {
sum += abs(samples[i] - ave);
}
return sum / (len / step);
}
int rms(int16_t *samples, int len, int step)
/* largely lifted from rtl_power */
{
int i;
long p, t, s;
double dc, err;
p = t = 0L;
for (i = 0; i < len; i += step) {
s = (long) samples[i];
t += s;
p += s * s;
}
/* correct for dc offset in squares */
dc = (double) (t * step) / (double) len;
err = t * 2 * dc - dc * dc * len;
return (int) sqrt((p - err) / len);
}
int squelch_to_rms(int db, struct dongle_state *dongle, Demodulate *demod)
/* 0 dB = 1 rms at 50dB gain and 1024 downsample */
{
double linear, gain, downsample;
if (db == 0) {
return 0;
}
linear = pow(10.0, (double) db / 20.0);
gain = 50.0;
// if (dongle->gain != AUTO_GAIN) {
// gain = (double) (dongle->gain) / 10.0;
// }
gain = 50.0 - gain;
gain = pow(10.0, gain / 20.0);
downsample = 1024.0 / (double) demod->downsample;
linear = linear / gain;
linear = linear / downsample;
return (int) linear + 1;
}
Demodulate::Demodulate() {
rate_in = DEFAULT_SAMPLE_RATE;
rate_out = DEFAULT_SAMPLE_RATE;
squelch_level = 0;
conseq_squelch = 10;
terminate_on_squelch = 0;
squelch_hits = 11;
downsample_passes = 0;
comp_fir_size = 0;
prev_index = 0;
post_downsample = 1; // once this works, default = 4
custom_atan = 0;
deemph = 0;
rotate_enable = 0;
rotate.len = 0;
rotate.sincos = NULL;
rate_out2 = -1; // flag for disabled
mode_demod = &fm_demod;
// pre_j = s->pre_r = s->now_r = s->now_j = 0;
prev_lpr_index = 0;
deemph_a = 0;
now_lpr = 0;
dc_block = 1;
dc_avg = 0;
output_target = &output.results[0];
lowpassed = NULL;
mode_demod = &fm_demod;
rate_in = 170000;
rate_out = 170000;
rate_out2 = 44000;
output.rate = 44000;
custom_atan = 1;
// post_downsample = 4;
deemph = 1;
squelch_level = 0;
int capture_freq;
// downsample = (SRATE / rate_in) + 1;
downsample = (SRATE / rate_in) + 1;
if (downsample_passes) {
downsample_passes = (int) log2(downsample) + 1;
downsample = 1 << downsample_passes;
}
if (deemph) {
deemph_a = (int)round(1.0/((1.0-exp(-1.0/(rate_out * 75e-6)))));
}
capture_freq=DEFAULT_FREQ;
//capture_freq = freq;
//capture_rate = downsample * rate_in;
int edge = 0;
// if (d->pre_rotate) {
// capture_freq = freq + capture_rate/4;}
capture_freq += edge * rate_in / 2;
output_scale = (1 << 15) / (128 * downsample);
if (output_scale < 1) {
output_scale = 1;
}
if (mode_demod == &fm_demod) {
output_scale = 1;
}
custom_atan = 1;
//demod.post_downsample = 4;
deemph = 1;
squelch_level = 0;
int r, opt;
int dev_given = 0;
int custom_ppm = 0;
output.results[0].trycond = 1;
output.results[0].buf = NULL;
output.results[1].trycond = 1;
output.results[1].buf = NULL;
// controller.freqs[0] = 100000000;
// controller.freq_len = 0;
// controller.edge = 0;
// controller.wb_mode = 0;
// if (strcmp("fm", optarg) == 0) {
// demod.mode_demod = &fm_demod;
// }
// if (strcmp("raw", optarg) == 0) {
// demod.mode_demod = &raw_demod;
// }
// if (strcmp("am", optarg) == 0) {
// demod.mode_demod = &am_demod;
// }
// if (strcmp("usb", optarg) == 0) {
// demod.mode_demod = &usb_demod;
// }
// if (strcmp("lsb", optarg) == 0) {
// demod.mode_demod = &lsb_demod;
// }
// if (strcmp("wbfm", optarg) == 0) {
// controller.wb_mode = 1;
// }
// break;
// rate_in *= post_downsample;
if (!output.rate) {
output.rate = rate_out;
}
// if (controller.freq_len > 1) {
// terminate_on_squelch = 0;
// }
output.padded = 0;
}
void Demodulate::demod(std::vector<int16_t> &buffer) {
int i, ds_p;
int do_squelch = 0;
int sr = 0;
if (rotate_enable) {
translate(this);
}
ds_p = downsample_passes;
lowpassed = &buffer[0];
lp_len = buffer.size();
if (ds_p) {
for (i = 0; i < ds_p; i++) {
fifth_order(lowpassed, (lp_len >> i), lp_i_hist[i]);
fifth_order(lowpassed + 1, (lp_len >> i) - 1, lp_q_hist[i]);
}
lp_len = lp_len >> ds_p;
/* droop compensation */
if (comp_fir_size == 9 && ds_p <= CIC_TABLE_MAX) {
generic_fir(lowpassed, lp_len, cic_9_tables[ds_p], droop_i_hist);
generic_fir(lowpassed + 1, lp_len - 1, cic_9_tables[ds_p], droop_q_hist);
}
} else {
low_pass(this);
}
/* power squelch */
if (squelch_level) {
sr = rms(lowpassed, lp_len, 1);
if (sr < squelch_level) {
do_squelch = 1;
}
}
if (do_squelch) {
squelch_hits++;
for (i = 0; i < lp_len; i++) {
lowpassed[i] = 0;
}
} else {
squelch_hits = 0;
}
mode_demod(this); /* lowpassed -> lowpassed */
if (mode_demod == &raw_demod) {
return;
}
if (dc_block) {
dc_block_filter(this);
}
/* todo, fm noise squelch */
// use nicer filter here too?
if (post_downsample > 1) {
lp_len = low_pass_simple(lowpassed, lp_len, post_downsample);
}
if (deemph) {
deemph_filter(this);
}
if (rate_out2 > 0) {
low_pass_real(this);
}
output_target->buf = lowpassed;
output_target->len = lp_len;
}
-93
View File
@@ -1,93 +0,0 @@
#pragma once
/*
* based on rtl_fm.c
* https://github.com/keenerd/rtl-sdr/blob/master/src/rtl_fm.c
*/
#include <vector>
#include <stdint.h>
#include <stddef.h>
#define FREQUENCIES_LIMIT 1000
struct translate_state {
double angle; /* radians */
int16_t *sincos; /* pairs */
int len;
int i;
};
struct buffer_bucket {
int16_t *buf;
int len;
int trycond;
};
struct output_state {
int exit_flag;
struct buffer_bucket results[2];
int rate;
int wav_format;
int padded;
int lrmix;
};
struct controller_state {
int exit_flag;
uint32_t freqs[FREQUENCIES_LIMIT];
int freq_len;
int freq_now;
int edge;
int wb_mode;
};
/* define our own complex math ops
because ARMv5 has no hardware float */
void multiply(int ar, int aj, int br, int bj, int *cr, int *cj);
int polar_discriminant(int ar, int aj, int br, int bj);
int fast_atan2(int y, int x);
int polar_disc_fast(int ar, int aj, int br, int bj);
int polar_disc_lut(int ar, int aj, int br, int bj);
class Demodulate {
public:
Demodulate();
~Demodulate() {
}
void demod(std::vector<int16_t> &buffer);
public:
int16_t *lowpassed;
int lp_len;
int16_t lp_i_hist[10][6];
int16_t lp_q_hist[10][6];
int16_t droop_i_hist[9];
int16_t droop_q_hist[9];
int rate_in;
int rate_out;
int rate_out2;
int now_r, now_j;
int pre_r, pre_j;
int prev_index;
int downsample; /* min 1, max 256 */
int post_downsample;
int output_scale;
int squelch_level, conseq_squelch, squelch_hits, terminate_on_squelch;
int downsample_passes;
int comp_fir_size;
int custom_atan;
int deemph, deemph_a;
int now_lpr;
int prev_lpr_index;
int dc_block, dc_avg;
int rotate_enable;
struct translate_state rotate;
void (*mode_demod)(Demodulate *);
struct buffer_bucket *output_target;
struct output_state output;
};
+64 -191
View File
@@ -14,8 +14,8 @@
#include "CubicSDRDefs.h"
#include "AppFrame.h"
#include <algorithm>
#include "Demodulate.h"
#include "complex.h"
#include "pa_debugprint.h"
wxString glGetwxString(GLenum name) {
const GLubyte *v = glGetString(name);
@@ -104,6 +104,37 @@ EVT_KEY_DOWN(TestGLCanvas::OnKeyDown)
EVT_IDLE(TestGLCanvas::OnIdle)
wxEND_EVENT_TABLE()
static int patestCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) {
TestGLCanvas *src = (TestGLCanvas *) userData;
if (!src->audio_queue.size()) {
return paContinue;
}
float *out = (float*) outputBuffer;
std::vector<float> *nextBuffer = src->audio_queue.front();
for (int i = 0; i < framesPerBuffer * 2; i++) {
out[i] = (*nextBuffer)[src->audio_queue_ptr];
src->audio_queue_ptr++;
if (src->audio_queue_ptr == nextBuffer->size()) {
src->audio_queue.pop();
delete nextBuffer;
src->audio_queue_ptr = 0;
if (!src->audio_queue.size()) {
break;
}
nextBuffer = src->audio_queue.front();
}
}
return paContinue;
}
TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList) :
wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize,
wxFULL_REPAINT_ON_RESIZE), parent(parent) {
@@ -124,65 +155,31 @@ TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList) :
fft_ceil_ma = fft_ceil_maa = 1.0;
dev = alcOpenDevice(NULL);
if (!dev) {
fprintf(stderr, "Oops\n");
}
ALCint contextAttr[] = {ALC_FREQUENCY,44100,0};
ctx = alcCreateContext(dev, contextAttr);
alcMakeContextCurrent(ctx);
if (!ctx) {
fprintf(stderr, "Oops2\n");
}
alGenBuffers(AL_NUM_BUFFERS, buffers);
alGenSources(1, &source);
alSourcef(source, AL_PITCH, 1.0f);
alSourcef(source, AL_GAIN, 1.0f);
alSourcei(source, AL_LOOPING, AL_FALSE);
// prime the buffers
int16_t buffer_init[AL_BUFFER_SIZE];
for (int i = 0; i < AL_BUFFER_SIZE; i++) {
buffer_init[i] = 0;
}
for (int i = 0; i < AL_NUM_BUFFERS; i++) {
alBufferData(buffers[i], AL_FORMAT_STEREO16, buffer_init, AL_BUFFER_SIZE*2, audio_frequency);
}
if (alGetError() != AL_NO_ERROR) {
std::cout << "Error priming :(\n";
}
alSourceQueueBuffers(source, AL_NUM_BUFFERS, buffers);
alSourcePlay(source);
if (alGetError() != AL_NO_ERROR) {
PaError err;
err = Pa_Initialize();
if (err != paNoError) {
std::cout << "Error starting :(\n";
}
/*
// define filter length, type, number of bands
unsigned int n = 55;
liquid_firdespm_btype btype = LIQUID_FIRDESPM_BANDPASS;
unsigned int num_bands = 3;
// band edge description [size: num_bands x 2]
float bands[6] = { 0.0f, 0.14f, 0.15f, 0.35f, 0.36f, 0.5f };
outputParameters.device = 5; /* default output device */
if (outputParameters.device == paNoDevice) {
std::cout << "Error: No default output device.\n";
}
// desired response [size: num_bands x 1]
float des[3] = { 1.0f, 0.0f, 1.0f };
outputParameters.channelCount = 2; /* Stereo output, most likely supported. */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
// relative weights [size: num_bands x 1]
float weights[3] = { 1.0f, 1.0f, 1.0f };
stream = NULL;
// in-band weighting functions [size: num_bands x 1]
liquid_firdespm_wtype wtype[3] = { LIQUID_FIRDESPM_FLATWEIGHT, LIQUID_FIRDESPM_EXPWEIGHT, LIQUID_FIRDESPM_FLATWEIGHT };
err = Pa_OpenStream(&stream, NULL, &outputParameters, 44100, 256, paClipOff, &patestCallback, this);
// allocate memory for array and design filter
float h[n];
firdespm_run(n, num_bands, bands, des, weights, wtype, btype, h);
*/
err = Pa_StartStream(stream);
if (err != paNoError) {
std::cout << "Error starting stream: " << Pa_GetErrorText(err) << std::endl;
std::cout << "\tPortAudio error: " << Pa_GetErrorText(err) << std::endl;
}
float fc = 0.5f * (bandwidth / SRATE); // filter cutoff frequency
float ft = 0.05f; // filter transition
@@ -196,11 +193,6 @@ TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList) :
fir_filter = firfilt_crcf_create(h, h_len);
unsigned int m = 5; // filter semi-length
float slsl = 60.0f; // filter sidelobe suppression level
fir_hil = firhilbf_create(m, slsl);
// create multi-stage arbitrary resampler object
resampler = msresamp_crcf_create(resample_ratio, As);
msresamp_crcf_print(resampler);
@@ -215,10 +207,10 @@ TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList) :
}
TestGLCanvas::~TestGLCanvas() {
alcMakeContextCurrent(NULL);
alcDestroyContext(ctx);
alcCloseDevice(dev);
PaError err;
err = Pa_StopStream(stream);
err = Pa_CloseStream(stream);
Pa_Terminate();
}
void TestGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
@@ -240,12 +232,12 @@ void TestGLCanvas::OnKeyDown(wxKeyEvent& event) {
switch (event.GetKeyCode()) {
case WXK_RIGHT:
freq = ((AppFrame*) parent)->getFrequency();
freq += 10000;
freq += 100000;
((AppFrame*) parent)->setFrequency(freq);
break;
case WXK_LEFT:
freq = ((AppFrame*) parent)->getFrequency();
freq -= 10000;
freq -= 100000;
((AppFrame*) parent)->setFrequency(freq);
break;
case WXK_DOWN:
@@ -275,67 +267,6 @@ float polar_discriminant2(float ar, float aj, float br, float bj) {
void TestGLCanvas::setData(std::vector<signed char> *data) {
if (data && data->size()) {
/*
std::vector<int16_t> tmp(data->begin(), data->end());
demod.demod(tmp);
if (waveform_points.size() < demod.lp_len * 2) {
waveform_points.resize(demod.lp_len * 2);
}
float waveform_ceil = 0;
for (int i = 0, iMax = demod.lp_len; i < iMax; i++) {
float v = fabs(demod.lowpassed[i]);
if (v > waveform_ceil) {
waveform_ceil = v;
}
}
for (int i = 0, iMax = demod.lp_len; i < iMax; i++) {
waveform_points[i * 2 + 1] = (float) demod.lowpassed[i] / waveform_ceil;
waveform_points[i * 2] = ((double) i / (double) iMax);
}
ALint val;
ALuint buffer;
alGetSourcei(source, AL_SOURCE_STATE, &val);
if (val != AL_PLAYING) {
alSourcePlay(source);
}
// std::cout << "buffer: " << demod.output_target->len << "@" << frequency << std::endl;
std::vector<ALuint> *newBuffer = new std::vector<ALuint>;
newBuffer->resize(demod.output_target->len);
memcpy(&(*newBuffer)[0],demod.output_target->buf,demod.output_target->len*2);
audio_queue.push(newBuffer);
frequency = demod.output.rate;
while (audio_queue.size()>8) {
alGetSourcei(source, AL_BUFFERS_PROCESSED, &val);
if (val <= 0) {
break;
}
std::vector<ALuint> *nextBuffer = audio_queue.front();
alSourceUnqueueBuffers(source, 1, &buffer);
alBufferData(buffer, format, &(*nextBuffer)[0], nextBuffer->size()*2, frequency);
alSourceQueueBuffers(source, 1, &buffer);
audio_queue.pop();
delete nextBuffer;
if (alGetError() != AL_NO_ERROR) {
std::cout << "Error buffering :(\n";
}
}
*/
if (spectrum_points.size() < FFT_SIZE * 2) {
spectrum_points.resize(FFT_SIZE * 2);
}
@@ -408,35 +339,16 @@ void TestGLCanvas::setData(std::vector<signed char> *data) {
// fftw_execute(plan[1]);
for (int i = 0, iMax = FFT_SIZE; i < iMax; i++) {
spectrum_points[i * 2 + 1] = fft_result_maa[i] / fft_ceil_maa;
spectrum_points[i * 2 + 1] = log10(fft_result_maa[i]) / log10(fft_ceil_maa);
// spectrum_points[i * 2 + 1] = (fft_result_maa[i]) / (fft_ceil_maa);
spectrum_points[i * 2] = ((double) i / (double) iMax);
}
float waveform_ceil = 0, waveform_floor = 0;
// std::vector<float> output_buffer;
// output_buffer.resize(num_written);
// for (int i = 0, iMax = BUF_SIZE / 2; i < iMax; i++) {
// liquid_float_complex x;
// x.real = in[i][0];
// x.imag = in[i][1];
// float y[2];
//
// firhilbf_interp_execute(fir_hil, x, y);
// output_buffer[i] = y[1];
//
// if (waveform_ceil < y[1]) {
// waveform_ceil = y[1];
// }
// }
int i;
float pcm = 0;
float pr = pre_r;
float pj = pre_j;
for (i = 0; i < num_written; i++) {
for (int i = 0; i < num_written; i++) {
freqdem_demodulate(fdem, resampled_output[i], &pcm);
resampled_output[i].real = (float) pcm;
@@ -451,13 +363,6 @@ void TestGLCanvas::setData(std::vector<signed char> *data) {
}
}
droop_ofs = -(waveform_ceil + waveform_floor) / 2.0;
droop_ofs_ma = droop_ofs_ma + (droop_ofs - droop_ofs_ma) * 0.01;
droop_ofs_maa = droop_ofs_maa + (droop_ofs_ma - droop_ofs_maa) * 0.01;
pre_r = pr;
pre_j = pj;
int audio_out_size = ceil((float) (num_written) * audio_resample_ratio);
liquid_float_complex resampled_audio_output[audio_out_size];
@@ -473,46 +378,14 @@ void TestGLCanvas::setData(std::vector<signed char> *data) {
waveform_points[i * 2] = ((double) i / (double) iMax);
}
// std::cout << num_audio_written << std::endl;
ALint val;
ALuint buffer;
// std::cout << "buffer: " << demod.output_target->len << "@" << frequency << std::endl;
std::vector<ALint> *newBuffer = new std::vector<ALint>;
newBuffer->resize(num_audio_written*2);
std::vector<float> *newBuffer = new std::vector<float>;
newBuffer->resize(num_audio_written * 2);
for (int i = 0; i < num_audio_written; i++) {
(*newBuffer)[i] = resampled_audio_output[i].real * 32767.0;
(*newBuffer)[i+num_audio_written] = resampled_audio_output[i].real * 32767.0;
(*newBuffer)[i * 2] = resampled_audio_output[i].real;
(*newBuffer)[i * 2 + 1] = resampled_audio_output[i].real;
}
audio_queue.push(newBuffer);
while (audio_queue.size() > 4) {
alGetSourcei(source, AL_BUFFERS_PROCESSED, &val);
if (val <= 0) {
break;
}
std::vector<ALint> *nextBuffer = audio_queue.front();
alSourceUnqueueBuffers(source, 1, &buffer);
alBufferData(buffer, AL_FORMAT_STEREO16, &(*nextBuffer)[0], nextBuffer->size()*2, audio_frequency);
alSourceQueueBuffers(source, 1, &buffer);
audio_queue.pop();
delete nextBuffer;
if (alGetError() != AL_NO_ERROR) {
std::cout << "Error buffering :(\n";
}
alGetSourcei(source, AL_SOURCE_STATE, &val);
if (val != AL_PLAYING) {
alSourcePlay(source);
}
}
}
}
+11 -27
View File
@@ -11,18 +11,8 @@
#include "liquid/liquid.h"
#include "Demodulate.h"
#ifdef WIN32
#include <AL/al.h>
#include <AL/alc.h>
#else
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#endif
#define AL_NUM_BUFFERS 16
#define AL_BUFFER_SIZE 8192
#include "portaudio.h"
#include "pa_stream.h"
class PrimaryGLContext: public wxGLContext {
public:
@@ -31,8 +21,6 @@ public:
void Plot(std::vector<float> &points, std::vector<float> &points2);
private:
// textures for the cube faces
GLuint m_textures[6];
};
class TestGLCanvas: public wxGLCanvas {
@@ -42,6 +30,9 @@ public:
void setData(std::vector<signed char> *data);
std::queue< std::vector <float> * > audio_queue;
unsigned int audio_queue_ptr;
private:
void OnPaint(wxPaintEvent& event);
void OnKeyDown(wxKeyEvent& event);
@@ -57,8 +48,6 @@ private:
firfilt_crcf fir_filter;
firhilbf fir_hil;
float pre_r;
float pre_j;
float droop_ofs, droop_ofs_ma, droop_ofs_maa;
@@ -67,7 +56,6 @@ private:
msresamp_crcf audio_resampler;
float resample_ratio;
freqdem fdem;
float fft_ceil_ma, fft_ceil_maa;
@@ -76,17 +64,13 @@ private:
std::vector<float> fft_result_ma;
std::vector<float> fft_result_maa;
std::queue< std::vector <ALint> * > audio_queue;
Demodulate demod;
ALCdevice *dev;
ALCcontext *ctx;
ALuint source, buffers[AL_NUM_BUFFERS];
ALuint bandwidth;
ALuint audio_frequency;
unsigned int bandwidth;
unsigned int audio_frequency;
float audio_resample_ratio;
PaStreamParameters outputParameters;
PaStream *stream;
wxDECLARE_EVENT_TABLE();
};