positional sound!

This commit is contained in:
Chris Forbes
2010-05-12 21:39:49 +12:00
parent 4bb268075c
commit dd52aa1728
19 changed files with 60 additions and 29 deletions

View File

@@ -50,7 +50,7 @@ namespace OpenRA
world.AddFrameEndTask( world.AddFrameEndTask(
w => w.Add(new Explosion(w, args.dest, warhead.Explosion, isWater))); w => w.Add(new Explosion(w, args.dest, warhead.Explosion, isWater)));
Sound.Play(GetImpactSound(warhead, isWater)); Sound.Play(GetImpactSound(warhead, isWater), args.dest);
if (warhead.SmudgeType != null) if (warhead.SmudgeType != null)
{ {

View File

@@ -312,6 +312,7 @@ namespace OpenRA
{ {
++RenderFrame; ++RenderFrame;
viewport.DrawRegions(world); viewport.DrawRegions(world);
Sound.SetListenerPosition(viewport.Location + .5f * new float2(viewport.Width, viewport.Height));
} }
PerfHistory.items["render"].Tick(); PerfHistory.items["render"].Tick();

View File

@@ -58,14 +58,24 @@ namespace OpenRA
stopped = false; stopped = false;
} }
public static void SetListenerPosition(float2 position) { soundEngine.SetListenerPosition(position); }
public static void Play(string name) public static void Play(string name)
{
if (name == "" || name == null)
return;
var sound = sounds[name];
soundEngine.Play2D(sound, false, true, float2.Zero);
}
public static void Play(string name, float2 pos)
{ {
if (name == "" || name == null) if (name == "" || name == null)
return; return;
var sound = sounds[name]; var sound = sounds[name];
// todo: positioning soundEngine.Play2D(sound, false, false, pos);
soundEngine.Play2D(sound, false);
} }
public static void PlayToPlayer(Player player, string name) public static void PlayToPlayer(Player player, string name)
@@ -74,6 +84,12 @@ namespace OpenRA
Play( name ); Play( name );
} }
public static void PlayToPlayer(Player player, string name, float2 pos)
{
if (player == player.World.LocalPlayer)
Play(name, pos);
}
public static void PlayMusic(string name) public static void PlayMusic(string name)
{ {
if (name == "" || name == null) if (name == "" || name == null)
@@ -83,7 +99,7 @@ namespace OpenRA
soundEngine.StopSound(music); soundEngine.StopSound(music);
var sound = sounds[name]; var sound = sounds[name];
music = soundEngine.Play2D(sound, true); music = soundEngine.Play2D(sound, true, true, float2.Zero);
music.Volume = musicVolume; music.Volume = musicVolume;
} }
@@ -160,12 +176,13 @@ namespace OpenRA
interface ISoundEngine interface ISoundEngine
{ {
ISoundSource AddSoundSourceFromMemory(byte[] data, int channels, int sampleBits, int sampleRate); ISoundSource AddSoundSourceFromMemory(byte[] data, int channels, int sampleBits, int sampleRate);
ISound Play2D(ISoundSource sound, bool loop); ISound Play2D(ISoundSource sound, bool loop, bool relative, float2 pos);
float Volume { get; set; } float Volume { get; set; }
void PauseSound(ISound sound, bool paused); void PauseSound(ISound sound, bool paused);
void StopSound(ISound sound); void StopSound(ISound sound);
void SetAllSoundsPaused(bool paused); void SetAllSoundsPaused(bool paused);
void StopAllSounds(); void StopAllSounds();
void SetListenerPosition(float2 position);
} }
interface ISoundSource {} interface ISoundSource {}
@@ -241,10 +258,10 @@ namespace OpenRA
return new OpenAlSoundSource(data, channels, sampleBits, sampleRate); return new OpenAlSoundSource(data, channels, sampleBits, sampleRate);
} }
public ISound Play2D(ISoundSource sound, bool loop) public ISound Play2D(ISoundSource sound, bool loop, bool relative, float2 pos)
{ {
int source = GetSourceFromPool(); int source = GetSourceFromPool();
return new OpenAlSound(source, (sound as OpenAlSoundSource).buffer, loop); return new OpenAlSound(source, (sound as OpenAlSoundSource).buffer, loop, relative, pos);
} }
public float Volume public float Volume
@@ -297,7 +314,15 @@ namespace OpenRA
Al.alSourceStop(key); Al.alSourceStop(key);
} }
} }
public void SetListenerPosition(float2 position)
{
var orientation = new [] { 0f, 0f, 1f, 0f, -1f, 0f };
Al.alListener3f(Al.AL_POSITION, position.X, position.Y, 50);
Al.alListenerfv(Al.AL_ORIENTATION, ref orientation[0]);
Al.alListenerf(Al.AL_METERS_PER_UNIT, .01f);
}
} }
class OpenAlSoundSource : ISoundSource class OpenAlSoundSource : ISoundSource
@@ -324,16 +349,20 @@ namespace OpenRA
public readonly int source = -1; public readonly int source = -1;
float volume = 1f; float volume = 1f;
public OpenAlSound(int source, int buffer, bool looping) public OpenAlSound(int source, int buffer, bool looping, bool relative, float2 pos)
{ {
if (source == -1) return; if (source == -1) return;
this.source = source; this.source = source;
Al.alSourcef(source, Al.AL_PITCH, 1f); Al.alSourcef(source, Al.AL_PITCH, 1f);
Al.alSourcef(source, Al.AL_GAIN, 1f); Al.alSourcef(source, Al.AL_GAIN, 1f);
Al.alSource3f(source, Al.AL_POSITION, 0f, 0f, 0f); Al.alSource3f(source, Al.AL_POSITION, pos.X, pos.Y, 0f);
Al.alSource3f(source, Al.AL_VELOCITY, 0f, 0f, 0f); Al.alSource3f(source, Al.AL_VELOCITY, 0f, 0f, 0f);
Al.alSourcei(source, Al.AL_BUFFER, buffer); Al.alSourcei(source, Al.AL_BUFFER, buffer);
Al.alSourcei(source, Al.AL_LOOPING, looping ? Al.AL_TRUE : Al.AL_FALSE); Al.alSourcei(source, Al.AL_LOOPING, looping ? Al.AL_TRUE : Al.AL_FALSE);
Al.alSourcei(source, Al.AL_SOURCE_RELATIVE, relative ? 1 : 0);
Al.alSourcef(source, Al.AL_REFERENCE_DISTANCE, 200);
Al.alSourcef(source, Al.AL_MAX_DISTANCE, 1500);
Al.alSourcePlay(source); Al.alSourcePlay(source);
} }

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Traits.Activities
initialLocation = self.CenterLocation; initialLocation = self.CenterLocation;
self.traits.Get<RenderInfantry>().Attacking(self); self.traits.Get<RenderInfantry>().Attacking(self);
Sound.Play("dogg5p.aud"); Sound.Play("dogg5p.aud", self.CenterLocation);
} }
public IActivity NextActivity { get; set; } public IActivity NextActivity { get; set; }

