Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以从PHP中的十六进制/二进制(原始数据)值生成音频文件?_Php_File_Audio_Binary_Hex - Fatal编程技术网

是否可以从PHP中的十六进制/二进制(原始数据)值生成音频文件?

是否可以从PHP中的十六进制/二进制(原始数据)值生成音频文件?,php,file,audio,binary,hex,Php,File,Audio,Binary,Hex,我想编写一个小型web应用程序,将十六进制数据转换为音频文件 音频文件的数据将采用十六进制格式,如下所示: DA1FFFF8B3AEEE2E23BBB9A2221F10400180001EF1C1E7F180F0FC0004FE067C03002FF92B924903450 E0C8D558F2475AF7232A28DF76493A54CE348B37D33F8616EE30A69EF56534D53320656E6320 如何使用PHP实现这一点?任何数据都可以转换为音频。。。如果你以每秒

我想编写一个小型web应用程序,将十六进制数据转换为音频文件

音频文件的数据将采用十六进制格式,如下所示:

DA1FFFF8B3AEEE2E23BBB9A2221F10400180001EF1C1E7F180F0FC0004FE067C03002FF92B924903450
E0C8D558F2475AF7232A28DF76493A54CE348B37D33F8616EE30A69EF56534D53320656E6320

如何使用PHP实现这一点?

任何数据都可以转换为音频。。。如果你以每秒500个周期合成一个正弦波,然后在你的十六进制数据上迭代,并用它来调制音频,你将听到由十六进制驱动的模式。。。当前十六进制值可能会改变sin循环正在合成的频率

因此,第一步可能是创建一个循环,在其中合成原始音频波。。。让这项工作呈现音频,然后使用循环迭代器索引遍历十六进制数据数组,并使当前十六进制值移动到合成正弦波时使用的参数

下面是一些伪代码,它将以合理的采样率和位深度填充原始音频的内存缓冲区,任何播放器都可以将其渲染为音频。。。这是我用OpenAL做音频渲染的概念证明的一部分

hex_buffer gets defined and populated above

float freq = 100.f;
float incr_freq = 0.1f;

int seconds = 4;
unsigned sample_rate = 44100;
double my_pi = 3.14159;
size_t buf_size = seconds * sample_rate; // define buffer size

// allocate PCM audio buffer        
short * samples = malloc(sizeof(short) * buf_size);

printf("\nhere is freq %f\n", freq);
int i=0;
int index_hex = 0;
for(; i<buf_size; ++i) {

    samples[i] = 32760 * sin( (2.f * my_pi * freq)/sample_rate * i );

    /*
    freq += incr_freq; // comment out to synthesize pure sin wav

    if (100.0 > freq || freq > 5000.0) {

        incr_freq *= -1.0f;
    }
    */

    freq += convert_hex_to_float(hex_buffer(index_hex++))
}
“是否可以使用PHP进行此操作?”-
int hex2int(char ch) {

    if (ch >= '0' && ch <= '9')
        return ch - '0';
    if (ch >= 'A' && ch <= 'F')
        return ch - 'A' + 10;
    if (ch >= 'a' && ch <= 'f')
        return ch - 'a' + 10;
    return -1;
}
func convert_hex_to_float(given_hex) { // return float

    left_hex_char = pluck_out_first_hex_char(given_hex)
    right_hex_char = pluck_out_second_hex_char(given_hex)

    return (16.0 * hex2int(left_hex_char) + 1.0 * hex2int(right_hex_char))
}