-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
124 lines (118 loc) · 3.78 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;
namespace SoundThermo
{
class Program
{
static void Main(string[] args)
{
CultureInfo.CurrentCulture = new CultureInfo("en-US");
var rootCommand = new RootCommand();
var listCommand = new Command("list", "List all available devices");
listCommand.Handler = CommandHandler.Create(ListDevices);
var recordCommand = new Command("record")
{
new Option<string>("--input", "Input device to record from (GUID)"),
new Option<float>("--duration", "How many seconds of audio you want to record"),
};
recordCommand.Handler = CommandHandler.Create<string, float>(RecordSamples);
var measureCommand = new Command("measure")
{
new Option<string>("--input", "Input device to record from (GUID)"),
new Option<string>("--output", "Output device to play sound to (GUID)"),
new Option<float>("--duration", "How many seconds of audio you want to record (uses twice this time because of recording on two channels)"),
};
measureCommand.Handler = CommandHandler.Create<string, string, float>(Measure);
rootCommand.AddCommand(listCommand);
rootCommand.AddCommand(measureCommand);
rootCommand.AddCommand(recordCommand);
rootCommand.InvokeAsync(args).Wait();
}
private async static Task Measure(string input, string output, float duration)
{
var leftMea = new Measurer(GetInputDevice(input), GetOutputDevice(output), Measurer.MeasureChannel.Left);
var leftResult = await leftMea.Measure(duration);
var rightMea = new Measurer(GetInputDevice(input), GetOutputDevice(output), Measurer.MeasureChannel.Right);
var rightResult = await rightMea.Measure(duration);
float k = 2 * rightResult;
float v0v = leftResult / k;
// assume r2 = 10K
float r1 = 10000 / v0v - 10000;
float t = 1.0f / (MathF.Log(r1 / 10000) / 4300.0f + 1.0f / 298.0f) - 273.0f;
Console.WriteLine(t.ToString());
}
private static WaveOutEvent GetOutputDevice(string guid)
{
WaveOutEvent device = null;
for (int n = -1; n < WaveOut.DeviceCount; n++)
{
var caps = WaveOut.GetCapabilities(n);
if (caps.ProductGuid.ToString() == guid)
{
device = new WaveOutEvent();
device.DeviceNumber = n;
break;
}
}
if (device == null)
{
throw new Exception("Unable to find device " + guid);
}
return device;
}
private static WaveInEvent GetInputDevice(string guid)
{
WaveInEvent device = null;
for (int n = -1; n < WaveIn.DeviceCount; n++)
{
var caps = WaveIn.GetCapabilities(n);
if (caps.ProductGuid.ToString() == guid)
{
device = new WaveInEvent();
device.DeviceNumber = n;
break;
}
}
if (device == null)
{
throw new Exception("Unable to find device " + guid);
}
return device;
}
private async static Task RecordSamples(string input, float duration)
{
WaveInEvent device = GetInputDevice(input);
var rec = new Recorder(device);
await rec.Record(duration);
using (FileStream fs = File.OpenWrite("recorded.txt"))
{
for (int i = 0; i < rec.samples.Count; i++)
{
fs.Write(Encoding.UTF8.GetBytes(rec.samples[i].ToString() + "\n"));
}
}
}
private static void ListDevices()
{
Console.WriteLine("List of audio devices in your system");
Console.WriteLine("Output devices:");
for (int n = -1; n < WaveOut.DeviceCount; n++)
{
var caps = WaveOut.GetCapabilities(n);
Console.WriteLine($"{n}: {caps.ProductName} {caps.ProductGuid}");
}
Console.WriteLine("Input devices:");
for (int n = -1; n < WaveIn.DeviceCount; n++)
{
var caps = WaveIn.GetCapabilities(n);
Console.WriteLine($"{n}: {caps.ProductName} {caps.ProductGuid}");
}
}
}
}