View File

@@ -194,7 +194,7 @@ namespace OpenRA.Traits
self.World.Add(projectile); self.World.Add(projectile);
if (!string.IsNullOrEmpty(args.weapon.Report)) if (!string.IsNullOrEmpty(args.weapon.Report))
Sound.Play(args.weapon.Report + ".aud"); Sound.Play(args.weapon.Report + ".aud", self.CenterLocation);
} }
}); });

View File

@@ -97,7 +97,7 @@ namespace OpenRA.Traits
if (e.DamageState == DamageState.Dead) if (e.DamageState == DamageState.Dead)
{ {
self.World.WorldActor.traits.Get<ScreenShaker>().AddEffect(10, self.CenterLocation, 1); self.World.WorldActor.traits.Get<ScreenShaker>().AddEffect(10, self.CenterLocation, 1);
Sound.Play(Info.DestroyedSound); Sound.Play(Info.DestroyedSound, self.CenterLocation);
} }
} }

View File

@@ -71,12 +71,12 @@ namespace OpenRA.Traits
void OnCloak() void OnCloak()
{ {
Sound.Play(self.Info.Traits.Get<CloakInfo>().CloakSound); Sound.Play(self.Info.Traits.Get<CloakInfo>().CloakSound, self.CenterLocation);
} }
void OnUncloak() void OnUncloak()
{ {
Sound.Play(self.Info.Traits.Get<CloakInfo>().UncloakSound); Sound.Play(self.Info.Traits.Get<CloakInfo>().UncloakSound, self.CenterLocation);
} }
} }
} }

