在 PHP 中生成 wav 音调
Generate wav tone in PHP
我想在 php 中生成正弦音调。但是构建我的 wav 我需要以字节为单位给出值。我不知道该怎么做:
这是我的代码:
$freqOfTone = 440;
$sampleRate = 44100;
$samplesCount = 80000;
$amplitude = 0.25 * 32768;
$w = 2 * pi() * $freqOfTone / $sampleRate;
//$dataArray = new
$text = "RIFF"
."80036"
."WAVE"
."fmt "
."16"
."1"
."1"
."44100"
."44100"
."1"
."8"
."data"
."80000";
for ($n = 0; $n < $samplesCount; $n++)
{
$text .= (int)($amplitude * sin($n * $w));
}
$myfile = fopen("sine.wav", "w") or die("Unable to open file!");
fwrite($myfile, $text);
fclose($myfile);
问题是您的算法将数字写为文本。而 .wav
文件对数据进行二进制编码。
例如,您可以使用 pack
对数据进行分组。
$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);
fclose($myfile);
这会产生这个 file。
请注意,您不能只重复使用上面的 header。有些方面是硬编码的不同(如文件的大小、通道数、比特率等)。但是,如果阅读 documentation,则可以轻松地相应地修改 header。
我想在 php 中生成正弦音调。但是构建我的 wav 我需要以字节为单位给出值。我不知道该怎么做:
这是我的代码:
$freqOfTone = 440;
$sampleRate = 44100;
$samplesCount = 80000;
$amplitude = 0.25 * 32768;
$w = 2 * pi() * $freqOfTone / $sampleRate;
//$dataArray = new
$text = "RIFF"
."80036"
."WAVE"
."fmt "
."16"
."1"
."1"
."44100"
."44100"
."1"
."8"
."data"
."80000";
for ($n = 0; $n < $samplesCount; $n++)
{
$text .= (int)($amplitude * sin($n * $w));
}
$myfile = fopen("sine.wav", "w") or die("Unable to open file!");
fwrite($myfile, $text);
fclose($myfile);
问题是您的算法将数字写为文本。而 .wav
文件对数据进行二进制编码。
例如,您可以使用 pack
对数据进行分组。
$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);
fclose($myfile);
这会产生这个 file。
请注意,您不能只重复使用上面的 header。有些方面是硬编码的不同(如文件的大小、通道数、比特率等)。但是,如果阅读 documentation,则可以轻松地相应地修改 header。