-
Notifications
You must be signed in to change notification settings - Fork 5
/
fbff.c
179 lines (159 loc) · 3.48 KB
/
fbff.c
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
/*
* fbff - a small ffmpeg-based framebuffer/oss media player
*
* Copyright (C) 2009-2011 Ali Gholami Rudi
*
* This program is released under GNU GPL version 2.
*/
#include <fcntl.h>
#include <pty.h>
#include <ctype.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/poll.h>
#include <sys/soundcard.h>
#include <pthread.h>
#include "config.h"
#include "ffs.h"
#include "draw.h"
#include "gain_analysis.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
static struct termios termios;
static int paused;
static int exited;
static int audio = 1;
static struct ffs *affs; /* audio ffmpeg stream */
static void stroll(void)
{
usleep(10000);
}
#define ABUFSZ (1 << 18)
static int a_cons;
static int a_prod;
static char a_buf[AUDIOBUFS][ABUFSZ];
static int a_len[AUDIOBUFS];
static int a_reset;
static int a_conswait(void)
{
return a_cons == a_prod;
}
static int a_prodwait(void)
{
return ((a_prod + 1) & (AUDIOBUFS - 1)) == a_cons;
}
static void a_doreset(int pause)
{
a_reset = 1 + pause;
while (audio && a_reset)
stroll();
}
#define JMP1 (1 << 5)
#define JMP2 (JMP1 << 3)
#define JMP3 (JMP2 << 5)
static void mainloop(void)
{
int eof = 0;
while (eof < audio) {
if (exited)
break;
if (paused) {
a_doreset(1);
continue;
}
while (audio && !eof && !a_prodwait()) {
int ret = ffs_adec(affs, a_buf[a_prod], ABUFSZ);
if (ret < 0)
eof++;
if (ret > 0) {
a_len[a_prod] = ret;
a_prod = (a_prod + 1) & (AUDIOBUFS - 1);
}
}
stroll();
}
exited = 1;
}
static void *process_audio(void *dat)
{
float max_pcm = 0;
while (1) {
while (!a_reset && (a_conswait() || paused) && !exited)
stroll();
if (exited)
goto ret;
if (a_reset) {
if (a_reset == 1)
a_cons = a_prod;
a_reset = 0;
continue;
}
Float_t analysis_buf[a_len[a_cons]];
int i;
for (i=0; i < a_len[a_cons]; i++) {
analysis_buf[i] = (((float)a_buf[a_cons][i])/127.0);
if (max_pcm < analysis_buf[i])
max_pcm = analysis_buf[i];
}
if (AnalyzeSamples(analysis_buf, NULL, a_len[a_cons], 1) != GAIN_ANALYSIS_OK) {
fprintf(stderr, "!!!!!!@$#!@#$# PROBLEM OFFICER: NYAN NYAN NYAN NYAN\n");
}
//write(afd, a_buf[a_cons], a_len[a_cons]);
a_cons = (a_cons + 1) & (AUDIOBUFS - 1);
}
ret:
printf("Max PCM value found: %f\n", max_pcm);
return NULL;
}
static void term_setup(void)
{
struct termios newtermios;
tcgetattr(STDIN_FILENO, &termios);
newtermios = termios;
newtermios.c_lflag &= ~ICANON;
newtermios.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &newtermios);
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
}
static void term_cleanup(void)
{
tcsetattr(STDIN_FILENO, 0, &termios);
}
static void sigcont(int sig)
{
term_setup();
}
int main(int argc, char *argv[])
{
int rate, ch, bps;
pthread_t a_thread;
char *path = argv[argc - 1];
if (argc < 2) {
printf("usage: %s filename\n", argv[0]);
return 1;
}
ffs_globinit();
if (audio && !(affs = ffs_alloc(path, 0)))
audio = 0;
if (!audio)
return 1;
ffs_ainfo(affs, &rate, &bps, &ch);
printf("Detected sample rate %dHz\n", rate);
long gain_rate = rate;
InitGainAnalysis(gain_rate);
pthread_create(&a_thread, NULL, process_audio, NULL);
term_setup();
signal(SIGCONT, sigcont);
mainloop();
term_cleanup();
pthread_join(a_thread, NULL);
ffs_free(affs);
float title_gain = GetTitleGain();
printf("Title gain: %f\n", title_gain);
return 0;
}