0

How to generate audible tones in Qt with different frequency. For ex, in the following code, I use the same frequency beep in the three conditions.

But, I need three different sounds to indicate three conditions

   if(a_vertical> LevelOne )
    {  
        QApplication::beep(); 
    }
 else if(a_vertical> LevelTwo )
    {  
        QApplication::beep();
    }
 else
    {  
        QApplication::beep();
    }

1 Answers1

0

You cannot do it directly in Qt. If you are on Windows, you can use window.h package to do that Beep(frequency, milliseconds)

However, in Qt, there are several other ways in which you can manipulate the sound. Answered here

#define FREQ_CONST ((2.0 * M_PI) / SAMPLE_RATE)

QByteArray* bytebuf = new QByteArray();
buf->resize(seconds * SAMPLE_RATE);

for (int i=0; i<(seconds * SAMPLE_RATE); i++) {
    qreal t = (qreal)(freq * i);
    t = t * FREQ_CONST;
    t = qSin(t);
    // now we normalize t
    t *= TG_MAX_VAL;
    (*bytebuf)[i] = (quint8)t;
}

Then we can take that buffer and do something like this to play it:

// Make a QBuffer from our QByteArray
QBuffer* input = new QBuffer(bytebuf);
input->open(QIODevice::ReadOnly);

// Create an output with our premade QAudioFormat (See example in QAudioOutput)
QAudioOutput* audio = new QAudioOutput(format, this);
audio->start(input);
Chilarai
  • 1,842
  • 2
  • 15
  • 33