-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudioCapture.cs
118 lines (116 loc) · 3.63 KB
/
AudioCapture.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
/*
* Application: AudioCapture Library
*
* Description:
* Creates a object with the ability to record audio from the default audio recoding device on a computer.
* the clips are saved and a event will be risin giving your the audio capture in a base64 string encoded format.
* To play the audio decode the string into a byte away and play the stream of bytes or save the bytes as a .wav file
*
* Version: 1.0
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
namespace CaptureLib
{
public class AudioCapture
{
//The delay used between, captures
public int Delay;
//Determine if its time to capture or not
private bool EnableCapture = false;
//The output path
string output;
//The thread to capture on
Thread CaptureThread;
/// <summary>
/// Creates a new AudioCapture Instance
/// </summary>
/// <param name="delay">The delay used between capturing snippets</param>
public AudioCapture(int delay)
{
this.Delay = delay;
output = Path.GetTempFileName();
}
/// <summary>
/// Start capturing audio
/// </summary>
public void Start()
{
if (!EnableCapture)
{
EnableCapture = true;
CaptureThread = new Thread(Capture);
CaptureThread.Start();
}
}
/// <summary>
/// The thread in which the audio is captured on
/// </summary>
private void Capture()
{
while (EnableCapture)
{
try
{
//Close Old Captures
mciSendString("close recsound", "", 0, 0);
//Create New Capture
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);
//Wait For Delay
Thread.Sleep(Delay);
//Save Recording
mciSendString("save recsound " + output, "", 0, 0);
BitCaptured(File.ReadAllBytes(output));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
/// <summary>
/// Stop the audio capture.
/// </summary>
public void Stop()
{
EnableCapture = false;
mciSendString("close recsound", "", 0, 0);
try
{
if (CaptureThread.IsAlive)
{
CaptureThread.Abort();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message)
}
try
{
if (File.Exists(output))
File.Delete(output);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message)
}
}
/// <summary>
/// New Bit Captured from audio
/// </summary>
/// <param name="Capture">The audio (.wav) encoded as a base 64 string </param>
public delegate void BitCapturedHandle(byte[] Capture);
public event BitCapturedHandle BitCaptured;
#region NativeAPI
//API's
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, string lpstrReturnString, int bufferSize, int hwndCallback);
#endregion
}
}