123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- #include "pch.h"
- class WaveTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- WaveTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<StringValue> pstring; CastTo(pstring, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreateWaveFileSoundTemplate(pTemplate, m_pmodeler->GetArtPath() + "/" + pstring->GetValue() + ".wav"));
- return pTemplate;
- }
- };
- class ThreeDSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- ThreeDSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<Number> pnumMinDist; CastTo(pnumMinDist, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(Create3DSoundTemplate(pTemplate, ptemplateBase, pnumMinDist->GetValue()));
- return pTemplate;
- }
- };
- class ConeSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- ConeSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<Number> pnumInnerAngle; CastTo(pnumInnerAngle, (IObject*)stack.Pop());
- TRef<Number> pnumOuterAngle; CastTo(pnumOuterAngle, (IObject*)stack.Pop());
- TRef<Number> pnumOuterGain; CastTo(pnumOuterGain, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreateSoundConeTemplate(pTemplate, ptemplateBase,
- pnumInnerAngle->GetValue(), pnumOuterAngle->GetValue(), pnumOuterGain->GetValue()));
- return pTemplate;
- }
- };
- class PitchSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- PitchSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<Number> pnumPitch; CastTo(pnumPitch, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreatePitchSoundTemplate(pTemplate, ptemplateBase, pnumPitch->GetValue()));
- return pTemplate;
- }
- };
- // a template adjusting the gain of another template
- class SoundGainValueTemplate : public ISoundTemplate
- {
- private:
- TRef<ISoundTemplate> m_pstBase;
- TRef<Number> m_pnumGain;
- public:
- // a sound instance wrapper which adjusts Gain changes
- class GainInstance : public SoundInstanceWrapper, public SoundTweakableWrapper, public Value
- {
- TRef<Number> m_pnumGain;
- float m_fCurrentGain;
- public:
- GainInstance(Number* pnumGain, ISoundInstance* pinstance,
- ISoundTweakable* ptweak) :
- SoundInstanceWrapper(pinstance),
- SoundTweakableWrapper(ptweak),
- m_fCurrentGain(0.0f),
- m_pnumGain(pnumGain),
- Value(pnumGain)
- {};
- // Sets the Gain shift, where 1.0 is normal, 0.5 is half of normal speed,
- // and 2.0 is twice normal speed.
- HRESULT SetGain(float fGain)
- {
- m_fCurrentGain = fGain;
- return SoundTweakableWrapper::SetGain(m_pnumGain->GetValue() + m_fCurrentGain);
- };
- // Gets an interface for tweaking the sound, if supported, NULL otherwise.
- TRef<ISoundTweakable> GetISoundTweakable()
- {
- return this;
- };
- void ChildChanged(Value* pvalue, Value* pvalueNew)
- {
- // the only child is the gain value, so let's adjust that.
- ZSucceeded(SetGain(m_fCurrentGain));
- };
- };
- public:
- SoundGainValueTemplate(ISoundTemplate* pstSource, Number* pnumGain)
- {
- m_pstBase = pstSource;
- m_pnumGain = pnumGain;
- };
- // Creates a new instance of the given sound
- virtual HRESULT CreateSound(TRef<ISoundInstance>& psoundNew,
- ISoundBufferSource* pbufferSource, ISoundPositionSource* psource)
- {
- HRESULT hr = m_pstBase->CreateSound(psoundNew, pbufferSource, psource);
- if (ZFailed(hr)) return hr;
- TRef<ISoundTweakable> ptweak = psoundNew->GetISoundTweakable();
- // we need to have a tweakable sound for this to work.
- if (!ptweak)
- {
- ZAssert(false);
- return E_FAIL;
- }
- hr = ptweak->SetGain(m_pnumGain->GetValue());
- if (ZFailed(hr)) return hr;
- psoundNew = new GainInstance(m_pnumGain, psoundNew, ptweak);
- return S_OK;
- }
- };
- class GainSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- GainSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<Number> pnumGain; CastTo(pnumGain, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- ZAssert(pnumGain && ptemplateBase);
- return new SoundGainValueTemplate(ptemplateBase, pnumGain);
- }
- };
- class PrioritySoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- PrioritySoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<Number> pnumPriority; CastTo(pnumPriority, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreatePrioritySoundTemplate(pTemplate, ptemplateBase, pnumPriority->GetValue()));
- return pTemplate;
- }
- };
- class LoopingSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- LoopingSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreateLoopingSoundTemplate(pTemplate, ptemplateBase));
- return pTemplate;
- }
- };
- class ASRSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- ASRSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<Number> pnumLoopStart; CastTo(pnumLoopStart, (IObject*)stack.Pop());
- TRef<Number> pnumLoopLength; CastTo(pnumLoopLength, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreateASRSoundTemplate(pTemplate, ptemplateBase,
- pnumLoopStart->GetValue(), pnumLoopLength->GetValue()));
- return pTemplate;
- }
- };
- class PairedSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- PairedSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<ISoundTemplate> ptemplateBase1; CastTo(ptemplateBase1, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase2; CastTo(ptemplateBase2, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreatePairedSoundTemplate(pTemplate, ptemplateBase1, ptemplateBase2));
- return pTemplate;
- }
- };
- class RepeatingFireSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- RepeatingFireSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<Number> pnumRepeatRate; CastTo(pnumRepeatRate, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreateRepeatingFireSoundTemplate(pTemplate, ptemplateBase, pnumRepeatRate->GetValue()));
- return pTemplate;
- }
- };
- class RandomSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- RandomSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<IObjectList> plist; CastTo(plist, (IObject*)stack.Pop());
- TRef<IRandomSoundTemplate> pTemplate;
- ZSucceeded(CreateRandomSoundTemplate(pTemplate));
- plist->GetFirst();
- while (plist->GetCurrent() != NULL) {
- IObjectPair* ppair; CastTo(ppair, plist->GetCurrent());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, ppair->GetFirst() );
- TRef<Number> pnumWeight; CastTo(pnumWeight, ppair->GetSecond() );
- ZSucceeded(pTemplate->AddSoundTemplate(ptemplateBase, pnumWeight->GetValue()));
- plist->GetNext();
- }
- return pTemplate;
- }
- };
- class IntermittentSoundTemplateFactory : public IFunction
- {
- TRef<Modeler> m_pmodeler;
- public:
- IntermittentSoundTemplateFactory(Modeler* pmodeler) :
- m_pmodeler(pmodeler)
- {
- }
- public:
- TRef<IObject> Apply(ObjectStack& stack)
- {
- TRef<Number> pnumPeriod; CastTo(pnumPeriod, (IObject*)stack.Pop());
- TRef<Boolean> pboolPlayMultiple; CastTo(pboolPlayMultiple, (IObject*)stack.Pop());
- TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
- TRef<ISoundTemplate> pTemplate;
- ZSucceeded(CreateIntermittentSoundTemplate(pTemplate, ptemplateBase,
- pnumPeriod->GetValue(), pboolPlayMultiple->GetValue()));
- return pTemplate;
- }
- };
- void AddSoundFactories(
- INameSpace* pns,
- Modeler* pmodeler
- )
- {
- pns->AddMember("ImportWave", new WaveTemplateFactory(pmodeler));
- pns->AddMember("ThreeDSound", new ThreeDSoundTemplateFactory(pmodeler));
- pns->AddMember("ConeSound", new ConeSoundTemplateFactory(pmodeler));
- pns->AddMember("PitchSound", new PitchSoundTemplateFactory(pmodeler));
- pns->AddMember("GainSound", new GainSoundTemplateFactory(pmodeler));
- pns->AddMember("PrioritySound", new PrioritySoundTemplateFactory(pmodeler));
- pns->AddMember("LoopingSound", new LoopingSoundTemplateFactory(pmodeler));
- pns->AddMember("ASRSound", new ASRSoundTemplateFactory(pmodeler));
- pns->AddMember("PairedSound", new PairedSoundTemplateFactory(pmodeler));
- pns->AddMember("RepeatingFireSound", new RepeatingFireSoundTemplateFactory(pmodeler));
- pns->AddMember("RandomSound", new RandomSoundTemplateFactory(pmodeler));
- pns->AddMember("IntermittentSound", new IntermittentSoundTemplateFactory(pmodeler));
- }
|