本文介绍了更快的替代windows.h的Beep()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事个人项目。



我的第一个草稿应用程序如下:



我用4个信号构造一个字节:




  • 5000hz表示00

    >
  • 6khz表示01


  • 7khz表示10 b

  • 8khz表示11


  • 9khz表示与上一个相同

    / ul>

    然后我将这些4对位合并在一起,并再次与下一个开始。



    解调的工作速度很快,应该足够快,但我的声音发生问题...它很慢...



    这是我的调试代码:

      #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;
    }

    我期待5声哔哔声,靠近在一起,100ms每个, get(在上面,五100ms哔声(),和底部,五20ms哔哔声):



    正如你可以看到的,我得到的是50ms蜂鸣声后75ms暂停,当我想要100ms蜂鸣声和10ms蜂鸣声后100ms暂停,当我想要20ms蜂鸣声。



    有比Windows的Beep()更快,更准确的东西(与linux一起工作会更好,因为最终应用程序应该在树莓派上工作)



    我会获得更高的可用带宽,有3ms的声音(.... 41字节/秒....对我的应用程序是足够的)



    编译器:g ++(mingw)



    Os:seven 64bits

    解决方案

    一种方法(因为你想要定位到Linux,所以是合适的)将是生成一个WAV文件, 。 (在Windows和Linux上,有 WAV文件的简单方法。)



    您可以使用库来生成WAV文件,或者自己创建,两种方法都很简单。有很多示例



    如果你自己这样做:

      / * WAV头,44字节* / 
    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;
    };

    初始化:

      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;
    }


    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

    解决方案

    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;
    }
    

    这篇关于更快的替代windows.h的Beep()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 19:58