Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Add XML sound parser #270

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 12 additions & 48 deletions source/OpenBVE/Audio/Sounds.Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,21 @@ protected override void UpdateLinearModel(double timeElapsed)
* Update the sound sources
* */
int actuallyPlaying = 0;
for (int i = 0; i < SourceCount; i++) {
for (int i = Sources.Count - 1; i > 0; i--) {
if (Sources[i].State == SoundSourceState.StopPending) {
/*
* The sound is still playing but is to be stopped.
* Stop the sound, then remove it from the list of
* sound sources.
* */
AL.DeleteSources(1, ref Sources[i].OpenAlSourceName);
Sources[i].State = SoundSourceState.Stopped;
Sources[i].OpenAlSourceName = 0;
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
} else if (Sources[i].State == SoundSourceState.Stopped) {
/*
* The sound was already stopped. Remove it from
* the list of sound sources.
* */
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
} else if (GlobalMute) {
/*
* The sound is playing or about to be played, but
Expand All @@ -84,11 +78,7 @@ protected override void UpdateLinearModel(double timeElapsed)
Sources[i].OpenAlSourceName = 0;
}
if (!Sources[i].Looped) {
Sources[i].State = SoundSourceState.Stopped;
Sources[i].OpenAlSourceName = 0;
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
}
} else {
/*
Expand Down Expand Up @@ -155,11 +145,7 @@ protected override void UpdateLinearModel(double timeElapsed)
Sources[i].OpenAlSourceName = 0;
}
if (!Sources[i].Looped) {
Sources[i].State = SoundSourceState.Stopped;
Sources[i].OpenAlSourceName = 0;
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
}
} else {
/*
Expand Down Expand Up @@ -199,11 +185,7 @@ protected override void UpdateLinearModel(double timeElapsed)
* Remove it from the list of sound sources.
* */
AL.DeleteSources(1, ref Sources[i].OpenAlSourceName);
Sources[i].State = SoundSourceState.Stopped;
Sources[i].OpenAlSourceName = 0;
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
} else {
actuallyPlaying++;
}
Expand Down Expand Up @@ -303,27 +285,21 @@ protected override void UpdateInverseModel(double timeElapsed)
* and ensure that all others are stopped.
* */
List<SoundSourceAttenuation> toBePlayed = new List<SoundSourceAttenuation>();
for (int i = 0; i < SourceCount; i++) {
for (int i = Sources.Count - 1; i > 0; i--) {
if (Sources[i].State == SoundSourceState.StopPending) {
/*
* The sound is still playing but is to be stopped.
* Stop the sound, then remove it from the list of
* sound sources.
* */
AL.DeleteSources(1, ref Sources[i].OpenAlSourceName);
Sources[i].State = SoundSourceState.Stopped;
Sources[i].OpenAlSourceName = 0;
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
} else if (Sources[i].State == SoundSourceState.Stopped) {
/*
* The sound was already stopped. Remove it from
* the list of sound sources.
* */
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
} else if (GlobalMute) {
/*
* The sound is playing or about to be played, but
Expand All @@ -337,11 +313,7 @@ protected override void UpdateInverseModel(double timeElapsed)
Sources[i].OpenAlSourceName = 0;
}
if (!Sources[i].Looped) {
Sources[i].State = SoundSourceState.Stopped;
Sources[i].OpenAlSourceName = 0;
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
}
} else {
/*
Expand All @@ -356,11 +328,7 @@ protected override void UpdateInverseModel(double timeElapsed)
* Remove it from the list of sound sources.
* */
AL.DeleteSources(1, ref Sources[i].OpenAlSourceName);
Sources[i].State = SoundSourceState.Stopped;
Sources[i].OpenAlSourceName = 0;
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
continue;
}
}
Expand Down Expand Up @@ -410,11 +378,7 @@ protected override void UpdateInverseModel(double timeElapsed)
Sources[i].OpenAlSourceName = 0;
}
if (!Sources[i].Looped) {
Sources[i].State = SoundSourceState.Stopped;
Sources[i].OpenAlSourceName = 0;
Sources[i] = Sources[SourceCount - 1];
SourceCount--;
i--;
Sources.RemoveAt(i);
}
} else {
/*
Expand Down
6 changes: 2 additions & 4 deletions source/OpenBVE/Audio/Sounds.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.IO;
using System.Linq;
using System.Linq;
using OpenBveApi.Sounds;
using OpenBveApi.Trains;
using OpenTK.Audio.OpenAL;
using SoundManager;
using TrainManager.Trains;
Expand All @@ -17,7 +15,7 @@ public override void StopAllSounds(object train)
if (train is TrainBase)
{
var t = (TrainBase) train;
for (int i = 0; i < SourceCount; i++)
for (int i = 0; i < Sources.Count; i++)
{
if (t.Cars.Contains(Sources[i].Parent) || Sources[i].Parent == train)
{
Expand Down
18 changes: 18 additions & 0 deletions source/OpenBVE/System/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ public override bool RegisterSound(string path, double radius, out SoundHandle h
return false;
}

/// <summary>Registers a sound and returns a handle to the sound.</summary>
/// <param name="path">The path to the file or folder that contains the sound.</param>
/// /// <param name="radius">The sound radius</param>
/// <param name="trailingSilence">The trailing silence at the end of the sound</param>
/// <param name="handle">Receives a handle to the sound.</param>
/// <returns>Whether loading the sound was successful.</returns>
public override bool RegisterSound(string path, double radius, double trailingSilence, out SoundHandle handle)
{
if (File.Exists(path) || Directory.Exists(path))
{
handle = Program.Sounds.RegisterBuffer(path, radius, trailingSilence);
return true;
}
ReportProblem(ProblemType.PathNotFound, path);
handle = null;
return false;
}

/// <summary>Registers a sound and returns a handle to the sound.</summary>
/// <param name="sound">The sound data.</param>
/// <param name="handle">Receives a handle to the sound.</param>
Expand Down
2 changes: 2 additions & 0 deletions source/OpenBveApi/Sounds/Sounds.SoundType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public enum SoundType
StaticObject = 8,
/// <summary>The sound source is emitted by an animated object, and may move</summary>
AnimatedObject = 9,
/// <summary>The sound is train based, but player triggered in the driver car</summary>
TrainPlayerTrigger = 10,
/// <summary>The sound source is undefined</summary>
Undefined = -1
}
Expand Down
14 changes: 14 additions & 0 deletions source/OpenBveApi/System/Hosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using OpenBveApi.Trains;
using OpenBveApi.World;
using SoundHandle = OpenBveApi.Sounds.SoundHandle;
// ReSharper disable RedundantNameQualifier - Needed as otherwise it actually breaks :/

namespace OpenBveApi.Hosts {

Expand Down Expand Up @@ -95,6 +96,7 @@ public HostPlatform Platform
{
try
{
// ReSharper disable once UnusedVariable
var version = GetWineVersion();
cachedPlatform = HostPlatform.WINE;
return cachedPlatform;
Expand Down Expand Up @@ -277,6 +279,18 @@ public virtual bool RegisterSound(string path, double radius, out SoundHandle ha
handle = null;
return false;
}

/// <summary>Registers a sound and returns a handle to the sound.</summary>
/// <param name="path">The path to the file or folder that contains the sound.</param>
/// <param name="radius">The sound radius</param>
/// <param name="trailingSilence">The trailing silence to be played upon repetition of the sound</param>
/// <param name="handle">Receives a handle to the sound.</param>
/// <returns>Whether loading the sound was successful.</returns>
public virtual bool RegisterSound(string path, double radius, double trailingSilence, out SoundHandle handle)
{
handle = null;
return false;
}

/// <summary>Registers a sound and returns a handle to the sound.</summary>
/// <param name="sound">The sound data.</param>
Expand Down
24 changes: 2 additions & 22 deletions source/Plugins/Route.CsvRw/CsvRwRouteParser.ApplyRouteData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,27 +486,7 @@ private void ApplyRouteData(string FileName, ref RouteData Data, bool PreviewOnl
{
for (int j = 0; j < Data.Blocks[i].SoundEvents.Length; j++)
{
if ((int)Data.Blocks[i].SoundEvents[j].Type > 1 && (int)Data.Blocks[i].SoundEvents[j].Type < 6 )
{
int m = CurrentRoute.Tracks[0].Elements[n].Events.Length;
Array.Resize(ref CurrentRoute.Tracks[0].Elements[n].Events, m + 1);
double d = Data.Blocks[i].SoundEvents[j].TrackPosition - StartingDistance;
switch (Data.Blocks[i].SoundEvents[j].Type)
{
case SoundType.TrainStatic:
CurrentRoute.Tracks[0].Elements[n].Events[m] = new SoundEvent(Plugin.CurrentHost, d, Data.Blocks[i].SoundEvents[j].SoundBuffer, true, true, false, Vector3.Zero);
break;
case SoundType.TrainAllCarStatic:
CurrentRoute.Tracks[0].Elements[n].Events[m] = new SoundEvent(Plugin.CurrentHost, d, Data.Blocks[i].SoundEvents[j].SoundBuffer, true, true, true, false, Vector3.Zero);
break;
case SoundType.TrainDynamic:
CurrentRoute.Tracks[0].Elements[n].Events[m] = new SoundEvent(Plugin.CurrentHost, d, Data.Blocks[i].SoundEvents[j].SoundBuffer, false, false, false, true, Vector3.Zero, Data.Blocks[i].SoundEvents[j].Speed);
break;
case SoundType.TrainAllCarDynamic:
CurrentRoute.Tracks[0].Elements[n].Events[m] = new SoundEvent(Plugin.CurrentHost, d, Data.Blocks[i].SoundEvents[j].SoundBuffer, false, true, false, true, Vector3.Zero, Data.Blocks[i].SoundEvents[j].Speed);
break;
}
}
Data.Blocks[i].SoundEvents[j].CreateEvent(CurrentRoute, n, StartingDistance);
}
}
// turn
Expand Down Expand Up @@ -763,7 +743,7 @@ private void ApplyRouteData(string FileName, ref RouteData Data, bool PreviewOnl
{
for (int k = 0; k < Data.Blocks[i].SoundEvents.Length; k++)
{
Data.Blocks[i].SoundEvents[k].Create(pos, StartingDistance, Direction, planar, updown);
Data.Blocks[i].SoundEvents[k].CreateAmbient(pos, StartingDistance, Direction, planar, updown);
}
}
// forms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ internal enum TrackCommand
/// <summary>Starts a repeating object cycle</summary>
PatternObj,
/// <summary>Ends a repeating object cycle</summary>
PatternEnd
PatternEnd,
/// <summary>Plays a sound</summary>
Sound
}
}
18 changes: 18 additions & 0 deletions source/Plugins/Route.CsvRw/Namespaces/Track/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2835,6 +2835,24 @@ private void ParseTrackCommand(TrackCommand Command, string[] Arguments, string
}
}
break;
case TrackCommand.Sound:
// XML Sound- All properties to be loaded from the XML
if (PreviewOnly)
{
break;
}
string fileName = Path.CombineFile(Arguments[0], SoundPath);
if (!System.IO.File.Exists(fileName))
{
Plugin.CurrentHost.AddMessage(MessageType.Error, false, "SoundFile " + Arguments[0] + " was not found in " + Command + " at line " + Expression.Line.ToString(Culture) + ", column " + Expression.Column.ToString(Culture) + " in file " + Expression.File);
}
else
{
int n = Data.Blocks[BlockIndex].SoundEvents.Length;
Array.Resize(ref Data.Blocks[BlockIndex].SoundEvents, n + 1);
Data.Blocks[BlockIndex].SoundEvents[n] = new Sound(Data.TrackPosition, fileName);
}
break;
case TrackCommand.PreTrain:
{
if (!PreviewOnly)
Expand Down
Loading