Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toolkit interfaces #8

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/LabelVoice.Toolkit/LabelVoice.Toolkit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NAudio.Core" Version="2.1.0" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/LabelVoice.Toolkit/Slicer/IAdaptiveAudioSlicer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using NAudio.Wave;

namespace LabelVoice.Toolkit.Slicer;

/// <summary>
/// An adaptive audio slicer slices audios with no knowledge or supervision other than the signal itself.
/// </summary>
public interface IAdaptiveAudioSlicer : IAudioSlicer
{
/// <summary>
/// Initialize the audio slicer with signal provided by <paramref name="provider"/>.
/// </summary>
void Init(ISampleProvider provider);
}
28 changes: 28 additions & 0 deletions src/LabelVoice.Toolkit/Slicer/IAudioSlicer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NAudio.Wave;

namespace LabelVoice.Toolkit.Slicer;

/// <summary>
/// An audio slicer slices audios into multiple pieces in a streaming way.
/// </summary>
public interface IAudioSlicer
{
/// <summary>
/// Try to get the next piece.
/// </summary>
/// <param name="range">When this method returns, contains the range of next piece in the given signal, or <code>null</code> if there are no more pieces to be sliced.</param>
/// <returns><see langword="true"/> if the next valid piece is sliced, otherwise <see langword="false"/>.</returns>
public bool TrySlice(out AudioRange? range);
}

public readonly struct AudioRange
{
public readonly double In;
public readonly double Out;

public AudioRange(double inPoint, double outPoint)
{
In = inPoint;
Out = outPoint;
}
}
17 changes: 17 additions & 0 deletions src/LabelVoice.Toolkit/Slicer/ISupervisedAudioSlicer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NAudio.Wave;

namespace LabelVoice.Toolkit.Slicer;

/// <summary>
/// A supervised audio slicer slices audios with given knowledge or supervision such as transcriptions or subtitles.
/// </summary>
/// <typeparam name="T">The type of the instance carrying the supervision.</typeparam>
public interface ISupervisedAudioSlicer<in T> : IAudioSlicer
{
/// <summary>
/// Initialize the audio slicer with signal provided by <paramref name="provider"/> and knowledge carried by <paramref name="supervision"/>.
/// </summary>
/// <param name="provider"></param>
/// <param name="supervision"></param>
public void Init(ISampleProvider provider, T supervision);
}
39 changes: 39 additions & 0 deletions src/LabelVoice.Toolkit/Slicer/MonoAudioSlicerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace LabelVoice.Toolkit.Slicer;

/// <summary>
/// This abstract class represents audio slicers that slices mono audio signals and provides property and conversion to ensure the given signal is mono.
/// Any class that inherits this class gets the `Provider` property whose setter converts stereo providers into mono providers. Thus, the provider got from this property is guaranteed to be mono.<br/>
/// Example:
/// <code>
/// public void Init(ISampleProvider provider)
/// {
/// Provider = provider;
/// // Once assigned, the `Provider` property is guaranteed to provide mono signals.
/// }
/// </code>
/// </summary>
public abstract class MonoAudioSlicerBase : IAudioSlicer
{
private ISampleProvider _provider;

/// <summary>
/// Represents the source provider which is guaranteed to provide mono signals.
/// </summary>
protected ISampleProvider Provider
{
get => _provider;
set => _provider = RequireMono(value);
}

private static ISampleProvider RequireMono(ISampleProvider provider)
{
return provider.WaveFormat.Channels >= 2
? new StereoToMonoSampleProvider(provider)
: provider;
}

public abstract bool TrySlice(out AudioRange? range);
}
19 changes: 19 additions & 0 deletions src/LabelVoice.Toolkit/Slicer/SilenceDetectionSlicer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NAudio.Wave;

namespace LabelVoice.Toolkit.Slicer;

/// <summary>
/// A silence detection slicer slices audios via silence detection, i.e. cuts off audios from detected silence parts.
/// </summary>
public class SilenceDetectionSlicer : MonoAudioSlicerBase, IAdaptiveAudioSlicer
{
public void Init(ISampleProvider provider)
{
Provider = provider;
}

public override bool TrySlice(out AudioRange? range)
{
throw new NotImplementedException();
}
}
8 changes: 7 additions & 1 deletion src/LabelVoice.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ VisualStudioVersion = 17.4.32916.344
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LabelVoice", "LabelVoice\LabelVoice.csproj", "{C7639302-E10B-4FBC-AD48-A645E4403C67}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LabelVoice.Core", "LabelVoice.Core\LabelVoice.Core.csproj", "{340ADDD3-5B13-4328-A836-34A87EC8DEC5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LabelVoice.Core", "LabelVoice.Core\LabelVoice.Core.csproj", "{340ADDD3-5B13-4328-A836-34A87EC8DEC5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Playback", "Test.Playback\Test.Playback.csproj", "{EFE88D45-1EFD-4E3A-879F-6A03ECFECCDB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LabelVoice.Toolkit", "LabelVoice.Toolkit\LabelVoice.Toolkit.csproj", "{09B69A2A-6F91-4CA6-8629-87BE08E26149}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{EFE88D45-1EFD-4E3A-879F-6A03ECFECCDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFE88D45-1EFD-4E3A-879F-6A03ECFECCDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFE88D45-1EFD-4E3A-879F-6A03ECFECCDB}.Release|Any CPU.Build.0 = Release|Any CPU
{09B69A2A-6F91-4CA6-8629-87BE08E26149}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09B69A2A-6F91-4CA6-8629-87BE08E26149}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09B69A2A-6F91-4CA6-8629-87BE08E26149}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09B69A2A-6F91-4CA6-8629-87BE08E26149}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down