AudIAudioOut.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using ChocolArm64.Memory;
  2. using Ryujinx.OsHle.Handles;
  3. using Ryujinx.OsHle.Ipc;
  4. using static Ryujinx.OsHle.Objects.ObjHelper;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using OpenTK.Audio;
  9. using OpenTK.Audio.OpenAL;
  10. namespace Ryujinx.OsHle.Objects
  11. {
  12. class AudIAudioOut
  13. {
  14. enum AudioOutState
  15. {
  16. Started,
  17. Stopped
  18. };
  19. //IAudioOut
  20. private static AudioOutState State = AudioOutState.Stopped;
  21. private static Queue<long> KeysQueue = new Queue<long>();
  22. //OpenAL
  23. private static bool OpenALInstalled = true;
  24. private static AudioContext AudioCtx;
  25. private static int Source;
  26. private static int Buffer;
  27. //Return State of IAudioOut
  28. public static long GetAudioOutState(ServiceCtx Context)
  29. {
  30. Context.ResponseData.Write((int)State);
  31. return 0;
  32. }
  33. public static long StartAudioOut(ServiceCtx Context)
  34. {
  35. if (State == AudioOutState.Stopped)
  36. {
  37. State = AudioOutState.Started;
  38. try
  39. {
  40. AudioCtx = new AudioContext(); //Create the audio context
  41. }
  42. catch (Exception)
  43. {
  44. Logging.Warn("OpenAL Error! PS: Install OpenAL Core SDK!");
  45. OpenALInstalled = false;
  46. }
  47. if (OpenALInstalled) AL.Listener(ALListenerf.Gain, (float)8.0); //Add more gain to it
  48. }
  49. return 0;
  50. }
  51. public static long StopAudioOut(ServiceCtx Context)
  52. {
  53. if (State == AudioOutState.Started)
  54. {
  55. if (OpenALInstalled)
  56. {
  57. if (AudioCtx == null) //Needed to call the instance of AudioContext()
  58. return 0;
  59. AL.SourceStop(Source);
  60. AL.DeleteSource(Source);
  61. }
  62. State = AudioOutState.Stopped;
  63. }
  64. return 0;
  65. }
  66. public static long AppendAudioOutBuffer(ServiceCtx Context)
  67. {
  68. long BufferId = Context.RequestData.ReadInt64();
  69. KeysQueue.Enqueue(BufferId);
  70. byte[] AudioOutBuffer = AMemoryHelper.ReadBytes(Context.Memory, Context.Request.SendBuff[0].Position, 0x28);
  71. using (MemoryStream MS = new MemoryStream(AudioOutBuffer))
  72. {
  73. BinaryReader Reader = new BinaryReader(MS);
  74. long PointerToSampleDataPointer = Reader.ReadInt64();
  75. long PointerToSampleData = Reader.ReadInt64();
  76. long CapacitySampleBuffer = Reader.ReadInt64();
  77. long SizeDataSampleBuffer = Reader.ReadInt64();
  78. long Unknown = Reader.ReadInt64();
  79. byte[] AudioSampleBuffer = AMemoryHelper.ReadBytes(Context.Memory, PointerToSampleData, (int)SizeDataSampleBuffer);
  80. if (OpenALInstalled)
  81. {
  82. if (AudioCtx == null) //Needed to call the instance of AudioContext()
  83. return 0;
  84. Buffer = AL.GenBuffer();
  85. AL.BufferData(Buffer, ALFormat.Stereo16, AudioSampleBuffer, AudioSampleBuffer.Length, 48000);
  86. Source = AL.GenSource();
  87. AL.SourceQueueBuffer(Source, Buffer);
  88. }
  89. }
  90. return 0;
  91. }
  92. public static long RegisterBufferEvent(ServiceCtx Context)
  93. {
  94. int Handle = Context.Ns.Os.Handles.GenerateId(new HEvent());
  95. Context.Response.HandleDesc = IpcHandleDesc.MakeCopy(Handle);
  96. return 0;
  97. }
  98. public static long GetReleasedAudioOutBuffer(ServiceCtx Context)
  99. {
  100. long TempKey = 0;
  101. if (KeysQueue.Count > 0) TempKey = KeysQueue.Dequeue();
  102. AMemoryHelper.WriteBytes(Context.Memory, Context.Request.ReceiveBuff[0].Position, BitConverter.GetBytes(TempKey));
  103. Context.ResponseData.Write((int)TempKey);
  104. if (OpenALInstalled)
  105. {
  106. if (AudioCtx == null) //Needed to call the instance of AudioContext()
  107. return 0;
  108. AL.SourcePlay(Source);
  109. int[] FreeBuffers = AL.SourceUnqueueBuffers(Source, 1);
  110. AL.DeleteBuffers(FreeBuffers);
  111. }
  112. return 0;
  113. }
  114. public static long ContainsAudioOutBuffer(ServiceCtx Context)
  115. {
  116. return 0;
  117. }
  118. public static long AppendAudioOutBuffer_ex(ServiceCtx Context)
  119. {
  120. return 0;
  121. }
  122. public static long GetReleasedAudioOutBuffer_ex(ServiceCtx Context)
  123. {
  124. return 0;
  125. }
  126. }
  127. }