-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass1.cs
119 lines (107 loc) · 5.23 KB
/
Class1.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
using System;
using NAudio.Wave;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using NAudio.Wave.SampleProviders;
using NAudio.CoreAudioApi;
namespace RecordAudio
{
public class Class1
{
public Class1()
{
}
public WasapiOut SilencePlayer = null;
public WaveFileWriter LoopbackWriter = null;
public WaveFileWriter MicrophoneWriter = null;
public WasapiLoopbackCapture LoopbackCapture = null;
public WaveInEvent MicrophoneCapture = null;
public string outputFilePath = "";
public string MicrophoneFilePath = "";
public string LoopbackFilePath = "";
[DllExport("Test", CallingConvention = CallingConvention.Cdecl)]
public void Test()
{
MessageBox.Show("Test");
}
[DllExport("StartRecording", CallingConvention = CallingConvention.Cdecl)]
public void StartRecording(string outputFilePath)
{
this.outputFilePath = outputFilePath;
this.LoopbackFilePath = Path.Combine(Path.GetDirectoryName(this.outputFilePath), Path.GetFileNameWithoutExtension(this.outputFilePath) + "_i" + Path.GetExtension(this.outputFilePath));
this.MicrophoneFilePath = Path.Combine(Path.GetDirectoryName(this.outputFilePath), Path.GetFileNameWithoutExtension(this.outputFilePath) + "_m" + Path.GetExtension(this.outputFilePath));
// Redefine the capturer instance with a new instance of the LoopbackCapture class
this.LoopbackCapture = new WasapiLoopbackCapture();
this.SilencePlayer = new WasapiOut(WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice(), AudioClientShareMode.Shared, false, 100);
this.SilencePlayer.Init(new SilenceProvider(LoopbackCapture.WaveFormat));
this.SilencePlayer.Play();
// Redefine the audio writer instance with the given configuration
this.LoopbackWriter = new WaveFileWriter(this.LoopbackFilePath, LoopbackCapture.WaveFormat);
// When the capturer receives audio, start writing the buffer into the mentioned file
this.LoopbackCapture.DataAvailable += (s, a) =>
{
this.LoopbackWriter.Write(a.Buffer, 0, a.BytesRecorded);
};
// When the Capturer Stops
this.LoopbackCapture.RecordingStopped += (s, a) =>
{
this.LoopbackWriter.Close();
this.LoopbackWriter.Dispose();
this.LoopbackWriter = null;
this.SilencePlayer.Stop();
LoopbackCapture.Dispose();
};
// Start recording !
this.LoopbackCapture.StartRecording();
this.MicrophoneCapture = new WaveInEvent();
this.MicrophoneCapture.DeviceNumber = 0;
this.MicrophoneCapture.WaveFormat = LoopbackCapture.WaveFormat;
this.MicrophoneCapture.BufferMilliseconds = 50;
this.MicrophoneWriter = new WaveFileWriter(this.MicrophoneFilePath, LoopbackCapture.WaveFormat);
this.MicrophoneCapture.DataAvailable += (s, a) =>
{
this.MicrophoneWriter.Write(a.Buffer, 0, a.BytesRecorded);
};
this.MicrophoneCapture.RecordingStopped += (s, a) =>
{
this.MicrophoneWriter.Close();
this.MicrophoneWriter.Dispose();
this.MicrophoneWriter = null;
MicrophoneCapture.Dispose();
};
MicrophoneCapture.StartRecording();
}
[DllExport("StopRecording", CallingConvention = CallingConvention.Cdecl)]
public void StopRecording()
{
// Stop recording !
this.LoopbackCapture.StopRecording();
this.MicrophoneCapture.StopRecording();
while (this.MicrophoneWriter != null || this.LoopbackWriter != null)
{ }
// this.MixRecordings(this.outputFilePath, this.LoopbackFilePath, this.MicrophoneFilePath, true);
}
[DllExport("MixRecordings", CallingConvention = CallingConvention.Cdecl)]
public void MixRecordings(string outputFilePath, string LoopbackFilePath, string MicrophoneFilePath, bool deleteSources = false)
{
this.outputFilePath = outputFilePath;
this.LoopbackFilePath = LoopbackFilePath;
this.MicrophoneFilePath = MicrophoneFilePath;
using (var loopbackReader = new AudioFileReader(this.LoopbackFilePath))
using (var microphoneReader = new AudioFileReader(this.MicrophoneFilePath))
{
var mixer = new MixingSampleProvider(new[] { loopbackReader, microphoneReader });
WaveFileWriter.CreateWaveFile16(this.outputFilePath, mixer);
}
if (File.Exists(this.outputFilePath) && deleteSources)
{
if (File.Exists(this.LoopbackFilePath))
File.Delete(this.LoopbackFilePath);
if (File.Exists(this.MicrophoneFilePath))
File.Delete(this.MicrophoneFilePath);
}
}
}
}