Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. using UnityEngine;
  2. using Unity.Collections;
  3. using Unity.Audio;
  4. using Unity.Mathematics;
  5. using Unity.Burst;
  6.  
  7. /*
  8. For allocations inside AudioKernels, use Allocator.AudioKernel
  9. */
  10.  
  11. public class DspApplication : MonoBehaviour {
  12. private DSPGraph _graph;
  13. private AudioOutputHandle _outputHandle;
  14. private DSPNode _node;
  15.  
  16. private const int SAMPLERATE = 48000;
  17. private const int NUMFRAMES = 1024;
  18. private const int NUMCHANNELS = 2;
  19.  
  20. private bool _isRunning;
  21.  
  22. private void Awake() {
  23. var audioConfig = AudioSettings.GetConfiguration();
  24. Debug.Log("Unity Audio Config:");
  25. Debug.Log(audioConfig.sampleRate);
  26. Debug.Log(audioConfig.dspBufferSize);
  27. Debug.Log(audioConfig.speakerMode);
  28.  
  29. _graph = DSPGraph.Create(SoundFormat.Stereo, NUMCHANNELS, NUMFRAMES, SAMPLERATE);
  30.  
  31. // Create a driver that just drives the graph mix based on when the system wants samples
  32. var driver = new DefaultDspGraphDriver { _graph = _graph };
  33.  
  34. // Attach this driver and graph to the audio output that's configured in Unity
  35. _outputHandle = driver.AttachToDefaultOutput();
  36.  
  37. var cmd = _graph.CreateCommandBlock();
  38. _node = cmd.CreateDSPNode<OscParams, OscProviders, Osc>();
  39. cmd.AddOutletPort(_node, NUMCHANNELS, SoundFormat.Stereo);
  40. cmd.Connect(_node, 0, _graph.RootDSP, 0);
  41. cmd.SetFloat<OscParams, OscProviders, Osc>(_node, OscParams.Frequency, 440f);
  42. cmd.Complete();
  43. }
  44.  
  45. private void OnDestroy() {
  46. var cmd = _graph.CreateCommandBlock();
  47. cmd.ReleaseDSPNode(_node);
  48. cmd.Complete();
  49.  
  50. _outputHandle.Dispose();
  51. }
  52.  
  53. private void Update() {
  54. var cmd = _graph.CreateCommandBlock();
  55. float pitch = 440f + 220f * Input.GetAxis("Mouse Y");
  56. cmd.SetFloat<OscParams, OscProviders, Osc>(_node, OscParams.Frequency, pitch);
  57. cmd.Complete();
  58. }
  59.  
  60. private const float TWOPI = math.PI * 2f;
  61.  
  62. private enum OscParams {
  63. Frequency
  64. }
  65.  
  66. private enum OscProviders {
  67. Provider
  68. }
  69.  
  70. [BurstCompile]
  71. private struct Osc : IAudioKernel<OscParams, OscProviders> {
  72. private float _phase;
  73. private float _phaseStep;
  74. private float _freq;
  75.  
  76. public void Initialize() {}
  77.  
  78. public void Dispose() {}
  79.  
  80. public void Execute(ref ExecuteContext<OscParams, OscProviders> context) {
  81. for (int outIdx = 0; outIdx < context.Outputs.Count; outIdx++) {
  82. var sampleBuffer = context.Outputs.GetSampleBuffer(outIdx);
  83. var buffer = sampleBuffer.Buffer;
  84. for (int i = 0; i < NUMFRAMES; i++) {
  85. /*
  86. Manually interpolate frequency parameter (have not yet been able to get the
  87. automatic parameter interpolation system to behave yet)
  88. */
  89. _freq = math.lerp(_freq, context.Parameters.GetFloat(OscParams.Frequency, i), 0.001f);
  90.  
  91. _phaseStep = (TWOPI * _freq) / (float)SAMPLERATE;
  92.  
  93. buffer[i*NUMCHANNELS] = math.sin(_phase);
  94. buffer[i*NUMCHANNELS+1] = buffer[i * NUMCHANNELS];
  95. _phase += _phaseStep;
  96. if (_phase > TWOPI) {
  97. _phase -= TWOPI;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104.  
  105. [BurstCompile]
  106. public struct DefaultDspGraphDriver : IAudioOutput {
  107. public DSPGraph _graph;
  108. private int _channelCount;
  109.  
  110. public void Initialize(int channelCount, SoundFormat format, int sampleRate, long dspBufferSize) {
  111. _channelCount = channelCount;
  112. }
  113.  
  114. public void BeginMix(int frameCount) {
  115. _graph.BeginMix(frameCount);
  116. }
  117.  
  118. public void EndMix(NativeArray<float> output, int frames) {
  119. _graph.ReadMix(output, frames, _channelCount);
  120. }
  121.  
  122. public void Dispose() {
  123. _graph.Dispose();
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement