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

升级到 .NET 9 #92

Merged
merged 1 commit into from
Dec 8, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/Console/RodelAudio.Console/RodelAudio.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Console/RodelChat.Console/RodelChat.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Console/RodelDraw.Console/RodelDraw.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RodelAgent.Context/RodelAgent.Context.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RodelAudio.Core/RodelAudio.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RodelChat.Core/RodelChat.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);SKEXP0110</NoWarn>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RodelDraw.Core/RodelDraw.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RodelTranslate.Core/RodelTranslate.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void Initialize()
}

private void OnSessionClick(object sender, RoutedEventArgs e)
=> GetSessionViewModel().LoadSessionCommand.Execute(ViewModel);
=> GetSessionViewModel().LoadSessionCommand.Execute(ViewModel.Data);

private void OnOpenItemClick(object sender, RoutedEventArgs e)
=> GetSessionViewModel().OpenAudioCommand.Execute(AppToolkit.GetSpeechPath(ViewModel.Data.Id));
Expand Down
12 changes: 2 additions & 10 deletions src/Desktop/RodelAgent.UI/Controls/Base/WavePanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ protected override void OnControlLoaded()
{
WaveCanvas.Draw += OnWaveCanvasDraw;
ViewModel.RedrawWave += OnRedrawWave;
if (ViewModel.IsRecording)
{
ViewModel.StopRecordingCommand.Execute(default);
}
else if (ViewModel.IsPlaying)
if (ViewModel.IsPlaying)
{
ViewModel.ResetPositionCommand.Execute(default);
}
Expand All @@ -52,11 +48,7 @@ protected override void OnControlUnloaded()
{
WaveCanvas.Draw -= OnWaveCanvasDraw;
ViewModel.RedrawWave -= OnRedrawWave;
if (ViewModel.IsRecording)
{
ViewModel.StopRecordingCommand.Execute(default);
}
else if (ViewModel.IsPlaying)
if (ViewModel.IsPlaying)
{
ViewModel.TogglePlayPauseCommand.Execute(default);
}
Expand Down
147 changes: 147 additions & 0 deletions src/Desktop/RodelAgent.UI/Extensions/AudioFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// <auto-generated />
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace RodelAgent.UI.Extensions;

/// <summary>
/// AudioFileReader simplifies opening an audio file in NAudio
/// Simply pass in the filename, and it will attempt to open the
/// file and set up a conversion path that turns into PCM IEEE float.
/// ACM codecs will be used for conversion.
/// It provides a volume property and implements both WaveStream and
/// ISampleProvider, making it possibly the only stage in your audio
/// pipeline necessary for simple playback scenarios
/// </summary>
public class AudioFileReader : WaveStream, ISampleProvider
{
private WaveStream readerStream; // the waveStream which we will use for all positioning
private readonly SampleChannel sampleChannel; // sample provider that gives us most stuff we need
private readonly int destBytesPerSample;
private readonly int sourceBytesPerSample;
private readonly long length;
private readonly object lockObject;

/// <summary>
/// Initializes a new instance of AudioFileReader
/// </summary>
/// <param name="fileName">The file to open</param>
public AudioFileReader(string fileName)
{
lockObject = new object();
FileName = fileName;
CreateReaderStream(fileName);
sourceBytesPerSample = (readerStream.WaveFormat.BitsPerSample / 8) * readerStream.WaveFormat.Channels;
sampleChannel = new SampleChannel(readerStream, false);
destBytesPerSample = 4 * sampleChannel.WaveFormat.Channels;
length = SourceToDest(readerStream.Length);
}

/// <summary>
/// Creates the reader stream, supporting all filetypes in the core NAudio library,
/// and ensuring we are in PCM format
/// </summary>
/// <param name="fileName">File Name</param>
private void CreateReaderStream(string fileName)
{
if (fileName.EndsWith(".wav", StringComparison.OrdinalIgnoreCase))
{
readerStream = new WaveFileReader(fileName);
}
}
/// <summary>
/// File Name
/// </summary>
public string FileName { get; }

/// <summary>
/// WaveFormat of this stream
/// </summary>
public override WaveFormat WaveFormat => sampleChannel.WaveFormat;

/// <summary>
/// Length of this stream (in bytes)
/// </summary>
public override long Length => length;

/// <summary>
/// Position of this stream (in bytes)
/// </summary>
public override long Position
{
get { return SourceToDest(readerStream.Position); }
set { lock (lockObject) { readerStream.Position = DestToSource(value); } }
}

/// <summary>
/// Reads from this wave stream
/// </summary>
/// <param name="buffer">Audio buffer</param>
/// <param name="offset">Offset into buffer</param>
/// <param name="count">Number of bytes required</param>
/// <returns>Number of bytes read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
var waveBuffer = new WaveBuffer(buffer);
int samplesRequired = count / 4;
int samplesRead = Read(waveBuffer.FloatBuffer, offset / 4, samplesRequired);
return samplesRead * 4;
}

/// <summary>
/// Reads audio from this sample provider
/// </summary>
/// <param name="buffer">Sample buffer</param>
/// <param name="offset">Offset into sample buffer</param>
/// <param name="count">Number of samples required</param>
/// <returns>Number of samples read</returns>
public int Read(float[] buffer, int offset, int count)
{
lock (lockObject)
{
return sampleChannel.Read(buffer, offset, count);
}
}

/// <summary>
/// Gets or Sets the Volume of this AudioFileReader. 1.0f is full volume
/// </summary>
public float Volume
{
get { return sampleChannel.Volume; }
set { sampleChannel.Volume = value; }
}

/// <summary>
/// Helper to convert source to dest bytes
/// </summary>
private long SourceToDest(long sourceBytes)
{
return destBytesPerSample * (sourceBytes / sourceBytesPerSample);
}

/// <summary>
/// Helper to convert dest to source bytes
/// </summary>
private long DestToSource(long destBytes)
{
return sourceBytesPerSample * (destBytes / destBytesPerSample);
}

/// <summary>
/// Disposes this AudioFileReader
/// </summary>
/// <param name="disposing">True if called from Dispose</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (readerStream != null)
{
readerStream.Dispose();
readerStream = null;
}
}
base.Dispose(disposing);
}
}
4 changes: 2 additions & 2 deletions src/Desktop/RodelAgent.UI/RodelAgent.UI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<TargetFramework>net9.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<WindowsSdkPackageVersion>10.0.22621.38</WindowsSdkPackageVersion>
<RootNamespace>RodelAgent.UI</RootNamespace>
Expand Down Expand Up @@ -238,7 +238,7 @@
</PackageReference>
<PackageReference Include="Microsoft.WindowsAppSDK" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" />
<PackageReference Include="NAudio" />
<PackageReference Include="NAudio.Core" />
<PackageReference Include="NLog.Extensions.Logging" />
<PackageReference Include="Roman-Numerals" />
<PackageReference Include="Tiktoken" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Rodel. All rights reserved.

