加了一堆东西 推送!!

This commit is contained in:
lichx
2024-04-02 21:57:04 +08:00
parent b2c833c6cb
commit 5b1d474cfd
28 changed files with 649 additions and 538 deletions
+141
View File
@@ -0,0 +1,141 @@
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
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;
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);
ms = new MemoryStream(StreamToBytes(sound));
ws = new Mp3FileReader(ms);
blockAlignReductionStream = new BlockAlignReductionStream(ws);
wave16ToFloatProvider = new Wave16ToFloatProvider(blockAlignReductionStream);
wave16ToFloatProvider.Volume = volume / 100f;
waveOutEvent = new WaveOutEvent();
waveOutEvent.Init(wave16ToFloatProvider);
waveOutEvent.PlaybackStopped += WaveOutEvent_PlaybackStopped;
}
internal AudioPlayer(string file_name, int volume, Action<string,AudioPlayer> finished)
{
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);
ms = new MemoryStream(StreamToBytes(sound));
ws = new Mp3FileReader(ms);
blockAlignReductionStream = new BlockAlignReductionStream(ws);
wave16ToFloatProvider = new Wave16ToFloatProvider(blockAlignReductionStream);
wave16ToFloatProvider.Volume = volume / 100f;
waveOutEvent = new WaveOutEvent();
waveOutEvent.Init(wave16ToFloatProvider);
waveOutEvent.PlaybackStopped += WaveOutEvent_PlaybackStopped;
}
public void volume_change(int val)
{
volume = val;
wave16ToFloatProvider.Volume = volume / 100f;
}
public void pause_song() => waveOutEvent.Pause();
public void resume_song() => waveOutEvent.Play();
private void WaveOutEvent_PlaybackStopped(object? sender, StoppedEventArgs e)
{
if(shutdown) return;
finished?.Invoke(file_name,this);
Dispose();
}
~AudioPlayer()
{
Dispose();
}
public void Dispose()
{
shutdown = true;
waveOutEvent.Stop();
sound.Dispose();
ms.Dispose();
//ws.Dispose();
//blockAlignReductionStream.Dispose();
waveOutEvent.Dispose();
}
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;
}
}
}
}
}
@@ -1,164 +0,0 @@
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using CDSAE3_Lian_Lian_Kan.Properties;
using NAudio.Wave.SampleProviders;
namespace CDSAE3_Lian_Lian_Kan.Sound
{
public class Audio_processer
{
class Audio_File_Processor
{
private WaveChannel32? volumeStream;
private List<string> audioFiles = new List<string>();
string base_path;
int next_song = 0;
int volume = 90;
public Audio_File_Processor()
{
base_path = AppDomain.CurrentDomain.BaseDirectory;
}
internal void set_Albums(string s)=>audioFiles = Settings.musics.TryGetValue(s, out List<string>? val) ? val : new List<string>();
internal Wave16ToFloatProvider get_next_song()
{
volumeStream?.Close();
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 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(volumeStream != null)
volumeStream.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;
}
}
}
~Audio_File_Processor()
{
volumeStream?.Close();
}
}
private WaveOutEvent songOutputDevice;
private WaveOutEvent infoOutputDevice;
Audio_File_Processor audio_File_Processor = new Audio_File_Processor();
public Audio_processer()
{
songOutputDevice = new WaveOutEvent();
infoOutputDevice = new WaveOutEvent();
set_albums(get_albums().First());
}
private void OnPlaybackStopped(object? sender, StoppedEventArgs e)
{
next_song();
resume_song();
}
public void pause_song()=>songOutputDevice.Pause();
public void resume_song()=>songOutputDevice.Play();
public void last_song()
{
waveOutEvent_Provider(audio_File_Processor.get_last_song());
}
public void next_song()
{
waveOutEvent_Provider(audio_File_Processor.get_next_song());
}
/// <summary>
/// 切换音乐
/// </summary>
/// <param name="waveChannel32"></param>
private void waveOutEvent_Provider(Wave16ToFloatProvider wave16ToFloatProvider)
{
songOutputDevice.Stop();
songOutputDevice.Dispose();
songOutputDevice = new WaveOutEvent();
songOutputDevice.Init(wave16ToFloatProvider);
songOutputDevice.PlaybackStopped += OnPlaybackStopped;
}
/// <summary>
/// 调整音乐音量
/// </summary>
/// <param name="val">音量大小 1-100</param>
public void volume_change(int val)=> audio_File_Processor.volume_change(val);
public List<string> get_albums() => Settings.musics.Select(x => x.Key).ToList();
public void set_albums(string s)
{
audio_File_Processor.set_Albums(s);
}
~Audio_processer()
{
songOutputDevice.Dispose();
infoOutputDevice.Dispose();
}
}
}
@@ -0,0 +1,71 @@
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace CDSAE3_Lian_Lian_Kan.Sound
{
public class Info_Audio_processer
{
Etcs.break_music soundScape_version = Etcs.break_music.breakA;
public int soundScape_volume { get; set; } = 90;
string last_break_soundScape = "";
Random random = new Random();
class Info_file_processer
{ }
internal void set_SoundScape_version(Etcs.break_music version) => soundScape_version = version;
internal void play_random_break_soundScape()
{
Task.Run(() =>
{
void finished(string s,AudioPlayer audioPlayer)
{
audioPlayer.Dispose();
}
AudioPlayer audioPlayer = new AudioPlayer(get_random_break_soundScape(), soundScape_volume,finished);
audioPlayer.resume_song();
//object obj = Settings.res_Manager.GetObject(get_random_break_soundScape(), Settings.res_Culture)!;
//var infoOutputDevice = new WaveOutEvent();
//using (var sound = new MemoryStream((byte[])obj))
//using (var ms = new MemoryStream(StreamToBytes(sound)))
//using (var ws = new Mp3FileReader(ms))
//using (var blockAlignReductionStream = new BlockAlignReductionStream(ws))
//{
// var wave16ToFloatProvider = new Wave16ToFloatProvider(blockAlignReductionStream);
// wave16ToFloatProvider.Volume = soundScape_volume / 100f;
// infoOutputDevice.Init(wave16ToFloatProvider);
// infoOutputDevice.PlaybackStopped += InfoOutputDevice_PlaybackStopped;
// infoOutputDevice.Play();
//}
});
}
private string get_random_break_soundScape()
{
string name;
for (; ; )
{
name = soundScape_version switch
{
Etcs.break_music.breakA => "breakA" + (random.Next(1, 9)).ToString(),
Etcs.break_music.breakB => throw new NotImplementedException(),
_ => "breakA" + (random.Next(0, 9)).ToString()
};
if (name != last_break_soundScape)
break;
}
return name;
//MemoryStream sound = new MemoryStream((byte[])obj);
//MemoryStream ms = new MemoryStream(StreamToBytes(sound));
//var ws = new Mp3FileReader(ms);
//BlockAlignReductionStream blockAlignReductionStream = new BlockAlignReductionStream(ws);
//Wave16ToFloatProvider wave16ToFloatProvider = new Wave16ToFloatProvider(blockAlignReductionStream);
//wave16ToFloatProvider.Volume = soundScape_volume / 100f;
//return wave16ToFloatProvider;
}
}
}
@@ -0,0 +1,147 @@
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using CDSAE3_Lian_Lian_Kan.Properties;
using NAudio.Wave.SampleProviders;
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;
private void OnPlaybackStopped(string s,object? obj)
{
set_song(get_next_song());
}
public void pause_song()=> audioPlayer?.pause_song();
public void resume_song()=> audioPlayer?.resume_song();
public string get_next_song()
{
string name = audioFiles[next_song];
next_song++;
next_song %= audioFiles.Count;
return name;
}
/// <summary>
/// 调整音乐音量
/// </summary>
/// <param name="val">音量大小 1-100</param>
public void volume_change(int val)=> audioPlayer?.volume_change(val);
public void set_albums(string s)
{
var result = Etcs.musics.Where(x => x.Key == s).ToList();
if (result.Count == 0)
throw new Exception("no such album");
audioFiles = result.First().Value;
}
public void set_song(string s)
{
audioPlayer?.Dispose();
try
{
audioPlayer = new AudioPlayer(s, Etcs.Song_Volume, OnPlaybackStopped);
}
catch (Exception e)
{
MessageBox.Show($"failed to play {s}\n{e.Message}");
return;
}
audioPlayer.resume_song();
}
public void Dispose()=> audioPlayer?.Dispose();
~Song_Audio_processer() => Dispose();
}
}