SoundPlayer setup

I’m wanting to play an audiobuffer that I’ve made, using the SoundPlayer class.

My header makes these objects:

SoundPlayer player;
AudioDeviceManager deviceManager;

and in my constructor I’m just trying to get them to play the test sound like this:

deviceManager.initialiseWithDefaultDevices (0, 2);
deviceManager.addAudioCallback (&player);
player.playTestSound();

But I’m not hearing anything… what am I doing wrong?

Thank you!

Maybe the default audio device is set to HDMI audio or something…
Try to add the AudioDeviceSelectorComponent to verify that…

I tried this:

 auto setup = deviceManager.getAudioDeviceSetup ();

and if I run the debugger, the outputDeviceName is “Built-In Output” (I’m on a Macbook).

If I click the options button in the top left and select audio midi settings, it is also on built in output and that test button does play a sine at me.

anything else I should look for?

What kind of a JUCE project are you trying this in?

Also tried adding the AudioDeviceSelectorComponent and it also says built in output, it’s test button does not work.

The thing I building resides within a synth (i’m not building that part of it and mainly just deal with GUI). Is it possible that the synth part is doing things with the audio / output and me adding a SoundPlayer isn’t going to work?

If it’s an audio plugin project, you should not use AudioDeviceManager (and by extension SoundPlayer) with that. You need to generate your audio in the processBlock method. If you build the plugin project as a standalone application it already has an AudioDeviceManager instance which is likely the reason your own AudioDeviceManager instance does not work.

I’m building it as a standalone at the moment. Do you know how I would access the AudioDeviceManger that the standalone is using? Or should I just find another way to play back this sound?

Ok… weirdly, if I change the output in the AudioDeviceSelectorComponent to the next option, which in my case is ‘Instant On Sound Effects’ then I do hear the test sound.

‘Instant On Sound Effects’ is something to do with a loopback audio driver I have installed.

Does this mean that the standalone AudioDeviceManager is ‘using’ the built in output, and the 2nd one I created can’t also use it?

You should not try getting access to the AudioDeviceManager. You need to figure out some other way to play the sound by implementing the processBlock method.

Messing around with the AudioDeviceManager(s) may kind of work with the standalone app build but it is not going to work properly when running the project as a plugin within a host.

The standalone plugin target provides a “mini host”, that has a AudioDeviceManager on its own and an AudioProcessorPlayer. You should not need to communicate with them at all, instead communicate through the plugin API.

Put most simply : SoundPlayer is not really compatible with plugin projects.

If you don’t need to do samplerate conversion, polyphony or anything fancy like that, playing an AudioBuffer from the processBlock method is pretty simple, though. You need a position counter member variable and you just copy the samples from your AudioBuffer to the AudioBuffer given to processBlock.

thank you, do you know where I could read to look into using the processBlock method?

Best to start with the tutorial and then look at the API docs:
AudioProcessor::processBlock()

Do you know of any example code that uses this method? Every tutorial I have found uses AudioAppComponent - which I don’t think I can use because my project is a plugin?

Well, the tutorial I linked in the post before doesn’t. It is a plugin and implements the two classes, one inheriting AudioProcessor and the other inheriting AudioProcessorEditor.

There are more examples in your JUCE/examples/Plugins, see here online:

There are plenty of tutorials and examples (in the JUCE source tree) that are plugin specific. I think none of them cover your exact use case of simply playing an AudioBuffer, though. (It can get a bit messy because to be correct, it really needs to implement the sample rate conversion step too…)

By the way, what are you actually trying to do? Why do you want to be able to play the contents of an AudioBuffer? If it’s for a sampler instrument of some kind, it’s not the best approach. JUCE has different classes that are meant for doing instruments.

The app is a synth, but I don’t do any of the synth code, until now I’ve only been doing GUI.

I’ve moved on to code that is creating sounds (as audiobuffers) that the synth will eventually use, and I’ve written a test page that draws audio waveforms.
I just want to play the sounds I’ve made in a simple manner. I can’t pass them to the synth engine proper because it doesn’t use audiobuffers.

I’d get my partner to do it, but he’s in the Southern Hemisphere and on holiday for summer!

What does it use, then? I think the easiest approach is to just try to use the existing synth engine, if at all possible, otherwise you will need to implement another synth of your own with JUCE…(There’s no really “simple” way in JUCE to play AudioBuffers like a synth with polyphony and sample rate correction, you would need to use the Synthesiser, SynthesiserVoice and SynthesiserSound classes.)

Rather than build a second synth engine, I have a hack that works for my initial approach. The solution is to tell the AudioDeviceManager which output to use…

deviceManager.initialise(0,2, nullptr, true, "Instant On Sound Effects", nullptr);
deviceManager.addAudioCallback (&player);

This is pretty nasty but this test page won’t exist in the final synth, and now at least I can play my audio buffers!

Thank you all for your input, it’s amazing to me that my stupid questions usually get an answer within a few minutes. :slight_smile:

Sorry to post again, but this approach won’t work.
A plugin is supposed to get audio and/or midi data from the host, NOT from an AudioIODevice.
And it is supposed to process or generate audio and/or MIDI to feed it into the host, NOT to the AudioIODevice.
This is, why any plugin must not use the AudioDeviceManager.

You may hear the output of your synth, but in fact the host will not hear the audio, because it is sent directly to the soundcard. And the user won’t be able to record that audio, process it with other plugins etc. So it is pointless to use your plugin in any DAW.