From 068539d8dbe1259e9d3fd6d199f8ffabb2e044b3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 29 Sep 2014 11:19:47 +0000 Subject: semi-working prototype --- Display.c | 42 +++++++++++++++++++++++++++++++++++++----- Display.h | 3 +++ DisplayP.h | 1 + Sgraph.c | 48 ++++++++++++++++-------------------------------- Sgraph.h | 4 ++-- SgraphP.h | 9 +++++---- alsa.c | 6 +++--- fft.c | 6 ++++-- fft.h | 2 +- sio.c | 6 +++--- sio.h | 2 +- spectrogram.c | 26 +++++++++++++------------- 12 files changed, 89 insertions(+), 66 deletions(-) diff --git a/Display.c b/Display.c index 08488df..c8d866b 100644 --- a/Display.c +++ b/Display.c @@ -13,11 +13,15 @@ static void Initialize(Widget, Widget, ArgList, Cardinal *); static XtGeometryResult GeometryManager(Widget, XtWidgetGeometry *, XtWidgetGeometry *); static void ChangeManaged(Widget); static void Resize(Widget); +static void Redisplay(Widget, XEvent *, Region); +static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *); #define Offset(field) XtOffsetOf(DisplayRec, display.field) static XtResource resources[] = { { XtNspace, XtCSpace, XtRDimension, sizeof(Dimension), Offset(space), XtRImmediate, (XtPointer)2 }, + { XtNdata, XtCData, XtRPointer, sizeof(int **), + Offset(data), XtRPointer, NULL }, }; #undef Offset @@ -53,8 +57,9 @@ DisplayClassRec displayClassRec = { .visible_interest = False, .destroy = NULL, .resize = Resize, - .expose = XtInheritExpose, - .set_values = NULL, + //.expose = XtInheritExpose, + .expose = Redisplay, + .set_values = SetValues, .set_values_hook = NULL, .set_values_almost = XtInheritSetValuesAlmost, .get_values_hook = NULL, @@ -101,18 +106,25 @@ ChangeManaged(Widget w) DisplayWidget dw = (DisplayWidget)w; Dimension width, height; Widget child; + int **data; int i; + Arg arg; warnx("Change Managed %s", XtClass(w)->core_class.class_name); width = w->core.width; height = w->core.height; + data = (int **)XtRealloc((char *)dw->display.data, dw->composite.num_children * sizeof(int *)); + dw->display.data = data; + for (i = 0; i < dw->composite.num_children; i++) { child = dw->composite.children[i]; - XtMoveWidget(child, - width + dw->display.space, dw->display.space); if (XtIsManaged(child)) { + XtSetArg(arg, XtNdata, &dw->display.data[i]); + XtGetValues(child, &arg, 1); + XtMoveWidget(child, + width + dw->display.space, dw->display.space); width += child->core.width + 2 * child->core.border_width + 2 * dw->display.space; @@ -134,7 +146,7 @@ Resize(Widget w) int n = dw->composite.num_children; int i; - width = (w->core.width - 2 * dw->display.space) / n; + width = w->core.width / n - (n + 1) * dw->display.space; height = w->core.height - 2 * dw->display.space; Printd(w, "Resize"); @@ -150,3 +162,23 @@ Resize(Widget w) } } } + +static void +Redisplay(Widget w, XEvent *event, Region region) +{ + DisplayWidget dw = (DisplayWidget)w; + Widget child; + int i; + + for (i = 0; i < dw->composite.num_children; i++) { + child = dw->composite.children[i]; + if (XtIsManaged(child)) + XtClass(child)->core_class.expose(child, event, region); + } +} + +static Boolean +SetValues(Widget old, Widget req, Widget new, ArgList args, Cardinal *n) +{ + return True; +} diff --git a/Display.h b/Display.h index b5f5172..a3716b9 100644 --- a/Display.h +++ b/Display.h @@ -3,6 +3,9 @@ #ifndef _Display_h #define _Display_h +#define XtNdata "data" +#define XtCData "Data" + typedef struct _DisplayClassRec *DisplayWidgetClass; typedef struct _DisplayRec *DisplayWidget; diff --git a/DisplayP.h b/DisplayP.h index f41670c..f58ffca 100644 --- a/DisplayP.h +++ b/DisplayP.h @@ -7,6 +7,7 @@ typedef struct { Dimension space; + int **data; } DisplayPart; typedef struct _DisplayRec { diff --git a/Sgraph.c b/Sgraph.c index df6edd0..fa0ed03 100644 --- a/Sgraph.c +++ b/Sgraph.c @@ -31,7 +31,6 @@ static void Initialize(Widget request, Widget w, ArgList args, Cardinal *nargs); static void Realize(Widget w, XtValueMask *mask, XSetWindowAttributes *attr); -static void Action(Widget w, XEvent *event, String *params, Cardinal *num_params); static void Resize(Widget w); static void Redisplay(Widget w, XEvent *event, Region r); static Boolean SetValues(Widget old, Widget reference, Widget new, ArgList args, Cardinal *num_args); @@ -50,28 +49,26 @@ static XtResource resources[] = { offset(background), XtRString, XtDefaultBackground }, { XtNmirror, XtCBoolean, XtRBoolean, sizeof(Boolean), offset(mirror), XtRBoolean, False }, - { XtNleftData, XtCParameter, XtRPointer, sizeof(XtPointer), - offset(leftData), XtRPointer, NULL }, + { XtNdata, XtCData, XtRPointer, sizeof(int *), + offset(data), XtRPointer, NULL }, + /* { XtNrightData, XtCParameter, XtRPointer, sizeof(XtPointer), offset(rightData), XtRPointer, NULL }, + */ { XtNsize, XtCsize, XtRInt, sizeof(int), offset(size), XtRImmediate, (XtPointer)2048 }, { XtNsamples, XtCsamples, XtRInt, sizeof(int), offset(samples), XtRImmediate, (XtPointer)0 }, + /* { XtNdataCallback, XtCCallback, XtRCallback, sizeof(XtCallbackProc), offset(data), XtRCallback, NULL }, { XtNfftCallback, XtCCallback, XtRCallback, sizeof(XtCallbackProc), offset(fft), XtRCallback, NULL }, + */ }; #undef goffset #undef offset -static XtActionsRec actions[] = { - { "sgraph", Action }, -}; - -static char translations[] = ":" "sgraph()\n"; - SgraphClassRec sgraphClassRec = { /* core */ { @@ -84,8 +81,8 @@ SgraphClassRec sgraphClassRec = { Initialize, /* initialize */ NULL, /* initialize_hook */ Realize, /* realize */ - actions, /* actions */ - XtNumber(actions), /* num_actions */ + NULL, /* actions */ + 0, /* num_actions */ resources, /* resources */ XtNumber(resources), /* num_resources */ NULLQUARK, /* xrm_class */ @@ -103,7 +100,7 @@ SgraphClassRec sgraphClassRec = { NULL, /* accept_focus */ XtVersion, /* version */ NULL, /* callback_private */ - translations, /* tm_table */ + XtInheritTranslations, /* tm_table */ XtInheritQueryGeometry, /* query_geometry */ XtInheritDisplayAccelerator, /* display_accelerator */ NULL, /* extension */ @@ -160,8 +157,7 @@ Initialize(Widget request, Widget w, ArgList args, Cardinal *nargs) errx(1, "XRender %d.%d error %d", major, minor, ret); */ - sw->sgraph.leftData = (double *)XtCalloc(sw->sgraph.size, sizeof(double)); - sw->sgraph.rightData = (double *)XtCalloc(sw->sgraph.size, sizeof(double)); + sw->sgraph.data = (int *)XtCalloc(sw->sgraph.size, sizeof(int)); Printd(w, "Initialize"); GetGC(w); @@ -212,29 +208,22 @@ static void Redisplay(Widget w, XEvent *event, Region r) { SgraphWidget sw = (SgraphWidget)w; - Dimension width = w->core.width / 2; - Dimension height = w->core.height / 4; - Dimension x, yl, yr; + Dimension x, y; Dimension bottom; XdbeSwapInfo swap; if (!XtIsRealized(w)) return; - bottom = sw->core.height - 10; + bottom = sw->core.height - 1; - for (x = 0; x < sw->sgraph.size / 2 - 1; x++) { - yl = sw->sgraph.leftData[x]; - yr = sw->sgraph.rightData[x]; + for (x = 0; x < sw->sgraph.size - 1; x++) { + y = sw->sgraph.data[x]; XDrawLine(XtDisplay(sw), sw->sgraph.backBuf, sw->sgraph.foreGC, - width - x - 2, bottom, - width - x - 2, bottom - yl); - XDrawLine(XtDisplay(sw), sw->sgraph.backBuf, - sw->sgraph.foreGC, - width + x + 1, bottom, - width + x + 1, bottom - yr); + x, bottom, + x, bottom - y); } swap.swap_window = XtWindow(sw); @@ -259,8 +248,3 @@ SetValues(Widget old, Widget reference, Widget new, ArgList args, Cardinal *num_ return False; } - -static void -Action(Widget w, XEvent *event, String *params, Cardinal *num_params) -{ -} diff --git a/Sgraph.h b/Sgraph.h index 324958b..e527e61 100644 --- a/Sgraph.h +++ b/Sgraph.h @@ -47,8 +47,8 @@ /* define any special resource names here that are not in */ #define XtNmirror "mirror" -#define XtNleftData "leftData" -#define XtNrightData "rightData" +#define XtNdata "data" +#define XtCData "Data" #define XtNsamples "samples" #define XtCsamples "Samples" diff --git a/SgraphP.h b/SgraphP.h index 9c507c6..7a08950 100644 --- a/SgraphP.h +++ b/SgraphP.h @@ -41,12 +41,13 @@ typedef struct { Pixel background; Pixel foreground; Boolean mirror; - double *leftData; - double *rightData; - int size; - int samples; + int *data; + size_t size; + size_t samples; + /* XtCallbackProc data; XtCallbackProc fft; + */ XdbeBackBuffer backBuf; GC foreGC; diff --git a/alsa.c b/alsa.c index 41f16b8..5593735 100644 --- a/alsa.c +++ b/alsa.c @@ -78,7 +78,7 @@ init_sio(void) } size_t -read_sio(double *left, double *right, size_t n) +read_sio(int *left, int *right, size_t n) { snd_pcm_sframes_t rc; struct data *data; @@ -98,8 +98,8 @@ read_sio(double *left, double *right, size_t n) /* split and normalize */ for (i = 0; i < n; i++) { - left[i] = data[i].left / (double)INT16_MAX; - right[i] = data[i].right / (double)INT16_MAX; + left[i] = data[i].left; + right[i] = data[i].right; } return n; diff --git a/fft.c b/fft.c index 3698447..ad5c493 100644 --- a/fft.c +++ b/fft.c @@ -43,8 +43,10 @@ hamming(size_t n) p = calloc(n, sizeof(double)); assert(p); - for (i = 0; i < n; i++) + for (i = 0; i < n; i++) { p[i] = alpha - beta * cos(2 * M_PI * i / (n - 1)); + p[i] /= (double)INT16_MAX; + } return p; } @@ -92,7 +94,7 @@ init_fft(size_t n) } void -exec_fft(double *io, size_t n) +exec_fft(int *io, size_t n) { int i; diff --git a/fft.h b/fft.h index 70a8b68..6a67311 100644 --- a/fft.h +++ b/fft.h @@ -20,7 +20,7 @@ __BEGIN_DECLS void init_fft(size_t); -void exec_fft(double *, size_t); +void exec_fft(int *, size_t); void free_fft(void); __END_DECLS diff --git a/sio.c b/sio.c index 46199ff..9416ee3 100644 --- a/sio.c +++ b/sio.c @@ -78,7 +78,7 @@ init_sio(void) } size_t -read_sio(double *left, double *right, size_t n) +read_sio(int *left, int *right, size_t n) { int done, i; char *p = (char *)buffer; @@ -105,8 +105,8 @@ read_sio(double *left, double *right, size_t n) /* split and normalize */ for (i = 0; i < n; i++) { - left[i] = data[i].left / (double)INT16_MAX; - right[i] = data[i].right / (double)INT16_MAX; + left[i] = data[i].left; + right[i] = data[i].right; } return n; diff --git a/sio.h b/sio.h index b4ac675..97659c0 100644 --- a/sio.h +++ b/sio.h @@ -20,7 +20,7 @@ __BEGIN_DECLS int init_sio(void); -size_t read_sio(double *, double *, size_t); +size_t read_sio(int *, int *, size_t); void free_sio(void); __END_DECLS diff --git a/spectrogram.c b/spectrogram.c index db88e4a..4cfef4c 100644 --- a/spectrogram.c +++ b/spectrogram.c @@ -71,26 +71,26 @@ static XtActionsRec actionsList[] = { }; static Boolean -worker(XtPointer data) +worker(XtPointer p) { Arg arg[10]; int n, size, samples; double *left, *right; + int **data; n = 0; XtSetArg(arg[n], XtNwidth, &size); n++; XtSetArg(arg[n], XtNsamples, &samples); n++; - XtSetArg(arg[n], XtNleftData, &left); n++; - XtSetArg(arg[n], XtNrightData, &right); n++; - XtGetValues(data, arg, n); + XtSetArg(arg[n], XtNdata, &data); n++; + XtGetValues(p, arg, n); - size = read_sio(left, right, size); - exec_fft(left, size); - exec_fft(right, size); + size = read_sio(data[0], data[1], size); + exec_fft(data[0], size); + exec_fft(data[1], size); n = 0; XtSetArg(arg[n], XtNsize, size); n++; - XtSetValues(data, arg, n); /* trigger expose */ + XtSetValues(p, arg, n); /* trigger expose */ return False; /* don't remove the work procedure from the list */ } @@ -107,7 +107,7 @@ int main(int argc, char **argv) { XtAppContext app; - Widget toplevel, display, sgraph; + Widget toplevel, display; int n, samples; Arg args[10]; @@ -130,14 +130,14 @@ main(int argc, char **argv) n = 0; XtSetArg(args[n], XtNsamples, samples); n++; - sgraph = XtCreateManagedWidget("SGraph", sgraphWidgetClass, + XtCreateManagedWidget("SGraph", sgraphWidgetClass, display, args, n); - sgraph = XtCreateManagedWidget("SGraph", sgraphWidgetClass, + XtCreateManagedWidget("SGraph", sgraphWidgetClass, display, args, n); - XtOverrideTranslations(sgraph, + XtOverrideTranslations(display, XtParseTranslationTable("q: quit()")); - XtAppAddWorkProc(app, worker, sgraph); + XtAppAddWorkProc(app, worker, display); XtRealizeWidget(toplevel); XtAppMainLoop(app); -- cgit v1.2.3