A Sound Output class

Description:

In the previous article http://www.codeguru.com/multimedia/scope_input_sound_class.shtml
we talked about the SoundIn class which was provided to interface
a sound card for input signal. The idea is now to create a class
for an Output sound .

The Sound Output class which is provided now, to be used as a
generator, is very similar to the Input class one. In fact, it
has been realised in a very short time by replacing all the
"In" string in the CSoundIn class by the
"Out" and by replacing the correct names in the base
functions of the Win32 Sound Api .

The CSoundOut::OpenOutput() is
now the main start procedure. It initialise all the parametres of
the sound card, create an EVENT object and start the sound
thread. The thread procedure is now used to signal to the sound
class, (instead that a buffer is filled for the Input) ,that it
has been transmited to the Sound card and the Win Sound system is
now requiring some new datas for transmiting.

How to use it

  • In the same way that the way we use for the Input Sound
    you just have to include the 2 Sound Output class files
    in ur project
  • The sample demo program use ….the same "quick
    & dirty" global declaration CSoundOut SoundOut; in
    CSndApp.cpp.
  • Call the function OpenOutput();
    in the InitInstance App procedure. (the CloseOutput() at ExitIntance is
    optional)

This is all you need to start the Sound process for audio
Output.

The Scope View:

Is unchanged. it will display the Input which is a mix of the
Input (Equvalent to the Mic) and the Output (Equivalent to the
Wave).

Run the scope/generator demo program:

In the new Demo sample the SoundIn class is still active, a
menu has been added to start/Stop the both Sound process. Because
some Sound system does’nt provide full duplex Input/Output you
may be required to start & stop selectively the 2 process. If
you modify the sampling rate, you must also take into account
that the Sound card use the same frequency rate for both
Input/Ouput and that the classes parametres must be set at the
same one.

Before running it, let’s have a look at the
"reame.txt" file included in the project demo.

Update

Thanks to peoples who have contributed to this update

As Tom Wheeler mentioned ,the CSoundOut use now 2 buffers
which are feed to the sound system, I saw source code sound
applications (like playing wave file)that use more than 2 buffers
which are feed to the sound system. When the fisrt buffer has
been played the thread is notified to send an other one to the
sound system. In this way the sound system has alway a buffer to
play and this avoid time leak beetwin buffers.

An other problem is that the generator samples must be
continus for the 2 buffers, if the sampling frequency change and
the sound signal frequency also change, it is not really simple
to have the boths buffers exactly synchronized to a precise
number of periodes. The CSinGenerator class
derived from CSoundOut has been
added which resolve this kind of problem by using a special
Sinusoid Oscillator algorithm. This is not a place to develop
this signal processing application ,but readers can D/L the
"goertzel.zip" file from my web to get.more
informations.

Download the new demo
code
– 40 Kb

Ps: If you are a little bit lazy to enhance the program by
yourself…, you can get my full application for signal
processing here: http://www.multimania.com/yannstrc
go to Visual Sound Processing Studio page and enjoy it!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read