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: " + e.Message,
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);
267 // If we are standing still, nothing to update now, but
268 // FMOD needs a 'tick' anyway for callbacks, etc. In looping
269 // 'game' programs, the loop is the 'tick'. Since Radegast
270 // uses events and has no loop, we use this position update
271 // thread to drive the FMOD tick. Have to move more than
272 // 500mm or turn more than 10 desgrees to bother with.
274 if (newPosition.ApproxEquals(lastpos, 0.5f) &&
275 Math.Abs(my.Movement.BodyRotation.W - lastface) < 0.2)
277 invoke(new SoundDelegate(delegate
279 FMODExec(system.update());
284 // We have moved or turned. Remember new position.
285 lastpos = newPosition;
286 lastface = my.Movement.BodyRotation.W;
288 // Convert coordinate spaces.
289 FMOD.VECTOR listenerpos = FromOMVSpace(newPosition);
291 // Get azimuth from the facing Quaternion. Note we assume the
292 // avatar is standing upright. Avatars in unusual positions
293 // hear things from unpredictable directions.
294 // By definition, facing.W = Cos( angle/2 )
295 // With angle=0 meaning East.
296 double angle = 2.0 * Math.Acos(my.Movement.BodyRotation.W);
298 // Construct facing unit vector in FMOD coordinates.
299 FMOD.VECTOR forward = new FMOD.VECTOR();
300 forward.x = (float)Math.Sin(-angle); // South
302 forward.z = (float)Math.Cos(angle); // East
306 "Standing at <{0:0.0},{1:0.0},{2:0.0}> facing {3:d}",
310 (int)(angle * 180.0 / 3.141592)),
311 Helpers.LogLevel.Debug);
313 // Tell FMOD the new orientation.
314 invoke( new SoundDelegate( delegate
316 FMODExec( system.set3DListenerAttributes(
318 ref listenerpos, // Position
319 ref ZeroVector, // Velocity
320 ref forward, // Facing direction
321 ref UpVector )); // Top of head
323 FMODExec(system.update());
329 /// Handle request to play a sound, which might (or mioght not) have been preloaded.
331 /// <param name="sender"></param>
332 /// <param name="e"></param>
333 private void Sound_SoundTrigger(object sender, SoundTriggerEventArgs e)
335 if (e.SoundID == UUID.Zero) return;
337 Logger.Log("Trigger sound " + e.SoundID.ToString() +
338 " in object " + e.ObjectID.ToString(),
339 Helpers.LogLevel.Debug);
346 e.Gain * ObjectVolume);
350 /// Handle sound attached to an object
352 /// <param name="sender"></param>
353 /// <param name="e"></param>
354 private void Sound_AttachedSound(object sender, AttachedSoundEventArgs e)
356 // We seem to get a lot of these zero sounds.
357 if (e.SoundID == UUID.Zero) return;
359 if ((e.Flags & SoundFlags.Stop) == SoundFlags.Stop)
361 BufferSound.Kill(e.SoundID);
365 // This event tells us the Object ID, but not the Prim info directly.
366 // So we look it up in our internal Object memory.
367 Simulator sim = Instance.Client.Network.CurrentSim;
368 Primitive p = sim.ObjectsPrimitives.Find((Primitive p2) => { return p2.ID == e.ObjectID; });
371 // If this is a child prim, its position is relative to the root.
372 Vector3 fullPosition = p.Position;
376 sim.ObjectsPrimitives.TryGetValue(p.ParentID, out parentP);
377 if (parentP == null) return;
378 fullPosition += parentP.Position;
383 (e.Flags & SoundFlags.Loop) == SoundFlags.Loop,
386 e.Gain * ObjectVolume);
391 /// Handle request to preload a sound for playing later.
393 /// <param name="sender"></param>
394 /// <param name="e"></param>
395 private void Sound_PreloadSound(object sender, PreloadSoundEventArgs e)
397 if (e.SoundID == UUID.Zero) return;
399 new BufferSound( e.SoundID );
402 private void Objects_ObjectPropertiesUpdated(object sender, ObjectPropertiesUpdatedEventArgs e)
404 HandleObjectSound(e.Prim, e.Simulator );
408 /// Handle object updates, looking for sound events
410 /// <param name="sender"></param>
411 /// <param name="e"></param>
412 private void Objects_ObjectUpdate(object sender, PrimEventArgs e)
414 HandleObjectSound(e.Prim, e.Simulator );
418 /// Handle deletion of a noise-making object
420 /// <param name="sender"></param>
421 /// <param name="e"></param>
422 void Objects_KillObject(object sender, KillObjectEventArgs e)
425 if (!e.Simulator.ObjectsPrimitives.TryGetValue(e.ObjectLocalID, out p)) return;
427 // Objects without sounds are not interesting.
428 if (p.Sound == null) return;
429 if (p.Sound == UUID.Zero) return;
431 BufferSound.Kill(p.Sound);
435 /// Common object sound processing for various Update events
437 /// <param name="p"></param>
438 /// <param name="s"></param>
439 private void HandleObjectSound(Primitive p, Simulator s)
441 // Objects without sounds are not interesting.
442 if (p.Sound == null) return;
443 if (p.Sound == UUID.Zero) return;
445 if ((p.SoundFlags & SoundFlags.Stop) == SoundFlags.Stop)
447 BufferSound.Kill(p.Sound);
451 // If this is a child prim, its position is relative to the root prim.
452 Vector3 fullPosition = p.Position;
456 if (!s.ObjectsPrimitives.TryGetValue(p.ParentID, out parentP)) return;
457 fullPosition += parentP.Position;
460 // See if this is an update to something we already know about.
461 if (allBuffers.ContainsKey(p.Sound))
463 // Exists already, so modify existing sound.
464 //TODO posible to change sound on the same object. Key by Object?
465 BufferSound snd = allBuffers[p.Sound];
466 snd.Volume = p.SoundGain * ObjectVolume;
467 snd.Position = fullPosition;
471 // Does not exist, so create a new one.
474 (p.SoundFlags & SoundFlags.Loop) == SoundFlags.Loop,
476 fullPosition, //Instance.State.GlobalPosition(e.Simulator, fullPosition),
477 p.SoundGain * ObjectVolume);
482 /// Control the volume of all inworld sounds
484 public float ObjectVolume
488 AllObjectVolume = value;
489 BufferSound.AdjustVolumes();
491 get { return AllObjectVolume; }
494 private bool m_objectEnabled = true;
496 /// Enable and Disable inworld sounds
498 public bool ObjectEnable
506 // Subscribe to events about inworld sounds
507 Instance.Client.Sound.SoundTrigger += new EventHandler<SoundTriggerEventArgs>(Sound_SoundTrigger);
508 Instance.Client.Sound.AttachedSound += new EventHandler<AttachedSoundEventArgs>(Sound_AttachedSound);
509 Instance.Client.Sound.PreloadSound += new EventHandler<PreloadSoundEventArgs>(Sound_PreloadSound);
510 Instance.Client.Objects.ObjectUpdate += new EventHandler<PrimEventArgs>(Objects_ObjectUpdate);
511 Instance.Client.Objects.ObjectPropertiesUpdated += new EventHandler<ObjectPropertiesUpdatedEventArgs>(Objects_ObjectPropertiesUpdated);
512 Instance.Client.Objects.KillObject += new EventHandler<KillObjectEventArgs>(Objects_KillObject);
513 Instance.Client.Network.SimChanged += new EventHandler<SimChangedEventArgs>(Network_SimChanged);
514 Logger.Log("Inworld sound enabled", Helpers.LogLevel.Info);
518 // Subscribe to events about inworld sounds
519 Instance.Client.Sound.SoundTrigger -= new EventHandler<SoundTriggerEventArgs>(Sound_SoundTrigger);
520 Instance.Client.Sound.AttachedSound -= new EventHandler<AttachedSoundEventArgs>(Sound_AttachedSound);
521 Instance.Client.Sound.PreloadSound -= new EventHandler<PreloadSoundEventArgs>(Sound_PreloadSound);
522 Instance.Client.Objects.ObjectUpdate -= new EventHandler<PrimEventArgs>(Objects_ObjectUpdate);
523 Instance.Client.Objects.ObjectPropertiesUpdated -= new EventHandler<ObjectPropertiesUpdatedEventArgs>(Objects_ObjectPropertiesUpdated);
524 Instance.Client.Objects.KillObject -= new EventHandler<KillObjectEventArgs>(Objects_KillObject);
525 Instance.Client.Network.SimChanged -= new EventHandler<SimChangedEventArgs>(Network_SimChanged);
527 // Stop all running sounds
528 BufferSound.KillAll();
530 Logger.Log("Inworld sound disabled", Helpers.LogLevel.Info);
535 System.Console.WriteLine("Error on enable/disable: "+e.Message);
538 m_objectEnabled = value;
540 get { return m_objectEnabled; }
544 /// Watch for Teleports to cancel all the old sounds
546 /// <param name="sender"></param>
547 /// <param name="e"></param>
548 void Network_SimChanged(object sender, SimChangedEventArgs e)
550 BufferSound.KillAll();
556 public class MediaException : Exception
558 public MediaException(string msg)