我使用此功能在php中生成wav摇摆音:
$samples = array();
$amplitude = 8192;
$sampleRate = 8000;
$samplesCount = 300000;
for ($n = 0; $n < $samplesCount; $n++) {
$freqOfTone= 4*sin($n*0.00628)+440;
$w = 2 * pi() * $freqOfTone / $sampleRate;
$samples[$n] = (int)($amplitude * sin($n * $w));
}
但是生成的声音并不是一个持续不断的晃动而是上升......(你可以在那里听到:Sound Link
有谁知道为什么?
谢谢
答案 0 :(得分:1)
并没有以同样的方式解决问题,但我花了一些时间与互联网上奇怪的数学角落一起玩,以帮助消除三角关闭。 This帮助我了解它。
无论如何,这就是我正在玩的东西(无论是放大器还是fm mod,我都可以使用它几个小时):
<?php
$bps = 16; // bits/s
$Bps = $bps / 8; // byte/s
$duration = 30; // Seconds
$frequency = 440; // Hz
$amplitude = 8192;
$samples = array();
$sampleRate = 44100;
$samplesCount = $sampleRate * $duration;
for ($n = 0; $n <= $samplesCount; $n++)
{
// Time
$t = $n / $sampleRate;
// Freq Mod
$mod = 0.1 * sin(2 * M_PI * $frequency * $t * 0.00628);
// Wave
$w = $amplitude * sin(2 * M_PI * $frequency * ($t + $mod));
// Amplitude Mod
($n / ($sampleRate / 10) % 2 == 0) ? $amplitude-- : $amplitude++;
$samples[] = (int)$w;
}
$str = call_user_func_array("pack",
array_merge(array("VVVVVvvVVvvVVv*"),
array(// header
0x46464952, // RIFF
($samplesCount * 2) + 44 - 8, // Filesize
0x45564157, // WAVE
0x20746d66, // "fmt " (chunk)
16, // chunk size
1, // compression (2 byte iteger)
1, // nchannels
$sampleRate, // sample rate
$sampleRate * $Bps, //bytes/second
$Bps, //block align
$bps, //bits/sample
0x61746164, //"data"
$samplesCount * 2 // samples * compressed int
),
$samples //data
)
);
// echo print_r($samples) . PHP_EOL;
$myfile = fopen("/tmp/sine.wav", "wb") or die("Unable to open file!");
fwrite($myfile, $str);
fclose($myfile);