-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAudio.cs
151 lines (132 loc) · 5.43 KB
/
Audio.cs
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
using System;
using System.IO;
using System.Reflection;
using NAudio.Wave;
using VGAudio;
using VGAudio.Cli;
using Xv2CoreLib.ACB;
namespace Sitreamai;
public static class Audio
{
public static void ConvertToMai(string srcPath, string savePath, float padding = 0, Stream src = null, string previewFilename = null, Stream preview = null)
{
var wrapper = new ACB_Wrapper(ACB_File.Load(ReadResourceFile(previewFilename is null ? "Sitreamai.Resources.nopreview.acb" : "Sitreamai.Resources.template.acb"), null));
var trackBytes = LoadAndConvertFile(srcPath, FileType.Hca, false, 9170825592834449000, padding, src);
wrapper.Cues[0].AddTrackToCue(trackBytes, true, false, EncodeType.HCA);
if (previewFilename is not null)
{
var previewTrackBytes = LoadAndConvertFile(previewFilename, FileType.Hca, true, 9170825592834449000, 0, preview);
wrapper.Cues[1].AddTrackToCue(previewTrackBytes, true, false, EncodeType.HCA);
}
wrapper.AcbFile.Save(savePath);
}
private static byte[] ReadResourceFile(string filename)
{
using var s = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename);
var buffer = new byte[1024];
using var ms = new MemoryStream();
while (true)
{
var read = s.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
// 不要 byte[] 转 memory stream 倒来倒去,直接传入 stream
public static byte[] LoadAndConvertFile(string path, FileType convertToType, bool loop, ulong encrpytionKey = 0, float padding = 0, Stream src = null)
{
using var read = src ?? File.OpenRead(path);
switch (Path.GetExtension(path).ToLowerInvariant())
{
case ".wav":
case ".mp3":
case ".ogg":
case ".wma":
case ".aac":
return ConvertFile(ConvertToWav(read, Path.GetExtension(path).Equals(".ogg", StringComparison.InvariantCultureIgnoreCase), padding), FileType.Wave, convertToType, loop, encrpytionKey);
case ".hca":
return ConvertFile(read, FileType.Hca, convertToType, loop, encrpytionKey);
case ".adx":
if (convertToType == FileType.Adx)
{
var ms = new MemoryStream();
read.CopyTo(ms);
return ms.ToArray();
}
return ConvertFile(read, FileType.Adx, convertToType, loop, encrpytionKey);
case ".at9":
return ConvertFile(read, FileType.Atrac9, convertToType, loop, encrpytionKey);
case ".dsp":
return ConvertFile(read, FileType.Dsp, convertToType, loop, encrpytionKey);
case ".bcwav":
return ConvertFile(read, FileType.Bcwav, convertToType, loop, encrpytionKey);
}
throw new InvalidDataException($"Filetype of \"{path}\" is not supported.");
}
public static Stream ConvertToWav(Stream src, bool isOgg, float padding = 0)
{
using WaveStream reader = isOgg ? new NAudio.Vorbis.VorbisWaveReader(src, true) : new StreamMediaFoundationReader(src);
var sample = reader.ToSampleProvider();
switch (padding)
{
case > 0:
{
var sp = new SilenceProvider(reader.WaveFormat);
var silence = sp.ToSampleProvider().Take(TimeSpan.FromSeconds(padding));
sample = silence.FollowedBy(sample);
break;
}
case < 0:
sample = sample.Skip(TimeSpan.FromSeconds(-padding));
break;
}
var stream = new MemoryStream();
WaveFileWriter.WriteWavFileToStream(stream, sample.ToWaveProvider16()); // 淦
stream.Position = 0; // 淦 x2
return stream;
}
public static byte[] ConvertFile(Stream s, FileType encodeType, FileType convertToType, bool loop,
ulong encryptionKey = 0)
{
ConvertStatics.SetLoop(loop, 0, 0);
var options = new Options
{
KeyCode = encryptionKey,
Loop = loop
};
if (options.Loop)
options.LoopEnd = int.MaxValue;
byte[] track = ConvertStream.ConvertFile(options, s, encodeType, convertToType);
//if (convertToType == FileType.Hca && loop)
// track = HCA.EncodeLoop(track, loop);
return track;
}
private static FileType GetFileType(EncodeType encodeType)
{
switch (encodeType)
{
case EncodeType.HCA:
case EncodeType.HCA_ALT:
return FileType.Hca;
case EncodeType.ADX:
return FileType.Adx;
case EncodeType.ATRAC9:
return FileType.Atrac9;
case EncodeType.DSP:
return FileType.Dsp;
case EncodeType.BCWAV:
return FileType.Bcwav;
default:
return FileType.NotSet;
}
}
public static byte[] AcbToWav(string acbPath)
{
var acb = ACB_File.Load(acbPath);
var wave = acb.GetWaveformsFromCue(acb.Cues[0])[0];
var entry = acb.GetAfs2Entry(wave.AwbId);
using MemoryStream stream = new MemoryStream(entry.bytes);
return ConvertStream.ConvertFile(new Options(), stream, GetFileType(wave.EncodeType), FileType.Wave);
}
}