View File

@@ -88,7 +88,7 @@ namespace OpenRA.Traits
break; break;
case DamageState.Half: case DamageState.Half:
anim.ReplaceAnim("damaged-idle"); anim.ReplaceAnim("damaged-idle");
Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound); Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound, self.CenterLocation);
break; break;
case DamageState.Dead: case DamageState.Dead:
self.World.AddFrameEndTask(w => w.Add(new Explosion(w, self.CenterLocation.ToInt2(), 7, false))); self.World.AddFrameEndTask(w => w.Add(new Explosion(w, self.CenterLocation.ToInt2(), 7, false)));

View File

@@ -36,7 +36,7 @@ namespace OpenRA.Traits
public void PlayCharge(Actor self) public void PlayCharge(Actor self)
{ {
Sound.Play(self.Info.Traits.Get<RenderBuildingChargeInfo>().ChargeAudio); Sound.Play(self.Info.Traits.Get<RenderBuildingChargeInfo>().ChargeAudio, self.CenterLocation);
anim.PlayThen(GetPrefix(self) + "active", anim.PlayThen(GetPrefix(self) + "active",
() => anim.PlayRepeating(GetPrefix(self) + "idle")); () => anim.PlayRepeating(GetPrefix(self) + "idle"));
} }

View File

@@ -48,7 +48,7 @@ namespace OpenRA.Traits
break; break;
case DamageState.Half: case DamageState.Half:
anim.Play( "damaged-idle" ); anim.Play( "damaged-idle" );
Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound); Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound, self.CenterLocation);
break; break;
} }
} }

View File

@@ -85,13 +85,13 @@ namespace OpenRA.Traits
break; break;
case ExtendedDamageState.Half: case ExtendedDamageState.Half:
seqName = "damaged-idle"; seqName = "damaged-idle";
Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound); Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound, self.CenterLocation);
break; break;
case ExtendedDamageState.Quarter: case ExtendedDamageState.Quarter:
if (damageStates >= 3) if (damageStates >= 3)
{ {
seqName = "critical-idle"; seqName = "critical-idle";
Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound); Sound.Play(self.Info.Traits.Get<BuildingInfo>().DamagedSound, self.CenterLocation);
} }
break; break;
} }

View File

@@ -74,12 +74,12 @@ namespace OpenRA.Traits
void OnSurface() void OnSurface()
{ {
Sound.Play(self.Info.Traits.Get<SubmarineInfo>().SurfaceSound); Sound.Play(self.Info.Traits.Get<SubmarineInfo>().SurfaceSound, self.CenterLocation);
} }
void OnDive() void OnDive()
{ {
Sound.Play(self.Info.Traits.Get<SubmarineInfo>().SubmergeSound); Sound.Play(self.Info.Traits.Get<SubmarineInfo>().SubmergeSound, self.CenterLocation);
} }
} }
} }

View File

