中间件探索

This commit is contained in:
lichx
2024-04-08 21:04:47 +08:00
parent e0a212af0f
commit 3e81b973e9
15 changed files with 753 additions and 161 deletions
+19 -20
View File
@@ -10,25 +10,24 @@ namespace CDSAE3_Lian_Lian_Kan.Sound
{
internal class AudioPlayer : IDisposable
{
object source_obj;
MemoryStream sound;
MemoryStream ms;
Mp3FileReader ws;
BlockAlignReductionStream blockAlignReductionStream;
Wave16ToFloatProvider wave16ToFloatProvider;
WaveOutEvent waveOutEvent;
Action<string,AudioPlayer>? finished;
string file_name;
bool shutdown = false;
readonly byte[] source_obj;
readonly MemoryStream sound;
readonly MemoryStream ms;
readonly Mp3FileReader ws;
readonly BlockAlignReductionStream blockAlignReductionStream;
readonly Wave16ToFloatProvider wave16ToFloatProvider;
readonly WaveOutEvent waveOutEvent;
readonly Action<string,AudioPlayer>? finished;
readonly string file_name;
bool shuting_down = false;
internal int volume { get; set; }
internal AudioPlayer(string file_name, int volume)
{
this.file_name = file_name;
this.volume = volume;
file_name = file_name.Split('.').First().Replace(" ", "_").Replace("-", "_");
source_obj = Etcs.res_Manager.GetObject(file_name, Etcs.res_Culture)!;
sound = new MemoryStream((byte[])source_obj);
source_obj = Etcs.audio_Res_Manager.Get_Audio_Resources(file_name);
sound = new MemoryStream(source_obj);
ms = new MemoryStream(StreamToBytes(sound));
ws = new Mp3FileReader(ms);
blockAlignReductionStream = new BlockAlignReductionStream(ws);
@@ -43,10 +42,9 @@ namespace CDSAE3_Lian_Lian_Kan.Sound
this.file_name = file_name;
this.volume = volume;
this.finished = finished;
file_name = file_name.Split('.').First().Replace(" ", "_").Replace("-", "_");
source_obj = Etcs.res_Manager.GetObject(file_name, Etcs.res_Culture)!;
sound = new MemoryStream((byte[])source_obj);
source_obj = Etcs.audio_Res_Manager.Get_Audio_Resources(file_name);
sound = new MemoryStream(source_obj);
ms = new MemoryStream(StreamToBytes(sound));
ws = new Mp3FileReader(ms);
blockAlignReductionStream = new BlockAlignReductionStream(ws);
@@ -65,7 +63,7 @@ namespace CDSAE3_Lian_Lian_Kan.Sound
public void resume_song() => waveOutEvent.Play();
private void WaveOutEvent_PlaybackStopped(object? sender, StoppedEventArgs e)
{
if(shutdown) return;
if(shuting_down) return;
finished?.Invoke(file_name,this);
Dispose();
}
@@ -77,13 +75,14 @@ namespace CDSAE3_Lian_Lian_Kan.Sound
public void Dispose()
{
shutdown = true;
shuting_down = true;
waveOutEvent.Stop();
sound.Dispose();
ms.Dispose();
//ws.Dispose();
//blockAlignReductionStream.Dispose();
ws.Dispose();
blockAlignReductionStream.Dispose();
waveOutEvent.Dispose();
GC.SuppressFinalize(this);
}
internal static byte[] StreamToBytes(Stream stream)
{
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CDSAE3_Lian_Lian_Kan.Sound
{
public class Audio_res_manager
{
Dictionary<string, byte[]> audio_res = new Dictionary<string, byte[]>();
internal byte[] Get_Audio_Resources(string name)
{
if(audio_res.TryGetValue(name, out byte[]? val))
return val;
object obj = Etcs.res_Manager.GetObject(name, Etcs.res_Culture)!;
byte[] res = (byte[])obj;
audio_res.Add(name, res);
return res;
}
internal void Clear_Audio_Resources()
{
audio_res.Clear();
}
}
}
@@ -12,92 +12,6 @@ namespace CDSAE3_Lian_Lian_Kan.Sound
{
public class Song_Audio_processer:IDisposable
{
//class Audio_File_Processor
//{
// private Wave16ToFloatProvider? wave16ToFloatProvider;
// private List<string> audioFiles = new List<string>();
// int next_song = 0;
// int volume = 90;
// internal void set_Albums(string s)=>audioFiles = Settings.musics.TryGetValue(s, out List<string>? val) ? val : new List<string>();
// internal Wave16ToFloatProvider get_next_song()
// {
// string name = audioFiles[next_song];
// next_song++;
// next_song %= audioFiles.Count;
// name = name.Split('.').First().Replace(" ","_").Replace("-","_");
// object obj = Settings.res_Manager.GetObject(name, Settings.res_Culture)!;
// MemoryStream sound = new MemoryStream((byte[])obj);
// MemoryStream ms = new MemoryStream(StreamToBytes(sound));
// var ws = new Mp3FileReader(ms);
// BlockAlignReductionStream blockAlignReductionStream = new BlockAlignReductionStream(ws);
// wave16ToFloatProvider = new Wave16ToFloatProvider(blockAlignReductionStream);
// wave16ToFloatProvider.Volume = volume / 100f;
// return wave16ToFloatProvider;
// }
// internal Wave16ToFloatProvider get_last_song()
// {
// next_song = (next_song - 2 + audioFiles.Count) % audioFiles.Count;
// return get_next_song();
// }
// internal void volume_change(int val)
// {
// volume = val;
// if(wave16ToFloatProvider != null)
// wave16ToFloatProvider.Volume = volume / 100f;
// }
// internal static byte[] StreamToBytes(Stream stream)
// {
// long originalPosition = 0;
// if (stream.CanSeek)
// {
// originalPosition = stream.Position;
// stream.Position = 0;
// }
// try
// {
// byte[] readBuffer = new byte[4096];
// int totalBytesRead = 0;
// int bytesRead;
// while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
// {
// totalBytesRead += bytesRead;
// if (totalBytesRead == readBuffer.Length)
// {
// int nextByte = stream.ReadByte();
// if (nextByte != -1)
// {
// byte[] temp = new byte[readBuffer.Length * 2];
// Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
// Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
// readBuffer = temp;
// totalBytesRead++;
// }
// }
// }
// byte[] buffer = readBuffer;
// if (readBuffer.Length != totalBytesRead)
// {
// buffer = new byte[totalBytesRead];
// Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
// }
// return buffer;
// }
// finally
// {
// if (stream.CanSeek)
// {
// stream.Position = originalPosition;
// }
// }
// }
//}
AudioPlayer? audioPlayer;
private List<string> audioFiles = new List<string>();
int next_song = 1;
@@ -141,7 +55,11 @@ namespace CDSAE3_Lian_Lian_Kan.Sound
}
audioPlayer.resume_song();
}
public void Dispose()=> audioPlayer?.Dispose();
public void Dispose()
{
audioPlayer?.Dispose();
GC.SuppressFinalize(this);
}
~Song_Audio_processer() => Dispose();
}
}