问题
I'm working on a personal project. I want to transmit some data over the air with an old ham radio.
My first draft application works like that :
I construct one byte with 4 "signals" :
5000hz means "00"
6khz means "01"
7khz means "10"
8khz means "11"
9khz means same as the previous one
then I merge those 4 couple of bits together and start again with the next one.
The demodulation works great and should be fast enough, but I'm having an issue with the sound generation... it's slow...
Here's my debug code :
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
Beep( 5000, 100 );
Beep( 6000, 100 );
Beep( 7000, 100 );
Beep( 8000, 100 );
Beep( 9000, 100 );
return 0;
}
I'm expecting 5 beeps, close together, 100ms each but here's what I get (on top, five "100ms Beeps(), and on the bottom, five "20ms Beeps()" :
As you can see, what I get is 50ms beeps followed 75ms pause when I want 100ms beeps, and 10ms beeps followed by 100ms pause when I want a 20ms beep.
Is there something faster and more accurate than Beep() for Windows ? (something that works with linux as well would be even better because the final application should work on a raspberry pi)
I would get the higher usable bandwidth with 3ms sounds (.... 41 bytes/sec.... which is more than enough for my application)
Compiler : g++ (mingw)
Os : seven 64bits
回答1:
One way (which would be suitable since you want to target Linux too) would be to generate a WAV file, then play that. (There are simple ways to play WAV files on both Windows and Linux.)
You could use a library to generate the WAV file, or create it yourself, both approaches are pretty simple. There are many examples on the web.
If you do it yourself:
/* WAV header, 44 bytes */
struct wav_header {
uint32_t riff packed;
uint32_t len packed;
uint32_t wave packed;
uint32_t fmt packed;
uint32_t flen packed;
uint16_t one packed;
uint16_t chan packed;
uint32_t hz packed;
uint32_t bpsec packed;
uint16_t bpsmp packed;
uint16_t bitpsmp packed;
uint32_t dat packed;
uint32_t dlen packed;
};
Initialize with:
void wav_header(wav_header *p, uint32_t dlen)
{
memcpy(p->riff, "RIFF", 4);
p->len = dlen + 44;
memcpy(p->wave, "WAVE", 4);
memcpy(p->fmt, "fmt ", 4);
p->flen = 0x10;
p->one = 1;
p->chan = 1;
p->hz = 22050;
p->bpsec = hz;
p->bpsmp = 1;
p->bitpsmp = 8;
memcpy(p->dat, "data", 4);
p->dlen = dlen;
}
来源:https://stackoverflow.com/questions/15992253/faster-alternative-to-windows-hs-beep