2014-10-27 21:22:29 -04:00
|
|
|
#include "PrimaryGLContext.h"
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !wxUSE_GLCANVAS
|
|
|
|
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "CubicSDR.h"
|
2014-10-30 22:51:33 -04:00
|
|
|
#include "CubicSDRDefs.h"
|
2014-11-04 19:52:11 -05:00
|
|
|
#include "AppFrame.h"
|
2014-10-30 22:51:33 -04:00
|
|
|
#include <algorithm>
|
2014-11-04 21:55:20 -05:00
|
|
|
#include "Demodulate.h"
|
2014-10-27 21:22:29 -04:00
|
|
|
|
|
|
|
wxString glGetwxString(GLenum name) {
|
|
|
|
const GLubyte *v = glGetString(name);
|
|
|
|
if (v == 0) {
|
|
|
|
// The error is not important. It is GL_INVALID_ENUM.
|
|
|
|
// We just want to clear the error stack.
|
|
|
|
glGetError();
|
|
|
|
|
|
|
|
return wxString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxString((const char*) v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckGLError() {
|
|
|
|
GLenum errLast = GL_NO_ERROR;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
GLenum err = glGetError();
|
|
|
|
if (err == GL_NO_ERROR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (err == errLast) {
|
|
|
|
wxLogError
|
|
|
|
(wxT("OpenGL error state couldn't be reset."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
errLast = err;
|
|
|
|
|
|
|
|
wxLogError
|
|
|
|
(wxT("OpenGL error %d"), err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrimaryGLContext::PrimaryGLContext(wxGLCanvas *canvas) :
|
|
|
|
wxGLContext(canvas) {
|
|
|
|
SetCurrent(*canvas);
|
|
|
|
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
CheckGLError();
|
|
|
|
}
|
|
|
|
|
2014-11-05 19:10:18 -05:00
|
|
|
void PrimaryGLContext::Plot(std::vector<float> &points, std::vector<float> &points2) {
|
2014-10-27 21:22:29 -04:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
2014-11-04 18:39:08 -05:00
|
|
|
// glEnable(GL_LINE_SMOOTH);
|
|
|
|
|
2014-10-31 01:37:01 -04:00
|
|
|
if (points.size()) {
|
2014-11-05 18:12:42 -05:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(-1.0f, -0.9f, 0.0f);
|
|
|
|
glScalef(2.0f, 1.0f, 1.0f);
|
2014-10-28 22:59:17 -04:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
2014-10-31 01:37:01 -04:00
|
|
|
glVertexPointer(2, GL_FLOAT, 0, &points[0]);
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, points.size() / 2);
|
2014-10-28 22:59:17 -04:00
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
2014-11-05 18:12:42 -05:00
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (points2.size()) {
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(-1.0f, 0.5f, 0.0f);
|
|
|
|
glScalef(2.0f, 0.5f, 1.0f);
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, &points2[0]);
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, points2.size() / 2);
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glPopMatrix();
|
2014-10-28 22:59:17 -04:00
|
|
|
}
|
2014-10-27 21:22:29 -04:00
|
|
|
|
|
|
|
glFlush();
|
|
|
|
|
|
|
|
CheckGLError();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxBEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) EVT_PAINT(TestGLCanvas::OnPaint)
|
|
|
|
EVT_KEY_DOWN(TestGLCanvas::OnKeyDown)
|
2014-10-28 22:59:17 -04:00
|
|
|
EVT_IDLE(TestGLCanvas::OnIdle)
|
2014-10-27 21:22:29 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
|
|
|
|
|
|
|
TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList) :
|
|
|
|
wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize,
|
2014-11-04 19:52:11 -05:00
|
|
|
wxFULL_REPAINT_ON_RESIZE), parent(parent) {
|
2014-10-27 21:22:29 -04:00
|
|
|
|
2014-10-31 19:10:53 -04:00
|
|
|
int in_block_size = BUF_SIZE / 2;
|
2014-10-30 22:51:33 -04:00
|
|
|
int out_block_size = FFT_SIZE;
|
|
|
|
|
|
|
|
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * in_block_size);
|
2014-10-31 01:37:01 -04:00
|
|
|
out[0] = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * out_block_size);
|
|
|
|
out[1] = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * out_block_size);
|
2014-10-31 19:10:53 -04:00
|
|
|
plan[0] = fftw_plan_dft_1d(out_block_size, in, out[0], FFTW_BACKWARD, FFTW_MEASURE);
|
|
|
|
plan[1] = fftw_plan_dft_1d(out_block_size, in, out[1], FFTW_FORWARD, FFTW_MEASURE);
|
2014-11-04 18:39:08 -05:00
|
|
|
|
2014-11-04 19:52:11 -05:00
|
|
|
fft_ceil_ma = fft_ceil_maa = 1.0;
|
2014-11-05 19:10:18 -05:00
|
|
|
|
|
|
|
dev = alcOpenDevice(NULL);
|
|
|
|
if (!dev) {
|
|
|
|
fprintf(stderr, "Oops\n");
|
|
|
|
}
|
|
|
|
ctx = alcCreateContext(dev, NULL);
|
|
|
|
alcMakeContextCurrent(ctx);
|
|
|
|
if (!ctx) {
|
|
|
|
fprintf(stderr, "Oops2\n");
|
|
|
|
}
|
2014-11-05 21:05:56 -05:00
|
|
|
|
|
|
|
alGenBuffers(AL_NUM_BUFFERS, buffers);
|
|
|
|
alGenSources(1, &source);
|
|
|
|
|
|
|
|
// prime the buffers
|
2014-11-06 01:33:59 -05:00
|
|
|
int16_t buffer_init[AL_BUFFER_SIZE];
|
2014-11-05 21:05:56 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < AL_BUFFER_SIZE; i++) {
|
2014-11-06 01:33:59 -05:00
|
|
|
buffer_init[i] = 0;
|
2014-11-05 21:05:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
format = AL_FORMAT_MONO16;
|
2014-11-06 01:33:59 -05:00
|
|
|
for (int i = 0; i < AL_NUM_BUFFERS; i++) {
|
|
|
|
alBufferData(buffers[i], format, buffer_init, AL_BUFFER_SIZE, demod.output.rate);
|
|
|
|
}
|
2014-11-05 21:05:56 -05:00
|
|
|
if (alGetError() != AL_NO_ERROR) {
|
|
|
|
std::cout << "Error priming :(\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
alSourceQueueBuffers(source, AL_NUM_BUFFERS, buffers);
|
|
|
|
alSourcePlay(source);
|
|
|
|
if (alGetError() != AL_NO_ERROR) {
|
|
|
|
std::cout << "Error starting :(\n";
|
|
|
|
}
|
|
|
|
|
2014-11-05 19:10:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TestGLCanvas::~TestGLCanvas() {
|
|
|
|
alcMakeContextCurrent(NULL);
|
|
|
|
alcDestroyContext(ctx);
|
|
|
|
alcCloseDevice(dev);
|
|
|
|
|
2014-10-31 19:10:53 -04:00
|
|
|
}
|
2014-10-27 21:22:29 -04:00
|
|
|
|
|
|
|
void TestGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
|
|
|
|
wxPaintDC dc(this);
|
|
|
|
const wxSize ClientSize = GetClientSize();
|
|
|
|
|
|
|
|
PrimaryGLContext& canvas = wxGetApp().GetContext(this);
|
|
|
|
glViewport(0, 0, ClientSize.x, ClientSize.y);
|
|
|
|
|
2014-11-05 19:10:18 -05:00
|
|
|
canvas.Plot(spectrum_points, waveform_points);
|
2014-10-27 21:22:29 -04:00
|
|
|
|
|
|
|
SwapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGLCanvas::OnKeyDown(wxKeyEvent& event) {
|
|
|
|
float angle = 5.0;
|
|
|
|
|
2014-11-04 19:52:11 -05:00
|
|
|
unsigned int freq;
|
2014-10-27 21:22:29 -04:00
|
|
|
switch (event.GetKeyCode()) {
|
|
|
|
case WXK_RIGHT:
|
2014-11-04 19:52:11 -05:00
|
|
|
freq = ((AppFrame*) parent)->getFrequency();
|
|
|
|
freq += 100000;
|
|
|
|
((AppFrame*) parent)->setFrequency(freq);
|
2014-10-27 21:22:29 -04:00
|
|
|
break;
|
|
|
|
case WXK_LEFT:
|
2014-11-04 19:52:11 -05:00
|
|
|
freq = ((AppFrame*) parent)->getFrequency();
|
|
|
|
freq -= 100000;
|
|
|
|
((AppFrame*) parent)->setFrequency(freq);
|
2014-10-27 21:22:29 -04:00
|
|
|
break;
|
|
|
|
case WXK_DOWN:
|
|
|
|
break;
|
|
|
|
case WXK_UP:
|
|
|
|
break;
|
|
|
|
case WXK_SPACE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
event.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 22:59:17 -04:00
|
|
|
void TestGLCanvas::setData(std::vector<signed char> *data) {
|
2014-10-30 22:51:33 -04:00
|
|
|
|
2014-10-28 22:59:17 -04:00
|
|
|
if (data && data->size()) {
|
2014-10-30 22:51:33 -04:00
|
|
|
|
2014-11-05 18:12:42 -05:00
|
|
|
std::vector<int16_t> tmp(data->begin(), data->end());
|
2014-11-04 23:05:04 -05:00
|
|
|
demod.demod(tmp);
|
|
|
|
|
2014-11-05 18:12:42 -05:00
|
|
|
if (waveform_points.size() < demod.lp_len * 2) {
|
|
|
|
waveform_points.resize(demod.lp_len * 2);
|
|
|
|
}
|
2014-11-04 23:05:04 -05:00
|
|
|
|
2014-11-05 18:12:42 -05:00
|
|
|
float waveform_ceil = 0;
|
2014-11-04 23:05:04 -05:00
|
|
|
|
2014-11-05 18:12:42 -05:00
|
|
|
for (int i = 0, iMax = demod.lp_len; i < iMax; i++) {
|
|
|
|
float v = fabs(demod.lowpassed[i]);
|
2014-11-05 19:10:18 -05:00
|
|
|
if (v > waveform_ceil) {
|
2014-11-05 18:12:42 -05:00
|
|
|
waveform_ceil = v;
|
|
|
|
}
|
2014-11-04 23:05:04 -05:00
|
|
|
}
|
|
|
|
|
2014-11-05 18:12:42 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-11-05 21:05:56 -05:00
|
|
|
ALint val;
|
|
|
|
ALuint buffer;
|
|
|
|
|
2014-11-06 01:33:59 -05:00
|
|
|
alGetSourcei(source, AL_SOURCE_STATE, &val);
|
|
|
|
if (val != AL_PLAYING) {
|
|
|
|
alSourcePlay(source);
|
|
|
|
}
|
2014-11-05 21:05:56 -05:00
|
|
|
|
2014-11-06 01:33:59 -05:00
|
|
|
// 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);
|
2014-11-05 21:05:56 -05:00
|
|
|
|
|
|
|
|
2014-11-06 01:33:59 -05:00
|
|
|
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();
|
|
|
|
|
2014-11-05 21:05:56 -05:00
|
|
|
alSourceUnqueueBuffers(source, 1, &buffer);
|
2014-11-06 01:33:59 -05:00
|
|
|
alBufferData(buffer, format, &(*nextBuffer)[0], nextBuffer->size()*2, frequency);
|
2014-11-05 21:05:56 -05:00
|
|
|
alSourceQueueBuffers(source, 1, &buffer);
|
|
|
|
|
2014-11-06 01:33:59 -05:00
|
|
|
audio_queue.pop();
|
|
|
|
|
|
|
|
delete nextBuffer;
|
|
|
|
|
2014-11-05 21:05:56 -05:00
|
|
|
if (alGetError() != AL_NO_ERROR) {
|
|
|
|
std::cout << "Error buffering :(\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 18:12:42 -05:00
|
|
|
if (spectrum_points.size() < FFT_SIZE * 2) {
|
|
|
|
spectrum_points.resize(FFT_SIZE * 2);
|
2014-10-28 22:59:17 -04:00
|
|
|
}
|
2014-10-30 22:51:33 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < BUF_SIZE / 2; i++) {
|
2014-10-31 01:37:01 -04:00
|
|
|
in[i][0] = (double) (*data)[i * 2] / 127.0f;
|
|
|
|
in[i][1] = (double) (*data)[i * 2 + 1] / 127.0f;
|
2014-10-30 22:51:33 -04:00
|
|
|
}
|
|
|
|
|
2014-10-31 01:37:01 -04:00
|
|
|
fftw_execute(plan[0]);
|
|
|
|
fftw_execute(plan[1]);
|
|
|
|
|
2014-11-04 17:25:04 -05:00
|
|
|
double fft_ceil = 0;
|
|
|
|
// fft_floor,
|
|
|
|
|
2014-11-04 18:39:08 -05:00
|
|
|
if (fft_result.size() < FFT_SIZE) {
|
|
|
|
fft_result.resize(FFT_SIZE);
|
|
|
|
fft_result_ma.resize(FFT_SIZE);
|
|
|
|
fft_result_maa.resize(FFT_SIZE);
|
2014-11-04 17:25:04 -05:00
|
|
|
}
|
2014-10-31 01:37:01 -04:00
|
|
|
|
|
|
|
for (int j = 0; j < 2; j++) {
|
2014-10-31 19:10:53 -04:00
|
|
|
for (int i = 0, iMax = FFT_SIZE / 2; i < iMax; i++) {
|
2014-11-04 18:39:08 -05:00
|
|
|
double a = out[j][j ? i : ((iMax - 1) - i)][0];
|
|
|
|
double b = out[j][j ? i : ((iMax - 1) - i)][1];
|
2014-10-31 19:10:53 -04:00
|
|
|
double c = sqrt(a * a + b * b);
|
2014-10-31 20:41:49 -04:00
|
|
|
|
2014-11-04 18:39:08 -05:00
|
|
|
double x = out[j ? 0 : 1][j ? ((FFT_SIZE - 1) - i) : ((FFT_SIZE / 2) + i)][0];
|
|
|
|
double y = out[j ? 0 : 1][j ? ((FFT_SIZE - 1) - i) : ((FFT_SIZE / 2) + i)][1];
|
2014-10-31 20:41:49 -04:00
|
|
|
double z = sqrt(x * x + y * y);
|
2014-11-04 18:39:08 -05:00
|
|
|
|
|
|
|
double r = (c < z) ? c : z;
|
2014-10-31 19:10:53 -04:00
|
|
|
|
|
|
|
if (!j) {
|
2014-11-04 17:25:04 -05:00
|
|
|
fft_result[i] = r;
|
2014-10-31 19:10:53 -04:00
|
|
|
} else {
|
2014-11-04 18:39:08 -05:00
|
|
|
fft_result[(FFT_SIZE / 2) + i] = r;
|
2014-10-31 19:10:53 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-31 01:37:01 -04:00
|
|
|
}
|
2014-10-31 19:10:53 -04:00
|
|
|
|
2014-11-04 18:39:08 -05:00
|
|
|
float time_slice = (float) SRATE / (float) (BUF_SIZE / 2);
|
2014-11-04 17:25:04 -05:00
|
|
|
|
|
|
|
for (int i = 0, iMax = FFT_SIZE; i < iMax; i++) {
|
2014-11-04 18:39:08 -05:00
|
|
|
fft_result_maa[i] += (fft_result_ma[i] - fft_result_maa[i]) * 0.65;
|
|
|
|
fft_result_ma[i] += (fft_result[i] - fft_result_ma[i]) * 0.65;
|
2014-11-04 17:25:04 -05:00
|
|
|
|
2014-11-04 18:39:08 -05:00
|
|
|
if (fft_result_maa[i] > fft_ceil) {
|
|
|
|
fft_ceil = fft_result_maa[i];
|
|
|
|
}
|
2014-10-28 22:59:17 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 19:52:11 -05:00
|
|
|
fft_ceil_ma = fft_ceil_ma + (fft_ceil - fft_ceil_ma) * 0.05;
|
|
|
|
fft_ceil_maa = fft_ceil_maa + (fft_ceil - fft_ceil_maa) * 0.05;
|
2014-11-04 18:39:08 -05:00
|
|
|
|
2014-10-31 19:10:53 -04:00
|
|
|
for (int i = 0, iMax = FFT_SIZE; i < iMax; i++) {
|
2014-11-05 18:12:42 -05:00
|
|
|
spectrum_points[i * 2 + 1] = fft_result_maa[i] / fft_ceil_maa;
|
|
|
|
spectrum_points[i * 2] = ((double) i / (double) iMax);
|
|
|
|
}
|
2014-10-28 22:59:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGLCanvas::OnIdle(wxIdleEvent &event) {
|
|
|
|
Refresh(false);
|
|
|
|
}
|
2014-10-30 22:51:33 -04:00
|
|
|
|