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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/*
vaud.c
A console based audio visualizer for JACK
Copyright (C) 2014 Alexander M. Pickering
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
A big thank you goes to Nicholas J. Humfrey, whos jack meter
was modified to make this. You can view the project here:
http://www.aelius.com/njh/jackmeter/
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <ncurses.h>
#include <jack/jack.h>
#include <getopt.h>
#include "config.h"
/*Stuff to get from the settings file*/
float bias = 1.0f;
float peak = 0.0f;
jack_default_audio_sample_t *data;
jack_nframes_t dframes;
int dpeak = 0;
int dtime = 0;
int decay_len;
int set = 0;
jack_port_t *input_port = NULL;
jack_client_t *client = NULL;
void create_newwin(int height, int width, int starty, int startx, WINDOW* win);
/* Read and reset the recent peak sample */
void read_peak(float tmp)
{
tmp = peak;
peak = 0.0f;
}
/* Callback called by JACK when audio is available.
Stores value of peak sample */
static int process_peak(jack_nframes_t nframes, void *arg)
{
dframes = nframes;
jack_default_audio_sample_t *in;
unsigned int i;
/* just incase the port isn't registered yet */
if (input_port == NULL) {
return 0;
}
/* get the audio samples, and find the peak sample */
in = (jack_default_audio_sample_t *) jack_port_get_buffer(input_port, nframes);
for (i = 0; i < nframes; i++) {
const float s = fabs(in[i]);
if (s > peak) {
peak = s;
}
}
data = in;
return 0;
}
/* Close down JACK when exiting */
static void cleanup()
{
const char **all_ports;
unsigned int i;
fprintf(stderr,"cleanup()\n");
if (input_port != NULL ) {
all_ports = jack_port_get_all_connections(client, input_port);
for (i=0; all_ports && all_ports[i]; i++) {
jack_disconnect(client, all_ports[i], jack_port_name(input_port));
}
}
/* Leave the jack graph */
jack_client_close(client);
}
/* Connect the chosen port to ours */
static void connect_port(jack_client_t *client, char *port_name)
{
jack_port_t *port;
// Get the port we are connecting to
port = jack_port_by_name(client, port_name);
if (port == NULL) {
fprintf(stderr, "Can't find port '%s'\n", port_name);
exit(1);
}
// Connect the port to our input port
fprintf(stderr,"Connecting '%s' to '%s'...\n", jack_port_name(port), jack_port_name(input_port));
if (jack_connect(client, jack_port_name(port), jack_port_name(input_port))) {
fprintf(stderr, "Cannot connect port '%s' to '%s'\n", jack_port_name(port), jack_port_name(input_port));
exit(1);
}
}
/* Sleep for a fraction of a second */
static int fsleep( float secs )
{
//#ifdef HAVE_USLEEP
return usleep( secs * 1000000 );
//#endif
}
/* Display how to use this program */
static int usage( const char * progname )
{
fprintf(stderr, "Visual Audio Version - %s\n\n", VERSION);
fprintf(stderr, "Usage %s [-f freqency] [-m meter] [-n] [<port>, ...]\n\n", progname);
fprintf(stderr, "where -f is how often to update the meter per second [8]\n");
fprintf(stderr, " -g is the display style of the meter (0-2)\n");
fprintf(stderr, " 0 - top down\n 1 - bottom up\n 2 - centered, symetric (window must be a multiple of 2!)");
fprintf(stderr, " -n changes mode to output meter level as number in decibels\n");
fprintf(stderr, " <port> the port(s) to monitor (multiple ports are mixed)\n");
exit(1);
}
/*Start the ncurses stuff
*/
void start_ncurses()
{
if(COLORFUL && !has_colors) //Settings tell us to be colorful,
//but terminal does not support colors.
{
printf("Your terminal does not support colors! Chage \"COLORFUL\" in config.h to 0 and rebuild.");
exit(1);
}
initscr(); //Start ncurses
curs_set(0); //Hide cursor
if(COLORFUL)
{
start_color(); //Tell ncurses to be colorufl!
use_default_colors();
//init_pair(1, BORCOLORFG, BORCOLORBG); //Color the border
//init_pair(2, BARCOLORFG, BARCOLORBG); //Color the bars
//init_pair(3, BLANKCOLORBG, BLANKCOLORFG); //Color the blank space
}
}
/* Creates a new NCurses window
*int height : the height of the window to make
*int width : the width of the window to make
*int startx : the column to start the window on
*int starty : the row to start the window on
*/
void create_newwin(int height, int width, int startx, int starty, WINDOW* win)
{
if(COLORFUL)
attron(COLOR_PAIR(1));
win = newwin(height, width, starty, startx);
box(win, 0 , 0);
wrefresh(win);
if(COLORFUL)
attroff(COLOR_PAIR(1));
}
void display_centerBars()
{
int width,height,startx,starty,i=0;
WINDOW *my_win;
/*Get width and height of console, make border*/
getmaxyx(stdscr,height,width);
//create_newwin(height,width,startx,starty,my_win);
refresh();
/*Figure out how which nodes we can safely get*/
if(height % 2 == 1)
{
int space = ((unsigned int) dframes) / width;
if(space > 0)
{
for(i=1;i<width-1;i+=1)
{
float samp = fabs(data[space * i]); //get the waveform data
float dec;
int j;
for(j=0;j<samp*((height-2)/2);j++)
{
//if(COLORFUL)
{attron(COLOR_PAIR(j));}
mvprintw((height/2)+j,i,BAR);
mvprintw((height/2)-j,i,BAR);
//if(COLORFUL)
{attroff(COLOR_PAIR(j));}
}
for(j=samp*(height/2);j<(height/2);j++)
{
if(COLORFUL)
attron(COLOR_PAIR(0));
mvprintw((height/2)+j,i,BLANK);
mvprintw((height/2)-j,i,BLANK);
if(COLORFUL)
attroff(COLOR_PAIR(0));
}
}
}
}
else
{
mvprintw(1,1,"Height needs to be a multiple of 2! Resize window!");
}
}
void display_rmeter()
{
int width,height,startx,starty,i=0;
WINDOW *my_win;
/*Get width and height of console, make border*/
getmaxyx(stdscr,height,width);
create_newwin(height,width,startx,starty,my_win);
refresh();
/*Figure out how which nodes we can safely get*/
int space = ((unsigned int) dframes) / width;
if(space > 0)
{
for(i=1;i<width-1;i+=1)
{
float samp = fabs(data[space * i]); //get the waveform data
float dec;
int j;
for(j=height-2;j>((1-samp)*height) && j>1;j--)
{
if(COLORFUL)
attron(COLOR_PAIR(2));
mvprintw(j,i,BAR);
if(COLORFUL)
attroff(COLOR_PAIR(2));
}
for(j=((1-samp)*height-2);j>0;j--)
{
if(COLORFUL)
attron(COLOR_PAIR(3));
mvprintw(j,i,BLANK);
if(COLORFUL)
attroff(COLOR_PAIR(3));
}
}
}
refresh();
}
/* Displays the graph
*/
void display_meter()
{
int width,height,startx,starty,i=0;
WINDOW *my_win;
/*Get width and height of console, make border*/
getmaxyx(stdscr,height,width);
create_newwin(height,width,startx,starty,my_win);
refresh();
/*Figure out how which nodes we can safely get*/
int space = ((unsigned int) dframes) / width;
if(space > 0)
{
for(i=1;i<width-1;i+=1)
{
float samp = fabs(data[space * i]); //get the waveform data
float dec;
int j;
for(j=1;j<((samp)*(height)) && j<height-1;j++)
{
if(COLORFUL)
attron(COLOR_PAIR(2));
mvprintw(j,i,BAR);
if(COLORFUL)
attroff(COLOR_PAIR(2));
}
for(j=((samp)*height)+1;j<height-1;j++)
{
if(COLORFUL)
attron(COLOR_PAIR(3));
mvprintw(j,i,BLANK);
if(COLORFUL)
attroff(COLOR_PAIR(3));
}
}
}
refresh();
}
int main(int argc, char *argv[])
{
int console_width = 79;
jack_status_t status;
int running = 1;
float ref_lev;
int decibels_mode = 0;
int rate = 8;
int opt;
int graph = 1;
// Make STDOUT unbuffered
setbuf(stdout, NULL);
while ((opt = getopt(argc, argv, "f:g:n")) != -1) {
switch (opt) {
case 'g':
graph = atoi(optarg);
break;
case 'f':
rate = atoi(optarg);
fprintf(stderr,"Updates per second: %d\n", rate);
break;
case 'n':
decibels_mode = 1;
break;
default:
/* Show usage/version information */
usage( argv[0] );
break;
}
}
// Register with Jack
if ((client = jack_client_open("meter", JackNullOption, &status)) == 0) {
fprintf(stderr, "Failed to start jack client: %d\n", status);
exit(1);
}
fprintf(stderr,"Registering as '%s'.\n", jack_get_client_name( client ) );
// Create our input port
if (!(input_port = jack_port_register(client, "in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0))) {
fprintf(stderr, "Cannot register input port 'meter'.\n");
exit(1);
}
// Register the cleanup function to be called when program exits
atexit( cleanup );
// Register the peak signal callback
jack_set_process_callback(client, process_peak, 0);
if (jack_activate(client)) {
fprintf(stderr, "Cannot activate client.\n");
exit(1);
}
// Connect our port to specified port(s)
if (argc > optind) {
while (argc > optind) {
connect_port( client, argv[ optind ] );
optind++;
}
} else {
fprintf(stderr,"Meter is not connected to a port.\n");
}
// Calculate the decay length (should be 1600ms)
decay_len = (int)(1.6f / (1.0f/rate));
// Display the scale
if (decibels_mode==0) {
start_ncurses();
}
float db;
float p;
while (running) {
read_peak(p);
db = 20.0f * log10f(p * bias);
if (decibels_mode==1) {
printf("%1.1f\n", db);
} else {
switch(graph)
{
case 0 :/* display_rmeter()*/;break;
case 1 :/* display_meter()*/;break;
case 2 :display_centerBars();break;
}
}
fsleep( 1.0f/rate );
}
endwin();
return 0;
}
|