Skip to content

Commit

Permalink
druha verze
Browse files Browse the repository at this point in the history
  • Loading branch information
Pheeck committed Jul 28, 2022
1 parent 6a93933 commit ab28ef0
Show file tree
Hide file tree
Showing 14 changed files with 1,757 additions and 74 deletions.
12 changes: 6 additions & 6 deletions CMDOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ffteq
{
abstract class CMDOptionArg
abstract class CMDOptionParam
{
abstract public string Name { get; }
abstract public string Description { get; }
Expand All @@ -13,15 +13,15 @@ abstract class CMDOptionArg
abstract public bool Validate(String s);
}

class CMDOptionArgDouble : CMDOptionArg
class CMDOptionParamDouble : CMDOptionParam
{
public double Value { private set; get; }
private string name;
private string description;
public override string Name { get { return name; } }
public override string Description { get { return description; } }

public CMDOptionArgDouble(string name, string description)
public CMDOptionParamDouble(string name, string description)
{
this.name = name;
this.description = description;
Expand All @@ -38,13 +38,13 @@ abstract class CMDOption
{
abstract public string Name { get; }
abstract public string Description { get; }
abstract public CMDOptionArg[] Args { get; }
public int ArgNum { get { return Args.Length; } }
abstract public CMDOptionParam[] Params { get; }
public int ParamNum { get { return Params.Length; } }

/// <summary>
/// Shall return signal with the same number of samples and sample
/// rate. The signal should be processed by an effect.
/// </summary>
abstract public Signal Execute(Signal inSignal, string[] argStrs);
abstract public Signal Execute(Signal inSignal, string[] args);
}
}
54 changes: 26 additions & 28 deletions CMDOptionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,15 @@ public CMDParsedOption(CMDOption opt, string[] args)

class CMDOptionParser
{
private const int LINE_WIDTH = 80;

private CMDOption[] availableOpts;
private string programDesc;

public CMDOptionParser(CMDOption[] availableOpts, string programDesc)
{
this.availableOpts = availableOpts;
this.programDesc = programDesc;
}

public void PrintOptions()
private void PrintOptions()
{
foreach (CMDOption o in availableOpts)
{
Console.Write(o.Name);
foreach (CMDOptionArg a in o.Args)
foreach (CMDOptionParam a in o.Params)
{
Console.Write(' ');
Console.Write(a.Name);
Expand All @@ -47,27 +39,33 @@ public void PrintOptions()
}
}

private static void PrintParamHelp(CMDOption o, CMDOptionParam p)
{
Console.WriteLine("Option: {0}", o.Name);
Console.WriteLine("Param: {0}", p.Name);
Console.WriteLine(p.Description);
}

public CMDOptionParser(CMDOption[] availableOpts, string programDesc)
{
this.availableOpts = availableOpts;
this.programDesc = programDesc;
}

public void PrintHelp()
{
Console.WriteLine(programDesc);
Console.WriteLine();
PrintOptions();
}

public static void PrintArgHelp(CMDOption o, CMDOptionArg a)
{
Console.WriteLine("Option: {0}", o.Name);
Console.WriteLine("Arg: {0}", a.Name);
Console.WriteLine(a.Description);
}

public List<CMDParsedOption> Parse(string[] strs)
public List<CMDParsedOption> Parse(string[] args)
{
List<CMDParsedOption> parsed = new List<CMDParsedOption>();

for (int i = 0; i < strs.Length; i++)
for (int i = 0; i < args.Length; i++)
{
string s = strs[i];
string s = args[i];
bool wrongOpt = true;
foreach (CMDOption o in availableOpts)
{
Expand All @@ -76,32 +74,32 @@ public List<CMDParsedOption> Parse(string[] strs)
wrongOpt = false;

// Prepare array of args of this option
if (i + o.ArgNum >= strs.Length)
if (i + o.ParamNum >= args.Length)
{
throw new ApplicationException(String.Format(
"Not enough arguments for option {0}",
o.Name
));
}
string[] argStrs = new string[o.ArgNum];
Array.Copy(strs, i + 1, argStrs, 0, o.ArgNum);
string[] oArgs = new string[o.ParamNum];
Array.Copy(args, i + 1, oArgs, 0, o.ParamNum);

// Validate args
for (int j = 0; j < o.ArgNum; j++)
for (int j = 0; j < o.ParamNum; j++)
{
if (!o.Args[j].Validate(argStrs[j]))
if (!o.Params[j].Validate(oArgs[j]))
{
PrintArgHelp(o, o.Args[j]);
PrintParamHelp(o, o.Params[j]);
throw new ApplicationException(
"Invalid argument"
);
}
}

parsed.Add(new CMDParsedOption(o, argStrs));
parsed.Add(new CMDParsedOption(o, oArgs));

// Skip args of this option
i += o.ArgNum;
i += o.ParamNum;

break;
}
Expand Down
43 changes: 22 additions & 21 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public override string Description
return DESCRIPTION;
}
}
private CMDOptionArg[] args = new CMDOptionArg[] {
new CMDOptionArgDouble("hz-from",
private CMDOptionParam[] params_ = new CMDOptionParam[] {
new CMDOptionParamDouble("hz-from",
"Decimal number. Hz where slope starts."),
new CMDOptionArgDouble("hz-to",
new CMDOptionParamDouble("hz-to",
"Decimal number. Hz where slope ends.")
};
public override CMDOptionArg[] Args { get { return args; } }
public override CMDOptionParam[] Params { get { return params_; } }

public override Signal Execute(Signal inSignal, string[] argStrs)
public override Signal Execute(Signal inSignal, string[] args)
{
double hzFrom = Double.Parse(argStrs[0]);
double hzTo = Double.Parse(argStrs[1]);
double hzFrom = Double.Parse(args[0]);
double hzTo = Double.Parse(args[1]);

Windowing windowing = new HannWindowing(WINDOW_SIZE);
Effect filter = new FFTFilter(true, hzFrom, hzTo);
Expand Down Expand Up @@ -71,18 +71,18 @@ public override string Description
return DESCRIPTION;
}
}
private CMDOptionArg[] args = new CMDOptionArg[] {
new CMDOptionArgDouble("hz-from",
private CMDOptionParam[] params_ = new CMDOptionParam[] {
new CMDOptionParamDouble("hz-from",
"Decimal number. Hz where slope starts."),
new CMDOptionArgDouble("hz-to",
new CMDOptionParamDouble("hz-to",
"Decimal Number. Hz where slope ends.")
};
public override CMDOptionArg[] Args { get { return args; } }
public override CMDOptionParam[] Params { get { return params_; } }

public override Signal Execute(Signal inSignal, string[] argStrs)
public override Signal Execute(Signal inSignal, string[] args)
{
double hzFrom = Double.Parse(argStrs[0]);
double hzTo = Double.Parse(argStrs[1]);
double hzFrom = Double.Parse(args[0]);
double hzTo = Double.Parse(args[1]);

Windowing windowing = new HannWindowing(WINDOW_SIZE);
Effect filter = new FFTFilter(false, hzFrom, hzTo);
Expand All @@ -108,14 +108,14 @@ public override string Description
return "Manipulate volume of the whole file.";
}
}
private CMDOptionArg[] args = new CMDOptionArg[] {
new CMDOptionArgDouble("db", "Decimal number. Ammount of gain.")
private CMDOptionParam[] params_ = new CMDOptionParam[] {
new CMDOptionParamDouble("db", "Decimal number. Ammount of gain.")
};
public override CMDOptionArg[] Args { get { return args; } }
public override CMDOptionParam[] Params { get { return params_; } }

public override Signal Execute(Signal inSignal, string[] argStrs)
public override Signal Execute(Signal inSignal, string[] args)
{
double db = Double.Parse(argStrs[0]);
double db = Double.Parse(args[0]);
Effect gain = new Gain(db);
return gain.Process(inSignal);
}
Expand Down Expand Up @@ -158,6 +158,7 @@ public static int Main(string[] args)
string inPath = args[0];
string outPath = args[1];

// Pass array of options and their arguments to option parser
string[] foo = new string[args.Length - 2];
Array.Copy(args, 2, foo, 0, args.Length - 2);
List<CMDParsedOption> parsedOpts = null;
Expand Down Expand Up @@ -216,12 +217,12 @@ public static int Main(string[] args)
for (int i = 0; i < parsedOpts.Count; i++)
{
CMDOption o = parsedOpts[i].Opt;
string[] strArgs = parsedOpts[i].Args;
string[] oArgs = parsedOpts[i].Args;

Console.WriteLine("Applying option {0}...", o.Name);
try
{
signal = o.Execute(signal, strArgs);
signal = o.Execute(signal, oArgs);
}
catch (ApplicationException e)
{
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ high-pass filter (filtruje nízké frekvence) a upravit hlasitost souboru.
Uživatel si na příkazové řádce navolí, které efekty a v jakém pořadí na soubor
aplikovat.

Účelem programu není poměřovat se s již existujícími filtry. Ty jsou vesměs
mocnější a uživatelsky přívětivější než tento. Účel programu je vzdělávací. Já
si vyzkoušel jednoduché zpracovávání zvuku a snad program bude i v budoucnu
užitečný někomu, kdo se bude chtít dozvědět, jak napsat frekvenční filtr.

### Jak to spustit

Program lze spustit na Windows a Linux x86\_64 strojích.
Expand All @@ -30,7 +35,7 @@ Program lze spustit na Windows a Linux x86\_64 strojích.

Připravte si 8bit 44,1kHz mono Wave soubor. Stáhněte si win-64 release.
Otevřete v kořenové složce terminál. Zadejte následující příkaz (pozor! pokud
výstupní soubor už existuje, program ho přemaže).
výstupní soubor už existuje, program ho přepíše).

```
ffteq <vstupní soubor>.wav <výstupní soubor>.wav gain 0
Expand All @@ -53,16 +58,16 @@ výstupnímu souboru. Následuje seznam options oddělených mezerami. Každá o
odpovídá aplikaci jednoho efektu. Options se mohou v seznamu opakovat. Jsou
aplikovány v tom pořadí, v jakém jsou zadány v terminálu.

Každá option má formu `<název option> <argument1> <argument2> ...`.
Každá option má formu `<název-option> <parametr1> <parametr2> ...`.

#### filterlo - Low-pass filter

```
filterlo <hz-from> <hz-to>
```

\<hz-from\> a \<hz-to\> jsou desetinná čísla. Zcela odfiltruje frekvence vyšší
než \<hz-to\>. Aby úprava nebyla tak agresivní, začíná filtrovat už na
\<hz-from\> a \<hz-to\> jsou desetinná čísla. Zcela odfiltruje frekvence
vyšší než \<hz-to\>. Aby úprava nebyla tak agresivní, začíná filtrovat už na
frekvenci \<hz-from\> a se zvyšující se frekvencí lineárně snižuje koeficient,
kterým násobí vstupní frekvence, až se dostane na nulu v \<hz-to\>.

Expand All @@ -83,7 +88,7 @@ vyšší než 2500hz.
filterhi <hz-from> <hz-to>
```

Obdobný jako low-pass filter. Zcela odfiltruje frekvence **nižší než
Podobný jako low-pass filter. Zcela odfiltruje frekvence **nižší než
\<hz-from\>**.

![high-cz.png](docs/high-cz.png)
Expand Down
14 changes: 7 additions & 7 deletions docs/diagram-ascii.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
+-------------------------------------+
|class CMDOptionDouble : CMDOptionArg | +----------------------------------------------+ +-----------------------------------+
+-------------------------------------+ |class FFTFilter : Effect | |class FFT |
|new(string name, string description) | +----------------------------------------------+ +-----------------------------------+
+-----------------------+-------------+ |new(bool lowPass, double hzFrom, double hzTo) | |static Complex[] DoFFT(Complex[]) |
+--------------------------------------------+
|class CMDOptionParamDouble : CMDOptionParam | +----------------------------------------------+ +-----------------------------------+
+--------------------------------------------+ |class FFTFilter : Effect | |class FFT |
|new(string name, string description) | +----------------------------------------------+ +-----------------------------------+
+-----------------------+--------------------+ |new(bool lowPass, double hzFrom, double hzTo) | |static Complex[] DoFFT(Complex[]) |
| +---------------------------+------------------+ |static Complex[] DoIFFT(Complex[]) |
| | +-----------------------------------+
| +--------------------+ |
+-----------------+-------------+ |class Gain : Effect | |
|abstract class CMDOptionArg | +--------------------+ |
|abstract class CMDOptionParam | +--------------------+ |
+-------------------------------+ |new(double db) | |
|abstract string Name; | +-------------+------+ |
|abstract string Description; | | |
Expand All @@ -24,7 +24,7 @@
+-----------------------------------------------+
|abstract string Name; |
|abstract string Description; | +----------------------------------------------+
|abstract CMDOptionArg[] Args; +-----------------------+ |class Signal |
|abstract CMDOptionParam[] Params; +-----------------------+ |class Signal |
| | | +----------------------------------------------+
|abstract Signal Execute(Signal, string[] args) | | |double[] Data; |
+----------------------------------------+------+ | |double this[int]; |
Expand Down
Binary file modified docs/diagram.pdf
Binary file not shown.
Loading

0 comments on commit ab28ef0

Please sign in to comment.