За последние 24 часа нас посетили 17778 программистов и 1662 робота. Сейчас ищут 828 программистов ...

Помогите убрать шум и background из волни wav файла.

Тема в разделе "Прочие вопросы по PHP", создана пользователем Sarvar, 26 янв 2014.

  1. Sarvar

    Sarvar Новичок

    С нами с:
    26 янв 2014
    Сообщения:
    1
    Симпатии:
    0
    Я скачал исходник который нарисует гистограмму вав файла. Помогите убрать шум и задный фон. Вот исходник:

    class.wav.php:
    Код (Text):
    1. <?php
    2.  
    3. / $File: class.wave.php $Date: 09-02-08 /
    4.  
    5. class wave {
    6.  
    7. var $fp, $filesize;
    8. var $data, $blocktotal, $blockfmt, $blocksize;
    9.  
    10. function __construct($file) {
    11.  
    12.     if(!$this->fp = @fopen($file, 'rb')) {
    13.         return false;
    14.     }
    15.  
    16.     $this->filesize = filesize($file);
    17.  
    18. }
    19.  
    20. function wavechunk() {
    21.  
    22.     rewind($this->fp);
    23.  
    24.     $riff_fmt = 'a4ID/VSize/a4Type';
    25.     $riff_cnk = @unpack($riff_fmt, fread($this->fp, 12));
    26.  
    27.     if($riff_cnk['ID'] != 'RIFF' || $riff_cnk['Type'] != 'WAVE') {
    28.         return -1;
    29.     }
    30.  
    31.     $format_header_fmt = 'a4ID/VSize';
    32.     $format_header_cnk = @unpack($format_header_fmt, fread($this->fp, 8));
    33.  
    34.     if($format_header_cnk['ID'] != 'fmt ' || !in_array($format_header_cnk['Size'], array(16, 18))) {
    35.         return -2;
    36.     }
    37.  
    38.     $format_fmt = 'vFormatTag/vChannels/VSamplesPerSec/VAvgBytesPerSec/vBlockAlign/vBitsPerSample'.($format_header_cnk['Size'] == 18 ? '/vExtra' : '');
    39.     $format_cnk = @unpack($format_fmt, fread($this->fp, $format_header_cnk['Size']));
    40.  
    41.     if($format_cnk['FormatTag'] != 1) {
    42.         return -3;
    43.     }
    44.  
    45.     if(!in_array($format_cnk['Channels'], array(1, 2))) {
    46.         return -4;
    47.     }
    48.  
    49.     $fact_fmt = 'a4ID/VSize/Vdata';
    50.     $fact_cnk = @unpack($fact_fmt, fread($this->fp, 12));
    51.  
    52.     if($fact_cnk['ID'] != 'fact') {
    53.         fseek($this->fp, ftell($this->fp) - 12);
    54.     }
    55.  
    56.     $data_fmt = 'a4ID/VSize';
    57.     $data_cnk = @unpack($data_fmt, fread($this->fp, 8));
    58.  
    59.     if($data_cnk['ID'] != 'data') {
    60.         return -5;
    61.     }
    62.  
    63.     if($data_cnk['Size'] % $format_cnk['BlockAlign'] != 0) {
    64.         return -6;
    65.     }
    66.  
    67.     $this->data = fread($this->fp, $data_cnk['Size']);
    68.     $this->blockfmt = $format_cnk['Channels'] == 1 ? 'sLeft' : 'sLeft/sRight';
    69.  
    70.     $this->blocktotal = $data_cnk['Size'] / 4;
    71.     $this->blocksize = $format_cnk['BlockAlign'];
    72.  
    73.     $return = array
    74.         (
    75.         'Channels' => $format_cnk['Channels'],
    76.         'SamplesPerSec' => $format_cnk['SamplesPerSec'],
    77.         'AvgBytesPerSec' => $format_cnk['AvgBytesPerSec'],
    78.         'BlockAlign' => $format_cnk['BlockAlign'],
    79.         'BitsPerSample' => $format_cnk['BitsPerSample'],
    80.         'Extra' => $format_cnk['Extra'],
    81.         'seconds' => ($data_cnk['Size'] / $format_cnk['AvgBytesPerSec'])
    82.         );
    83.  
    84.     return $return;
    85.  
    86. }
    87.  
    88. function waveimage($channel = 'Left', $width = 1000, $height = 300, $bgcolor = array(255, 255, 255), $cenlinecolor = array(180, 180, 180), $imgcolor = array(0, 0, 0)) {
    89.  
    90.     if(!$this->data) {
    91.         if(!is_array($this->wavechunk())) {
    92.             return false;
    93.         }
    94.     }
    95.  
    96.     $width = max(10, $width);
    97.     $height = max(10, $height);
    98.  
    99.     $border = 1;
    100.     $center = ($height - $border * 2) / 2;
    101.  
    102.     $num_merge = max(1, round($this->blocktotal / $width));
    103.     $width = min($width, ceil($this->blocktotal / $num_merge));
    104.  
    105.     $max = $min = 0;
    106.     $x = 1;
    107.  
    108.     $pixels = array();
    109.     $wavetotal = 0;
    110.  
    111.     for($i = 0; $i < $this->blocktotal; $i++) {
    112.         $blocks[] = @unpack($this->blockfmt, substr($this->data, $i * 4, $this->blocksize));
    113.     }
    114.  
    115.     if(!isset($blocks[0][$channel])) {
    116.         return false;
    117.     }
    118.  
    119.     for($i = 0; $i < $this->blocktotal; ) {
    120.  
    121.         for($j = 0; $j < $num_merge; $j++) {
    122.             if(isset($blocks[$i + $j])) {
    123.                 $wavetotal += $blocks[$i + $j][$channel];
    124.             }
    125.         }
    126.  
    127.         $y = round($wavetotal / ($j + 1));
    128.         $pixels[] = array($x, $y);
    129.  
    130.         $max = max($max, $y);
    131.         $min = min($min, $y);
    132.  
    133.         $x++;
    134.         $i += $num_merge;
    135.         $wavetotal = 0;
    136.  
    137.     }
    138.  
    139.     $maxblock = max(abs($max), abs($min));
    140.     $zoom = $maxblock / $center;
    141.  
    142.     $im = imagecreate($width, $height);
    143.     $background = imagecolorallocate($im, $bgcolor[0], $bgcolor[1], $bgcolor[2]);
    144.  
    145.     if($cenlinecolor) {
    146.         $cenline = imagecolorallocate($im, $cenlinecolor[0], $cenlinecolor[1], $cenlinecolor[2]);
    147.         imageline($im, 1, $center, $width, $center, $cenline);
    148.     }
    149.  
    150.     $color = imagecolorallocate($im, $imgcolor[0], $imgcolor[1], $imgcolor[2]);
    151.     $y_old = 0;
    152.  
    153.     foreach($pixels as $pixel) {
    154.  
    155.         $y = $center - round($pixel[1] / $zoom);
    156.         imagesetpixel($im, $pixel[0], $y, $color);
    157.  
    158.         if($pixel[0] > 1) {
    159.             imageline($im, $pixel[0] - 1, $y_old, $pixel[0], $y, $color);
    160.         }
    161.  
    162.         $y_old = $y;
    163.  
    164.     }
    165.  
    166.     return $im;
    167.  
    168. }
    169. }
    170.  
    171. ?>
    example_draw.php:
    Код (Text):
    1.  <?php
    2.  
    3. if(($file = $_FILES['upload']) && $file['name'] != 'none' && $file['size'] && $file['tmp_name']) {
    4.  
    5. include 'class.wave.php';
    6.  
    7. $wave = new wave($file['tmp_name']);
    8. $chunk = $wave->wavechunk();
    9.  
    10. if(!is_array($chunk)) {
    11.     exit('This is not a wav file.');
    12. }
    13.  
    14. if($chunk['seconds'] > 999) {
    15.  
    16.     echo "The wav is too long.\r\n\r\n";
    17.     print_r($chunk);
    18.  
    19. } else {
    20.  
    21.     set_time_limit(999);
    22.  
    23.     if($im = $wave->waveimage()) {
    24.         header('Content-Type: image/png');
    25.         imagepng($im, 'simple.png');
    26.  
    27.         } else {
    28.         echo "wrong!\r\n\r\n";
    29.         print_r($chunk);
    30.     }
    31.  
    32. }
    33. } else {
    34.  
    35. ?>
    36.  
    37. <form action="?" id="upload_form" method="post" enctype="multipart/form-data"> <input type="file" name="upload" /><br /> <button type="submit">Submit Wav file</button> </form>
    38.  
    39. <?php
    40.  
    41. }
    42.  
    43. ?>
     
  2. Fell-x27

    Fell-x27 Суперстар
    Команда форума Модератор

    С нами с:
    25 июл 2013
    Сообщения:
    12.156
    Симпатии:
    1.771
    Адрес:
    :сердА
    Сдается мне, надо поправить файл.