2017-06-29 75 views
0

我在stackoverflow上發現了以下不合適的代碼。請有人可以幫我添加第二個通道(立體聲)生成WAV並添加第二個(立體聲)頻道

會有nchannels = 2 文件大小必須(以某種方式)增加,並且必須添加2.數組(即樣本2)。

當我嘗試我只能達到更高的頻率。很多來自德國

感謝:馬丁

$freqOfTone = 440; 
$sampleRate = 44100; 
$samplesCount = 80000; 

$amplitude = 0.25 * 32768; 
$w = 2 * pi() * $freqOfTone/$sampleRate; 

$samples = array(); 
for ($n = 0; $n < $samplesCount; $n++) { 
$samples[] = (int)($amplitude * sin($n * $w)); 
} 

$srate = 44100; //sample rate 
$bps = 16; //bits per sample 
$Bps = $bps/8; //bytes per sample /// I EDITED 

$str = call_user_func_array("pack", 
array_merge(array("VVVVVvvVVvvVVv*"), 
    array(//header 
     0x46464952, //RIFF 
     160038,  //File size 
     0x45564157, //WAVE 
     0x20746d66, //"fmt " (chunk) 
     16, //chunk size 
     1, //compression 
     1, //nchannels 
     $srate, //sample rate 
     $Bps*$srate, //bytes/second 
     $Bps, //block align 
     $bps, //bits/sample 
     0x61746164, //"data" 
     160000 //chunk size 
    ), 
    $samples //data 
) 
); 
$myfile = fopen("sine.wav", "wb") or die("Unable to open file!"); 
fwrite($myfile, $str); 
+0

不要擔心:D需要時間,我記得自己開始。請享用! – Bugs

回答

0
從問題

感動的解決方案來回答:

SELF SOLUTION:我查了一下recorder.js Web工作,發現了SPEC以及如何使用它。這裏有一個稍微修改的代碼,用於在1秒內從右側揚聲器發向左側的音調。也許它有助於某人。

$freqOfTone = 440; 
$sampleRate = 44100; 
$samplesCount = 80000; 

$amplitude = 0.25 * 32768; 
$w = 2 * pi() * $freqOfTone/$sampleRate; 

$samples = array(); 
$samples2 = array(); 
for ($n = 0; $n < $samplesCount; $n++) { 
    $samples[] = (int)($amplitude*($n/$samplesCount) * sin($n * $w)); 
    $samples2[] = (int)($amplitude*(1-$n/$samplesCount) * sin($n * $w)); 
} 

function interleave($l,$r){ 
$data=array(); 
$ix=0; 
for($i=0;$i<count($l);$i++){ 
    $data[$ix++]=$l[$i]; 
    $data[$ix++]=$r[$i]; 
} 
return $data; 
} 

$stereo=interleave($samples,$samples2); 


echo(count($samples)); 
echo(count($stereo)); 

$srate = 44100; //sample rate 
$bps = 16; //bits per sample 
$Bps = $bps/8; //bytes per sample /// I EDITED 


$samples_merge=array_merge($samples,$samples2); 

$str = call_user_func_array("pack", 
    array_merge(array("VVVVVvvVVvvVVv*"), 
     array(//header 
      0x46464952, //RIFF 
      80000*2*2+36,  //File size *2*Channels +36 
     0x45564157, //WAVE 
     0x20746d66, //"fmt " (chunk) 
     16, //chunk size 
     1, //compression 
     2, //nchannels 
     $srate, //sample rate 
     $Bps*$srate, //bytes/second 
     $Bps, //block align 
     $bps, //bits/sample 
     0x61746164, //"data" 
     80000 * 2 *2 //chunk size (filesize * 2 *Channels) 
    ), 
    $stereo // Stereo tone left[0], right[0], left[1], right[1], ... 
) 
); 
$myfile = fopen("sine.wav", "wb") or die("Unable to open file!"); 
fwrite($myfile, $str); 
fclose($myfile); 
相關問題