Skip to content

Commit

Permalink
Fixed subtraction bug, moved generators into their own file, rearrang…
Browse files Browse the repository at this point in the history
…ed some parser functions
  • Loading branch information
dzamkov committed Nov 14, 2011
1 parent d5fb55e commit 60da9e6
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 153 deletions.
1 change: 1 addition & 0 deletions BitOrchestra.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Expression.cs" />
<Compile Include="Generator.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
115 changes: 0 additions & 115 deletions Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,121 +640,6 @@ public override void Generate(Value Start, Value[] Buffer)
}
}

/// <summary>
/// An evaluator for a generator function.
/// </summary>
public abstract class GeneratorEvaluator : UnaryEvaluator
{
public GeneratorEvaluator(Evaluator Source, double Period, double Scale)
: base(Source)
{
this.Period = Period;
this.Scale = Scale;
}

/// <summary>
/// The length of a period for this generator.
/// </summary>
public readonly double Period;

/// <summary>
/// The amount the output value is scaled by.
/// </summary>
public readonly double Scale;
}

/// <summary>
/// An evaluator for a saw generator.
/// </summary>
public sealed class SawEvaluator : GeneratorEvaluator
{
public SawEvaluator(Evaluator Source, double Period, double Scale)
: base(Source, Period, Scale)
{

}

public override void Generate(Value Start, Value[] Buffer)
{
this.Source.Generate(Start, Buffer);
for (int t = 0; t < Buffer.Length; t++)
{
double input = ((Buffer[t] / this.Period) % 1.0 + 1.0) % 1.0;
double output = input * 2.0 - 1.0;
Buffer[t] = (Value)(output * Scale);
}
}
}

/// <summary>
/// An evaluator for a sine generator.
/// </summary>
public sealed class SineEvaluator : GeneratorEvaluator
{
public SineEvaluator(Evaluator Source, double Period, double Scale)
: base(Source, Period, Scale)
{

}

public override void Generate(Value Start, Value[] Buffer)
{
this.Source.Generate(Start, Buffer);
for (int t = 0; t < Buffer.Length; t++)
{
double input = Buffer[t] / this.Period;
double output = Math.Sin(input * 2.0 * Math.PI);
Buffer[t] = (Value)(output * Scale);
}
}
}

/// <summary>
/// An evaluator for a square generator.
/// </summary>
public sealed class SquareEvaluator : GeneratorEvaluator
{
public SquareEvaluator(Evaluator Source, double Period, double Scale)
: base(Source, Period, Scale)
{

}

public override void Generate(Value Start, Value[] Buffer)
{
this.Source.Generate(Start, Buffer);
for (int t = 0; t < Buffer.Length; t++)
{
double input = ((Buffer[t] / this.Period) % 1.0 + 1.0) % 1.0;
double output = input > 0.5 ? 1.0 : -1.0;
Buffer[t] = (Value)(output * Scale);
}
}
}

/// <summary>
/// An evaluator for a triangle generator.
/// </summary>
public sealed class TriangleEvaluator : GeneratorEvaluator
{
public TriangleEvaluator(Evaluator Source, double Period, double Scale)
: base(Source, Period, Scale)
{

}

public override void Generate(Value Start, Value[] Buffer)
{
this.Source.Generate(Start, Buffer);
for (int t = 0; t < Buffer.Length; t++)
{
double input = ((Buffer[t] / this.Period) % 1.0 + 1.0) % 1.0;
double output = input < 0.5 ? input * 4.0 - 1.0 : input * -4.0 + 3.0;
Buffer[t] = (Value)(output * Scale);
}
}
}

/// <summary>
/// An evaluator for a sequencer.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected override Evaluator CreateEvaluator(Dictionary<Expression,Evaluator> Ca
if (constleft && constright)
return new ConstantEvaluator(leftval - rightval);
if (constleft)
return new AddConstantEvaluator(righteval, -leftval);
return new SubtractEvaluator(lefteval, righteval.GetBuffered(BufferSize));
if (constright)
return new AddConstantEvaluator(lefteval, -rightval);
return new SubtractEvaluator(lefteval, righteval.GetBuffered(BufferSize));
Expand Down
123 changes: 123 additions & 0 deletions Generator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Value = System.Int32;

namespace BitOrchestra
{
/// <summary>
/// An evaluator for a generator function.
/// </summary>
public abstract class GeneratorEvaluator : UnaryEvaluator
{
public GeneratorEvaluator(Evaluator Source, double Period, double Scale)
: base(Source)
{
this.Period = Period;
this.Scale = Scale;
}

/// <summary>
/// The length of a period for this generator.
/// </summary>
public readonly double Period;

/// <summary>
/// The amount the output value is scaled by.
/// </summary>
public readonly double Scale;
}

/// <summary>
/// An evaluator for a saw generator.
/// </summary>
public sealed class SawEvaluator : GeneratorEvaluator
{
public SawEvaluator(Evaluator Source, double Period, double Scale)
: base(Source, Period, Scale)
{

}

public override void Generate(Value Start, Value[] Buffer)
{
this.Source.Generate(Start, Buffer);
for (int t = 0; t < Buffer.Length; t++)
{
double input = ((Buffer[t] / this.Period) % 1.0 + 1.0) % 1.0;
double output = input * 2.0 - 1.0;
Buffer[t] = (Value)(output * Scale);
}
}
}

/// <summary>
/// An evaluator for a sine generator.
/// </summary>
public sealed class SineEvaluator : GeneratorEvaluator
{
public SineEvaluator(Evaluator Source, double Period, double Scale)
: base(Source, Period, Scale)
{

}

public override void Generate(Value Start, Value[] Buffer)
{
this.Source.Generate(Start, Buffer);
for (int t = 0; t < Buffer.Length; t++)
{
double input = Buffer[t] / this.Period;
double output = Math.Sin(input * 2.0 * Math.PI);
Buffer[t] = (Value)(output * Scale);
}
}
}

/// <summary>
/// An evaluator for a square generator.
/// </summary>
public sealed class SquareEvaluator : GeneratorEvaluator
{
public SquareEvaluator(Evaluator Source, double Period, double Scale)
: base(Source, Period, Scale)
{

}

public override void Generate(Value Start, Value[] Buffer)
{
this.Source.Generate(Start, Buffer);
for (int t = 0; t < Buffer.Length; t++)
{
double input = ((Buffer[t] / this.Period) % 1.0 + 1.0) % 1.0;
double output = input > 0.5 ? 1.0 : -1.0;
Buffer[t] = (Value)(output * Scale);
}
}
}

/// <summary>
/// An evaluator for a triangle generator.
/// </summary>
public sealed class TriangleEvaluator : GeneratorEvaluator
{
public TriangleEvaluator(Evaluator Source, double Period, double Scale)
: base(Source, Period, Scale)
{

}

public override void Generate(Value Start, Value[] Buffer)
{
this.Source.Generate(Start, Buffer);
for (int t = 0; t < Buffer.Length; t++)
{
double input = ((Buffer[t] / this.Period) % 1.0 + 1.0) % 1.0;
double output = input < 0.5 ? input * 4.0 - 1.0 : input * -4.0 + 3.0;
Buffer[t] = (Value)(output * Scale);
}
}
}
}
Loading

0 comments on commit 60da9e6

Please sign in to comment.