--- /dev/null
+<?php
+$GLOBALS['options'] = array(
+ 'analyze_input' => FALSE,
+ 'reduce_noise' => FALSE,
+ 'ignore_noise' => FALSE,
+ 'keep_noise' => FALSE,
+ 'buffer_size' => 8,
+);
+
+if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'a') {
+ $GLOBALS['options']['analyze_input'] = TRUE;
+} elseif (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'r') {
+ $GLOBALS['options']['reduce_noise'] = TRUE;
+} elseif (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'k') {
+ $GLOBALS['options']['keep_noise'] = TRUE;
+}
+
+if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'i') {
+ $GLOBALS['options']['ignore_noise'] = TRUE;
+} elseif (isset($_SERVER['argv'][2]) && $_SERVER['argv'][2] == 'i') {
+ $GLOBALS['options']['ignore_noise'] = TRUE;
+} elseif (isset($_SERVER['argv'][2]) && $_SERVER['argv'][2] == 'k') {
+ $GLOBALS['options']['keep_noise'] = TRUE;
+}
+
+function analyzeForNoiseOnly ($data) {
+ $GLOBALS['analysis']['breakdown'] = array();
+ $GLOBALS['analysis']['average'] = 0;
+
+ for ($i = 0; $i < strlen($data); $i++) {
+ $ord = ord(substr($data, $i, 1));
+ $GLOBALS['analysis']['breakdown'][$ord] = TRUE;
+ $GLOBALS['analysis']['average'] += $ord;
+ }
+
+ if (!$GLOBALS['options']['keep_noise'] && !$GLOBALS['options']['ignore_noise'] && count($GLOBALS['analysis']['breakdown']) == 2 && isset($GLOBALS['analysis']['breakdown'][127]) && isset($GLOBALS['analysis']['breakdown'][128])) {
+ if ($GLOBALS['options']['analyze_input']) {
+ //echo 'NOISE1!' . PHP_EOL;
+ }
+ return NULL;
+ } elseif (!$GLOBALS['options']['keep_noise'] && !$GLOBALS['options']['ignore_noise'] && count($GLOBALS['analysis']['breakdown']) == 1 && isset($GLOBALS['analysis']['breakdown'][127])) {
+ if ($GLOBALS['options']['analyze_input']) {
+ //echo 'NOISE2!' . PHP_EOL;
+ }
+ return NULL;
+ } elseif (!$GLOBALS['options']['keep_noise'] && $GLOBALS['options']['ignore_noise'] && count($GLOBALS['analysis']['breakdown']) < 2) {
+ if ($GLOBALS['options']['analyze_input']) {
+ //echo 'NOISE3!' . PHP_EOL;
+ }
+ return NULL;
+ }
+
+ // Return given data
+ return $data;
+}
+
+//$pcm = fopen('output1.pcm', 'rb') or die('Cannot read from audio');
+$pcm = fopen('/dev/dsp2', 'rb') or die('Cannot read from audio');
+
+$empty = str_repeat(chr(128), $GLOBALS['options']['buffer_size']);
+
+while (TRUE) {
+ //usleep(100);
+ $data = trim(fread($pcm, $GLOBALS['options']['buffer_size']));
+ if ((empty($data)) || ($data == $empty)) {
+ if ($GLOBALS['options']['analyze_input']) {
+ //echo 'EMPTY!' . PHP_EOL;
+ }
+ continue;
+ }
+
+ // Detect noise
+ $data = analyzeForNoiseOnly($data);
+
+ // Empty?
+ if (empty($data)) {
+ // Skip this
+ continue;
+ } // END - if
+
+ $GLOBALS['analysis']['average'] = floor($GLOBALS['analysis']['average'] / strlen($data));
+
+ if ($GLOBALS['options']['analyze_input']) {
+ $simpleAnalysis = '';
+ for ($i = 0; $i < strlen($data); $i++) {
+ $ord = ord(substr($data, $i, 1));
+ $simpleAnalysis .= str_pad($ord, 3, '0', STR_PAD_LEFT) . '=0x' . str_pad(dechex($ord), 2, '0', STR_PAD_LEFT) . ',';
+ }
+ $simpleAnalysis = substr($simpleAnalysis, 0, -1) . ' (' . count($GLOBALS['analysis']['breakdown']) . ')' . PHP_EOL;
+
+ echo $simpleAnalysis;
+ } elseif ($GLOBALS['options']['reduce_noise']) {
+ $out = '';
+ for ($i = 0; $i < strlen($data); $i++) {
+ $ord = ord(substr($data, $i, 1));
+ // @TODO What do here to remove noise?
+ $ord -= $GLOBALS['analysis']['average'];
+ $out .= chr($ord);
+ }
+
+ $out = analyzeForNoiseOnly($out);
+
+ if (empty($out)) {
+ // Skip this
+ continue;
+ } // END - if
+
+ echo $out;
+ } else {
+ echo $data;
+ }
+
+ if (feof($pcm)) {
+ if ($GLOBALS['options']['analyze_input']) {
+ echo 'EOF!' . PHP_EOL;
+ }
+ break;
+ }
+}
+
+fclose($pcm);