Fex

Check-in [9d6841b701]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:strict -Wall -Werror patching up
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | master
Files: files | file ages | folders
SHA3-256: 9d6841b7012b959e8a4d5e5b5f8595f8f840b65f1b0f2271e18a7ad26461f09b
User & Date: jesse@mccluresk9.com 2014-10-07 17:07:38
Context
2014-10-07
17:37
code cleanup check-in: 6ba49fe548 user: jesse@mccluresk9.com tags: trunk, master
17:07
strict -Wall -Werror patching up check-in: 9d6841b701 user: jesse@mccluresk9.com tags: trunk, master
2014-10-02
16:57
added links to readme check-in: 047054bbf6 user: jesse@mccluresk9.com tags: trunk, master
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to doc/fex-1.tex.

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
\documentclass{latex2man}

\begin{Name}{1}{fex}{Jesse McClure}{Frequency Excusion}{FEX --- The frequency excursion calculator}

\Prog{Fex} - The frequency excursion calculator

\end{Name}















\section{Description}

\Prog{Fex} is a bioacoustic analysis tool for calculating the frequency
excursion of an audio recording of bird songs or other sound waves.

\section{Fex-gtk}

\Prog{Fex-gtk} is a wrapper script for fex that facilitates batch
processing of large numbers of wave files.  \Prog{Fex-gtk} can be
provided a list of file names, or can be run with no parameters to
provide a dialog window to select input wave files.  \Prog{Fex} will be
run on each input file, and the results can either be stored to a data
file or displayed in a dialog window upon completion.

\Prog{Fex-gtk} is also the executable target of \File{fex.desktop} to
allow for drag-and-drop operation of \Prog{fex}.  The desktop file can
accept any number of wave files as a drop target.














\section{Configuration}
\Prog{Fex} is configured via a runtime configuration file read from one
of
\File{\$XDG\_CONFIG\_HOME/fex/config},
\File{\$HOME/.config/fex/config},
\File{\$HOME/.fexrc}, or







>
>
>
>
>
>
>
>
>
>
>
>
>
>






<
<










>
>
>
>
>
>
>
>
>
>
>
>
>







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
\documentclass{latex2man}

\begin{Name}{1}{fex}{Jesse McClure}{Frequency Excusion}{FEX --- The frequency excursion calculator}

\Prog{Fex} - The frequency excursion calculator

\end{Name}

\section{Synopsis}

\Prog{fex}
	[\Opt{-l}|\Opt{--long}]
	[\Opt{-v}|\Opt{--version}]
	[\Opt{-v}|\Opt{--version}]
	\Arg{<wave file>}

\Prog{fex-gtk}
	[\Opt{-l}|\Opt{--long}]
	\Arg{<wave file>}
	[\Arg{<wave file>} ...]


\section{Description}

\Prog{Fex} is a bioacoustic analysis tool for calculating the frequency
excursion of an audio recording of bird songs or other sound waves.



\Prog{Fex-gtk} is a wrapper script for fex that facilitates batch
processing of large numbers of wave files.  \Prog{Fex-gtk} can be
provided a list of file names, or can be run with no parameters to
provide a dialog window to select input wave files.  \Prog{Fex} will be
run on each input file, and the results can either be stored to a data
file or displayed in a dialog window upon completion.

\Prog{Fex-gtk} is also the executable target of \File{fex.desktop} to
allow for drag-and-drop operation of \Prog{fex}.  The desktop file can
accept any number of wave files as a drop target.

\section{Options}

\begin{description}
\item[\Opt{-l}|\Opt{--long}]
	Provide three column output specifying the path length, path
	duration, and frequency excursion (calculated as path length over
	duration).  Default is to provide only the frequency excursion value.
\item[\Opt{-h}|\Opt{--help}]
	Print help message and exit.
\item[\Opt{-v}|\Opt{--version}]
	Print version information and exit.
\end{description}

\section{Configuration}
\Prog{Fex} is configured via a runtime configuration file read from one
of
\File{\$XDG\_CONFIG\_HOME/fex/config},
\File{\$HOME/.config/fex/config},
\File{\$HOME/.fexrc}, or

Changes to doc/fex.1.

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
'\" t
.\" Manual page created with latex2man on Wed Jul  2 13:29:59 EDT 2014
.\" NOTE: This file is generated, DO NOT EDIT.
.de Vb
.ft CW
.nf
..
.de Ve
.ft R

.fi
..
.TH "FEX" "1" "02 July 2014" "Frequency Excusion " "Frequency Excusion "
.SH NAME

.PP
Fex
\- The frequency excursion calculator 















.PP
.SH DESCRIPTION

.PP
Fex
is a bioacoustic analysis tool for calculating the frequency 
excursion of an audio recording of bird songs or other sound waves. 
.PP
.SH FEX\-GTK

