MCPcopy Create free account
hub / github.com/LabSound/LabSound / play

Method play

examples/src/Examples.hpp:783–837  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

781struct ex_hrtf_spatialization : public labsound_example
782{
783 virtual void play(int argc, char ** argv) override
784 {
785 const auto defaultAudioDeviceConfigurations = GetDefaultAudioDeviceConfiguration();
786 context = lab::MakeRealtimeAudioContext(defaultAudioDeviceConfigurations.second, defaultAudioDeviceConfigurations.first);
787 lab::AudioContext& ac = *context.get();
788
789 if (!ac.loadHrtfDatabase("hrtf")) {
790 std::string path = std::string(SAMPLE_SRC_DIR) + "/hrtf";
791 if (!ac.loadHrtfDatabase(path)) {
792 printf("Could not load spatialization database");
793 return;
794 }
795 }
796
797 std::shared_ptr<AudioBus> audioClip = MakeBusFromSampleFile("samples/trainrolling.wav", argc, argv);
798 std::shared_ptr<SampledAudioNode> audioClipNode = std::make_shared<SampledAudioNode>(ac);
799 if (audioClipNode)
800 {
801 std::shared_ptr<PannerNode> panner = std::make_shared<PannerNode>(ac);
802 _nodes.push_back(audioClipNode);
803 _nodes.push_back(panner);
804
805
806 panner->setPanningModel(PanningModel::HRTF);
807 {
808 ContextRenderLock r(context.get(), "ex_hrtf_spatialization");
809 audioClipNode->setBus(r, audioClip);
810 }
811 audioClipNode->schedule(0.0, -1); // -1 to loop forever
812 context->connect(panner, audioClipNode, 0, 0);
813 context->connect(context->device(), panner, 0, 0);
814
815 context->listener()->setPosition({0, 0, 0});
816 panner->setVelocity(4, 0, 0);
817
818 context->synchronizeConnections();
819 //context->debugTraverse(context->device().get());
820
821 const int seconds = 10;
822 float halfTime = seconds * 0.5f;
823 for (float i = 0; i < seconds; i += 0.01f)
824 {
825 float x = (i - halfTime) / halfTime;
826
827 // Put position a +up && +front, because if it goes right through the
828 // listener at (0, 0, 0) it abruptly switches from left to right.
829 panner->setPosition({x, 0.1f, 0.1f});
830 Wait(10);
831 }
832 }
833 else
834 {
835 std::cerr << "Couldn't initialize train node to play" << std::endl;
836 }
837 }
838};
839
840//-------------------------

Callers

nothing calls this directly

Calls 13

MakeRealtimeAudioContextFunction · 0.85
getMethod · 0.80
loadHrtfDatabaseMethod · 0.80
setPanningModelMethod · 0.80
scheduleMethod · 0.80
deviceMethod · 0.80
listenerMethod · 0.80
setBusMethod · 0.45
connectMethod · 0.45
setPositionMethod · 0.45

Tested by

no test coverage detected