2 // Radegast Metaverse Client
3 // Copyright (c) 2009, Radegast Development Team
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of the application "Radegast", nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 using System.Collections.Generic;
35 using System.Threading;
37 using OpenMetaverse.Assets;
39 namespace Radegast.Media
41 public class MediaManager : MediaObject
44 /// Indicated wheather spund sytem is ready for use
46 public bool SoundSystemAvailable { get { return soundSystemAvailable; } }
47 private bool soundSystemAvailable = false;
48 private Thread soundThread;
49 private Thread listenerThread;
50 public RadegastInstance Instance;
52 private List<MediaObject> sounds = new List<MediaObject>();
54 public MediaManager(RadegastInstance instance)
57 this.Instance = instance;
60 loadCallback = new FMOD.SOUND_NONBLOCKCALLBACK(DispatchNonBlockCallback);
61 endCallback = new FMOD.CHANNEL_CALLBACK(DispatchEndCallback);
62 allBuffers = new Dictionary<UUID,BufferSound>();
64 // Start the background thread that does all the FMOD calls.
65 soundThread = new Thread(new ThreadStart(CommandLoop));
66 soundThread.IsBackground = true;
67 soundThread.Name = "SoundThread";
70 // Start the background thread that updates listerner position.
71 listenerThread = new Thread(new ThreadStart(ListenerUpdate));
72 listenerThread.IsBackground = true;
73 listenerThread.Name = "ListenerThread";
74 listenerThread.Start();
76 // Initial inworld-sound setting comes from Config.
81 /// Thread that processes FMOD calls.
83 private void CommandLoop()
85 SoundDelegate action = null;
87 // Initialze a bunch of static values
95 allSounds = new Dictionary<IntPtr,MediaObject>();
96 allChannels = new Dictionary<IntPtr, MediaObject>();
98 // Initialize the FMOD sound package
100 if (!this.soundSystemAvailable) return;
102 // Initialize the command queue.
103 queue = new Queue<SoundDelegate>();
107 // Wait for something to show up in the queue.
110 while (queue.Count == 0)
114 action = queue.Dequeue();
117 // We have an action, so call it.
125 Logger.Log("Error in sound action:\n " + e.Message + "\n" + e.StackTrace,
126 Helpers.LogLevel.Error);
132 /// Initialize the FMOD sound system.
134 private void InitFMOD()
138 FMODExec(FMOD.Factory.System_Create(ref system));
140 FMODExec(system.getVersion(ref version));
142 if (version < FMOD.VERSION.number)
143 throw new MediaException("You are using an old version of FMOD " +
144 version.ToString("X") +
145 ". This program requires " +
146 FMOD.VERSION.number.ToString("X") + ".");
148 // Assume no special hardware capabilities except 5.1 surround sound.
149 FMOD.CAPS caps = FMOD.CAPS.NONE;
150 FMOD.SPEAKERMODE speakermode = FMOD.SPEAKERMODE._5POINT1;
152 // Get the capabilities of the driver.
153 int minfrequency = 0, maxfrequency = 0;
154 StringBuilder name = new StringBuilder(128);
155 FMODExec(system.getDriverCaps(0, ref caps,
161 // Set FMOD speaker mode to what the driver supports.
162 FMODExec(system.setSpeakerMode(speakermode));
164 // Forcing the ALSA sound system on Linux seems to avoid a CPU loop
165 if (System.Environment.OSVersion.Platform == PlatformID.Unix)
166 FMODExec(system.setOutput(FMOD.OUTPUTTYPE.ALSA));
168 // The user has the 'Acceleration' slider set to off, which
169 // is really bad for latency. At 48khz, the latency between
170 // issuing an fmod command and hearing it will now be about 213ms.
171 if ((caps & FMOD.CAPS.HARDWARE_EMULATED) == FMOD.CAPS.HARDWARE_EMULATED)
173 FMODExec(system.setDSPBufferSize(1024, 10));
176 // Get driver information so we can check for a wierd one.
177 FMOD.GUID guid = new FMOD.GUID();
178 FMODExec(system.getDriverInfo(0, name, 128, ref guid));
180 // Sigmatel sound devices crackle for some reason if the format is pcm 16bit.
181 // pcm floating point output seems to solve it.
182 if (name.ToString().IndexOf("SigmaTel") != -1)
184 FMODExec(system.setSoftwareFormat(
186 FMOD.SOUND_FORMAT.PCMFLOAT,
188 FMOD.DSP_RESAMPLER.LINEAR)
192 // Try to initialize with all those settings, and Max 32 channels.
193 FMOD.RESULT result = system.init(32, FMOD.INITFLAG.NORMAL, (IntPtr)null);
194 if (result == FMOD.RESULT.ERR_OUTPUT_CREATEBUFFER)
196 // Can not handle surround sound - back to Stereo.
197 FMODExec(system.setSpeakerMode(FMOD.SPEAKERMODE.STEREO));
200 FMODExec(system.init(
202 FMOD.INITFLAG.NORMAL,
207 // Set real-world effect scales.
208 FMODExec(system.set3DSettings(
209 1.0f, // Doppler scale
210 1.0f, // Distance scale is meters
211 1.0f) // Rolloff factor
214 soundSystemAvailable = true;
215 Logger.Log("Initialized FMOD Ex", Helpers.LogLevel.Debug);
219 Logger.Log("Failed to initialize the sound system: ", Helpers.LogLevel.Warning, ex);
223 public override void Dispose()
227 for (int i = 0; i < sounds.Count; i++)
229 if (!sounds[i].Disposed)
239 Logger.Log("FMOD interface stopping", Helpers.LogLevel.Info);
248 /// Thread to update listener position and generally keep
251 private void ListenerUpdate()
253 // Notice changes in position or direction.
254 Vector3 lastpos = new Vector3(0.0f, 0.0f, 0.0f );
255 float lastface = 0.0f;
259 // Two updates per second.
262 if (system == null) continue;
264 AgentManager my = Instance.Client.Self;
265 Vector3 newPosition = new Vector3(my.SimPosition);
266 float newFace = my.SimRotation.W;
268 // If we are standing still, nothing to update now, but
269 // FMOD needs a 'tick' anyway for callbacks, etc. In looping
270 // 'game' programs, the loop is the 'tick'. Since Radegast
271 // uses events and has no loop, we use this position update
272 // thread to drive the FMOD tick. Have to move more than
273 // 500mm or turn more than 10 desgrees to bother with.
275 if (newPosition.ApproxEquals(lastpos, 0.5f) &&
276 Math.Abs(newFace - lastface) < 0.2)
278 invoke(new SoundDelegate(delegate
280 FMODExec(system.update());
285 // We have moved or turned. Remember new position.
286 lastpos = newPosition;
289 // Convert coordinate spaces.
290 FMOD.VECTOR listenerpos = FromOMVSpace(newPosition);
292 // Get azimuth from the facing Quaternion. Note we assume the
293 // avatar is standing upright. Avatars in unusual positions
294 // hear things from unpredictable directions.
295 // By definition, facing.W = Cos( angle/2 )
296 // With angle=0 meaning East.
297 double angle = 2.0 * Math.Acos(newFace);
299 // Construct facing unit vector in FMOD coordinates.
300 // Z is East, X is South, Y is up.
301 FMOD.VECTOR forward = new FMOD.VECTOR();
302 forward.x = (float)Math.Sin(angle); // South
304 forward.z = (float)Math.Cos(angle); // East
308 "Standing at <{0:0.0},{1:0.0},{2:0.0}> facing {3:d}",
312 (int)(angle * 180.0 / 3.141592)),
313 Helpers.LogLevel.Debug);
315 // Tell FMOD the new orientation.
316 invoke( new SoundDelegate( delegate
318 FMODExec( system.set3DListenerAttributes(
320 ref listenerpos, // Position
321 ref ZeroVector, // Velocity
322 ref forward, // Facing direction
323 ref UpVector )); // Top of head
325 FMODExec(system.update());
331 /// Handle request to play a sound, which might (or mioght not) have been preloaded.
333 /// <param name="sender"></param>
334 /// <param name="e"></param>
335 private void Sound_SoundTrigger(object sender, SoundTriggerEventArgs e)
337 if (e.SoundID == UUID.Zero) return;
339 Logger.Log("Trigger sound " + e.SoundID.ToString() +
340 " in object " + e.ObjectID.ToString(),
341 Helpers.LogLevel.Debug);
349 e.Gain * ObjectVolume);
353 /// Handle sound attached to an object
355 /// <param name="sender"></param>
356 /// <param name="e"></param>
357 private void Sound_AttachedSound(object sender, AttachedSoundEventArgs e)
359 // We seem to get a lot of these zero sounds.
360 if (e.SoundID == UUID.Zero) return;
362 if ((e.Flags & SoundFlags.Stop) == SoundFlags.Stop)
364 BufferSound.Kill(e.SoundID);
368 // This event tells us the Object ID, but not the Prim info directly.
369 // So we look it up in our internal Object memory.
370 Simulator sim = Instance.Client.Network.CurrentSim;
371 Primitive p = sim.ObjectsPrimitives.Find((Primitive p2) => { return p2.ID == e.ObjectID; });
374 // If this is a child prim, its position is relative to the root.
375 Vector3 fullPosition = p.Position;
379 sim.ObjectsPrimitives.TryGetValue(p.ParentID, out parentP);
380 if (parentP == null) return;
381 fullPosition += parentP.Position;
387 (e.Flags & SoundFlags.Loop) == SoundFlags.Loop,
390 e.Gain * ObjectVolume);
395 /// Handle request to preload a sound for playing later.
397 /// <param name="sender"></param>
398 /// <param name="e"></param>
399 private void Sound_PreloadSound(object sender, PreloadSoundEventArgs e)
401 if (e.SoundID == UUID.Zero) return;
403 new BufferSound( e.SoundID );
406 private void Objects_ObjectPropertiesUpdated(object sender, ObjectPropertiesUpdatedEventArgs e)
408 HandleObjectSound(e.Prim, e.Simulator );
412 /// Handle object updates, looking for sound events
414 /// <param name="sender"></param>
415 /// <param name="e"></param>
416 private void Objects_ObjectUpdate(object sender, PrimEventArgs e)
418 HandleObjectSound(e.Prim, e.Simulator );
422 /// Handle deletion of a noise-making object
424 /// <param name="sender"></param>
425 /// <param name="e"></param>
426 void Objects_KillObject(object sender, KillObjectEventArgs e)
429 if (!e.Simulator.ObjectsPrimitives.TryGetValue(e.ObjectLocalID, out p)) return;
431 // Objects without sounds are not interesting.
432 if (p.Sound == null) return;
433 if (p.Sound == UUID.Zero) return;
435 BufferSound.Kill(p.Sound);
439 /// Common object sound processing for various Update events
441 /// <param name="p"></param>
442 /// <param name="s"></param>
443 private void HandleObjectSound(Primitive p, Simulator s)
445 // Objects without sounds are not interesting.
446 if (p.Sound == null) return;
447 if (p.Sound == UUID.Zero) return;
449 if ((p.SoundFlags & SoundFlags.Stop) == SoundFlags.Stop)
451 BufferSound.Kill(p.ID);
455 // If this is a child prim, its position is relative to the root prim.
456 Vector3 fullPosition = p.Position;
460 if (!s.ObjectsPrimitives.TryGetValue(p.ParentID, out parentP)) return;
461 fullPosition += parentP.Position;
464 // See if this is an update to something we already know about.
465 if (allBuffers.ContainsKey(p.ID))
467 // Exists already, so modify existing sound.
468 //TODO posible to change sound on the same object. Key by Object?
469 BufferSound snd = allBuffers[p.ID];
470 snd.Volume = p.SoundGain * ObjectVolume;
471 snd.Position = fullPosition;
475 // Does not exist, so create a new one.
479 (p.SoundFlags & SoundFlags.Loop) == SoundFlags.Loop,
481 fullPosition, //Instance.State.GlobalPosition(e.Simulator, fullPosition),
482 p.SoundGain * ObjectVolume);
487 /// Control the volume of all inworld sounds
489 public float ObjectVolume
493 AllObjectVolume = value;
494 BufferSound.AdjustVolumes();
496 get { return AllObjectVolume; }
499 private bool m_objectEnabled = true;
501 /// Enable and Disable inworld sounds
503 public bool ObjectEnable
511 // Subscribe to events about inworld sounds
512 Instance.Client.Sound.SoundTrigger += new EventHandler<SoundTriggerEventArgs>(Sound_SoundTrigger);
513 Instance.Client.Sound.AttachedSound += new EventHandler<AttachedSoundEventArgs>(Sound_AttachedSound);
514 Instance.Client.Sound.PreloadSound += new EventHandler<PreloadSoundEventArgs>(Sound_PreloadSound);
515 Instance.Client.Objects.ObjectUpdate += new EventHandler<PrimEventArgs>(Objects_ObjectUpdate);
516 Instance.Client.Objects.ObjectPropertiesUpdated += new EventHandler<ObjectPropertiesUpdatedEventArgs>(Objects_ObjectPropertiesUpdated);
517 Instance.Client.Objects.KillObject += new EventHandler<KillObjectEventArgs>(Objects_KillObject);
518 Instance.Client.Network.SimChanged += new EventHandler<SimChangedEventArgs>(Network_SimChanged);
519 Logger.Log("Inworld sound enabled", Helpers.LogLevel.Info);
523 // Subscribe to events about inworld sounds
524 Instance.Client.Sound.SoundTrigger -= new EventHandler<SoundTriggerEventArgs>(Sound_SoundTrigger);
525 Instance.Client.Sound.AttachedSound -= new EventHandler<AttachedSoundEventArgs>(Sound_AttachedSound);
526 Instance.Client.Sound.PreloadSound -= new EventHandler<PreloadSoundEventArgs>(Sound_PreloadSound);
527 Instance.Client.Objects.ObjectUpdate -= new EventHandler<PrimEventArgs>(Objects_ObjectUpdate);
528 Instance.Client.Objects.ObjectPropertiesUpdated -= new EventHandler<ObjectPropertiesUpdatedEventArgs>(Objects_ObjectPropertiesUpdated);
529 Instance.Client.Objects.KillObject -= new EventHandler<KillObjectEventArgs>(Objects_KillObject);
530 Instance.Client.Network.SimChanged -= new EventHandler<SimChangedEventArgs>(Network_SimChanged);
532 // Stop all running sounds
533 BufferSound.KillAll();
535 Logger.Log("Inworld sound disabled", Helpers.LogLevel.Info);
540 System.Console.WriteLine("Error on enable/disable: "+e.Message);
543 m_objectEnabled = value;
545 get { return m_objectEnabled; }
549 /// Watch for Teleports to cancel all the old sounds
551 /// <param name="sender"></param>
552 /// <param name="e"></param>
553 void Network_SimChanged(object sender, SimChangedEventArgs e)
555 BufferSound.KillAll();
561 public class MediaException : Exception
563 public MediaException(string msg)