.PP
Fex\-gtk
is a wrapper script for fex that facilitates batch 
processing of large numbers of wave files. Fex\-gtk
can be 
provided a list of file names, or can be run with no parameters to 
provide a dialog window to select input wave files. Fex
will be 
run on each input file, and the results can either be stored to a data 
file or displayed in a dialog window upon completion. 
.PP
Fex\-gtk
is also the executable target of fex.desktop
to 
allow for drag\-and\-drop operation of fex\&.
The desktop file can 
accept any number of wave files as a drop target. 
.PP















.SH CONFIGURATION

Fex
is configured via a runtime configuration file read from one 
of 
$XDG_CONFIG_HOME/fex/config,
$HOME/.config/fex/config,

|










|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







<
<
<


















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
'\" t
.\" Manual page created with latex2man on Wed Oct  1 12:46:47 EDT 2014
.\" NOTE: This file is generated, DO NOT EDIT.
.de Vb
.ft CW
.nf
..
.de Ve
.ft R

.fi
..
.TH "FEX" "1" "01 October 2014" "Frequency Excusion " "Frequency Excusion "
.SH NAME

.PP
Fex
\- The frequency excursion calculator 
.PP
.SH SYNOPSIS

.PP
fex
[\fB\-l\fP|\fB\-\-long\fP]
[\fB\-v\fP|\fB\-\-version\fP]
[\fB\-v\fP|\fB\-\-version\fP]
\fI<wave file>\fP
.PP
fex\-gtk
[\fB\-l\fP|\fB\-\-long\fP]
\fI<wave file>\fP
[\fI<wave file>\fP
\&.\&.\&.] 
.PP
.SH DESCRIPTION

.PP
Fex
is a bioacoustic analysis tool for calculating the frequency 
excursion of an audio recording of bird songs or other sound waves. 



.PP
Fex\-gtk
is a wrapper script for fex that facilitates batch 
processing of large numbers of wave files. Fex\-gtk
can be 
provided a list of file names, or can be run with no parameters to 
provide a dialog window to select input wave files. Fex
will be 
run on each input file, and the results can either be stored to a data 
file or displayed in a dialog window upon completion. 
.PP
Fex\-gtk
is also the executable target of fex.desktop
to 
allow for drag\-and\-drop operation of fex\&.
The desktop file can 
accept any number of wave files as a drop target. 
.PP
.SH OPTIONS

.PP
.TP
\fB\-l\fP|\fB\-\-long\fP
 Provide three column output specifying the path length, path 
duration, and frequency excursion (calculated as path length over 
duration). Default is to provide only the frequency excursion value. 
.TP
\fB\-h\fP|\fB\-\-help\fP
 Print help message and exit. 
.TP
\fB\-v\fP|\fB\-\-version\fP
 Print version information and exit. 
.PP
.SH CONFIGURATION

Fex
is configured via a runtime configuration file read from one 
of 
$XDG_CONFIG_HOME/fex/config,
$HOME/.config/fex/config,

Changes to share/fex.desktop.

1
2
3
4
5
6
7
8
9
10
[Desktop Entry]
Name=Fex
Comment=Calculates the frequency excursion of a sound wave
Exec=fex-gtk %F
#Exec=fex-gtk --long %F
Terminal=false
Type=Application
Categories=Science
Icon=/usr/share/pixmaps/fex.png
MimeType=audio/vnd.wav









|
1
2
3
4
5
6
7
8
9
10
[Desktop Entry]
Name=Fex
Comment=Calculates the frequency excursion of a sound wave
Exec=fex-gtk %F
#Exec=fex-gtk --long %F
Terminal=false
Type=Application
Categories=Science
Icon=/usr/share/pixmaps/fex.png
MimeType=audio/*

Changes to src/fex.h.

28
29
30
31
32
33
34

35
36
37
38
39
40
41
#include <unistd.h>
#include <locale.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

#include <string.h>
#include <sndfile.h>
#include <fftw3.h>
#include <math.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <cairo-ft.h>







>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <unistd.h>
#include <locale.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/xpm.h>
#include <string.h>
#include <sndfile.h>
#include <fftw3.h>
#include <math.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <cairo-ft.h>
109
110
111
112
113
114
115




116
117
118
119
120
121
122
extern const char *configure(int, const char **);
/* fft.c */
extern FFT *create_fft(Wave *);
extern int free_fft(FFT **);
/* spectro.c */
extern int create_spectro(FFT *, const char *);
extern int free_spectro();




/* wave.c */
extern Wave *create_wave(const char *);
extern int free_wave(Wave **);
/* xlib.c */
extern int create_xlib();
extern int free_xlib();
extern cairo_t *xlib_context();







>
>
>
>







110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
extern const char *configure(int, const char **);
/* fft.c */
extern FFT *create_fft(Wave *);
extern int free_fft(FFT **);
/* spectro.c */
extern int create_spectro(FFT *, const char *);
extern int free_spectro();
extern int spectro_spec();
extern int spectro_thresh();
extern int spectro_points();
extern int spectro_draw();
/* wave.c */
extern Wave *create_wave(const char *);
extern int free_wave(Wave **);
/* xlib.c */
extern int create_xlib();
extern int free_xlib();
extern cairo_t *xlib_context();

