從CharGPT那邊得到的另一種C++播放bytebeat的代碼
// 聲明必要的頭文件?
#include <iostream>
#include <cstdlib>?
#include <cmath>
#include <algorithm>?
#include <fstream>
#include <string>
#include <Windows.h>?
using namespace std;?
// 定義緩沖區(qū)的大小
const int buffer_size = 2048;?
// 定義BYTEBEAT函數(shù)?
int bytebeat(int t) { ? ?
// 這里可以定義一些BYTEBEAT的公式,然后返回一個處理后的聲音 ??
?int sound = (t * 7 + t * (t >> 5) + t * (t >> 11)) & 0xff; ? ?
return sound; }
int main() { ? ?
// 創(chuàng)建一個緩沖區(qū)?
? short *buffer = new short[buffer_size]; ??
?// 將BYTEBEAT函數(shù)的結(jié)果放到緩沖區(qū)中
? for (int i = 0; i < buffer_size; i++)? ? ?{? ? ? ? ??
buffer[i] = bytebeat(i); ? ?
} ? ?
// 將緩沖區(qū)中的數(shù)據(jù)保存到文件 ? ?
ofstream out("bytebeat.dat", ios::binary); ? ?
out.write((char*)buffer, buffer_size * sizeof(short)); ??
out.close(); ??
?// 將緩沖區(qū)中的數(shù)據(jù)播放出來 ??
?PlaySound("bytebeat.dat", NULL, SND_FILENAME | SND_ASYNC); ? ?delete[] buffer; ? ?return 0; }