这是一个让你入门的例子.
// filename "wf.cpp" (simple wave-form generator)
#include <iostream>
#include <cmath>
#include <stdint.h>
int main()
{
const double R=8000; // sample rate (samples per second)
const double C=261.625565; // frequency of middle-C (hertz)
const double F=R/256; // bytebeat frequency of 1*t due to 8-bit truncation (hertz)
const double V=127; // a volume constant
for ( int t=0; ; t++ )
{
uint8_t temp = (sin(t*2*M_PI/R*C)+1)*V; // pure middle C sine wave
// uint8_t temp = t/F*C; // middle C saw wave (bytebeat style)
// uint8_t temp = (t*5&t>>7)|(t*3&t>>10); // viznut bytebeat composition
std::cout<<temp;
}
}
Run Code Online (Sandbox Code Playgroud)
通过ALSA接口在Linux上编译和运行:
make wf && ./wf |aplay
Run Code Online (Sandbox Code Playgroud)
通过GStreamer接口在Linux上编译和运行:
make wf && ./wf |gst-launch-0.10 -v filesrc location=/dev/stdin ! 'audio/x-raw-int,rate=8000,channels=1,depth=8' ! autoaudiosink
Run Code Online (Sandbox Code Playgroud)
GStreamer声称是跨平台的.感兴趣的主要特征是您可以创建(或使用现有的)插件来构建音频过滤器管道.
在某些Unix(ish)系统中,您可以将音频数据写入/dev/audio(或/dev/dsp)并播放.在使用ALSA的现代Linux系统上,您可能需要将其管道化aplay.但在任何一种情况下,您都不需要使用任何特定的声音库 - 只需打开输出流并写入即可.这就是所有那些bytebeat one-liners的做法.