Changes to src/spectro.c.

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
	cairo_scale(ctx, 1.0, -4.0);
	cairo_translate(ctx, 0, -1.0 * spect->fft_h * conf.scale);
	cairo_set_source_surface(ctx,spect->s_points, 0, 0);
	cairo_paint_with_alpha(ctx, 0.8);
	cairo_surface_write_to_png(img,"/tmp/img3.png");
	cairo_destroy(ctx);
	cairo_surface_destroy(img);

}

int spectro_draw() {
	cairo_t *c = xlib_context();
	cairo_set_source_rgba(c,1.0,1.0,1.0,1.0);
	cairo_rectangle(c,0,0,spect->fft_w,spect->fft_h);
	cairo_fill(c);
	set_color(c,RGBA_SPECT);
	cairo_mask_surface(c,spect->m_spec,0,0);
	if (conf.layers) {
		set_color(c,RGBA_THRESH);
		cairo_mask_surface(c,spect->m_thresh,0,0);
		cairo_scale(c,1.0/conf.scale,1.0/conf.scale);
		cairo_set_source_surface(c,spect->s_points,0,0);
		cairo_paint(c);
	}
	cairo_destroy(c);

}

int create_spectro(FFT *fft, const char *fname) {
	spect = (Spectro *) calloc(1, sizeof(Spectro));
	/* names */
	spect->fname = fname;
	char *c = strrchr(spect->fname,'/');







>

















>







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
	cairo_scale(ctx, 1.0, -4.0);
	cairo_translate(ctx, 0, -1.0 * spect->fft_h * conf.scale);
	cairo_set_source_surface(ctx,spect->s_points, 0, 0);
	cairo_paint_with_alpha(ctx, 0.8);
	cairo_surface_write_to_png(img,"/tmp/img3.png");
	cairo_destroy(ctx);
	cairo_surface_destroy(img);
	return 0;
}

int spectro_draw() {
	cairo_t *c = xlib_context();
	cairo_set_source_rgba(c,1.0,1.0,1.0,1.0);
	cairo_rectangle(c,0,0,spect->fft_w,spect->fft_h);
	cairo_fill(c);
	set_color(c,RGBA_SPECT);
	cairo_mask_surface(c,spect->m_spec,0,0);
	if (conf.layers) {
		set_color(c,RGBA_THRESH);
		cairo_mask_surface(c,spect->m_thresh,0,0);
		cairo_scale(c,1.0/conf.scale,1.0/conf.scale);
		cairo_set_source_surface(c,spect->s_points,0,0);
		cairo_paint(c);
	}
	cairo_destroy(c);
	return 0;
}

int create_spectro(FFT *fft, const char *fname) {
	spect = (Spectro *) calloc(1, sizeof(Spectro));
	/* names */
	spect->fname = fname;
	char *c = strrchr(spect->fname,'/');

Changes to src/wave.c.

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

64
			w->d[nframe] = mix / winfo->channels;
		}
		free(multi);
	}
	if (n != w->samples)
		fprintf(stderr,"Error reading \"%s\"\n\t"
				"\t%d samples read of\n\t%d reported size\n",
				fname, n, w->samples);
	sf_close(wfile);
	return w;
}

int free_wave(Wave **wp) {
	Wave *w = *wp;
	free(w->d);
	free(w);

}







|








>

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
			w->d[nframe] = mix / winfo->channels;
		}
		free(multi);
	}
	if (n != w->samples)
		fprintf(stderr,"Error reading \"%s\"\n\t"
				"\t%d samples read of\n\t%d reported size\n",
				fname, (int) n, w->samples);
	sf_close(wfile);
	return w;
}

int free_wave(Wave **wp) {
	Wave *w = *wp;
	free(w->d);
	free(w);
	return 0;
}

Changes to src/xlib.c.

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

#define MODE_NULL		0x00
#define MODE_ERASE	0x01
#define MODE_CROP		0x02

#define EVENT_MASK	ExposureMask | ButtonPressMask | KeyPressMask | \
							StructureNotifyMask



static void buttonpress(XEvent *);
static void clientmessage(XEvent *);
static void configurenotify(XEvent *);
static void expose(XEvent *);
static void keypress(XEvent *);
static void motionnotify(XEvent *);

static int crop(int, int);
static int erase(int, int);
static int eraser_cursor(int, int);
static int move(double, double);
static int play(float);
static int pt_line(double, double);
static int screenshot();
static int sp_floor(double);
static int threshold(double);
static int zoom(double);

static Display *dpy;
static int scr, ww, wh, mx, my;

