diff --git a/AudioStreamer/AudioStreamer.cs b/AudioStreamer/AudioStreamer.cs index eeb7217..7242402 100644 --- a/AudioStreamer/AudioStreamer.cs +++ b/AudioStreamer/AudioStreamer.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Collections.Generic; +using System.Linq.Expressions; namespace AudioStreamer { public class AudioStreamer @@ -58,7 +59,11 @@ namespace AudioStreamer Monitor.Exit(this); foreach (AudioSource source in this.sources){ - source.Source(); + try { + source.Source(); + } catch (Exception e) { + Console.WriteLine("pusher(): Exception: {0}", e); + } } Monitor.Enter(this); diff --git a/AudioStreamer/AudioStreamer.csproj b/AudioStreamer/AudioStreamer.csproj index 9ed462c..bcbf172 100644 --- a/AudioStreamer/AudioStreamer.csproj +++ b/AudioStreamer/AudioStreamer.csproj @@ -45,15 +45,17 @@ - - - - + + + + + + @@ -124,6 +126,11 @@ + + + + + diff --git a/AudioStreamer/BaseAudioOutput.cs b/AudioStreamer/BaseAudioOutput.cs index 94e0f99..a0afb26 100644 --- a/AudioStreamer/BaseAudioOutput.cs +++ b/AudioStreamer/BaseAudioOutput.cs @@ -13,13 +13,15 @@ namespace AudioStreamer { if (NewAudioAvailable != null) { - if (NewAudioAvailable.GetInvocationList().Length == 1){ + if (NewAudioAvailable.GetInvocationList().Length == 0){ + + } else if (NewAudioAvailable.GetInvocationList().Length == 1){ NewAudioAvailable.Invoke(this, samples); } else { foreach (Delegate d in NewAudioAvailable.GetInvocationList()){ Int16[] copy = new Int16[samples.Length]; Array.Copy(samples,copy,samples.Length); - d.Method.Invoke(d.Target, new object[] { copy }); + d.Method.Invoke(d.Target, new object[] { this, copy }); } } } diff --git a/AudioStreamer/ModuloList.cs b/AudioStreamer/ModuloList.cs new file mode 100644 index 0000000..a6d2826 --- /dev/null +++ b/AudioStreamer/ModuloList.cs @@ -0,0 +1,34 @@ +using System; +using System.Runtime.CompilerServices; +namespace AudioStreamer +{ + public class ModuloList + { + T[] buffer; + + public ModuloList(int size) + { + buffer = new T[size]; + } + + public T this[int n]{ + get { + n %= this.buffer.Length; + if (n<0){ + n += this.buffer.Length; + } + return this.buffer[n]; + } + set { + n %= this.buffer.Length; + if (n<0){ + n += this.buffer.Length; + } + this.buffer[n] = value; + } + } + + public int Size { get { return this.buffer.Length; } } + + } +} diff --git a/AudioStreamer/OpenALPlayback.cs b/AudioStreamer/Playbacks/OpenALPlayback.cs similarity index 97% rename from AudioStreamer/OpenALPlayback.cs rename to AudioStreamer/Playbacks/OpenALPlayback.cs index 5d3e866..4e933aa 100644 --- a/AudioStreamer/OpenALPlayback.cs +++ b/AudioStreamer/Playbacks/OpenALPlayback.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using OpenTK.Audio.OpenAL; using OpenTK.Audio; -namespace AudioStreamer +namespace AudioStreamer.Playbacks { public class OpenALPlayback : AudioInput { diff --git a/AudioStreamer/Amplifier.cs b/AudioStreamer/Processors/Amplifier.cs similarity index 85% rename from AudioStreamer/Amplifier.cs rename to AudioStreamer/Processors/Amplifier.cs index 35c938e..4e0308a 100644 --- a/AudioStreamer/Amplifier.cs +++ b/AudioStreamer/Processors/Amplifier.cs @@ -1,5 +1,5 @@ using System; -namespace AudioStreamer +namespace AudioStreamer.Processors { public class Amplifier : BaseAudioProcessor { @@ -23,7 +23,7 @@ namespace AudioStreamer protected override void process(ref short[] samples) { for (int n = 0; n < samples.Length;n++){ - samples[n] = (Int16)(((UInt32)samples[n] * this.amplification) >> 16); + samples[n] = (Int16)(((Int32)samples[n] * this.amplification) >> 16); } } } diff --git a/AudioStreamer/Echo.cs b/AudioStreamer/Processors/Echo.cs similarity index 97% rename from AudioStreamer/Echo.cs rename to AudioStreamer/Processors/Echo.cs index 0c04b1a..1f9297f 100644 --- a/AudioStreamer/Echo.cs +++ b/AudioStreamer/Processors/Echo.cs @@ -1,5 +1,5 @@ using System; -namespace AudioStreamer +namespace AudioStreamer.Processors { public class Echo : BaseAudioProcessor { diff --git a/AudioStreamer/Processors/FIRFilter.cs b/AudioStreamer/Processors/FIRFilter.cs new file mode 100644 index 0000000..cdb1650 --- /dev/null +++ b/AudioStreamer/Processors/FIRFilter.cs @@ -0,0 +1,50 @@ +using System; +namespace AudioStreamer.Processors +{ + public class FIRFilter : BaseAudioProcessor + { + Int32[] amplifications; + ModuloList delay; + int delayPosition; + + public FIRFilter(Int32[] amps) + { + this.amplifications = amps; + this.delay = new ModuloList(amps.Length); + } + public FIRFilter(float[] amps) + { + this.amplifications = new Int32[amps.Length]; + this.delay = new ModuloList(amps.Length); + + for (int n = 0; n < amps.Length;n++){ + this.setAmplification(n,amps[n]); + } + } + + public Int32[] Amplifications { get { return this.amplifications; } } + + public float getAmplification(int n){ + return ((float)this.amplifications[n]) / 65536.0f; + } + public void setAmplification(int n, float amp){ + this.amplifications[n] = (Int32)(amp * 65536.0f); + } + + + protected override void process(ref short[] samples) + { + for (int n = 0; n < samples.Length; n++){ + this.delay[n + delayPosition] = samples[n]; + samples[n] = 0; + + for (int pa = 0; pa < amplifications.Length; pa++){ + samples[n] += (Int16)((this.amplifications[pa] * delay[delayPosition - pa])>>16); + } + } + + delayPosition += samples.Length; + delayPosition %= this.amplifications.Length; + } + } +} diff --git a/AudioStreamer/Program.cs b/AudioStreamer/Program.cs index 84a0af0..abec4d0 100644 --- a/AudioStreamer/Program.cs +++ b/AudioStreamer/Program.cs @@ -1,5 +1,8 @@ using System; using System.Threading; +using AudioStreamer.Sources; +using AudioStreamer.Playbacks; +using AudioStreamer.Processors; namespace AudioStreamer { @@ -12,9 +15,12 @@ namespace AudioStreamer OpenALSource source = new OpenALSource(); OpenALPlayback playback = new OpenALPlayback(); - Amplifier amp = new Amplifier(2.0f); - Echo echo = new Echo(16000); - echo.FloatAmplification = 0.75f; + FIRFilter fir = new FIRFilter(new float[] { 0.013333f, 0.026665f, 0.013333f, -1.541256f, 0.594546f }); + + Amplifier amp = new Amplifier(0.75f); + + Echo echo = new Echo(32); + echo.FloatAmplification = 0.85f; streamer.addAudioSource(source); @@ -23,6 +29,8 @@ namespace AudioStreamer amp.Connect(source); echo.Connect(amp); + fir.Connect(echo); + playback.Connect(echo); source.Start(); diff --git a/AudioStreamer/OpenALSource.cs b/AudioStreamer/Sources/OpenALSource.cs similarity index 96% rename from AudioStreamer/OpenALSource.cs rename to AudioStreamer/Sources/OpenALSource.cs index 3f63254..1d335be 100644 --- a/AudioStreamer/OpenALSource.cs +++ b/AudioStreamer/Sources/OpenALSource.cs @@ -1,7 +1,7 @@ using System; using OpenTK.Audio; -namespace AudioStreamer +namespace AudioStreamer.Sources { public class OpenALSource : BaseAudioSource, AudioOutput {