-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathWaveMixer.cs
238 lines (203 loc) · 7.94 KB
/
WaveMixer.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//Mixes two wav file to out single mp3 file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using CSCore;
//using CSCore.Codecs.WAV;
//using CSCore.SoundIn;
using System.Threading;
using System.IO;
using NAudio.Wave;
using System.Diagnostics;
namespace AudRec
{
class WavMixer
{
// public static CSCore.SoundIn.WasapiCapture spkAud;
//public static NAudio.Wave.WaveInEvent micAud;
//public static WaveFileWriter wfw;
private string _micLoc;
private string _spkLoc;
private string _finLoc;
private string _lameLoc;
private string _destLoc;
//args[0]: spk file location
//args[1]: mic file location
//args[2]: final file location
static void Main(string[] args)
{
Program pp = new Program();
if (args.Length == 5 && args[0] == "merge2wav")
{
pp._spkLoc = args[1];
pp._micLoc = args[2];
pp._finLoc = args[3];
pp._lameLoc = args[4];
pp.MergeWav(pp._spkLoc, pp._micLoc, pp._finLoc, pp._lameLoc);
}
else if (args.Length == 4 && args[0] == "convert2wav")
{
//Console.WriteLine("Number of parameters: " + args.Length);
pp._finLoc = args[1];
pp._destLoc = args[2];
pp._lameLoc = args[3];
//string wavOutput = Path.Combine(Path.GetDirectoryName(pp._finLoc), Path.GetFileNameWithoutExtension(pp._finLoc)) + ".wav";
pp.LameMp3ToWav(pp._finLoc, pp._destLoc, pp._lameLoc);
}
else if (args.Length == 4 && args[0] == "convert2mp3")
{
pp._finLoc = args[1];
pp._destLoc = args[2];
pp._lameLoc = args[3];
pp.LameWavToMp3(pp._finLoc, pp._destLoc, pp._lameLoc);
}
else
{
Console.WriteLine("Invalid parameters: " + args.Length);
foreach (string s in args)
Console.WriteLine(s);
return;
pp._spkLoc = "spk.wav";
pp._micLoc = "mic.wav";
pp._finLoc = "fin.wav";
}
//Console.WriteLine("Starting the mic and speaker recording");
//StartRecording();
// while (true) ;
}
/*
public static void StartRecording()
{
//AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
micAud = new NAudio.Wave.WaveInEvent();
micAud.WaveFormat = new NAudio.Wave.WaveFormat(44100, 2);
micAud.DataAvailable += MicAud_DataAvailable;
micAud.RecordingStopped += MicAud_RecordingStopped;
// micAud.DataAvailable += (s, capData) => wfw.Write(capData.Buffer, 0, capData.BytesRecorded);
wfw = new WaveFileWriter(_micLoc, micAud.WaveFormat);
micAud.StartRecording();
using (spkAud = new CSCore.SoundIn.WasapiLoopbackCapture())
{
spkAud.Initialize();
using (var w = new WaveWriter(_spkLoc, spkAud.WaveFormat))
{
spkAud.DataAvailable += (s, capData) => w.Write(capData.Data, capData.Offset, capData.ByteCount);
spkAud.Start();
Debug.WriteLine("Sleeping thread");
Thread.Sleep(3000);
//spkAud.Dispose();
spkAud.Stop();
micAud.StopRecording();
}
}
}
/*
static void OnProcessExit(object sender, EventArgs e)
{
Debug.WriteLine("I'm out of here");
Debug.WriteLine("Stopping the mic recording");
MergeWav(_spkLoc, _micLoc, _finLoc);
}
//
private static void MicAud_RecordingStopped(object sender, NAudio.Wave.StoppedEventArgs e)
{
if (micAud != null)
{
micAud.Dispose();
micAud = null;
}
if (wfw != null)
{
wfw.Dispose();
wfw = null;
}
}
private static void MicAud_DataAvailable(object sender, WaveInEventArgs e)
{
wfw.Write(e.Buffer, 0, e.BytesRecorded);
wfw.Flush();
}
*/
public void MergeWav(string inp1, string inp2, string output, string lamLoc)
{
if (!File.Exists(inp1))
return;
Console.WriteLine(inp1);
Console.WriteLine(inp2);
var paths = new[] {
inp1,
inp2
};
// open all the input files
var readers = paths.Select(f => new NAudio.Wave.WaveFileReader(f)).ToArray();
// choose the sample rate we will mix at
var maxSampleRate = readers.Max(r => r.WaveFormat.SampleRate);
// create the mixer inputs, resampling if necessary
var mixerInputs = readers.Select(r => r.WaveFormat.SampleRate == maxSampleRate ?
r.ToSampleProvider() :
new MediaFoundationResampler(r, NAudio.Wave.WaveFormat.CreateIeeeFloatWaveFormat(maxSampleRate, r.WaveFormat.Channels)).ToSampleProvider());
// create the mixer
var mixer = new NAudio.Wave.SampleProviders.MixingSampleProvider(mixerInputs);
// write the mixed audio to a 16 bit WAV file
WaveFileWriter.CreateWaveFile16(output, mixer);
// clean up the readers
foreach (var reader in readers)
{
reader.Dispose();
}
string mp3output = Path.Combine(Path.GetDirectoryName(output), Path.GetFileNameWithoutExtension(output)) + ".mp3";
//Console.WriteLine("output loc: " + mp3output);
LameWavToMp3(output, mp3output, lamLoc);
//????????????????????????????Uncomment when taking the final build
//File.Delete(inp1);
//File.Delete(inp2);
//Console.WriteLine("Done converting!");
//Console.ReadLine();
}
public void LameWavToMp3(string wavFile, string outmp3File, string lameLoc)
{
try
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = lameLoc;
psi.Arguments = "-V2 " + wavFile + " " + outmp3File;
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(psi);
p.WaitForExit();
// Console.WriteLine("Finished converting to mp3...");
File.Delete(wavFile);
}
catch (Exception e)
{
Console.WriteLine("Unable to execute lame\n:"+lameLoc);
Console.WriteLine(wavFile);
Console.WriteLine(outmp3File);
Console.WriteLine(e.ToString());
}
}
public void LameMp3ToWav(string mp3File, string outWavFile, string lameLoc)
{
Console.WriteLine(mp3File);
Console.WriteLine(outWavFile);
try
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = lameLoc;
psi.Arguments = "-V2 " + mp3File + " " + outWavFile;
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(psi);
p.WaitForExit();
//Console.WriteLine("Finished converting to mp3...");
}
catch (Exception e)
{
Console.WriteLine("Unable to execute lame");
Console.WriteLine(mp3File);
Console.WriteLine(outWavFile);
Console.WriteLine(e.ToString());
}
}
}
}