Solved

DirectSound Frequency change causes "click" on Audio

Posted on 2006-06-23
10
1,290 Views
Last Modified: 2013-11-13
I am trying to generate a continuous tone with DirectX (C#, VS 2003.NET) and vary the frequency on the fly. It appears to work except that everytime the frequency changes I get a "blip or click" (some unwanted noise) on the sound. Should I should generate my own data instead of opening a generic tone file and varying the frequency at which it plays? This would allow re-writing it (I think) on the fly....

This was supposed to take 15 minutes..... ha ha ha
You get 500 points for this question. I don't know if it is easy or difficult, I just know I want to get it done yesterday.

            static Device applicationDevice = null;
            static SecondaryBuffer applicationBuffer = null;
            static string strFileName = @"c:\Tone1.wav";
            BufferPlayFlags PlayFlags  =  BufferPlayFlags.Looping ;
            BufferDescription desc = new BufferDescription();


            public void InitApplicationBuffer()
            {
                  if (null != applicationBuffer)
                              applicationBuffer.Dispose();

                  desc.ControlFrequency  = true;
                  desc.ControlPan = true;
                  desc.ControlVolume = true;
                  desc.GlobalFocus = true ;

                  applicationBuffer = new SecondaryBuffer(strFileName, desc, applicationDevice);
                  applicationBuffer.Pan = 0;
                  applicationBuffer.Volume = -1000;
            }

            public void PlayNote(int freq)
            {

                  if( applicationBuffer == null)
                        return ;

                  try
                  {
                        applicationBuffer.Stop();
                        applicationBuffer.Frequency = freq;
                        applicationBuffer.SetCurrentPosition(0);
                        
                                if(firstrun == true)
                        {
                              applicationBuffer.Play(0, PlayFlags);
                              firstrun = false ;
                        }

                  }
                  catch(Exception e)
                  {
                        MessageBox.Show(null, e.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                  }

            }
0
Comment
Question by:shawnricker
  • 4
  • 3
10 Comments
 
LVL 18

Expert Comment

by:JoseParrot
ID: 16974108
Hi,

   applicationBuffer.Stop();           <---- here the program stops the sound
   applicationBuffer.Frequency = freq;
   applicationBuffer.SetCurrentPosition(0);
        if(firstrun == true)
        {
             applicationBuffer.Play(0, PlayFlags);  <-- here restarts the sound

In theory, the timeframe between STOP and PLAY, in a Pentium 4, is negligenciable. I am not sure that if this program was in C the problem would not exist. A few weeks ago I had a chance to compare two programs with absolutely the same algorithm, one in Visual C++ 6.0 and other in C# .net 2003. One of them was 30 times faster. The C program, of course.

Two tests:
- code in C/C++ just to compare.
- try the following modifications:

  applicationBuffer.Frequency = freq;
  // applicationBuffer.Stop();           <---- (1) first try to not stop
  // applicationBuffer.SetCurrentPosition(0); <--- (2) try to not rewind
        if(firstrun == true)
        {
             applicationBuffer.Play(0, PlayFlags);

I am not sure if must stop to the frequency change make effect.
If you can accept a small delay to the new frequency, good. If not, maybe you can edit the tone to be shorter, thus having an acceptable delay.

Jose
 
0
 

Author Comment

by:shawnricker
ID: 16975678
Jose,
I am new to C# but very experienced in C/C++ and I am sure you are correct that C# is much slower. With respect to your comments, I continued to work with this and I posted my code snippet with a mistake, I had already commented out the two lines you suggested to no avail. I have also tried cloning the buffer and starting to play the clone before stopping the buffer with the previous frequency which also did not work. I wonder if only one buffer can play at a time? I will try to play two tones simultaneously to find out, if I can't then cloning will never help.

Thanks for your effort.
0
 
LVL 18

Assisted Solution

by:JoseParrot
JoseParrot earned 500 total points
ID: 16981445
Hi,

If the click is something not well treated inside the runtime, may be a little trick could minimize or even solve the problem. The idea is

       if(firstrun == true)
       {
             applicationBuffer.volume(low);
             applicationBuffer.Play(0, PlayFlags);
             applicationBuffer.volume(normal);

I don't have a guide here, so the "volume" statement should be incorrect, but I'm sure you get it.

Another suggestion is to use MIDI, instead of wave. If you want just tones, MIDI is perfect to that: from triangle and square waves to flutes and sci-fi UFO sounds. You change the tones just changing the notes. MIDI was made to that!

Jose
0
 
LVL 2

Expert Comment

by:gran88
ID: 16991731
Ah, but MIDI is platform dependent.  It runs using the basic DirectX Midi Sound Library, whatever they call it, and the instruments can change depending on the drivers/devices you have installed.  Just a little tidbit.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 18

Expert Comment

by:JoseParrot
ID: 17003481
If you are using DirectX with C#, VS 2003.NET you are already totally platform dependent: Microsoft. MIDI will use absolutelly the same DirectX C#, VS 2003.NET...

Jose
0
 
LVL 18

Accepted Solution

by:
JoseParrot earned 500 total points
ID: 17003586
Think a bit more... you're right, as MIDI boards can have different synthetizers, samplings with different final sounds.

But, as your question is about continuous tone, no noticeable differences will happen between an 8 bits Creative Sound Blasterof the 90's and the last Audigy2 when the waveform is a triangle or square. With a advantage over wave playing: no clicks.

Jose
0
 

Author Comment

by:shawnricker
ID: 17004039
I am still here and still very much interested in resolving this. I will look into MIDI and let you know what I find. Time has been short lately but I am persistant to a fault.

Thanks
0
 

Author Comment

by:shawnricker
ID: 17271145
I apologize to the moderators. Really this issue is unresolved but nobody seems to care on the user end. I split the points for the effort.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now