Branch
Hash :
adb9e235
Author :
Thomas de Grivel
Date :
2023-01-14T18:26:30
license
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
/* rtbuf
* Copyright 2018-2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers granted the above copyright notice and
* this permission paragraph are included in all copies and
* substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "../../librtbuf/rtbuf.h"
#include "../../librtbuf/lib.h"
#include "../signal.h"
#include "../signal_type.h"
#include "../music.h"
#include "../music_type.h"
#include "../glfw3.h"
s_rtbuf_lib_proc_in rtbuf_glfw3_oscilloscope_in[] =
{{ "black", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0, 1.0 },
{ "red" , RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0, 1.0 },
{ "green", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0, 1.0 },
{ "blue" , RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0, 1.0 },
{ 0, 0, 0.0, 0.0, 0.0, 0.0 }};
s_rtbuf_lib_proc_out rtbuf_glfw3_oscilloscope_out[] =
{{ "window", "void*" },
{ 0, 0 }};
int rtbuf_lib_init (s_rtbuf_lib *lib)
{
(void) lib;
glfwInit();
return 0;
}
void rtbuf_glfw3_oscilloscope_size (GLFWwindow *w, int width,
int height)
{
(void) w;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void rtbuf_glfw3_oscilloscope_draw (GLFWwindow *window)
{
s_rtbuf *rtb = (s_rtbuf*) glfwGetWindowUserPointer(window);
s_rtbuf_signal_fun in[4];
const float color[4][3] = { { 0.0f, 0.0f, 0.0f },
{ 1.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f } };
unsigned int i = 0;
unsigned int j;
assert(rtb);
for (j = 0; j < 4; j++)
rtbuf_signal_fun(rtb, j, &in[j]);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
for (j = 0; j < 4; j++) {
glColor3f(color[j][0], color[j][1], color[j][2]);
glBegin(GL_LINE_STRIP);
for (i = 0; i < RTBUF_SIGNAL_SAMPLES; i++) {
float x = (double) i / RTBUF_SIGNAL_SAMPLES;
float s = in[j].sample_fun(in[j].signal, i);
glVertex2f(x, s / 4.0f + 0.5f);
}
glEnd();
}
glfwSwapBuffers(window);
}
GLFWwindow * rtbuf_glfw3_oscilloscope_window (s_rtbuf *rtb)
{
GLFWwindow *window = glfwCreateWindow(512, 512,
"rtbuf_glfw3_oscilloscope",
NULL, NULL);
if (!window) {
rtbuf_err("glfw3_oscilloscope: glfwCreateWindow failed");
return 0;
}
glfwMakeContextCurrent(window);
glfwSetWindowUserPointer(window, rtb);
glfwSetWindowSizeCallback(window, rtbuf_glfw3_oscilloscope_size);
glfwSetWindowRefreshCallback(window, rtbuf_glfw3_oscilloscope_draw);
glfwShowWindow(window);
return window;
}
int rtbuf_glfw3_oscilloscope_start (s_rtbuf *rtb)
{
s_rtbuf_glfw3_oscilloscope_data *data;
assert(rtb->proc->out_bytes == sizeof(*data));
data = (s_rtbuf_glfw3_oscilloscope_data*) rtb->data;
if (!data->window &&
!(data->window = rtbuf_glfw3_oscilloscope_window(rtb)))
return -1;
return 0;
}
int rtbuf_glfw3_oscilloscope (s_rtbuf *rtb)
{
s_rtbuf_glfw3_oscilloscope_data *data;
assert(rtb);
assert(rtb->data);
assert(rtb->proc);
data = (s_rtbuf_glfw3_oscilloscope_data*) rtb->data;
rtbuf_glfw3_oscilloscope_draw(data->window);
glfwPollEvents();
return 0;
}
s_rtbuf_lib_proc rtbuf_lib_proc =
{ "oscilloscope",
rtbuf_glfw3_oscilloscope,
rtbuf_glfw3_oscilloscope_start,
0,
rtbuf_glfw3_oscilloscope_in,
rtbuf_glfw3_oscilloscope_out };
unsigned long rtbuf_lib_ver = RTBUF_LIB_VER;