Faster alternative to windows.h Beep ()

I am working on a personal project. I want to transmit some data over the air using an old ham radio.

My first draft application works like this:

I will build one byte with 4 "signals":

  • 5000hz means "00"

  • 6khz means "01"

  • 7khz means "10"

  • 8khz means "11"

  • 9khz means the same as the previous one

then I will combine these 4 pairs of bits together and start again with the following.

Demodulation works fine and should be fast enough, but I had a problem with sound generation ... it's slow ...

Here is 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 expect 5 beeps, close to each other, 100 ms each, but here's what I get (on top, five "100 ms sounds" (), and below - five "20 ms pins" (): a

As you can see, what I get is 50 ms, after a pause of 75 ms, when I want a sound signal of 100 ms, and 10 ms, after which a pause of 100 ms when I want to receive a signal for 20 ms.

Is there anything faster and more accurate than Beep () for Windows? (something that works with linux would be even better, because the last application should work on raspberry pi)

I would get a higher used bandwidth with 3 ms sounds (.... 41 bytes / sec .... which is more than enough for my application)

Compiler: g ++ (mingw)

Os: seven 64 bits

+7
source share
1 answer

One way (which would be appropriate since you would like to target Linux too) is to create a WAV file and then play it. (There are simple ways to play WAV files on both Windows and Linux.)

You can use the library to create a WAV file or create it yourself, both approaches are quite simple. There are many examples on the Internet.

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; } 
+2
source

All Articles