efsound.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include "pch.h"
  2. class WaveTemplateFactory : public IFunction
  3. {
  4. TRef<Modeler> m_pmodeler;
  5. public:
  6. WaveTemplateFactory(Modeler* pmodeler) :
  7. m_pmodeler(pmodeler)
  8. {
  9. }
  10. public:
  11. TRef<IObject> Apply(ObjectStack& stack)
  12. {
  13. TRef<StringValue> pstring; CastTo(pstring, (IObject*)stack.Pop());
  14. TRef<ISoundTemplate> pTemplate;
  15. ZSucceeded(CreateWaveFileSoundTemplate(pTemplate, m_pmodeler->GetArtPath() + "/" + pstring->GetValue() + ".wav"));
  16. return pTemplate;
  17. }
  18. };
  19. class ThreeDSoundTemplateFactory : public IFunction
  20. {
  21. TRef<Modeler> m_pmodeler;
  22. public:
  23. ThreeDSoundTemplateFactory(Modeler* pmodeler) :
  24. m_pmodeler(pmodeler)
  25. {
  26. }
  27. public:
  28. TRef<IObject> Apply(ObjectStack& stack)
  29. {
  30. TRef<Number> pnumMinDist; CastTo(pnumMinDist, (IObject*)stack.Pop());
  31. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  32. TRef<ISoundTemplate> pTemplate;
  33. ZSucceeded(Create3DSoundTemplate(pTemplate, ptemplateBase, pnumMinDist->GetValue()));
  34. return pTemplate;
  35. }
  36. };
  37. class ConeSoundTemplateFactory : public IFunction
  38. {
  39. TRef<Modeler> m_pmodeler;
  40. public:
  41. ConeSoundTemplateFactory(Modeler* pmodeler) :
  42. m_pmodeler(pmodeler)
  43. {
  44. }
  45. public:
  46. TRef<IObject> Apply(ObjectStack& stack)
  47. {
  48. TRef<Number> pnumInnerAngle; CastTo(pnumInnerAngle, (IObject*)stack.Pop());
  49. TRef<Number> pnumOuterAngle; CastTo(pnumOuterAngle, (IObject*)stack.Pop());
  50. TRef<Number> pnumOuterGain; CastTo(pnumOuterGain, (IObject*)stack.Pop());
  51. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  52. TRef<ISoundTemplate> pTemplate;
  53. ZSucceeded(CreateSoundConeTemplate(pTemplate, ptemplateBase,
  54. pnumInnerAngle->GetValue(), pnumOuterAngle->GetValue(), pnumOuterGain->GetValue()));
  55. return pTemplate;
  56. }
  57. };
  58. class PitchSoundTemplateFactory : public IFunction
  59. {
  60. TRef<Modeler> m_pmodeler;
  61. public:
  62. PitchSoundTemplateFactory(Modeler* pmodeler) :
  63. m_pmodeler(pmodeler)
  64. {
  65. }
  66. public:
  67. TRef<IObject> Apply(ObjectStack& stack)
  68. {
  69. TRef<Number> pnumPitch; CastTo(pnumPitch, (IObject*)stack.Pop());
  70. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  71. TRef<ISoundTemplate> pTemplate;
  72. ZSucceeded(CreatePitchSoundTemplate(pTemplate, ptemplateBase, pnumPitch->GetValue()));
  73. return pTemplate;
  74. }
  75. };
  76. // a template adjusting the gain of another template
  77. class SoundGainValueTemplate : public ISoundTemplate
  78. {
  79. private:
  80. TRef<ISoundTemplate> m_pstBase;
  81. TRef<Number> m_pnumGain;
  82. public:
  83. // a sound instance wrapper which adjusts Gain changes
  84. class GainInstance : public SoundInstanceWrapper, public SoundTweakableWrapper, public Value
  85. {
  86. TRef<Number> m_pnumGain;
  87. float m_fCurrentGain;
  88. public:
  89. GainInstance(Number* pnumGain, ISoundInstance* pinstance,
  90. ISoundTweakable* ptweak) :
  91. SoundInstanceWrapper(pinstance),
  92. SoundTweakableWrapper(ptweak),
  93. m_fCurrentGain(0.0f),
  94. m_pnumGain(pnumGain),
  95. Value(pnumGain)
  96. {};
  97. // Sets the Gain shift, where 1.0 is normal, 0.5 is half of normal speed,
  98. // and 2.0 is twice normal speed.
  99. HRESULT SetGain(float fGain)
  100. {
  101. m_fCurrentGain = fGain;
  102. return SoundTweakableWrapper::SetGain(m_pnumGain->GetValue() + m_fCurrentGain);
  103. };
  104. // Gets an interface for tweaking the sound, if supported, NULL otherwise.
  105. TRef<ISoundTweakable> GetISoundTweakable()
  106. {
  107. return this;
  108. };
  109. void ChildChanged(Value* pvalue, Value* pvalueNew)
  110. {
  111. // the only child is the gain value, so let's adjust that.
  112. ZSucceeded(SetGain(m_fCurrentGain));
  113. };
  114. };
  115. public:
  116. SoundGainValueTemplate(ISoundTemplate* pstSource, Number* pnumGain)
  117. {
  118. m_pstBase = pstSource;
  119. m_pnumGain = pnumGain;
  120. };
  121. // Creates a new instance of the given sound
  122. virtual HRESULT CreateSound(TRef<ISoundInstance>& psoundNew,
  123. ISoundBufferSource* pbufferSource, ISoundPositionSource* psource)
  124. {
  125. HRESULT hr = m_pstBase->CreateSound(psoundNew, pbufferSource, psource);
  126. if (ZFailed(hr)) return hr;
  127. TRef<ISoundTweakable> ptweak = psoundNew->GetISoundTweakable();
  128. // we need to have a tweakable sound for this to work.
  129. if (!ptweak)
  130. {
  131. ZAssert(false);
  132. return E_FAIL;
  133. }
  134. hr = ptweak->SetGain(m_pnumGain->GetValue());
  135. if (ZFailed(hr)) return hr;
  136. psoundNew = new GainInstance(m_pnumGain, psoundNew, ptweak);
  137. return S_OK;
  138. }
  139. };
  140. class GainSoundTemplateFactory : public IFunction
  141. {
  142. TRef<Modeler> m_pmodeler;
  143. public:
  144. GainSoundTemplateFactory(Modeler* pmodeler) :
  145. m_pmodeler(pmodeler)
  146. {
  147. }
  148. public:
  149. TRef<IObject> Apply(ObjectStack& stack)
  150. {
  151. TRef<Number> pnumGain; CastTo(pnumGain, (IObject*)stack.Pop());
  152. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  153. ZAssert(pnumGain && ptemplateBase);
  154. return new SoundGainValueTemplate(ptemplateBase, pnumGain);
  155. }
  156. };
  157. class PrioritySoundTemplateFactory : public IFunction
  158. {
  159. TRef<Modeler> m_pmodeler;
  160. public:
  161. PrioritySoundTemplateFactory(Modeler* pmodeler) :
  162. m_pmodeler(pmodeler)
  163. {
  164. }
  165. public:
  166. TRef<IObject> Apply(ObjectStack& stack)
  167. {
  168. TRef<Number> pnumPriority; CastTo(pnumPriority, (IObject*)stack.Pop());
  169. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  170. TRef<ISoundTemplate> pTemplate;
  171. ZSucceeded(CreatePrioritySoundTemplate(pTemplate, ptemplateBase, pnumPriority->GetValue()));
  172. return pTemplate;
  173. }
  174. };
  175. class LoopingSoundTemplateFactory : public IFunction
  176. {
  177. TRef<Modeler> m_pmodeler;
  178. public:
  179. LoopingSoundTemplateFactory(Modeler* pmodeler) :
  180. m_pmodeler(pmodeler)
  181. {
  182. }
  183. public:
  184. TRef<IObject> Apply(ObjectStack& stack)
  185. {
  186. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  187. TRef<ISoundTemplate> pTemplate;
  188. ZSucceeded(CreateLoopingSoundTemplate(pTemplate, ptemplateBase));
  189. return pTemplate;
  190. }
  191. };
  192. class ASRSoundTemplateFactory : public IFunction
  193. {
  194. TRef<Modeler> m_pmodeler;
  195. public:
  196. ASRSoundTemplateFactory(Modeler* pmodeler) :
  197. m_pmodeler(pmodeler)
  198. {
  199. }
  200. public:
  201. TRef<IObject> Apply(ObjectStack& stack)
  202. {
  203. TRef<Number> pnumLoopStart; CastTo(pnumLoopStart, (IObject*)stack.Pop());
  204. TRef<Number> pnumLoopLength; CastTo(pnumLoopLength, (IObject*)stack.Pop());
  205. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  206. TRef<ISoundTemplate> pTemplate;
  207. ZSucceeded(CreateASRSoundTemplate(pTemplate, ptemplateBase,
  208. pnumLoopStart->GetValue(), pnumLoopLength->GetValue()));
  209. return pTemplate;
  210. }
  211. };
  212. class PairedSoundTemplateFactory : public IFunction
  213. {
  214. TRef<Modeler> m_pmodeler;
  215. public:
  216. PairedSoundTemplateFactory(Modeler* pmodeler) :
  217. m_pmodeler(pmodeler)
  218. {
  219. }
  220. public:
  221. TRef<IObject> Apply(ObjectStack& stack)
  222. {
  223. TRef<ISoundTemplate> ptemplateBase1; CastTo(ptemplateBase1, (IObject*)stack.Pop());
  224. TRef<ISoundTemplate> ptemplateBase2; CastTo(ptemplateBase2, (IObject*)stack.Pop());
  225. TRef<ISoundTemplate> pTemplate;
  226. ZSucceeded(CreatePairedSoundTemplate(pTemplate, ptemplateBase1, ptemplateBase2));
  227. return pTemplate;
  228. }
  229. };
  230. class RepeatingFireSoundTemplateFactory : public IFunction
  231. {
  232. TRef<Modeler> m_pmodeler;
  233. public:
  234. RepeatingFireSoundTemplateFactory(Modeler* pmodeler) :
  235. m_pmodeler(pmodeler)
  236. {
  237. }
  238. public:
  239. TRef<IObject> Apply(ObjectStack& stack)
  240. {
  241. TRef<Number> pnumRepeatRate; CastTo(pnumRepeatRate, (IObject*)stack.Pop());
  242. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  243. TRef<ISoundTemplate> pTemplate;
  244. ZSucceeded(CreateRepeatingFireSoundTemplate(pTemplate, ptemplateBase, pnumRepeatRate->GetValue()));
  245. return pTemplate;
  246. }
  247. };
  248. class RandomSoundTemplateFactory : public IFunction
  249. {
  250. TRef<Modeler> m_pmodeler;
  251. public:
  252. RandomSoundTemplateFactory(Modeler* pmodeler) :
  253. m_pmodeler(pmodeler)
  254. {
  255. }
  256. public:
  257. TRef<IObject> Apply(ObjectStack& stack)
  258. {
  259. TRef<IObjectList> plist; CastTo(plist, (IObject*)stack.Pop());
  260. TRef<IRandomSoundTemplate> pTemplate;
  261. ZSucceeded(CreateRandomSoundTemplate(pTemplate));
  262. plist->GetFirst();
  263. while (plist->GetCurrent() != NULL) {
  264. IObjectPair* ppair; CastTo(ppair, plist->GetCurrent());
  265. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, ppair->GetFirst() );
  266. TRef<Number> pnumWeight; CastTo(pnumWeight, ppair->GetSecond() );
  267. ZSucceeded(pTemplate->AddSoundTemplate(ptemplateBase, pnumWeight->GetValue()));
  268. plist->GetNext();
  269. }
  270. return pTemplate;
  271. }
  272. };
  273. class IntermittentSoundTemplateFactory : public IFunction
  274. {
  275. TRef<Modeler> m_pmodeler;
  276. public:
  277. IntermittentSoundTemplateFactory(Modeler* pmodeler) :
  278. m_pmodeler(pmodeler)
  279. {
  280. }
  281. public:
  282. TRef<IObject> Apply(ObjectStack& stack)
  283. {
  284. TRef<Number> pnumPeriod; CastTo(pnumPeriod, (IObject*)stack.Pop());
  285. TRef<Boolean> pboolPlayMultiple; CastTo(pboolPlayMultiple, (IObject*)stack.Pop());
  286. TRef<ISoundTemplate> ptemplateBase; CastTo(ptemplateBase, (IObject*)stack.Pop());
  287. TRef<ISoundTemplate> pTemplate;
  288. ZSucceeded(CreateIntermittentSoundTemplate(pTemplate, ptemplateBase,
  289. pnumPeriod->GetValue(), pboolPlayMultiple->GetValue()));
  290. return pTemplate;
  291. }
  292. };
  293. void AddSoundFactories(
  294. INameSpace* pns,
  295. Modeler* pmodeler
  296. )
  297. {
  298. pns->AddMember("ImportWave", new WaveTemplateFactory(pmodeler));
  299. pns->AddMember("ThreeDSound", new ThreeDSoundTemplateFactory(pmodeler));
  300. pns->AddMember("ConeSound", new ConeSoundTemplateFactory(pmodeler));
  301. pns->AddMember("PitchSound", new PitchSoundTemplateFactory(pmodeler));
  302. pns->AddMember("GainSound", new GainSoundTemplateFactory(pmodeler));
  303. pns->AddMember("PrioritySound", new PrioritySoundTemplateFactory(pmodeler));
  304. pns->AddMember("LoopingSound", new LoopingSoundTemplateFactory(pmodeler));
  305. pns->AddMember("ASRSound", new ASRSoundTemplateFactory(pmodeler));
  306. pns->AddMember("PairedSound", new PairedSoundTemplateFactory(pmodeler));
  307. pns->AddMember("RepeatingFireSound", new RepeatingFireSoundTemplateFactory(pmodeler));
  308. pns->AddMember("RandomSound", new RandomSoundTemplateFactory(pmodeler));
  309. pns->AddMember("IntermittentSound", new IntermittentSoundTemplateFactory(pmodeler));
  310. }