Changin audio output

Hi, I’m trying to make an audio output selector, but it only works for the first time it chooses one of the available output devices (I’m in windows).

I also have an input selector that works fine. Every time I choose a diferent input it just works, I can several input lines working at once. But when I try to change the output the inputs freezes and stops proceesing.

This is how I change the output:

void changeOutput(string output)
{
	auto ctx = ci::audio::master();
	auto device = ci::audio::Device::findDeviceByName(output);
	if (device)		
	   ci::audio::OutputDeviceNodeRef outputDevice = ctx->createOutputDeviceNode(device);
	ctx->setOutput(outputDevice);
}

Where output is sleceted from one of ci::audio::Device::getOutputDevices()

then I try to update the connection of a BufferPlayer node

void updateConnection(){
       auto ctx = ci::audio::Context::master();
	if (mGain) {
		if (mGain->isConnectedToOutput(ctx->getOutput())) {
			mGain->disconnect(ctx->getOutput());
		}
	}

	mGain = ctx->makeNode(new ci::audio::GainNode(1.0f));
	mSoundBufferPing >> mGain;
	mGain >> ctx->getOutput();
	ctx->enable();
}

Where:
ci::audio::GainNodeRef mGain;
ci::audio::BufferPlayerNodeRef mSoundBufferPing;

The sound is loaded like this:

void AudioInputSelectApp::loadPing()
{
	
	auto ctx = ci::audio::Context::master();
	try {

		auto audioTrack = ci::audio::load(ci::loadFile(ci::app::getAssetPath("ping.mp3")), ctx->getSampleRate());

		ci::audio::BufferRef buffer = audioTrack->loadBuffer();
		mSoundBufferPing = ctx->makeNode(new ci::audio::BufferPlayerNode(buffer));
		
	}
	catch (ci::Exception &exc) {
		ci::app::console() << "[xumo::AudioModule::loadPing] failed to load sound." << exc.what() << std::endl;
	}
	ci::app::console() << "[xumo::AudioModule::loadPing] laoaded" << std::endl;
}

Then, this works:

loadPing();
changeOutput("Correct output device name");
updateConnection();
mSoundBufferPing->start();

That plays a sound correctly, but then calling:

changeOutput("Another correct output device name");
updateConnection();
mSoundBufferPing->start();

This doesn’t work anymore.

Anyone knows how to do this?

Hi, sorry for the delay…,

First, I think it’s best to disable() the audio::Context before changing the output. Ideally this wouldn’t be necessary but to be safe…

Then, can you also try a Context::printGraphToString() and paste the output, when you can no longer hear anything?

Lastly, try placing a breakpoint in the OutputDeviceNodeWasapi’s render method and see if that is firing.

cheers,
Rich

Disabling the audio::Context before changing the output does the trick. Thanks.

Just uploaded a quick tool a made. I use it to find out if the audio setup in a pc can work with my Cinder apps, very usefull when I’m not able to be on site.

1 Like