aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Audio.Backends.SoundIo/Native/SoundIoOutStreamContext.cs
blob: 2e432b311997bd5734e5b4ee9c33c531dfc6ab3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static Ryujinx.Audio.Backends.SoundIo.Native.SoundIo;

namespace Ryujinx.Audio.Backends.SoundIo.Native
{
    public class SoundIoOutStreamContext : IDisposable
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private unsafe delegate void WriteCallbackDelegate(IntPtr ctx, int frameCountMin, int frameCountMax);

        private IntPtr _context;
        private IntPtr _nameStored;
        private Action<int, int> _writeCallback;
        private WriteCallbackDelegate _writeCallbackNative;

        public IntPtr Context => _context;

        internal SoundIoOutStreamContext(IntPtr context)
        {
            _context = context;
            _nameStored = IntPtr.Zero;
            _writeCallback = null;
            _writeCallbackNative = null;
        }

        private ref SoundIoOutStream GetOutContext()
        {
            unsafe
            {
                return ref Unsafe.AsRef<SoundIoOutStream>((SoundIoOutStream*)_context);
            }
        }

        public string Name
        {
            get => Marshal.PtrToStringAnsi(GetOutContext().Name);
            set
            {
                var context = GetOutContext();

                if (_nameStored != IntPtr.Zero && context.Name == _nameStored)
                {
                    Marshal.FreeHGlobal(_nameStored);
                }

                _nameStored = Marshal.StringToHGlobalAnsi(value);
                GetOutContext().Name = _nameStored;
            }
        }

        public SoundIoChannelLayout Layout
        {
            get => GetOutContext().Layout;
            set => GetOutContext().Layout = value;
        }

        public SoundIoFormat Format
        {
            get => GetOutContext().Format;
            set => GetOutContext().Format = value;
        }

        public int SampleRate
        {
            get => GetOutContext().SampleRate;
            set => GetOutContext().SampleRate = value;
        }

        public float Volume
        {
            get => GetOutContext().Volume;
            set => GetOutContext().Volume = value;
        }

        public int BytesPerFrame
        {
            get => GetOutContext().BytesPerFrame;
            set => GetOutContext().BytesPerFrame = value;
        }

        public int BytesPerSample
        {
            get => GetOutContext().BytesPerSample;
            set => GetOutContext().BytesPerSample = value;
        }

        public Action<int, int> WriteCallback
        {
            get { return _writeCallback; }
            set
            {
                _writeCallback = value;

                if (_writeCallback == null)
                {
                    _writeCallbackNative = null;
                }
                else
                {
                    _writeCallbackNative = (ctx, frameCountMin, frameCountMax) => _writeCallback(frameCountMin, frameCountMax);
                }

                GetOutContext().WriteCallback = Marshal.GetFunctionPointerForDelegate(_writeCallbackNative);
            }
        }

        private static void CheckError(SoundIoError error)
        {
            if (error != SoundIoError.None)
            {
                throw new SoundIoException(error);
            }
        }

        public void Open() => CheckError(soundio_outstream_open(_context));

        public void Start() => CheckError(soundio_outstream_start(_context));

        public void Pause(bool pause) => CheckError(soundio_outstream_pause(_context, pause));

        public void SetVolume(double volume) => CheckError(soundio_outstream_set_volume(_context, volume));

        public Span<SoundIoChannelArea> BeginWrite(ref int frameCount)
        {
            IntPtr arenas = default;
            int nativeFrameCount = frameCount;

            unsafe
            {
                var frameCountPtr = &nativeFrameCount;
                var arenasPtr = &arenas;
                CheckError(soundio_outstream_begin_write(_context, (IntPtr)arenasPtr, (IntPtr)frameCountPtr));

                frameCount = *frameCountPtr;

                return new Span<SoundIoChannelArea>((void*)arenas, Layout.ChannelCount);
            }
        }

        public void EndWrite() => CheckError(soundio_outstream_end_write(_context));

        protected virtual void Dispose(bool disposing)
        {
            if (_context != IntPtr.Zero)
            {
                soundio_outstream_destroy(_context);
                _context = IntPtr.Zero;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~SoundIoOutStreamContext()
        {
            Dispose(false);
        }
    }
}