static GC gc;
static Window root, win;
static Pixmap buf, ibuf, sbuf;
static Atom WM_DELETE_WINDOW;
static cairo_t *ictx, *sctx;
static Bool running = True;
static double xsc = 1.0, ysc = 1.0, xoff = 0.0, yoff = 0.0;
static int mode = 0, ew = 31, eh = 64;
static char e_col[2][8];
static void (*handler[LASTEvent])(XEvent *) = {
	[ButtonPress]			= buttonpress,
	[ClientMessage]		= clientmessage,







>
>




















|
>


|

|







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

#define MODE_NULL		0x00
#define MODE_ERASE	0x01
#define MODE_CROP		0x02

#define EVENT_MASK	ExposureMask | ButtonPressMask | KeyPressMask | \
							StructureNotifyMask

extern int img_draw();

static void buttonpress(XEvent *);
static void clientmessage(XEvent *);
static void configurenotify(XEvent *);
static void expose(XEvent *);
static void keypress(XEvent *);
static void motionnotify(XEvent *);

static int crop(int, int);
static int erase(int, int);
static int eraser_cursor(int, int);
static int move(double, double);
static int play(float);
static int pt_line(double, double);
static int screenshot();
static int sp_floor(double);
static int threshold(double);
static int zoom(double);

static Display *dpy;
static int scr;
static unsigned int ww, wh, mx, my;
static GC gc;
static Window root, win;
static Pixmap buf; //, ibuf, sbuf;
static Atom WM_DELETE_WINDOW;
//static cairo_t *ictx, *sctx;
static Bool running = True;
static double xsc = 1.0, ysc = 1.0, xoff = 0.0, yoff = 0.0;
static int mode = 0, ew = 31, eh = 64;
static char e_col[2][8];
static void (*handler[LASTEvent])(XEvent *) = {
	[ButtonPress]			= buttonpress,
	[ClientMessage]		= clientmessage,
125
126
127
128
129
130
131

132
133
134
135
136
137
138
139
	XConfigureEvent *e = &ev->xconfigure;
	if (e->window == win) {
		/* The XServer is not storing the new/current values for width +
		 * height.  These must be retrieved directly.
		 * Is this an X11 bug? */
		Window wig;
		int ig;

		XGetGeometry(dpy, win, &wig, &ig, &ig, &ww, &wh, &ig, &ig);
		//ww = e->width; wh = e->height;
		spectro_draw();
		XSetWindowBackgroundPixmap(dpy, win, buf);
		XClearWindow(dpy,win);
	}
}








>
|







128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	XConfigureEvent *e = &ev->xconfigure;
	if (e->window == win) {
		/* The XServer is not storing the new/current values for width +
		 * height.  These must be retrieved directly.
		 * Is this an X11 bug? */
		Window wig;
		int ig;
		unsigned int uig;
		XGetGeometry(dpy, win, &wig, &ig, &ig, &ww, &wh, &uig, &uig);
		//ww = e->width; wh = e->height;
		spectro_draw();
		XSetWindowBackgroundPixmap(dpy, win, buf);
		XClearWindow(dpy,win);
	}
}

198
199
200
201
202
203
204
205

206
207
208
209
210

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
	}
	else if (sym == XK_F2) {
		if ( (info->vis = !info->vis) ) XMapRaised(dpy,info->win);
		else XUnmapWindow(dpy,info->win);
		XFlush(dpy);
	}
	else if (sym == XK_e) {
		mode = MODE_ERASE & (mode ^= MODE_ERASE);

		eraser_cursor(0,0);
		info->draw(info);
	}
	else if (sym == XK_c) {
		mode = MODE_CROP & (mode ^= MODE_CROP);

		if (!(mode & MODE_CROP)) XDefineCursor(dpy, win, None);
		else XDefineCursor(dpy, win, XCreateFontCursor(dpy, 34));
		info->draw(info);
	}
	else if (sym == XK_Escape) {
		mode = MODE_NULL;
		XDefineCursor(dpy, win, None);
		info->draw(info);
	}
	else if (sym == XK_p) play(1.0);
	else if (sym == XK_t) {
		conf.layers = !conf.layers;
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	}
	else if (sym == XK_u && mode & (MODE_ERASE)) erase(-1,-1);
	while(XCheckMaskEvent(dpy, KeyPressMask, ev));
}

void motionnotify(XEvent *ev) {
	static int px, py;
	int x = ev->xmotion.x, y = ev->xmotion.y;
	mx = spect->fft_w * x / (xsc * ww) - 1.0 * xoff + spect->fft_x;
	my = spect->fft_h * (1.0 - y / (ysc * wh)) - 1.0 * yoff + spect->fft_y;
	info->draw(info);
	if (mode == MODE_CROP) {
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		XDrawLine(dpy, win, gc, x, 0, x, wh);







|
>




|
>




















|







202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
	}
	else if (sym == XK_F2) {
		if ( (info->vis = !info->vis) ) XMapRaised(dpy,info->win);
		else XUnmapWindow(dpy,info->win);
		XFlush(dpy);
	}
	else if (sym == XK_e) {
		//mode = MODE_ERASE & (mode ^= MODE_ERASE);
		mode = (mode & MODE_ERASE ? 0 : MODE_ERASE);
		eraser_cursor(0,0);
		info->draw(info);
	}
	else if (sym == XK_c) {
		//mode = MODE_CROP & (mode ^= MODE_CROP);
		mode = (mode & MODE_CROP ? 0 : MODE_CROP);
		if (!(mode & MODE_CROP)) XDefineCursor(dpy, win, None);
		else XDefineCursor(dpy, win, XCreateFontCursor(dpy, 34));
		info->draw(info);
	}
	else if (sym == XK_Escape) {
		mode = MODE_NULL;
		XDefineCursor(dpy, win, None);
		info->draw(info);
	}
	else if (sym == XK_p) play(1.0);
	else if (sym == XK_t) {
		conf.layers = !conf.layers;
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	}
	else if (sym == XK_u && mode & (MODE_ERASE)) erase(-1,-1);
	while(XCheckMaskEvent(dpy, KeyPressMask, ev));
}

void motionnotify(XEvent *ev) {
	//static int px, py;
	int x = ev->xmotion.x, y = ev->xmotion.y;
	mx = spect->fft_w * x / (xsc * ww) - 1.0 * xoff + spect->fft_x;
	my = spect->fft_h * (1.0 - y / (ysc * wh)) - 1.0 * yoff + spect->fft_y;
	info->draw(info);
	if (mode == MODE_CROP) {
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		XDrawLine(dpy, win, gc, x, 0, x, wh);
332
333
334
335
336
337
338

339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

364
365
366
367
368
369
370
371
372
373
374
375
376
377

378
379
380
381
382
383
384
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		info_draw(info);
		if (e.type == ButtonRelease) break;
		while(XCheckMaskEvent(dpy, PointerMotionMask, &e));
	}
	XUngrabPointer(dpy, CurrentTime);

}

int eraser_cursor(int w, int h) {
	XUndefineCursor(dpy, win);
	if ( !(mode & MODE_ERASE) ) return 0;
	if ( (ew+=w) < 3 ) ew = 3;
	if ( (eh+=h) < 3 ) eh = 3;
	if ( ew > ww/4 ) ew = ww / 4;
	if ( eh > wh/2 ) eh = wh / 2;
	int i, j, stride = ew/ 8 + 1;
	char data[stride * eh];
	char mask[stride * eh];
	XColor bg, fg;
	XAllocNamedColor(dpy,DefaultColormap(dpy,scr),e_col[0],&bg,&bg);
	XAllocNamedColor(dpy,DefaultColormap(dpy,scr),e_col[1],&fg,&fg);
	memset(mask, 0xAA, stride * eh);
	memset(data, 0xAA, stride * eh);
	for (i = 0; i < eh; i+= 2) memset(&mask[stride * i], 0x55, stride);
	Pixmap cd = XCreateBitmapFromData(dpy, win, data, ew, eh);
	Pixmap cm = XCreateBitmapFromData(dpy, win, mask, ew, eh);
	Cursor c = XCreatePixmapCursor(dpy, cd, cm, &fg, &bg, ew/2, eh/2);
	XDefineCursor(dpy, win, c);
	XFreePixmap(dpy, cd);
	XFreePixmap(dpy, cm);
	XFlush(dpy);

}

int move(double x, double y) {
	xoff += x / xsc * spect->fft_w; yoff += y / ysc * spect->fft_h;
	if ( yoff > spect->fft_h * (1.0 - 1.0/ysc) )
		yoff = spect->fft_h * (1.0 - 1.0/ysc);
	if ( yoff < 0) yoff = 0;
	if ( xoff < spect->fft_w * (1.0/xsc - 1.0) )
		xoff = spect->fft_w * (1.0/xsc - 1.0);
	if ( xoff > 0) xoff = 0;
	if (x || y) { /* don't redraw for offset checks */
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	}

}

int play(float speed) {
	/* calculate fft coordinates of current view window */
	int tw = spect->fft_w / xsc;
	int th = spect->fft_h / ysc;
	int tx = spect->fft_x - xoff;







>









|















>














>







338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
		info_draw(info);
		if (e.type == ButtonRelease) break;
		while(XCheckMaskEvent(dpy, PointerMotionMask, &e));
	}
	XUngrabPointer(dpy, CurrentTime);
	return 0;
}

int eraser_cursor(int w, int h) {
	XUndefineCursor(dpy, win);
	if ( !(mode & MODE_ERASE) ) return 0;
	if ( (ew+=w) < 3 ) ew = 3;
	if ( (eh+=h) < 3 ) eh = 3;
	if ( ew > ww/4 ) ew = ww / 4;
	if ( eh > wh/2 ) eh = wh / 2;
	int i, stride = ew/ 8 + 1;
	char data[stride * eh];
	char mask[stride * eh];
	XColor bg, fg;
	XAllocNamedColor(dpy,DefaultColormap(dpy,scr),e_col[0],&bg,&bg);
	XAllocNamedColor(dpy,DefaultColormap(dpy,scr),e_col[1],&fg,&fg);
	memset(mask, 0xAA, stride * eh);
	memset(data, 0xAA, stride * eh);
	for (i = 0; i < eh; i+= 2) memset(&mask[stride * i], 0x55, stride);
	Pixmap cd = XCreateBitmapFromData(dpy, win, data, ew, eh);
	Pixmap cm = XCreateBitmapFromData(dpy, win, mask, ew, eh);
	Cursor c = XCreatePixmapCursor(dpy, cd, cm, &fg, &bg, ew/2, eh/2);
	XDefineCursor(dpy, win, c);
	XFreePixmap(dpy, cd);
	XFreePixmap(dpy, cm);
	XFlush(dpy);
	return 0;
}