using NAudio.Wave;
using RodelAgent.UI.Extensions;
using RodelAgent.UI.Toolkits;

namespace RodelAgent.UI.ViewModels.Components;
Expand Down Expand Up @@ -62,75 +62,4 @@ await Task.Run(() =>
RedrawWave?.Invoke(this, EventArgs.Empty);
});
}

private void ResetRecording(string sessionId)
{
_sessionId = sessionId;
_samples.Clear();
Seconds = 0;
var format = new WaveFormat(SampleRate, 1);
_waveFileWriter = !string.IsNullOrEmpty(_sessionId)
? new WaveFileWriter(GetRecordingFilePath(_sessionId), format)
: default;

var waveIn = new WaveInEvent();
waveIn.WaveFormat = format;
waveIn.DataAvailable += async (sender, e) =>
{
if (!IsRecording)
{
_dispatcherQueue.TryEnqueue(() =>
{
IsRecording = true;
});
}

var bytesPerSample = waveIn.WaveFormat.BitsPerSample / 8;
var sampleCount = e.BytesRecorded / bytesPerSample;
var floatBuffer = new float[sampleCount];
for (var index = 0; index < sampleCount; index++)
{
// Convert bytes to float
floatBuffer[index] = BitConverter.ToInt16(e.Buffer, index * bytesPerSample) / 32768f;
}

_samples.AddRange(floatBuffer);

_dispatcherQueue.TryEnqueue(() =>
{
Seconds = _samples.Count / (SampleRate * 1.0);
RedrawWave?.Invoke(this, EventArgs.Empty);
});

if (_waveFileWriter != null)
{
await WriteFileAsync(e.Buffer, e.BytesRecorded);
}
};

waveIn.RecordingStopped += (sender, e) =>
{
_waveIn.Dispose();
_waveFileWriter?.Dispose();
_waveIn = null;
_waveFileWriter = null;

_dispatcherQueue.TryEnqueue(() =>
{
IsRecording = false;
});
};

_waveIn = waveIn;
}

private async Task WriteFileAsync(byte[] buffer, int bytesRecorded)
{
if (_waveFileWriter == null)
{
return;
}

await _waveFileWriter.WriteAsync(buffer, 0, bytesRecorded);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Rodel. All rights reserved.

using Microsoft.UI.Dispatching;
using NAudio.Wave;
using Windows.Media.Playback;

namespace RodelAgent.UI.ViewModels.Components;
Expand All @@ -15,9 +14,6 @@ public sealed partial class AudioWaveModuleViewModel
private readonly DispatcherQueue _dispatcherQueue;
private readonly ILogger<AudioWaveModuleViewModel> _logger;
private List<float> _samples;
private WaveFileWriter _waveFileWriter;
private WaveInEvent _waveIn;
private string _sessionId;
private MediaPlayer _mediaPlayer;
private bool _isMediaEnded;

Expand Down
Loading
Loading