2 $GLOBALS['options'] = array(
3 'analyze_input' => FALSE,
4 'reduce_noise' => FALSE,
5 'ignore_noise' => FALSE,
10 if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'a') {
11 $GLOBALS['options']['analyze_input'] = TRUE;
12 } elseif (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'r') {
13 $GLOBALS['options']['reduce_noise'] = TRUE;
14 } elseif (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'k') {
15 $GLOBALS['options']['keep_noise'] = TRUE;
18 if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'i') {
19 $GLOBALS['options']['ignore_noise'] = TRUE;
20 } elseif (isset($_SERVER['argv'][2]) && $_SERVER['argv'][2] == 'i') {
21 $GLOBALS['options']['ignore_noise'] = TRUE;
22 } elseif (isset($_SERVER['argv'][2]) && $_SERVER['argv'][2] == 'k') {
23 $GLOBALS['options']['keep_noise'] = TRUE;
26 function analyzeForNoiseOnly ($data) {
27 $GLOBALS['analysis']['breakdown'] = array();
28 $GLOBALS['analysis']['average'] = 0;
30 for ($i = 0; $i < strlen($data); $i++) {
31 $ord = ord(substr($data, $i, 1));
32 $GLOBALS['analysis']['breakdown'][$ord] = TRUE;
33 $GLOBALS['analysis']['average'] += $ord;
36 if (!$GLOBALS['options']['keep_noise'] && !$GLOBALS['options']['ignore_noise'] && count($GLOBALS['analysis']['breakdown']) == 2 && isset($GLOBALS['analysis']['breakdown'][127]) && isset($GLOBALS['analysis']['breakdown'][128])) {
37 if ($GLOBALS['options']['analyze_input']) {
38 //echo 'NOISE1!' . PHP_EOL;
41 } elseif (!$GLOBALS['options']['keep_noise'] && !$GLOBALS['options']['ignore_noise'] && count($GLOBALS['analysis']['breakdown']) == 1 && isset($GLOBALS['analysis']['breakdown'][127])) {
42 if ($GLOBALS['options']['analyze_input']) {
43 //echo 'NOISE2!' . PHP_EOL;
46 } elseif (!$GLOBALS['options']['keep_noise'] && $GLOBALS['options']['ignore_noise'] && count($GLOBALS['analysis']['breakdown']) < 2) {
47 if ($GLOBALS['options']['analyze_input']) {
48 //echo 'NOISE3!' . PHP_EOL;
57 //$pcm = fopen('output1.pcm', 'rb') or die('Cannot read from audio');
58 $pcm = fopen('/dev/dsp2', 'rb') or die('Cannot read from audio');
60 $empty = str_repeat(chr(128), $GLOBALS['options']['buffer_size']);
64 $data = trim(fread($pcm, $GLOBALS['options']['buffer_size']));
65 if ((empty($data)) || ($data == $empty)) {
66 if ($GLOBALS['options']['analyze_input']) {
67 //echo 'EMPTY!' . PHP_EOL;
73 $data = analyzeForNoiseOnly($data);
81 $GLOBALS['analysis']['average'] = floor($GLOBALS['analysis']['average'] / strlen($data));
83 if ($GLOBALS['options']['analyze_input']) {
85 for ($i = 0; $i < strlen($data); $i++) {
86 $ord = ord(substr($data, $i, 1));
87 $simpleAnalysis .= str_pad($ord, 3, '0', STR_PAD_LEFT) . '=0x' . str_pad(dechex($ord), 2, '0', STR_PAD_LEFT) . ',';
89 $simpleAnalysis = substr($simpleAnalysis, 0, -1) . ' (' . count($GLOBALS['analysis']['breakdown']) . ')' . PHP_EOL;
92 } elseif ($GLOBALS['options']['reduce_noise']) {
94 for ($i = 0; $i < strlen($data); $i++) {
95 $ord = ord(substr($data, $i, 1));
96 // @TODO What do here to remove noise?
97 $ord -= $GLOBALS['analysis']['average'];
101 $out = analyzeForNoiseOnly($out);
114 if ($GLOBALS['options']['analyze_input']) {
115 echo 'EOF!' . PHP_EOL;