int move(double x, double y) {
	xoff += x / xsc * spect->fft_w; yoff += y / ysc * spect->fft_h;
	if ( yoff > spect->fft_h * (1.0 - 1.0/ysc) )
		yoff = spect->fft_h * (1.0 - 1.0/ysc);
	if ( yoff < 0) yoff = 0;
	if ( xoff < spect->fft_w * (1.0/xsc - 1.0) )
		xoff = spect->fft_w * (1.0/xsc - 1.0);
	if ( xoff > 0) xoff = 0;
	if (x || y) { /* don't redraw for offset checks */
		spectro_draw();
		XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	}
	return 0;
}

int play(float speed) {
	/* calculate fft coordinates of current view window */
	int tw = spect->fft_w / xsc;
	int th = spect->fft_h / ysc;
	int tx = spect->fft_x - xoff;
415
416
417
418
419
420
421

422
423
424
425
426
427
428
429
430
431

432
433
434
435
436
437
438
439
440
441

442
443
444
445
446
447
448
449

450
451
452
453
454
455
456
457
458

459
460
461
462
463
464
465
466
467
468
469
470

471
472
473
474
475
476
477
		XDrawLine(dpy, win, gc, i, 0, i, wh);
		XDrawLine(dpy, win, gc, i+step, 0, i+step, wh);
		XFlush(dpy);
		usleep(usec);
	}
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	XFlush(dpy);

}

int pt_line(double p, double l) {
	conf.col[RGBA_POINTS].w += p;
	conf.col[RGBA_LINES].w += l;
	if (conf.col[RGBA_POINTS].w < 0) conf.col[RGBA_POINTS].w = 0.0;
	if (conf.col[RGBA_LINES].w < 0) conf.col[RGBA_LINES].w = 0.0;
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);

}

int screenshot() {
	char fname[256];
	static int n = 0;
	snprintf(fname, 256, "%s-%d.png", spect->name, n++);
	cairo_surface_t *t = cairo_xlib_surface_create(dpy, buf,
			DefaultVisual(dpy,scr), ww, wh);
	cairo_surface_write_to_png(t, fname);
	cairo_surface_destroy(t);

}

int sp_floor(double f) {
	conf.spect_floor += f;
	spectro_spec();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	if (info->vis) info->draw(info);

}

int threshold(double f) {
	conf.thresh += f;
	spectro_thresh();
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	if (info->vis) info->draw(info);

}

int zoom(double f) {
	double px = xsc, py = ysc;
	xsc += f; ysc += f;
	if (xsc < 1.0) xsc = 1.0;
	if (ysc < 1.0) ysc = 1.0;
	xoff += (spect->fft_w/xsc - spect->fft_w/px) / 2.0;
	yoff += (spect->fft_h/py - spect->fft_h/ysc) / 2.0;
	if (f < 0) move(0,0); /* check offsets for bounds */
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);

}

int create_xlib() {
	if (!setlocale(LC_CTYPE,"")) die("unable to set locale");
	if (!XSupportsLocale()) die("unsupported locale");
	if (!XSetLocaleModifiers("")) die("unable to set locale modifiers");
	if ( !(dpy=XOpenDisplay(0x0)) ) die("unable to open display");







>










>










>








>









>












>







424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
		XDrawLine(dpy, win, gc, i, 0, i, wh);
		XDrawLine(dpy, win, gc, i+step, 0, i+step, wh);
		XFlush(dpy);
		usleep(usec);
	}
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	XFlush(dpy);
	return 0;
}

int pt_line(double p, double l) {
	conf.col[RGBA_POINTS].w += p;
	conf.col[RGBA_LINES].w += l;
	if (conf.col[RGBA_POINTS].w < 0) conf.col[RGBA_POINTS].w = 0.0;
	if (conf.col[RGBA_LINES].w < 0) conf.col[RGBA_LINES].w = 0.0;
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	return 0;
}

int screenshot() {
	char fname[256];
	static int n = 0;
	snprintf(fname, 256, "%s-%d.png", spect->name, n++);
	cairo_surface_t *t = cairo_xlib_surface_create(dpy, buf,
			DefaultVisual(dpy,scr), ww, wh);
	cairo_surface_write_to_png(t, fname);
	cairo_surface_destroy(t);
	return 0;
}

