Skip to content

Commit

Permalink
Add support for events and process
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 3, 2022
1 parent 37ad1b0 commit f975cc1
Show file tree
Hide file tree
Showing 12 changed files with 769 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/NPlug.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="https://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MIDICC/@EntryIndexedValue">MIDICC</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=MIDICC/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=sidechain/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unkwown/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Vtbl/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
6 changes: 2 additions & 4 deletions src/NPlug/AudioBusData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ namespace NPlug;

public ref struct AudioBusData
{
public int Count => Buffers.Length;

public Span<AudioBusBuffers> Buffers;

public AudioParameterChanges? ParameterChanges;
public AudioParameterChanges ParameterChanges;

public AudioEventList? Events;
public AudioEventList Events;
}
101 changes: 101 additions & 0 deletions src/NPlug/AudioEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System.Runtime.InteropServices;

namespace NPlug;

public struct AudioEvent
{
/// <summary>
/// event bus index
/// </summary>
public int BusIndex;

/// <summary>
/// sample frames related to the current block start sample position
/// </summary>
public int SampleOffset;

/// <summary>
/// position in project
/// </summary>
public double Position;

/// <summary>
/// combination of @ref EventFlags
/// </summary>
public AudioEventFlags Flags;

/// <summary>
/// a value from @ref EventTypes
/// </summary>
public AudioEventKind Kind;

/// <summary>
/// The value of this event.
/// </summary>
public AudioEventValue Value;

/// <summary>
/// The value of an <see cref="AudioEvent"/> that depends on the <see cref="AudioEvent.Kind"/>
/// </summary>
[StructLayout(LayoutKind.Explicit)]
public struct AudioEventValue
{
/// <summary>
/// type == kNoteOnEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.NoteOnEvent NoteOn;

/// <summary>
/// type == kNoteOffEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.NoteOffEvent NoteOff;

/// <summary>
/// type == kDataEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.DataEvent Data;

/// <summary>
/// type == kPolyPressureEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.PolyPressureEvent PolyPressure;

/// <summary>
/// type == kNoteExpressionValueEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.NoteExpressionValueEvent NoteExpressionValue;

/// <summary>
/// type == kNoteExpressionTextEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.NoteExpressionTextEvent NoteExpressionText;

/// <summary>
/// type == kChordEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.ChordEvent Chord;

/// <summary>
/// type == kScaleEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.ScaleEvent Scale;

/// <summary>
/// type == kLegacyMIDICCOutEvent
/// </summary>
[FieldOffset(0)]
public AudioEvents.LegacyMIDICCOutEvent MidiCCOut;
}
}
25 changes: 25 additions & 0 deletions src/NPlug/AudioEventFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

namespace NPlug;

public enum AudioEventFlags : ushort
{
None = 0,

/// <summary>
/// Indicates that the event is played live (directly from keyboard)
/// </summary>
IsLive = 1 << 0,

/// <summary>
/// reserved for user (for internal use)
/// </summary>
UserReserved1 = 1 << 14,

/// <summary>
/// reserved for user (for internal use)
/// </summary>
UserReserved2 = 1 << 15,
}
53 changes: 53 additions & 0 deletions src/NPlug/AudioEventKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

namespace NPlug;

public enum AudioEventKind : ushort
{
/// <summary>
/// is @ref NoteOnEvent
/// </summary>
NoteOn = 0,

/// <summary>
/// is @ref NoteOffEvent
/// </summary>
NoteOff = 1,

/// <summary>
/// is @ref DataEvent
/// </summary>
Data = 2,

/// <summary>
/// is @ref PolyPressureEvent
/// </summary>
PolyPressure = 3,

/// <summary>
/// is @ref NoteExpressionValueEvent
/// </summary>
NoteExpressionValue = 4,

/// <summary>
/// is @ref NoteExpressionTextEvent
/// </summary>
NoteExpressionText = 5,

/// <summary>
/// is @ref ChordEvent
/// </summary>
Chord = 6,

/// <summary>
/// is @ref ScaleEvent
/// </summary>
Scale = 7,

/// <summary>
/// is @ref LegacyMIDICCOutEvent
/// </summary>
LegacyMIDICCOut = 65535,
}
64 changes: 63 additions & 1 deletion src/NPlug/AudioEventList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,71 @@
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;
using System;

namespace NPlug;

public class AudioEventList
public readonly ref struct AudioEventList
{
private readonly IntPtr _nativeContext;
private readonly IAudioEventListBackend? _backend;
internal AudioEventList(IntPtr nativeContext, IAudioEventListBackend? backend)
{
_nativeContext = nativeContext;
_backend = backend;
}

/// <summary>
/// Returns the count of events.
/// </summary>
public int Count
{
get
{
return _nativeContext == IntPtr.Zero ? 0 : _backend!.GetEventCount(_nativeContext);
}
}

/// <summary>
/// Gets parameter by index.
/// </summary>
public bool TryGetEvent(int index, out AudioEvent evt)
{
if (_nativeContext == IntPtr.Zero) ThrowNotInitialized();
return _backend!.TryGetEvent(_nativeContext, index, out evt);
}

/// <summary>
/// Adds a new event.
/// </summary>
public bool TryAddEvent(IntPtr context, in AudioEvent e)
{
if (_nativeContext == IntPtr.Zero) ThrowNotInitialized();
return _backend!.TryAddEvent(_nativeContext, e);
}

[DoesNotReturn]
private static void ThrowNotInitialized()
{
throw new InvalidOperationException("This event list is empty.");
}
}

public interface IAudioEventListBackend
{
/// <summary>
/// Returns the count of events.
/// </summary>
int GetEventCount(IntPtr context);

/// <summary>
/// Gets parameter by index.
/// </summary>
bool TryGetEvent(IntPtr context, int index, out AudioEvent evt);

/// <summary>
/// Adds a new event.
/// </summary>
bool TryAddEvent(IntPtr context, in AudioEvent evt);
}
Loading

0 comments on commit f975cc1

Please sign in to comment.