Skip to content

Commit

Permalink
NAudio解码修改
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYunPlayer committed Jun 13, 2024
1 parent b7e840a commit 5f85068
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
64 changes: 53 additions & 11 deletions TuneLab/Audio/NAudio/NAudioCodec.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using NAudio.Wave;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using NAudio.Wave;
using NAudio.Flac;
using NLayer.NAudioSupport;
using TuneLab.Base.Science;

namespace TuneLab.Audio.NAudio;

internal class NAudioCodec : IAudioCodec
{
public IEnumerable<string> AllDecodableFormats { get; } = ["wav", "mp3", "aiff", "aac", "wma", "mp4"];
public IEnumerable<string> AllDecodableFormats { get; } = ["wav", "mp3", "flac", "aiff", "aif", "aifc"];

public void EncodeToWav(string path, float[] buffer, int samplingRate, int bitPerSample, int channelCount)
{
Expand All @@ -21,7 +25,7 @@ public void EncodeToWav(string path, float[] buffer, int samplingRate, int bitPe

public AudioInfo GetAudioInfo(string path)
{
using var reader = new AudioFileReader(path);
using var reader = new NAudioFileReader(path);
return new AudioInfo() { duration = reader.TotalTime.TotalSeconds };
}

Expand Down Expand Up @@ -53,26 +57,64 @@ class NAudioFileReader : IAudioStream
public int SamplingRate { get; }
public int ChannelCount { get; }
public int SamplesPerChannel { get; }
public TimeSpan TotalTime { get; }

public NAudioFileReader(string path)
{
mAudioFileReader = new(path);
SamplingRate = mAudioFileReader.WaveFormat.SampleRate;
ChannelCount = mAudioFileReader.WaveFormat.Channels;
SamplesPerChannel = (int)mAudioFileReader.Length;
mWaveStream = Create(path);
mSampleProvider = mWaveStream.ToSampleProvider();
SamplingRate = mWaveStream.WaveFormat.SampleRate;
ChannelCount = mWaveStream.WaveFormat.Channels;
TotalTime = mWaveStream.TotalTime;
var count = TotalTime.TotalSeconds * SamplingRate;
SamplesPerChannel = count.Round();
}

public void Dispose()
{
mAudioFileReader.Dispose();
mWaveStream.Dispose();
}

public void Read(float[] buffer, int offset, int count)
{
mAudioFileReader.Read(buffer, offset, count);
mSampleProvider.Read(buffer, offset, count);
}

WaveStream Create(string filepath)
{
var ext = Path.GetExtension(filepath);
byte[] buffer = new byte[128];
string tag = "";
using (var stream = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
if (stream.CanSeek)
{
stream.Read(buffer, 0, 128);
tag = System.Text.Encoding.UTF8.GetString(buffer.AsSpan(0, 4));
}
}
if (tag == "RIFF")
{
return new WaveFileReader(filepath);
}
if (ext == ".mp3")
{
return new Mp3FileReaderBase(filepath, wf => new Mp3FrameDecompressor(wf));
}
if (tag == "fLaC")
{
return new FlacReader(filepath);
}
if (ext == ".aiff" || ext == ".aif" || ext == ".aifc")
{
return new AiffFileReader(filepath);
}

throw new Exception("Unsupported audio file format.");
}

readonly AudioFileReader mAudioFileReader;
readonly WaveStream mWaveStream;
readonly ISampleProvider mSampleProvider;
}

class NAudioResamplerStream : IAudioStream
Expand Down
2 changes: 2 additions & 0 deletions TuneLab/TuneLab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
<PackageReference Include="Avalonia.Desktop" Version="11.0.10" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.10" />
<PackageReference Include="BunLabs.NAudio.Flac" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.3" />
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLayer.NAudioSupport" Version="1.4.0" />
<PackageReference Include="PinYinConverterCore" Version="1.0.2" />
<PackageReference Include="ppy.SDL2-CS" Version="1.0.741-alpha" />
<PackageReference Include="Svg.Skia" Version="1.0.0.17" />
Expand Down

0 comments on commit 5f85068

Please sign in to comment.