欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php生成指定频率和声音的音频文件(代码教程)

程序员文章站 2022-03-21 08:35:34
找了好久没有找到,php生成音频的代码,自己写了份,代码如下: > 24 & 255; $wav[6] = $headerLen >> 1...

找了好久没有找到,php生成音频的代码,自己写了份,代码如下:

> 24 & 255;
    $wav[6] = $headerLen >> 16 & 255;
    $wav[5] = $headerLen >> 8 & 255;
    $wav[4] = ord(chr($headerLen));

    $wav[8] = 87;
    $wav[9] = 65;
    $wav[10] = 86;
    $wav[11] = 69;

    $wav[12] = 102;
    $wav[13] = 109;
    $wav[14] = 116;
    $wav[15] = 32;

    $wav[16] = 16;
    $wav[17] = 0;
    $wav[18] = 0;
    $wav[19] = 0;

    $wav[20] = 1;
    $wav[21] = 0;

    $wav[22] = M_CHANNELS;
    $wav[23] = 0;

    $wav[24] = ord(chr(M_SAMPLEFREQ));

    $wav[25] = M_SAMPLEFREQ >> 8 & 255;
    $wav[26] = M_SAMPLEFREQ >> 16 & 255;
    $wav[27] = M_SAMPLEFREQ >> 24 & 255;

    $nAvgBytesPerSec = M_SAMPLEFREQ * M_CHANNELS * M_CHANNELBITS / 8;
    $wav[28] = ord(chr($nAvgBytesPerSec));
    $wav[29] = $nAvgBytesPerSec >> 8 & 255;
    $wav[30] = $nAvgBytesPerSec >> 16 & 255;
    $wav[31] = $nAvgBytesPerSec >> 24 & 255;
    $wav[32] = 2;
    $wav[33] = 0;

    $wav[34] = M_CHANNELBITS;

    $wav[35] = 0;

    $wav[36] = 100;
    $wav[37] = 97;
    $wav[38] = 116;
    $wav[39] = 97;

    $dataLen = $totalLen - WAVE_HEAD_LENGTH;

    $wav[43] = $dataLen >> 24 & 255;
    $wav[42] = $dataLen >> 16 & 255;
    $wav[41] = $dataLen >> 8 & 255;
    $wav[40] = ord(chr($dataLen));

    $len = $totalLen - WAVE_HEAD_LENGTH;
    $dLen = intval($len / 10);


    for ($i = 1; $i <= 10; $i++) {
        wavData(M_SAMPLEFREQ, $freq + 100 * $i, $volume, $wav, WAVE_HEAD_LENGTH + $dLen * ($i - 1), $dLen);
    }

    return $wav;
}

function wavData($rate, $freq, $amp, &$p, $pp, $len)
{
    for ($i = 0; $i <= $len - 1; $i += 2) {
        $v = sin(($len - $i) * PI / $rate * $freq) / 180 * ($amp * 32768 + 32768);
        $p[$pp + $i] = $v & 255;
        $p[$pp + $i + 1] = $v >> 8 & 255;
    }
}

$data = makeWav(2000, 89, 5000);
$res = fopen('./test.wav', 'wb');

$bin = pack("c*", ...$data);
fwrite($res, $bin, strlen($bin));
fclose($res);