PageRenderTime 69ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/CSPspEmu.Hle.Modules/sc_sascore/sceSasCore.cs

http://github.com/soywiz/cspspemu
C# | 355 lines | 194 code | 34 blank | 127 comment | 16 complexity | 4e714891716524a74b0d17d48ecf8276 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CSPspEmu.Hle.Attributes;
  6. using CSPspEmu.Core.Audio;
  7. namespace CSPspEmu.Hle.Modules.sc_sascore
  8. {
  9. [HlePspModule(ModuleFlags = ModuleFlags.UserMode | ModuleFlags.Flags0x00010011)]
  10. unsafe public partial class sceSasCore : HleModuleHost
  11. {
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. /// <param name="SasCore"></param>
  16. /// <returns></returns>
  17. [HlePspFunction(NID = 0xBD11B7C2, FirmwareVersion = 150)]
  18. public int __sceSasGetGrain(uint SasCorePointer)
  19. {
  20. var SasCore = GetSasCore(SasCorePointer);
  21. return SasCore.GrainSamples;
  22. }
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. /// <param name="SasCore"></param>
  27. /// <param name="Grain"></param>
  28. /// <returns></returns>
  29. [HlePspFunction(NID = 0xD1E0A01E, FirmwareVersion = 150)]
  30. public int __sceSasSetGrain(uint SasCorePointer, int Grain)
  31. {
  32. var SasCore = GetSasCore(SasCorePointer);
  33. try
  34. {
  35. return 0;
  36. //return SasCore.GrainSamples;
  37. }
  38. finally
  39. {
  40. SasCore.GrainSamples = Grain;
  41. }
  42. }
  43. /// <summary>
  44. ///
  45. /// </summary>
  46. /// <param name="SasCore"></param>
  47. /// <returns></returns>
  48. [HlePspFunction(NID = 0xE175EF66, FirmwareVersion = 150)]
  49. public OutputMode __sceSasGetOutputmode(uint SasCorePointer)
  50. {
  51. var SasCore = GetSasCore(SasCorePointer);
  52. //throw(new NotImplementedException());
  53. return SasCore.OutputMode;
  54. }
  55. /// <summary>
  56. ///
  57. /// </summary>
  58. /// <param name="SasCore"></param>
  59. /// <param name="OutputMode"></param>
  60. /// <returns></returns>
  61. [HlePspFunction(NID = 0xE855BF76, FirmwareVersion = 150)]
  62. public int __sceSasSetOutputmode(uint SasCorePointer, OutputMode OutputMode)
  63. {
  64. var SasCore = GetSasCore(SasCorePointer);
  65. //throw(new NotImplementedException());
  66. try
  67. {
  68. return 0;
  69. //return SasCore.OutputMode;
  70. }
  71. finally
  72. {
  73. SasCore.OutputMode = OutputMode;
  74. }
  75. }
  76. protected int[] VoiceOnCount;
  77. protected StereoIntSoundSample[] BufferTemp;
  78. protected StereoShortSoundSample[] BufferShort;
  79. protected StereoShortSoundSample[] MixBufferShort;
  80. /// <summary>
  81. /// Initialized a sasCore structure.
  82. /// </summary>
  83. /// <remarks>
  84. /// PSP can only handle one at a time.
  85. /// </remarks>
  86. /// <example>
  87. /// __sceSasInit(&sasCore, PSP_SAS_GRAIN_SAMPLES, PSP_SAS_VOICES_MAX, OutputMode.PSP_SAS_OUTPUTMODE_STEREO, 44100);
  88. /// </example>
  89. /// <param name="sasCore">Pointer to a SasCore structure that will contain information.</param>
  90. /// <param name="grainSamples">Number of grainSamples</param>
  91. /// <param name="maxVoices">Max number of voices</param>
  92. /// <param name="outputMode">Out Mode</param>
  93. /// <param name="sampleRate">Sample Rate</param>
  94. /// <returns>0 on success</returns>
  95. [HlePspFunction(NID = 0x42778A9F, FirmwareVersion = 150)]
  96. //[HlePspNotImplemented]
  97. public uint __sceSasInit(uint SasCorePointer, int GrainSamples, int MaxVoices, OutputMode OutputMode, int SampleRate)
  98. {
  99. if (SampleRate != 44100) throw (new NotImplementedException("(SampleRate != 44100)"));
  100. if (MaxVoices != 32) throw (new NotImplementedException("(MaxVoices != 32)"));
  101. //if (MaxVoices != 32) throw (new NotImplementedException("(MaxVoices != 32)"));
  102. var SasCore = GetSasCore(SasCorePointer, CreateIfNotExists: true);
  103. {
  104. SasCore.Initialized = true;
  105. SasCore.GrainSamples = GrainSamples;
  106. SasCore.MaxVoices = MaxVoices;
  107. SasCore.OutputMode = OutputMode;
  108. SasCore.SampleRate = SampleRate;
  109. }
  110. VoiceOnCount = new int[SasCore.GrainSamples];
  111. BufferTemp = new StereoIntSoundSample[SasCore.GrainSamples];
  112. BufferShort = new StereoShortSoundSample[SasCore.GrainSamples];
  113. MixBufferShort = new StereoShortSoundSample[SasCore.GrainSamples];
  114. return 0;
  115. }
  116. /// <summary>
  117. /// Return a bitfield indicating the end of the voices.
  118. /// </summary>
  119. /// <param name="SasCore">Core</param>
  120. /// <returns>A set of flags indiciating the end of the voices.</returns>
  121. [HlePspFunction(NID = 0x68A46B95, FirmwareVersion = 150)]
  122. public uint __sceSasGetEndFlag(uint SasCorePointer)
  123. {
  124. var SasCore = GetSasCore(SasCorePointer);
  125. return SasCore.EndFlags;
  126. }
  127. /// <summary>
  128. /// Sets the WaveformEffectType to the specified sasCore.
  129. /// </summary>
  130. /// <param name="SasCore">Core</param>
  131. /// <param name="WaveformEffectType">Effect</param>
  132. /// <returns>0 on success.</returns>
  133. [HlePspFunction(NID = 0x33D4AB37, FirmwareVersion = 150)]
  134. public uint __sceSasRevType(uint SasCorePointer, WaveformEffectType WaveformEffectType)
  135. {
  136. var SasCore = GetSasCore(SasCorePointer);
  137. SasCore.WaveformEffectType = WaveformEffectType;
  138. return 0;
  139. }
  140. /// <summary>
  141. /// Sets the waveformEffectIsDry and waveformEffectIsWet to the specified sasCore.
  142. /// </summary>
  143. /// <param name="SasCore">Core</param>
  144. /// <param name="WaveformEffectIsDry">waveformEffectIsDry</param>
  145. /// <param name="WaveformEffectIsWet">waveformEffectIsWet</param>
  146. /// <returns>0 on success.</returns>
  147. [HlePspFunction(NID = 0xF983B186, FirmwareVersion = 150)]
  148. public uint __sceSasRevVON(uint SasCorePointer, bool WaveformEffectIsDry, bool WaveformEffectIsWet)
  149. {
  150. var SasCore = GetSasCore(SasCorePointer);
  151. SasCore.WaveformEffectIsDry = WaveformEffectIsDry;
  152. SasCore.WaveformEffectIsWet = WaveformEffectIsWet;
  153. return 0;
  154. }
  155. /// <summary>
  156. /// Sets the effect left and right volumes for the specified sasCore.
  157. /// </summary>
  158. /// <param name="SasCore">Core</param>
  159. /// <param name="LeftVolume">Left volume</param>
  160. /// <param name="RightVolume">Right volume</param>
  161. /// <returns>0 on success</returns>
  162. [HlePspFunction(NID = 0xD5A229C9, FirmwareVersion = 150)]
  163. public uint __sceSasRevEVOL(uint SasCorePointer, int LeftVolume, int RightVolume)
  164. {
  165. var SasCore = GetSasCore(SasCorePointer);
  166. SasCore.LeftVolume = LeftVolume;
  167. SasCore.RightVolume = RightVolume;
  168. //throw(new NotImplementedException());
  169. return 0;
  170. }
  171. /// <summary>
  172. ///
  173. /// </summary>
  174. /// <param name="SasCore"></param>
  175. /// <param name="Delay"></param>
  176. /// <param name="Feedback"></param>
  177. /// <returns></returns>
  178. [HlePspFunction(NID = 0x267A6DD2, FirmwareVersion = 150)]
  179. public int __sceSasRevParam(uint SasCorePointer, int Delay, int Feedback)
  180. {
  181. var SasCore = GetSasCore(SasCorePointer);
  182. SasCore.Delay = Delay;
  183. SasCore.Feedback = Feedback;
  184. //throw(new NotImplementedException());
  185. return 0;
  186. }
  187. /// <summary>
  188. /// Pauses a set of voice channels for that sasCore.
  189. /// </summary>
  190. /// <param name="sasCore">SasCore</param>
  191. /// <param name="voice_bits">Voice Bit Set</param>
  192. /// <returns>0 on success.</returns>
  193. [HlePspFunction(NID = 0x787D04D5, FirmwareVersion = 150)]
  194. [HlePspNotImplemented]
  195. public int __sceSasSetPause(uint SasCorePointer, uint voice_bits)
  196. {
  197. //throw(new NotImplementedException());
  198. return 0;
  199. }
  200. /// <summary>
  201. ///
  202. /// </summary>
  203. /// <param name="sasCore"></param>
  204. /// <returns></returns>
  205. [HlePspFunction(NID = 0x2C8E6AB3, FirmwareVersion = 150)]
  206. //[HlePspNotImplemented]
  207. public int __sceSasGetPauseFlag(uint SasCorePointer)
  208. {
  209. //throw(new NotImplementedException());
  210. return 0;
  211. }
  212. /// <summary>
  213. /// Process the voices and generate the next samples.
  214. /// Mix the resulting samples in an exiting buffer.
  215. /// </summary>
  216. /// <param name="SasCore">sasCore handle</param>
  217. /// <param name="SasInOut">
  218. /// address for the input and output buffer.
  219. /// Samples are stored as 2 16-bit values
  220. /// (left then right channel samples)
  221. /// </param>
  222. /// <param name="LeftVolume">Left channel volume, [0..0x1000].</param>
  223. /// <param name="RightVolume">Right channel volume, [0..0x1000].</param>
  224. /// <returns>
  225. /// if OK 0
  226. /// ERROR_SAS_NOT_INIT if an invalid sasCore handle is provided
  227. /// </returns>
  228. [HlePspFunction(NID = 0x50A14DFC, FirmwareVersion = 150)]
  229. //[HlePspNotImplemented]
  230. public int __sceSasCoreWithMix(uint SasCorePointer, StereoShortSoundSample* SasInOut, int LeftVolume, int RightVolume)
  231. {
  232. #if false
  233. var SasCore = GetSasCore(SasCorePointer);
  234. int NumberOfChannels = SasCore.OutputMode == OutputMode.PSP_SAS_OUTPUTMODE_STEREO ? 2 : 1;
  235. int NumberOfSamples = SasCore.GrainSamples * NumberOfChannels;
  236. fixed (short* FixedMixBufferShort = MixBufferShort)
  237. {
  238. __sceSasCore(SasCorePointer, FixedMixBufferShort);
  239. }
  240. int MaxVolume = 0x1000;
  241. int LeftVolumeComp = MaxVolume - LeftVolume;
  242. int RightVolumeComp = MaxVolume - RightVolume;
  243. for (int n = 0; n < NumberOfSamples; n += 2)
  244. {
  245. SasInOut[n + 0] = (short)(((int)SasInOut[n + 0] * LeftVolumeComp + (int)MixBufferShort[n + 0] * LeftVolume) * short.MaxValue / MaxVolume);
  246. SasInOut[n + 1] = (short)(((int)SasInOut[n + 1] * RightVolumeComp + (int)MixBufferShort[n + 1] * RightVolume) * short.MaxValue / MaxVolume);
  247. }
  248. //throw (new NotImplementedException());
  249. return 0;
  250. #else
  251. return __sceSasCore(SasCorePointer, SasInOut);
  252. #endif
  253. }
  254. /// <summary>
  255. /// Process the voices and generate the next samples.
  256. /// </summary>
  257. /// <param name="SasCore">sasCore handle</param>
  258. /// <param name="SasOut">
  259. /// address for the output buffer.
  260. /// Samples are stored as 2 16-bit values
  261. /// (left then right channel samples)
  262. /// </param>
  263. /// <returns>
  264. /// if OK 0
  265. /// ERROR_SAS_NOT_INIT if an invalid sasCore handle is provided
  266. /// </returns>
  267. [HlePspFunction(NID = 0xA3589D81, FirmwareVersion = 150)]
  268. //[HlePspNotImplemented]
  269. public int __sceSasCore(uint SasCorePointer, StereoShortSoundSample* SasOut)
  270. {
  271. var SasCore = GetSasCore(SasCorePointer);
  272. if (SasCore.OutputMode != OutputMode.PSP_SAS_OUTPUTMODE_STEREO) throw (new NotImplementedException());
  273. int NumberOfChannels = SasCore.OutputMode == OutputMode.PSP_SAS_OUTPUTMODE_STEREO ? 2 : 1;
  274. int NumberOfSamples = SasCore.GrainSamples;
  275. for (int n = 0; n < NumberOfSamples; n++)
  276. {
  277. BufferTemp[n] = default(StereoIntSoundSample);
  278. VoiceOnCount[n] = 0;
  279. }
  280. // Read and mix voices.
  281. foreach (var Voice in SasCore.Voices)
  282. {
  283. if (Voice.OnAndPlaying)
  284. {
  285. for (int n = 0; n < NumberOfSamples; n++)
  286. {
  287. if (Voice.SampleOffset < Voice.Vag.SamplesCount)
  288. {
  289. VoiceOnCount[n]++;
  290. BufferTemp[n] += Voice.Vag.GetSampleAt(Voice.SampleOffset++);
  291. }
  292. else
  293. {
  294. Voice.SetPlaying(false);
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. // Normalize output
  301. for (int n = 0; n < NumberOfSamples; n++)
  302. {
  303. if (VoiceOnCount[n] > 0)
  304. {
  305. BufferShort[n] = (BufferTemp[n] / VoiceOnCount[n]);
  306. }
  307. else
  308. {
  309. BufferShort[n] = default(StereoShortSoundSample);
  310. }
  311. }
  312. // Output converted 44100 data
  313. for (int n = 0; n < NumberOfSamples; n++)
  314. {
  315. SasOut[n] = BufferShort[n];
  316. }
  317. //throw(new NotImplementedException());
  318. return 0;
  319. }
  320. }
  321. }