IAudioOut.cs 5.5 KB

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