int sp_floor(double f) {
	conf.spect_floor += f;
	spectro_spec();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	if (info->vis) info->draw(info);
	return 0;
}

int threshold(double f) {
	conf.thresh += f;
	spectro_thresh();
	spectro_points();
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	if (info->vis) info->draw(info);
	return 0;
}

int zoom(double f) {
	double px = xsc, py = ysc;
	xsc += f; ysc += f;
	if (xsc < 1.0) xsc = 1.0;
	if (ysc < 1.0) ysc = 1.0;
	xoff += (spect->fft_w/xsc - spect->fft_w/px) / 2.0;
	yoff += (spect->fft_h/py - spect->fft_h/ysc) / 2.0;
	if (f < 0) move(0,0); /* check offsets for bounds */
	spectro_draw();
	XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
	return 0;
}

int create_xlib() {
	if (!setlocale(LC_CTYPE,"")) die("unable to set locale");
	if (!XSupportsLocale()) die("unsupported locale");
	if (!XSetLocaleModifiers("")) die("unable to set locale modifiers");
	if ( !(dpy=XOpenDisplay(0x0)) ) die("unable to open display");
528
529
530
531
532
533
534

535
536
537
538
539
540
541
}

int free_xlib() {
	XUndefineCursor(dpy, win);
	toolwin_destroy();
	XFlush(dpy);
	XCloseDisplay(dpy);

}

cairo_t *xlib_context() {
	cairo_surface_t *t = cairo_xlib_surface_create(dpy, buf,
			DefaultVisual(dpy,scr), ww, wh);
	cairo_t *c = cairo_create(t);
	cairo_surface_destroy(t);







>







543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
}

int free_xlib() {
	XUndefineCursor(dpy, win);
	toolwin_destroy();
	XFlush(dpy);
	XCloseDisplay(dpy);
	return 0;
}

cairo_t *xlib_context() {
	cairo_surface_t *t = cairo_xlib_surface_create(dpy, buf,
			DefaultVisual(dpy,scr), ww, wh);
	cairo_t *c = cairo_create(t);
	cairo_surface_destroy(t);
549
550
551
552
553
554
555

556
557
558
int xlib_event_loop() {
	XMapWindow(dpy, win);
	XMapWindow(dpy, info->win);
	XEvent ev;
	while (running && !XNextEvent(dpy,&ev))
		if (ev.type < LASTEvent && handler[ev.type])
			handler[ev.type](&ev);

}









>



565
566
567
568
569
570
571
572
573
574
575
int xlib_event_loop() {
	XMapWindow(dpy, win);
	XMapWindow(dpy, info->win);
	XEvent ev;
	while (running && !XNextEvent(dpy,&ev))
		if (ev.type < LASTEvent && handler[ev.type])
			handler[ev.type](&ev);
	return 0;
}


Changes to src/xlib_toolwin.c.

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	Bool vis;
	int (*backing)(ToolWin *);
	int (*draw)(ToolWin *);
	int (*button)(ToolWin *, XButtonEvent *);
};

static int toolwin_backing(ToolWin *);
static int toolwin_button(ToolWin *, XButtonEvent *);
static int toolwin_create();
static int toolwin_destroy();
static int toolwin_draw(ToolWin *);
static int toolwin_win_create(ToolWin *);
static int toolwin_win_destroy(ToolWin *);

static ToolWin *info; //, *help;







|







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	Bool vis;
	int (*backing)(ToolWin *);
	int (*draw)(ToolWin *);
	int (*button)(ToolWin *, XButtonEvent *);
};

static int toolwin_backing(ToolWin *);
//static int toolwin_button(ToolWin *, XButtonEvent *);
static int toolwin_create();
static int toolwin_destroy();
static int toolwin_draw(ToolWin *);
static int toolwin_win_create(ToolWin *);
static int toolwin_win_destroy(ToolWin *);

static ToolWin *info; //, *help;
145
146
147
148
149
150
151
152
153
154
155
156

157
158
159
160
161
162
163

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

184
185
186
187
188
189
190
191
192
193
194
195


196
197
198
199
200
201
202
	cairo_show_text(tw->ctx,"Crop");
	/* draw */
	toolwin_draw(tw);
	return 0;
}

static int info_button(ToolWin *tw, XButtonEvent *e) {
	if (e->y < 170 || e->y > 190) return;
	if (e->x < 10 || e->x > tw->w - 10) return;
	if (e->x < tw->w / 2.0 - 5) { /* erase button */
		if (e->button == 1) {
			mode = MODE_ERASE & (mode ^= MODE_ERASE);

			eraser_cursor(0,0);
			tw->draw(tw);
		}
	}
	else { /* crop button */
		if (e->button == 1) {
			mode = MODE_CROP & (mode ^= MODE_CROP);

			if (!(mode & MODE_CROP)) XDefineCursor(dpy, win, None);
			else XDefineCursor(dpy, win, XCreateFontCursor(dpy, 34));
			tw->draw(tw);
		}
		else if (e->button == 3) { /* uncrop */
			mode |= MODE_CROP;
			tw->draw(tw);
			spect->fft_x = 0;
			spect->fft_y = spect->fft_lo;
			spect->fft_w = spect->fft->ntime;
			spect->fft_h = spect->fft_hi - spect->fft_lo;
			spectro_spec();
			spectro_thresh();
			spectro_points();
			spectro_draw();
			XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
			mode &= ~MODE_CROP;
			tw->draw(tw);
		}
	}

}

