0

I made a program that's working fine in Turbo C++. It makes a kind of squeaking sound that works because of changing frequencies played very quickly

#include<dos.>
void main()
{
    for( int i=500 ; i<=2000 ; i++ )
    {
        sound( i ) ;
    }
    nosound() ;
}

How do I make this in Code Blocks? I tried using Beep() function but it's not working. Here is the code:

#include<windows.h>
int main()
{
    for( int i=500 ; i<=2000 ; i++ )
    {
        Beep( i, 1 ) ;
    }
    return 0 ;
}
Ojuswi Rastogi
  • 119
  • 1
  • 3
  • Playing audio with C++ is going to rrquire an API. For Windows you could try WASAPI, port audio or OpenAL. It is sadly not a trivial task. [Here is a simple port audio example to illustrate](http://portaudio.com/docs/v19-doxydocs/paex__sine_8c_source.html) – fdcpp Jan 25 '20 at 14:19
  • on windows is WAVEIN/WAVEOUT your best option its not that hard to use and has low latency ... You feed audio buffer to it so you need to "render" you rsound into array and send it to your WAVEOUT continuously see [Find start point (time) of each cycle in a sine wave](https://stackoverflow.com/a/37583214/2521214) on how to generate it. In MSDOS you can use Speaker IO port directly or use PIT – Spektre Jan 29 '20 at 09:09

1 Answers1

0

The second parameter of Beep is the duration of the sound, in milliseconds, Try setting it up a bit larger such as Beep(i,100) to make the sound more clear.

Drake Wu
  • 6,927
  • 1
  • 7
  • 30
  • it takes some time to start the sound and to end it, which creates small breaks in the sound and also slows down the execution. It not as smooth as in 1st case. – Ojuswi Rastogi Jan 27 '20 at 15:26
  • 1
    You could use [`waveOutWrite`](https://learn.microsoft.com/en-us/windows/win32/api/mmeapi/nf-mmeapi-waveoutwrite), try to write the data and send this data block to the given waveform-audio output device. Here is an [sample](https://www.codeproject.com/Forums/1647/C-Cplusplus-MFC.aspx?df=90&mpp=25&sort=Position&spc=Relaxed&select=3456645&tid=3456645) you can refer to. – Drake Wu Jan 29 '20 at 09:27