public SoundEffect GetSound(string name) { string namelow = name.ToLowerFast(); SoundEffect sfx; if (Effects.TryGetValue(namelow, out sfx)) { return sfx; } sfx = LoadSound(namelow); if (sfx != null) { return sfx; } sfx = new SoundEffect(); sfx.Name = namelow; sfx.Internal = Noise.Internal; return sfx; }
public ActiveSound(SoundEffect sfx) { Effect = sfx; }
public void Init(Client tclient, ClientCVar cvar) { if (Context != null) { Context.Dispose(); } TheClient = tclient; CVars = cvar; Context = new AudioContext(AudioContext.DefaultDevice, 0, 0, false, true); Context.MakeCurrent(); try { if (Microphone != null) { Microphone.StopEcho(); } Microphone = new MicrophoneHandler(this); } catch (Exception ex) { SysConsole.Output("Loading microphone handling", ex); } if (Effects != null) { foreach (SoundEffect sfx in Effects.Values) { sfx.Internal = -2; } } Effects = new Dictionary<string, SoundEffect>(); PlayingNow = new List<ActiveSound>(); Noise = LoadSound(new DataStream(Convert.FromBase64String(NoiseDefault.NoiseB64)), "noise"); }
/// <summary> /// NOTE: *NOT* guaranteed to play a sound effect immediately, regardless of input! Some sound effects will be delayed! /// </summary> public void Play(SoundEffect sfx, bool loop, Location pos, float pitch = 1, float volume = 1, float seek_seconds = 0, Action<ActiveSound> callback = null) { if (sfx.Internal == -2) { Play(GetSound(sfx.Name), loop, pos, pitch, volume, seek_seconds, callback); return; } if (pitch <= 0 || pitch > 2) { throw new ArgumentException("Must be between 0 and 2", "pitch"); } if (volume == 0) { return; } if (volume <= 0 || volume > 1) { throw new ArgumentException("Must be between 0 and 1", "volume"); } Action playSound = () => { ActiveSound actsfx = new ActiveSound(sfx); actsfx.Engine = this; actsfx.Position = pos; actsfx.Pitch = pitch * CVars.a_globalpitch.ValueF; actsfx.Gain = volume; actsfx.Loop = loop; actsfx.Create(); actsfx.Play(); if (seek_seconds != 0) { actsfx.Seek(seek_seconds); } PlayingNow.Add(actsfx); if (callback != null) { callback(actsfx); } }; lock (sfx) { if (sfx.Internal == -1) { sfx.Loaded += (o, e) => { playSound(); }; return; } } playSound(); }
public SoundEffect LoadSound(DataStream stream, string name) { SoundEffect sfx = new SoundEffect(); sfx.Name = name; int channels; int bits; int rate; byte[] data = LoadWAVE(stream, out channels, out bits, out rate); sfx.Internal = AL.GenBuffer(); AL.BufferData(sfx.Internal, GetSoundFormat(channels, bits), data, data.Length, rate); return sfx; }
public SoundEffect LoadSound(string name) { try { string newname = "sounds/" + name + ".ogg"; if (!TheClient.Files.Exists(newname)) { return null; } SoundEffect tsfx = new SoundEffect(); tsfx.Name = name; tsfx.Internal = -1; TheClient.Schedule.StartASyncTask(() => { SoundEffect ts = LoadVorbisSound(TheClient.Files.ReadToStream(newname), name); lock (tsfx) { tsfx.Internal = ts.Internal; } if (tsfx.Loaded != null) { TheClient.Schedule.ScheduleSyncTask(() => { if (tsfx.Loaded != null) { tsfx.Loaded.Invoke(tsfx, null); } }); } }); return tsfx; } catch (Exception ex) { SysConsole.Output(OutputType.ERROR, "Reading sound file '" + name + "': " + ex.ToString()); return null; } }