此函数将使用浮点值填充缓冲区,这些浮点值表示给定频率的原始音频曲线的高度
var pop_audio_buffer_custom = function (number_of_samples, given_freq, samples_per_second) {
var number_of_samples = Math.round(number_of_samples);
var audio_obj = {};
var source_buffer = new Float32Array(number_of_samples);
audio_obj.buffer = source_buffer;
var incr_theta = (2.0 * Math.PI * given_freq) / samples_per_second;
var theta = 0.0;
for (var curr_sample = 0; curr_sample < number_of_samples; curr_sample++) {
audio_obj.buffer[curr_sample] = Math.sin(theta);
console.log(audio_obj.buffer[curr_sample] , "theta ", theta);
theta += incr_theta;
}
return audio_obj;
}; // pop_audio_buffer_custom
var number_of_samples = 10000; // long enough to be audible
var given_freq = 300;
var samples_per_second = 44100; // CD quality sample rate
var wav_output_filename = "/tmp/wav_output_filename.wav"
var synthesized_obj = {};
synthesized_obj.buffer = pop_audio_buffer_custom(number_of_samples, given_freq, samples_per_second);
数字音频的世界并非微不足道...一旦有了音频缓冲区,下一步就是将浮点表示转换为可以以字节存储的内容(通常为 16 位整数,具体取决于您选择的位深度) ... 然后需要将 16 位整数缓冲区写为 WAV 文件
音频是一种有时被称为时间序列的波...当您将拳头捶打在桌子上时,桌子会上下摆动,从而推动微小的空气分子与摆动一致...这种空气的摆动会在房间内传播并到达麦克风膜片或者你的耳膜,它会随着波的共振而摆动……如果你把铅笔粘在膜片上,它会随着膜片一起摇晃,然后你慢慢地沿着铅尖滑动一条纸铅笔你会看到一条曲线被写在纸条上......这是音频曲线......音频样本只是该曲线在某一时刻的高度......如果你反复写下这个曲线高度值以恒定速率每秒 X 次,您将获得原始音频的数据点列表(这是上面的函数创建的)......所以给定的音频样本只是给定时刻的音频曲线高度的值...因为计算机不是连续的,而是磁盘rete 他们无法处理整个铅笔绘制的曲线,所以只关心这个瞬时测量的曲线高度值列表......那些是音频样本
32 位以上的浮点缓冲区可以输入以下函数以返回 16 位整数缓冲区
var convert_32_bit_float_into_signed_16_bit_int_lossy = function(input_32_bit_buffer) {
// this method is LOSSY - intended as preliminary step when saving audio into WAV format files
// output is a byte array where the 16 bit output format
// is spread across two bytes in little endian ordering
var size_source_buffer = input_32_bit_buffer.length;
var buffer_byte_array = new Int16Array(size_source_buffer * 2); // Int8Array 8-bit twos complement signed integer
var value_16_bit_signed_int;
var index_byte = 0;
console.log("size_source_buffer", size_source_buffer);
for (var index = 0; index < size_source_buffer; index++) {
value_16_bit_signed_int = ~~((0 < input_32_bit_buffer[index]) ? input_32_bit_buffer[index] * 0x7FFF :
input_32_bit_buffer[index] * 0x8000);
buffer_byte_array[index_byte] = value_16_bit_signed_int & 0xFF; // bitwise AND operation to pluck out only the least significant byte
var byte_two_of_two = (value_16_bit_signed_int >> 8); // bit shift down to access the most significant byte
buffer_byte_array[index_byte + 1] = byte_two_of_two;
index_byte += 2;
};
// ---
return buffer_byte_array;
};
下一步是将 16 位以上的 int 缓冲区保存到 wav 文件中...我建议您为此使用众多 nodejs 库之一(或者甚至更好地编写自己的库,因为它只有两页代码;-)) )