int toolwin_backing(ToolWin *tw) {
	cairo_set_source_rgba(tw->ctx,0.6,0.6,0.6,1.0);
	cairo_rectangle(tw->ctx, 0, 0, tw->w,tw->h);
	cairo_fill(tw->ctx);
	return 0;
}

int toolwin_button(ToolWin *tw, XButtonEvent *e) {
	XRaiseWindow(dpy, tw->win);
}



int toolwin_create() {
	info = (ToolWin *) calloc(1, sizeof(ToolWin));
	//help = (ToolWin *) calloc(1, sizeof(ToolWin));
	info->w = 240; info->h = 200;
	info->name = spect->name;
	info->vis = True;







|
|


|
>






|
>




















>









|
|
<
>
>







145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

198
199
200
201
202
203
204
205
206
	cairo_show_text(tw->ctx,"Crop");
	/* draw */
	toolwin_draw(tw);
	return 0;
}

static int info_button(ToolWin *tw, XButtonEvent *e) {
	if (e->y < 170 || e->y > 190) return 0;
	if (e->x < 10 || e->x > tw->w - 10) return 0;
	if (e->x < tw->w / 2.0 - 5) { /* erase button */
		if (e->button == 1) {
			//mode = MODE_ERASE & (mode ^= MODE_ERASE);
			mode = (mode & MODE_ERASE ? 0 : MODE_ERASE);
			eraser_cursor(0,0);
			tw->draw(tw);
		}
	}
	else { /* crop button */
		if (e->button == 1) {
			//mode = MODE_CROP & (mode ^= MODE_CROP);
			mode = (mode & MODE_CROP ? 0 : MODE_CROP);
			if (!(mode & MODE_CROP)) XDefineCursor(dpy, win, None);
			else XDefineCursor(dpy, win, XCreateFontCursor(dpy, 34));
			tw->draw(tw);
		}
		else if (e->button == 3) { /* uncrop */
			mode |= MODE_CROP;
			tw->draw(tw);
			spect->fft_x = 0;
			spect->fft_y = spect->fft_lo;
			spect->fft_w = spect->fft->ntime;
			spect->fft_h = spect->fft_hi - spect->fft_lo;
			spectro_spec();
			spectro_thresh();
			spectro_points();
			spectro_draw();
			XCopyArea(dpy, buf, win, gc, 0, 0, ww, wh, 0, 0);
			mode &= ~MODE_CROP;
			tw->draw(tw);
		}
	}
	return 0;
}

int toolwin_backing(ToolWin *tw) {
	cairo_set_source_rgba(tw->ctx,0.6,0.6,0.6,1.0);
	cairo_rectangle(tw->ctx, 0, 0, tw->w,tw->h);
	cairo_fill(tw->ctx);
	return 0;
}

//int toolwin_button(ToolWin *tw, XButtonEvent *e) {
//	XRaiseWindow(dpy, tw->win);

//	return 0;
//}

int toolwin_create() {
	info = (ToolWin *) calloc(1, sizeof(ToolWin));
	//help = (ToolWin *) calloc(1, sizeof(ToolWin));
	info->w = 240; info->h = 200;
	info->name = spect->name;
	info->vis = True;
213
214
215
216
217
218
219

220
221
222
223
224
225
226
}

int toolwin_destroy() {
	toolwin_win_destroy(info);
	//toolwin_win_destroy(help);
	free(info);
	//free(help);

}

int toolwin_draw(ToolWin *tw) {
	XClearWindow(dpy,tw->win);
	XFlush(dpy);
	return 0;
}







>







217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
}

int toolwin_destroy() {
	toolwin_win_destroy(info);
	//toolwin_win_destroy(help);
	free(info);
	//free(help);
	return 0;
}

int toolwin_draw(ToolWin *tw) {
	XClearWindow(dpy,tw->win);
	XFlush(dpy);
	return 0;
}
251
252
253
254
255
256
257

258
259
	return 0;
}

int toolwin_win_destroy(ToolWin *tw) {
	cairo_destroy(tw->ctx);
	XDestroyWindow(dpy, tw->win);
	XFreePixmap(dpy, tw->buf);

}








>


256
257
258
259
260
261
262
263
264
265
	return 0;
}

int toolwin_win_destroy(ToolWin *tw) {
	cairo_destroy(tw->ctx);
	XDestroyWindow(dpy, tw->win);
	XFreePixmap(dpy, tw->buf);
	return 0;
}