aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/display_wave.c
blob: a10debed70fa014ba4a0f6d1ea1214cd19110e82 (plain)
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
/* display wave form functions
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "sender.h"

#define DISPLAY_INTERVAL 0.04

#define WIDTH	80
#define HEIGHT	10

static char screen[HEIGHT][WIDTH+1];
static int wave_on = 0;

void display_wave_init(sender_t *sender, int samplerate)
{
	dispwav_t *disp = &sender->dispwav;

	memset(disp, 0, sizeof(*disp));
	disp->interval_max = (double)samplerate * DISPLAY_INTERVAL + 0.5;
}

void display_wave_on(int on)
{
	int j;

	if (on < 0)
		wave_on = 1 - wave_on;
	else
		wave_on = on;

	memset(&screen, ' ', sizeof(screen));
	printf("\0337\033[H");
	for (j = 0; j < HEIGHT; j++) {
		screen[j][WIDTH] = '\0';
		puts(screen[j]);
	}
	printf("\0338"); fflush(stdout);
}

void display_wave(sender_t *sender, int16_t *samples, int length)
{
	dispwav_t *disp = &sender->dispwav;
	int pos, max;
	int16_t *buffer;
	int i, j, y;

	if (!wave_on)
		return;

	pos = disp->interval_pos;
	max = disp->interval_max;
	buffer = disp->buffer;

	for (i = 0; i < length; i++) {
		if (pos >= WIDTH) {
			if (++pos == max)
				pos = 0;
			continue;
		}
		buffer[pos++] = samples[i];
		if (pos == WIDTH) {
			memset(&screen, ' ', sizeof(screen));
			for (j = 0; j < WIDTH; j++) {
				/* must divide by 65536, because we may never reach HEIGHT*2! */
				y = (32767 - (int)buffer[j]) * HEIGHT * 2 / 65536;
				screen[y >> 1][j] = (y & 1) ? '_' : '-';
			}
			printf("\0337\033[H");
			for (j = 0; j < HEIGHT; j++) {
				screen[j][WIDTH] = '\0';
				puts(screen[j]);
			}
			printf("\0338"); fflush(stdout);
		}
	}

	disp->interval_pos = pos;
}