粉福 flags: https://www.luogu.com.cn/problem/U681911
昵称不代表一切,主要还是看头像()
同意揉脸和摸头,但不要太用力摸 qwq
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#include <vector>
#include <cmath>
#pragma comment (lib, "winmm.lib")
namespace Sounds_Wave {
const int Rate = 44100;
const double PI = 3.14159265358979323846;
const double Volume = 0.2;
HWAVEOUT hwo = NULL;
bool Opened = false;
void openDevice (void) {
if (Opened) return;
WAVEFORMATEX fmt;
ZeroMemory (&fmt, sizeof (fmt));
fmt.wFormatTag = WAVE_FORMAT_PCM;
fmt.nChannels = 1;
fmt.nSamplesPerSec = Rate;
fmt.wBitsPerSample = 16;
fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign;
if (waveOutOpen (&hwo, WAVE_MAPPER, &fmt, 0, 0, CALLBACK_NULL) == MMSYSERR_NOERROR) {
Opened = true;
}
}
void closeDevice (void) {
if (! Opened) return;
waveOutClose (hwo);
hwo = NULL;
Opened = false;
}
void playSin (double Frequency, double time_seconds) {
if (! Opened) openDevice ();
if (! Opened) return;
int n = (int) (Rate * time_seconds);
std :: vector <short> samples (n);
for (int i = 0; i < n; ++ i) {
double t = (double) i / Rate;
double y = std :: sin (2.0 * PI * Frequency * t);
samples[i] = (short) (y * Volume * 32767);
}
WAVEHDR hdr;
ZeroMemory (&hdr, sizeof (hdr));
hdr.lpData = (LPSTR) samples.data ();
hdr.dwBufferLength = (DWORD) (samples.size () * sizeof (short));
waveOutPrepareHeader (hwo, &hdr, sizeof (hdr));
waveOutWrite (hwo, &hdr, sizeof (hdr));
while ((hdr.dwFlags & WHDR_DONE) == 0) {
Sleep (10);
}
waveOutUnprepareHeader (hwo, &hdr, sizeof (hdr));
}
double noteFreq (int midi) {
return 440.0 * std :: pow (2.0, (midi - 69) / 12.0);
}
}
signed main () {
using namespace Sounds_Wave;
int midi;
double seconds;
while (std :: cin >> midi >> seconds) {
playSin (noteFreq (midi), seconds);
}
closeDevice ();
return 0;
}编译器选项:-lwinmm