@@ -73,7 +73,8 @@ namespace OpenRA.Mods.Aftermath
Game.controller.CancelInputMode(); Game.controller.CancelInputMode();
self.CancelActivity(); self.CancelActivity();
self.QueueActivity(new Teleport(order.TargetLocation)); self.QueueActivity(new Teleport(order.TargetLocation));
Sound.Play("chrotnk1.aud"); Sound.Play("chrotnk1.aud", self.CenterLocation);
Sound.Play("chrotnk1.aud", Game.CellSize * order.TargetLocation.ToFloat2());
chargeTick = 25 * self.Info.Traits.Get<ChronoshiftDeployInfo>().ChargeTime; chargeTick = 25 * self.Info.Traits.Get<ChronoshiftDeployInfo>().ChargeTime;
foreach (var a in self.World.Queries.WithTrait<ChronoshiftPaletteEffect>()) foreach (var a in self.World.Queries.WithTrait<ChronoshiftPaletteEffect>())

View File

@@ -58,7 +58,7 @@ namespace OpenRA.Mods.Aftermath
Combat.DoExplosion(self, info.PrimaryWeapon, detonateLocation, altitude); Combat.DoExplosion(self, info.PrimaryWeapon, detonateLocation, altitude);
var report = self.GetPrimaryWeapon().Report; var report = self.GetPrimaryWeapon().Report;
if (report != null) if (report != null)
Sound.Play(report + ".aud"); Sound.Play(report + ".aud", self.CenterLocation);
// Remove from world // Remove from world
self.Health = 0; self.Health = 0;

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Cnc
{ {
Owner.World.AddFrameEndTask(w => Owner.World.AddFrameEndTask(w =>
{ {
Sound.Play(Info.LaunchSound); Sound.Play(Info.LaunchSound, Game.CellSize * order.TargetLocation.ToFloat2());
w.Add(new IonCannon(self, w, order.TargetLocation)); w.Add(new IonCannon(self, w, order.TargetLocation));
}); });

View File

@@ -70,7 +70,7 @@ namespace OpenRA.Mods.RA
self.World.Add(args.weapon.Projectile.Create(args)); self.World.Add(args.weapon.Projectile.Create(args));
if (!string.IsNullOrEmpty(args.weapon.Report)) if (!string.IsNullOrEmpty(args.weapon.Report))
Sound.Play(args.weapon.Report + ".aud"); Sound.Play(args.weapon.Report + ".aud", self.CenterLocation);
} }
} }
} }

View File

@@ -55,7 +55,7 @@ namespace OpenRA.Mods.RA
if (curtain != null) if (curtain != null)
curtain.traits.Get<RenderBuilding>().PlayCustomAnim(curtain, "active"); curtain.traits.Get<RenderBuilding>().PlayCustomAnim(curtain, "active");
Sound.Play("ironcur9.aud"); Sound.Play("ironcur9.aud", order.TargetActor.CenterLocation);
order.TargetActor.traits.Get<IronCurtainable>().Activate(order.TargetActor, order.TargetActor.traits.Get<IronCurtainable>().Activate(order.TargetActor,
(int)((Info as IronCurtainPowerInfo).Duration * 25 * 60)); (int)((Info as IronCurtainPowerInfo).Duration * 25 * 60));

View File

@@ -73,7 +73,7 @@ namespace OpenRA.Mods.RA
Util.CenterOfCell((1 / 24f * self.CenterLocation).ToInt2()), Util.CenterOfCell((1 / 24f * self.CenterLocation).ToInt2()),
self.traits.Get<Unit>().Altitude, a))); self.traits.Get<Unit>().Altitude, a)));
Sound.Play("chute1.aud"); Sound.Play("chute1.aud", self.CenterLocation);
} }
} }
} }

View File

@@ -77,7 +77,7 @@ namespace OpenRA.Mods.RA.SupportPowers
if (success) if (success)
{ {
Sound.Play("chrono2.aud"); Sound.Play("chrono2.aud", chronosphere.CenterLocation);
// Trigger screen desaturate effect // Trigger screen desaturate effect
foreach (var a in self.World.Queries.WithTrait<ChronoshiftPaletteEffect>()) foreach (var a in self.World.Queries.WithTrait<ChronoshiftPaletteEffect>())