mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-18 12:05:14 -04:00
Fix skip ssl validation widget not actually showing, oops. (Thanks Xerolyph)
This commit is contained in:
parent
6e9b5a8e31
commit
318d722158
@ -70,7 +70,7 @@ namespace ClassicalSharp.Audio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PlayCurrentSound(IAudioOutput[] outputs, float volume) {
|
void PlayCurrentSound(IAudioOutput[] outputs, float volume) {
|
||||||
for (int i = 0; i < monoOutputs.Length; i++) {
|
for (int i = 0; i < outputs.Length; i++) {
|
||||||
IAudioOutput output = outputs[i];
|
IAudioOutput output = outputs[i];
|
||||||
if (output == null) {
|
if (output == null) {
|
||||||
output = GetPlatformOut();
|
output = GetPlatformOut();
|
||||||
@ -87,7 +87,7 @@ namespace ClassicalSharp.Audio {
|
|||||||
|
|
||||||
// This time we try to play the sound on all possible devices,
|
// This time we try to play the sound on all possible devices,
|
||||||
// even if it requires the expensive case of recreating a device
|
// even if it requires the expensive case of recreating a device
|
||||||
for (int i = 0; i < monoOutputs.Length; i++) {
|
for (int i = 0; i < outputs.Length; i++) {
|
||||||
IAudioOutput output = outputs[i];
|
IAudioOutput output = outputs[i];
|
||||||
if (!output.IsFinished()) continue;
|
if (!output.IsFinished()) continue;
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ namespace Launcher.Gui.Screens {
|
|||||||
UpdateSignInInfo(Get(0), Get(1));
|
UpdateSignInInfo(Get(0), Get(1));
|
||||||
|
|
||||||
CheckboxWidget skip = widgets[view.sslIndex] as CheckboxWidget;
|
CheckboxWidget skip = widgets[view.sslIndex] as CheckboxWidget;
|
||||||
if (skip != null && skip.Value) {
|
if (skip != null && skip.Visible && skip.Value) {
|
||||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||||
Options.Set("skip-ssl-check", true);
|
Options.Set("skip-ssl-check", true);
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include "Block.h"
|
#include "Block.h"
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
|
|
||||||
|
StringsBuffer files;
|
||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
*------------------------------------------------------Soundboard---------------------------------------------------------*
|
*------------------------------------------------------Soundboard---------------------------------------------------------*
|
||||||
*#########################################################################################################################*/
|
*#########################################################################################################################*/
|
||||||
@ -36,32 +37,32 @@ static ReturnCode Sound_ReadWaveData(struct Stream* stream, struct Sound* snd) {
|
|||||||
UInt32 fourCC, size, pos, len;
|
UInt32 fourCC, size, pos, len;
|
||||||
ReturnCode res;
|
ReturnCode res;
|
||||||
|
|
||||||
fourCC = Stream_ReadU32_LE(&stream);
|
fourCC = Stream_ReadU32_LE(stream);
|
||||||
if (fourCC != WAV_FourCC('R','I','F','F')) return WAV_ERR_STREAM_HDR;
|
if (fourCC != WAV_FourCC('R','I','F','F')) return WAV_ERR_STREAM_HDR;
|
||||||
Stream_ReadU32_LE(&stream); /* file size, but we don't care */
|
Stream_ReadU32_LE(stream); /* file size, but we don't care */
|
||||||
fourCC = Stream_ReadU32_LE(&stream);
|
fourCC = Stream_ReadU32_LE(stream);
|
||||||
if (fourCC != WAV_FourCC('W','A','V','E')) return WAV_ERR_STREAM_TYPE;
|
if (fourCC != WAV_FourCC('W','A','V','E')) return WAV_ERR_STREAM_TYPE;
|
||||||
|
|
||||||
while (!(res = stream->Position(&stream, &pos)) && !(res = stream->Length(&stream, &len)) && len < pos) {
|
while (!(res = stream->Position(stream, &pos)) && !(res = stream->Length(stream, &len)) && len < pos) {
|
||||||
fourCC = Stream_ReadU32_LE(&stream);
|
fourCC = Stream_ReadU32_LE(stream);
|
||||||
size = Stream_ReadU32_LE(&stream);
|
size = Stream_ReadU32_LE(stream);
|
||||||
|
|
||||||
if (fourCC == WAV_FourCC('f','m','t',' ')) {
|
if (fourCC == WAV_FourCC('f','m','t',' ')) {
|
||||||
if (Stream_GetU16_LE(&stream) != 1) return WAV_ERR_DATA_TYPE;
|
if (Stream_GetU16_LE(stream) != 1) return WAV_ERR_DATA_TYPE;
|
||||||
|
|
||||||
snd->Format.Channels = Stream_ReadU16_LE(&stream);
|
snd->Format.Channels = Stream_ReadU16_LE(stream);
|
||||||
snd->Format.Frequency = Stream_ReadU32_LE(&stream);
|
snd->Format.SampleRate = Stream_ReadU32_LE(stream);
|
||||||
Stream_Skip(&stream, 6);
|
Stream_Skip(&stream, 6);
|
||||||
snd->Format.BitsPerSample = Stream_ReadU16_LE(&stream);
|
snd->Format.BitsPerSample = Stream_ReadU16_LE(stream);
|
||||||
size -= 16;
|
size -= 16;
|
||||||
} else if (fourCC == WAV_FourCC('d','a','t','a')) {
|
} else if (fourCC == WAV_FourCC('d','a','t','a')) {
|
||||||
snd->Data = Platform_MemAlloc(size, sizeof(UInt8), "WAV sound data");
|
snd->Data = Platform_MemAlloc(size, sizeof(UInt8), "WAV sound data");
|
||||||
Stream_Read(&stream, snd->Data, size);
|
Stream_Read(stream, snd->Data, size);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Skip over unhandled data */
|
/* Skip over unhandled data */
|
||||||
if (size) Stream_Skip(&stream, size);
|
if (size) Stream_Skip(stream, size);
|
||||||
}
|
}
|
||||||
return res ? res : WAV_ERR_NO_DATA;
|
return res ? res : WAV_ERR_NO_DATA;
|
||||||
}
|
}
|
||||||
@ -122,113 +123,31 @@ static void Soundboard_Init(struct Soundboard* board, STRING_PURE String* boardN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Sound* Soundboard_PickRandom(struct Soundboard* board, UInt8 type) {
|
||||||
|
if (type == SOUND_NONE || type >= SOUND_COUNT) return NULL;
|
||||||
|
if (type == SOUND_METAL) type = SOUND_STONE;
|
||||||
|
string name = SoundType.Names[type];
|
||||||
|
|
||||||
|
struct SoundGroup* group = Soundboard_Find(board, name);
|
||||||
|
if (group == NULL) return NULL;
|
||||||
|
return group.Sounds[rnd.Next(group.Sounds.Count)];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
*-----------------------------------------------------Audio sounds--------------------------------------------------------*
|
*--------------------------------------------------------Sounds-----------------------------------------------------------*
|
||||||
*#########################################################################################################################*/
|
*#########################################################################################################################*/
|
||||||
struct Soundboard digBoard, stepBoard;
|
struct Soundboard digBoard, stepBoard;
|
||||||
#define AUDIO_MAX_HANDLES 6
|
#define AUDIO_MAX_HANDLES 6
|
||||||
|
AudioHandle monoOutputs[AUDIO_MAX_HANDLES] = { -1, -1, -1, -1, -1, -1 };
|
||||||
|
AudioHandle stereoOutputs[AUDIO_MAX_HANDLES] = { -1, -1, -1, -1, -1, -1 };
|
||||||
|
|
||||||
void Audio_SetSounds(Int32 volume) {
|
static void PlaySound(AudioHandle output, Real32 volume) {
|
||||||
if (volume > 0) InitSound();
|
|
||||||
else DisposeSound();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Audio_InitSound(void) {
|
|
||||||
if (digBoard == null) InitSoundboards();
|
|
||||||
monoOutputs = new IAudioOutput[maxSounds];
|
|
||||||
stereoOutputs = new IAudioOutput[maxSounds];
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Audio_InitSoundboards(void) {
|
|
||||||
digBoard = new Soundboard();
|
|
||||||
digBoard.Init("dig_", files);
|
|
||||||
stepBoard = new Soundboard();
|
|
||||||
stepBoard.Init("step_", files);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Audio_PlayBlockSound(object sender, BlockChangedEventArgs e) {
|
|
||||||
if (e.Block == BLOCK_AIR) {
|
|
||||||
PlayDigSound(Block_DigSounds[e.OldBlock]);
|
|
||||||
} else if (!Game_ClassicMode) {
|
|
||||||
PlayDigSound(Block_StepSounds[e.Block]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AudioChunk chunk = new AudioChunk();
|
|
||||||
static void PlaySound(byte type, Soundboard board) {
|
|
||||||
if (type == SoundType.None || monoOutputs == null) return;
|
|
||||||
Sound snd = board.PickRandomSound(type);
|
|
||||||
if (snd == null) return;
|
|
||||||
|
|
||||||
chunk.Channels = snd.Channels;
|
|
||||||
chunk.BitsPerSample = snd.BitsPerSample;
|
|
||||||
chunk.BytesOffset = 0;
|
|
||||||
chunk.BytesUsed = snd.Data.Length;
|
|
||||||
chunk.Data = snd.Data;
|
|
||||||
|
|
||||||
float volume = game.SoundsVolume / 100.0f;
|
|
||||||
if (board == digBoard) {
|
|
||||||
if (type == SoundType.Metal) chunk.SampleRate = (snd.SampleRate * 6) / 5;
|
|
||||||
else chunk.SampleRate = (snd.SampleRate * 4) / 5;
|
|
||||||
} else {
|
|
||||||
volume *= 0.50f;
|
|
||||||
|
|
||||||
if (type == SoundType.Metal) chunk.SampleRate = (snd.SampleRate * 7) / 5;
|
|
||||||
else chunk.SampleRate = snd.SampleRate;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (snd.Channels == 1) {
|
|
||||||
PlayCurrentSound(monoOutputs, volume);
|
|
||||||
} else if (snd.Channels == 2) {
|
|
||||||
PlayCurrentSound(stereoOutputs, volume);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Audio_PlayDigSound(UInt8 type) { Audio_PlaySound(type, &digBoard); }
|
|
||||||
void Audio_PlayStepSound(UInt8 type) { Audio_PlaySound(type, &stepBoard); }
|
|
||||||
|
|
||||||
|
|
||||||
IAudioOutput firstSoundOut;
|
|
||||||
static void PlayCurrentSound(IAudioOutput[] outputs, float volume) {
|
|
||||||
for (int i = 0; i < monoOutputs.Length; i++) {
|
|
||||||
IAudioOutput output = outputs[i];
|
|
||||||
if (output == null) output = MakeSoundOutput(outputs, i);
|
|
||||||
if (!output.DoneRawAsync()) continue;
|
|
||||||
|
|
||||||
LastChunk l = output.Last;
|
|
||||||
if (l.Channels == 0 || (l.Channels == chunk.Channels && l.BitsPerSample == chunk.BitsPerSample
|
|
||||||
&& l.SampleRate == chunk.SampleRate)) {
|
|
||||||
PlaySound(output, volume); return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This time we try to play the sound on all possible devices,
|
|
||||||
// even if it requires the expensive case of recreating a device
|
|
||||||
for (int i = 0; i < monoOutputs.Length; i++) {
|
|
||||||
IAudioOutput output = outputs[i];
|
|
||||||
if (!output.DoneRawAsync()) continue;
|
|
||||||
|
|
||||||
PlaySound(output, volume); return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static IAudioOutput MakeSoundOutput(IAudioOutput[] outputs, int i) {
|
|
||||||
IAudioOutput output = GetPlatformOut();
|
|
||||||
output.Create(1, firstSoundOut);
|
|
||||||
if (firstSoundOut == null)
|
|
||||||
firstSoundOut = output;
|
|
||||||
|
|
||||||
outputs[i] = output;
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PlaySound(IAudioOutput output, float volume) {
|
|
||||||
try {
|
try {
|
||||||
output.SetVolume(volume);
|
output.SetVolume(volume);
|
||||||
output.PlayRawAsync(chunk);
|
output.PlayRawAsync(chunk);
|
||||||
} catch (InvalidOperationException ex) {
|
}
|
||||||
|
catch (InvalidOperationException ex) {
|
||||||
ErrorHandler.LogError("AudioPlayer.PlayCurrentSound()", ex);
|
ErrorHandler.LogError("AudioPlayer.PlayCurrentSound()", ex);
|
||||||
if (ex.Message == "No audio devices found")
|
if (ex.Message == "No audio devices found")
|
||||||
game.Chat.Add("&cNo audio devices found, disabling sounds.");
|
game.Chat.Add("&cNo audio devices found, disabling sounds.");
|
||||||
@ -240,83 +159,118 @@ static void PlaySound(IAudioOutput output, float volume) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DisposeSound() {
|
AudioChunk chunk = new AudioChunk();
|
||||||
DisposeOutputs(ref monoOutputs);
|
static void PlaySound(UInt8 type, struct Soundboard* board) {
|
||||||
DisposeOutputs(ref stereoOutputs);
|
if (type == SOUND_NONE || Game_SoundsVolume == 0) return;
|
||||||
if (firstSoundOut != null) {
|
struct Sound* snd = Soundboard_PickRandom(board, type);
|
||||||
firstSoundOut.Dispose();
|
if (snd == NULL) return;
|
||||||
firstSoundOut = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void DisposeOutputs(ref IAudioOutput[] outputs) {
|
struct AudioFormat fmt = snd->Format;
|
||||||
if (outputs == null) return;
|
chunk.BytesUsed = snd.Data.Length;
|
||||||
bool soundPlaying = true;
|
chunk.Data = snd.Data;
|
||||||
|
|
||||||
while (soundPlaying) {
|
Real32 volume = Game_SoundsVolume / 100.0f;
|
||||||
soundPlaying = false;
|
if (board == &digBoard) {
|
||||||
for (int i = 0; i < outputs.Length; i++) {
|
if (type == SOUND_METAL) fmt.SampleRate = (fmt.SampleRate * 6) / 5;
|
||||||
if (outputs[i] == null) continue;
|
else fmt.SampleRate = (fmt.SampleRate * 4) / 5;
|
||||||
soundPlaying |= !outputs[i].DoneRawAsync();
|
|
||||||
}
|
|
||||||
if (soundPlaying)
|
|
||||||
Thread.Sleep(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < outputs.Length; i++) {
|
|
||||||
if (outputs[i] == null || outputs[i] == firstSoundOut) continue;
|
|
||||||
outputs[i].Dispose();
|
|
||||||
}
|
|
||||||
outputs = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
|
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading;
|
|
||||||
using SharpWave;
|
|
||||||
using SharpWave.Codecs.Vorbis;
|
|
||||||
|
|
||||||
namespace ClassicalSharp.Audio{
|
|
||||||
|
|
||||||
public sealed partial class AudioPlayer : IGameComponent {
|
|
||||||
|
|
||||||
IAudioOutput musicOut;
|
|
||||||
IAudioOutput[] monoOutputs, stereoOutputs;
|
|
||||||
string[] files, musicFiles;
|
|
||||||
Thread musicThread;
|
|
||||||
Game game;
|
|
||||||
|
|
||||||
void IGameComponent.Init(Game game) {
|
|
||||||
this.game = game;
|
|
||||||
if (Platform.DirectoryExists("audio")) {
|
|
||||||
files = Platform.DirectoryFiles("audio");
|
|
||||||
} else {
|
} else {
|
||||||
files = new string[0];
|
volume *= 0.50f;
|
||||||
|
if (type == SOUND_METAL) fmt.SampleRate = (fmt.SampleRate * 7) / 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
game.MusicVolume = GetVolume(OptionsKey.MusicVolume, OptionsKey.UseMusic);
|
AudioHandle* outputs = NULL;
|
||||||
SetMusic(game.MusicVolume);
|
if (fmt.Channels == 1) outputs = monoOutputs;
|
||||||
game.SoundsVolume = GetVolume(OptionsKey.SoundsVolume, OptionsKey.UseSound);
|
if (fmt.Channels == 2) outputs = stereoOutputs;
|
||||||
SetSounds(game.SoundsVolume);
|
if (outputs == NULL) return; /* TODO: > 2 channel sound?? */
|
||||||
game.UserEvents.BlockChanged += PlayBlockSound;
|
Int32 i;
|
||||||
|
|
||||||
|
/* Try to play on fresh device, or device with same data format */
|
||||||
|
for (i = 0; i < AUDIO_MAX_HANDLES; i++) {
|
||||||
|
AudioHandle output = outputs[i];
|
||||||
|
if (output == -1) {
|
||||||
|
Platform_AudioInit(&output, 1);
|
||||||
|
outputs[i] = output;
|
||||||
|
}
|
||||||
|
if (!Platform_AudioIsFinished(output)) continue;
|
||||||
|
|
||||||
|
struct AudioFormat* l = Platform_AudioGetFormat(output);
|
||||||
|
if (l->Channels == 0 || AudioFormat_Eq(l, &fmt)) {
|
||||||
|
PlaySound(output, volume); return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int GetVolume(string volKey, string boolKey) {
|
/* Try again with all devices, even if need to recreate one (expensive) */
|
||||||
int volume = Options.GetInt(volKey, 0, 100, 0);
|
for (i = 0; i < AUDIO_MAX_HANDLES; i++) {
|
||||||
if (volume != 0) return volume;
|
AudioHandle output = outputs[i];
|
||||||
|
if (!Platform_AudioIsFinished(output)) continue;
|
||||||
|
|
||||||
volume = Options.GetBool(boolKey, false) ? 100 : 0;
|
PlaySound(output, volume); return;
|
||||||
Options.Set(boolKey, null);
|
}
|
||||||
return volume;
|
}
|
||||||
|
|
||||||
|
static void Audio_PlayBlockSound(void* obj, Vector3I coords, BlockID oldBlock, BlockID block) {
|
||||||
|
if (block == BLOCK_AIR) {
|
||||||
|
PlayDigSound(Block_DigSounds[oldBlock]);
|
||||||
|
} else if (!Game_ClassicMode) {
|
||||||
|
PlayDigSound(Block_StepSounds[block]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Audio_FreeOutputs(AudioHandle* outputs) {
|
||||||
|
bool anyPlaying = true;
|
||||||
|
Int32 i;
|
||||||
|
|
||||||
|
while (anyPlaying) {
|
||||||
|
anyPlaying = false;
|
||||||
|
for (i = 0; i < AUDIO_MAX_HANDLES; i++) {
|
||||||
|
if (outputs[i] == -1) continue;
|
||||||
|
anyPlaying |= !Platform_AudioIsFinished(outputs[i]);
|
||||||
|
}
|
||||||
|
if (anyPlaying) Platform_ThreadSleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetMusic(int volume) {
|
for (i = 0; i < AUDIO_MAX_HANDLES; i++) {
|
||||||
|
if (outputs[i] == -1) continue;
|
||||||
|
Platform_AudioFree(outputs[i]);
|
||||||
|
outputs[i] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Audio_InitSounds(void) {
|
||||||
|
if (digBoard.Count || stepBoard.Count) return;
|
||||||
|
String dig = String_FromConst("dig_");
|
||||||
|
Soundboard_Init(&digBoard, &dig, &files);
|
||||||
|
String step = String_FromConst("step_");
|
||||||
|
Soundboard_Init(&stepBoard, &step, &files);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Audio_FreeSounds(void) {
|
||||||
|
Audio_FreeOutputs(monoOutputs);
|
||||||
|
Audio_FreeOutputs(stereoOutputs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Audio_SetSounds(Int32 volume) {
|
||||||
|
if (volume) Audio_InitSounds();
|
||||||
|
else Audio_FreeSounds();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Audio_PlayDigSound(UInt8 type) { Audio_PlaySound(type, &digBoard); }
|
||||||
|
void Audio_PlayStepSound(UInt8 type) { Audio_PlaySound(type, &stepBoard); }
|
||||||
|
|
||||||
|
|
||||||
|
/*########################################################################################################################*
|
||||||
|
*--------------------------------------------------------Music------------------------------------------------------------*
|
||||||
|
*#########################################################################################################################*/
|
||||||
|
AudioHandle musicOut = -1;
|
||||||
|
string[] files, musicFiles;
|
||||||
|
Thread musicThread;
|
||||||
|
|
||||||
|
void SetMusic(int volume) {
|
||||||
if (volume > 0) InitMusic();
|
if (volume > 0) InitMusic();
|
||||||
else DisposeMusic();
|
else DisposeMusic();
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitMusic() {
|
void InitMusic() {
|
||||||
if (musicThread != null) { musicOut.SetVolume(game.MusicVolume / 100.0f); return; }
|
if (musicThread != null) { musicOut.SetVolume(game.MusicVolume / 100.0f); return; }
|
||||||
|
|
||||||
int musicCount = 0;
|
int musicCount = 0;
|
||||||
@ -334,10 +288,10 @@ namespace ClassicalSharp.Audio{
|
|||||||
musicOut = GetPlatformOut();
|
musicOut = GetPlatformOut();
|
||||||
musicOut.Create(10);
|
musicOut.Create(10);
|
||||||
musicThread = MakeThread(DoMusicThread, "ClassicalSharp.DoMusic");
|
musicThread = MakeThread(DoMusicThread, "ClassicalSharp.DoMusic");
|
||||||
}
|
}
|
||||||
|
|
||||||
EventWaitHandle musicHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
|
EventWaitHandle musicHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
|
||||||
void DoMusicThread() {
|
void DoMusicThread() {
|
||||||
if (musicFiles.Length == 0) return;
|
if (musicFiles.Length == 0) return;
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
while (!disposingMusic) {
|
while (!disposingMusic) {
|
||||||
@ -363,9 +317,9 @@ namespace ClassicalSharp.Audio{
|
|||||||
int delay = 2000 * 60 + rnd.Next(0, 5000 * 60);
|
int delay = 2000 * 60 + rnd.Next(0, 5000 * 60);
|
||||||
musicHandle.WaitOne(delay, false);
|
musicHandle.WaitOne(delay, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleMusicError(InvalidOperationException ex) {
|
void HandleMusicError(InvalidOperationException ex) {
|
||||||
ErrorHandler.LogError("AudioPlayer.DoMusicThread()", ex);
|
ErrorHandler.LogError("AudioPlayer.DoMusicThread()", ex);
|
||||||
if (ex.Message == "No audio devices found")
|
if (ex.Message == "No audio devices found")
|
||||||
game.Chat.Add("&cNo audio devices found, disabling music.");
|
game.Chat.Add("&cNo audio devices found, disabling music.");
|
||||||
@ -374,37 +328,24 @@ namespace ClassicalSharp.Audio{
|
|||||||
|
|
||||||
SetMusic(0);
|
SetMusic(0);
|
||||||
game.MusicVolume = 0;
|
game.MusicVolume = 0;
|
||||||
}
|
}
|
||||||
|
bool disposingMusic;
|
||||||
|
|
||||||
bool disposingMusic;
|
void DisposeMusic() {
|
||||||
void IDisposable.Dispose() {
|
|
||||||
DisposeMusic();
|
|
||||||
DisposeSound();
|
|
||||||
musicHandle.Close();
|
|
||||||
game.UserEvents.BlockChanged -= PlayBlockSound;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisposeMusic() {
|
|
||||||
disposingMusic = true;
|
disposingMusic = true;
|
||||||
musicHandle.Set();
|
musicHandle.Set();
|
||||||
DisposeOf(ref musicOut, ref musicThread);
|
DisposeOf(ref musicOut, ref musicThread);
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread MakeThread(ThreadStart func, string name) {
|
Thread MakeThread(ThreadStart func, string name) {
|
||||||
Thread thread = new Thread(func);
|
Thread thread = new Thread(func);
|
||||||
thread.Name = name;
|
thread.Name = name;
|
||||||
thread.IsBackground = true;
|
thread.IsBackground = true;
|
||||||
thread.Start();
|
thread.Start();
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
IAudioOutput GetPlatformOut() {
|
void DisposeOf(ref IAudioOutput output, ref Thread thread) {
|
||||||
if (OpenTK.Configuration.RunningOnWindows && !Options.GetBool(OptionsKey.ForceOpenAL, false))
|
|
||||||
return new WinMmOut();
|
|
||||||
return new OpenALOut();
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisposeOf(ref IAudioOutput output, ref Thread thread) {
|
|
||||||
if (output == null) return;
|
if (output == null) return;
|
||||||
output.Stop();
|
output.Stop();
|
||||||
thread.Join();
|
thread.Join();
|
||||||
@ -412,6 +353,38 @@ namespace ClassicalSharp.Audio{
|
|||||||
output.Dispose();
|
output.Dispose();
|
||||||
output = null;
|
output = null;
|
||||||
thread = null;
|
thread = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*########################################################################################################################*
|
||||||
|
*--------------------------------------------------------General----------------------------------------------------------*
|
||||||
|
*#########################################################################################################################*/
|
||||||
|
static Int32 Audio_GetVolume(const UChar* volKey, const UChar* boolKey) {
|
||||||
|
Int32 volume = Options_GetInt(volKey, 0, 100, 0);
|
||||||
|
if (volume) return volume;
|
||||||
|
|
||||||
|
volume = Options_GetBool(boolKey, false) ? 100 : 0;
|
||||||
|
Options_Set(boolKey, NULL);
|
||||||
|
return volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Audio_Init(void) {
|
||||||
|
StringsBuffer_Init(&files);
|
||||||
|
String path = String_FromConst("audio");
|
||||||
|
if (Platform_DirectoryExists(&path)) {
|
||||||
|
files = Platform.DirectoryFiles("audio");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Game_MusicVolume = GetVolume(OPT_MUSIC_VOLUME, OPT_USE_MUSIC);
|
||||||
|
Audio_SetMusic(Game_MusicVolume);
|
||||||
|
Game_SoundsVolume = GetVolume(OPT_SOUND_VOLUME, OPT_USE_SOUND);
|
||||||
|
Audio_SetSounds(Game_SoundsVolume);
|
||||||
|
game.UserEvents.BlockChanged += PlayBlockSound;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void Audio_Free(void) {
|
||||||
|
DisposeMusic();
|
||||||
|
DisposeSound();
|
||||||
|
musicHandle.Close();
|
||||||
|
game.UserEvents.BlockChanged -= PlayBlockSound;
|
||||||
}
|
}
|
||||||
|
@ -114,14 +114,15 @@ ReturnCode Platform_HttpFreeRequest(void* handle);
|
|||||||
ReturnCode Platform_HttpFree(void);
|
ReturnCode Platform_HttpFree(void);
|
||||||
|
|
||||||
#define AUDIO_MAX_CHUNKS 5
|
#define AUDIO_MAX_CHUNKS 5
|
||||||
struct AudioFormat { UInt8 Channels, BitsPerSample; UInt16 Frequency };
|
struct AudioFormat { UInt8 Channels, BitsPerSample; Int32 SampleRate; };
|
||||||
#define AudioFormat_Eq(a, b) (a->Channels == b->Channels && a->BitsPerSample == b->BitsPerSample && a->Frequency == b->Frequency)
|
#define AudioFormat_Eq(a, b) ((a)->Channels == (b)->Channels && (a)->BitsPerSample == (b)->BitsPerSample && (a)->SampleRate == (b)->SampleRate)
|
||||||
|
typedef Int32 AudioHandle;
|
||||||
|
|
||||||
void Platform_AudioInit(Int32* handle, Int32 buffers);
|
void Platform_AudioInit(AudioHandle* handle, Int32 buffers);
|
||||||
void Platform_AudioFree(Int32 handle);
|
void Platform_AudioFree(AudioHandle handle);
|
||||||
struct AudioFormat* Platform_AudioGetFormat(Int32 handle);
|
struct AudioFormat* Platform_AudioGetFormat(AudioHandle handle);
|
||||||
void Platform_AudioSetFormat(Int32 handle, struct AudioFormat* format);
|
void Platform_AudioSetFormat(AudioHandle handle, struct AudioFormat* format);
|
||||||
void Platform_AudioPlayData(Int32 handle, Int32 idx, void* data, UInt32 dataSize);
|
void Platform_AudioPlayData(AudioHandle handle, Int32 idx, void* data, UInt32 dataSize);
|
||||||
bool Platform_AudioIsCompleted(Int32 handle, Int32 idx);
|
bool Platform_AudioIsCompleted(AudioHandle handle, Int32 idx);
|
||||||
bool Platform_AudioIsFinished(Int32 handle);
|
bool Platform_AudioIsFinished(AudioHandle handle);
|
||||||
#endif
|
#endif
|
||||||
|
@ -608,7 +608,7 @@ struct AudioContext {
|
|||||||
};
|
};
|
||||||
struct AudioContext Audio_Contexts[20];
|
struct AudioContext Audio_Contexts[20];
|
||||||
|
|
||||||
void Platform_AudioInit(Int32* handle, Int32 buffers) {
|
void Platform_AudioInit(AudioHandle* handle, Int32 buffers) {
|
||||||
Int32 i, j;
|
Int32 i, j;
|
||||||
for (i = 0; i < Array_Elems(Audio_Contexts); i++) {
|
for (i = 0; i < Array_Elems(Audio_Contexts); i++) {
|
||||||
struct AudioContext* ctx = &Audio_Contexts[i];
|
struct AudioContext* ctx = &Audio_Contexts[i];
|
||||||
@ -624,7 +624,7 @@ void Platform_AudioInit(Int32* handle, Int32 buffers) {
|
|||||||
ErrorHandler_Fail("No free audio contexts");
|
ErrorHandler_Fail("No free audio contexts");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform_AudioFree(Int32 handle) {
|
void Platform_AudioFree(AudioHandle handle) {
|
||||||
struct AudioContext* ctx = &Audio_Contexts[handle];
|
struct AudioContext* ctx = &Audio_Contexts[handle];
|
||||||
if (!ctx->Handle) return;
|
if (!ctx->Handle) return;
|
||||||
|
|
||||||
@ -633,12 +633,12 @@ void Platform_AudioFree(Int32 handle) {
|
|||||||
ErrorHandler_CheckOrFail(result, "Audio - closing device");
|
ErrorHandler_CheckOrFail(result, "Audio - closing device");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AudioFormat* Platform_AudioGetFormat(Int32 handle) {
|
struct AudioFormat* Platform_AudioGetFormat(AudioHandle handle) {
|
||||||
struct AudioContext* ctx = &Audio_Contexts[handle];
|
struct AudioContext* ctx = &Audio_Contexts[handle];
|
||||||
return &ctx->Format;
|
return &ctx->Format;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform_AudioSetFormat(Int32 handle, struct AudioFormat* format) {
|
void Platform_AudioSetFormat(AudioHandle handle, struct AudioFormat* format) {
|
||||||
struct AudioContext* ctx = &Audio_Contexts[handle];
|
struct AudioContext* ctx = &Audio_Contexts[handle];
|
||||||
struct AudioFormat* cur = &ctx->Format;
|
struct AudioFormat* cur = &ctx->Format;
|
||||||
|
|
||||||
@ -651,7 +651,7 @@ void Platform_AudioSetFormat(Int32 handle, struct AudioFormat* format) {
|
|||||||
fmt.wFormatTag = WAVE_FORMAT_PCM;
|
fmt.wFormatTag = WAVE_FORMAT_PCM;
|
||||||
fmt.wBitsPerSample = format->BitsPerSample;
|
fmt.wBitsPerSample = format->BitsPerSample;
|
||||||
fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
|
fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
|
||||||
fmt.nSamplesPerSec = format->Frequency;
|
fmt.nSamplesPerSec = format->SampleRate;
|
||||||
fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign;
|
fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign;
|
||||||
|
|
||||||
if (waveOutGetNumDevs() == 0u) ErrorHandler_Fail("No audio devices found");
|
if (waveOutGetNumDevs() == 0u) ErrorHandler_Fail("No audio devices found");
|
||||||
@ -659,7 +659,7 @@ void Platform_AudioSetFormat(Int32 handle, struct AudioFormat* format) {
|
|||||||
ErrorHandler_CheckOrFail(result, "Audio - opening device");
|
ErrorHandler_CheckOrFail(result, "Audio - opening device");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform_AudioPlayData(Int32 handle, Int32 idx, void* data, UInt32 dataSize) {
|
void Platform_AudioPlayData(AudioHandle handle, Int32 idx, void* data, UInt32 dataSize) {
|
||||||
struct AudioContext* ctx = &Audio_Contexts[handle];
|
struct AudioContext* ctx = &Audio_Contexts[handle];
|
||||||
WAVEHDR* hdr = &ctx->Headers[idx];
|
WAVEHDR* hdr = &ctx->Headers[idx];
|
||||||
Platform_MemSet(hdr, 0, sizeof(WAVEHDR));
|
Platform_MemSet(hdr, 0, sizeof(WAVEHDR));
|
||||||
@ -674,7 +674,7 @@ void Platform_AudioPlayData(Int32 handle, Int32 idx, void* data, UInt32 dataSize
|
|||||||
ErrorHandler_CheckOrFail(result, "Audio - write header");
|
ErrorHandler_CheckOrFail(result, "Audio - write header");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Platform_AudioIsCompleted(Int32 handle, Int32 idx) {
|
bool Platform_AudioIsCompleted(AudioHandle handle, Int32 idx) {
|
||||||
struct AudioContext* ctx = &Audio_Contexts[handle];
|
struct AudioContext* ctx = &Audio_Contexts[handle];
|
||||||
WAVEHDR* hdr = &ctx->Headers[idx];
|
WAVEHDR* hdr = &ctx->Headers[idx];
|
||||||
if (!(hdr->dwFlags & WHDR_DONE)) return false;
|
if (!(hdr->dwFlags & WHDR_DONE)) return false;
|
||||||
@ -686,7 +686,7 @@ bool Platform_AudioIsCompleted(Int32 handle, Int32 idx) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Platform_AudioIsFinished(Int32 handle) {
|
bool Platform_AudioIsFinished(AudioHandle handle) {
|
||||||
struct AudioContext* ctx = &Audio_Contexts[handle];
|
struct AudioContext* ctx = &Audio_Contexts[handle];
|
||||||
Int32 i;
|
Int32 i;
|
||||||
for (i = 0; i < ctx->NumBuffers; i++) {
|
for (i = 0; i < ctx->NumBuffers; i++) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user