Can't properly stop the ToneGeneratorAudioSource


Hi all,

Another silly question. I have problems with the ToneGeneratorAudioSource.

I launch it like that :

[code]ToneGenerator=new ToneGeneratorAudioSource();

audioDeviceManager->setAudioCallback (this);[/code]

I works but then, I try to stop it like that :

audioDeviceManager->setAudioCallback( 0); audioSourcePlayer.setSource(0); deleteAndZero(ToneGenerator);

It results in a awful noise (maybe mixed with the puretone). It sounds like if a corrupted buffer is still played looping.

I have missed something ?



If you call setAudioCallback (0), it stops the audio device making any noise - I’ve never seen any problems with this, are you doing anything strange? Do all your audio devices do the same thing?


Well, I’m doing anything strange in my opinion.

After several tries, my opinion is that it may have to be with a corrupted buffer that is still running).

When sending the audioDeviceManeger->setAudioCallback(0), the noise starts with in the background my sinewave tone (it’s frequency is the same that the one used in the generator).

Futhermore I noticed that if, in my (actually your) audiosettings dialog, I change the “Audio buffer size”, it has an impact on the residual noise that appears after the setAudioCallback(0). The shorter the buffer size is, the higher the frequency of the noise is.

I will try to recreate this problem in a smaller app (in the helloworld app). and post the whole code if it is still here.

Note : I’m on Mac (macbook pro intel, i’ve not tested it yet under windows).



Thanks. It does sound like a buffer being repeated, but I can’t think why it’d happen. You can see at line 636 of juce_mac_CoreAudio.cpp that it clears the buffers when there’s no callback. Have you got some kind of unusual channel or device setup? What happens with other audio devices?


Hi, Ive done the test modifying the helloworldcomponent of the sample project.

The problem is still there in this simple framework. I post below the cource code of the modified Helloworld component (that’s the only thing I have change in the sample project).

It shows 2 buttons :
B1 -> start the sinewave generator.
B2 -> stop it… on my setup it leads to the tone+some noise…

Any hints ?


class HelloWorldContentComponent : public Component, public ButtonListener, public AudioIODeviceCallback
AudioDeviceManager audioDeviceManager;
AudioSourcePlayer audioSourcePlayer;
ToneGeneratorAudioSource *ToneGenerator;
Button *Btn1;
Button *Btn2;

AudioFormatManager formatManager;
const String error (audioDeviceManager.initialise (0, /* number of input channels /
2, /
number of output channels /
0, /
no XML settings… /
true /
select default device on failure */));

	addAndMakeVisible (Btn1 = new TextButton (T("B1")));
	Btn1->setButtonText (T("B1"));
	Btn1->addButtonListener (this);
	Btn1->setBounds (10,20,50,50);
	addAndMakeVisible (Btn2= new TextButton (T("B2")));
	Btn2->setButtonText (T("B2"));
	Btn2->addButtonListener (this);
	Btn2->setBounds (60,60,100,30);

	if (ToneGenerator!=NULL) deleteAndZero(ToneGenerator);

void buttonClicked (Button* buttonThatWasClicked)
	if (buttonThatWasClicked == Btn1)
		ToneGenerator=new ToneGeneratorAudioSource();
		audioDeviceManager.setAudioCallback (this);
	else if (buttonThatWasClicked == Btn2)
		if (ToneGenerator!=NULL) deleteAndZero(ToneGenerator);

void paint (Graphics& g)
    // clear the background with solid white
    g.fillAll (Colours::white);
    // set our drawing colour to black..
    g.setColour (Colours::black);
    // choose a suitably sized font
    g.setFont (20.0f, Font::bold);
    // and draw the text, centred in this component
    g.drawText (T("Hello World!"),
                0, 0, getWidth(), getHeight(),
                Justification::centred, false);

void audioDeviceIOCallback (const float** inputChannelData,
							int totalNumInputChannels,
							float** outputChannelData,
							int totalNumOutputChannels,
							int numSamples)
	audioSourcePlayer.audioDeviceIOCallback (inputChannelData, totalNumInputChannels, outputChannelData, totalNumOutputChannels, numSamples);

void audioDeviceAboutToStart (double sampleRate, int numSamplesPerBlock)
	audioSourcePlayer.audioDeviceAboutToStart (sampleRate, numSamplesPerBlock);

void audioDeviceStopped()



Right… but that’s not really any use, because I’ve never had any problems like this on any system I’ve got. We need to work out why your setup is different. Maybe if you answer the questions I asked in my last post, that might help!


Copying & compiling my piece of code don’t replicate this on your system ?

so I have no unusual channel or device setup. Only the built-in sound device of a macbook pro.

I’ll try tomorrow with an external m-audio firewire interface @ work.

I will also try on the same computer under windows xp (in a virtual machine or in native mode).

I’ll post the results.

I use Juce since a year now and I never had this problem. however in all my apps I use a positionable source or a personal source that is not continuous (that play a single shot sample). Never had a problem with that…

thank your help.



I have compiled above mentioned code in windows XP on the same macbook pro computer :

  • in a virtual machine (using parallels desktop)
  • running it natively through bootcamp

The result is the same… I was afraid that is was a Leopard Issue but since it’s the same under windows…

Tomorrow I’ll test on an external device and I may try to launch the little demo program on another macbook pro (newer version than mine)…

I can send the OSX app to anyone who wants to try it on his Mac…



I can try it if you want. I have an MBP with Leopard.

Can you post a link and I’ll grab it? I’ll send you a private message too with my email.



Ah, so sorry, you’re right - it’s a bug in AudioDeviceManager. No idea how I’ve managed to never hit the problem before, but I’ve checked in a fix for it now!


Oh, that’s a delightful sound. Brown noise anyone?

Confirmed, for what it’s worth. In true juce style, the bug is fixed before the ink on the web page dries.

Jules, don’t fancy maintaining my old apps do you?



Impressive Speed !!!

it’s working like a charm now with your fix…

Thanks a lot for your help.

Sylvain Clément