-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
194 lines (176 loc) · 6.84 KB
/
Form1.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
using NAudio.Wave;
using System;
using System.IO;
using System.Windows.Forms;
namespace SpeechDatasetRecorder
{
public partial class Form1 : Form
{
private int lineNum;
private WaveIn waveSource = null;
private WaveFileWriter waveFile = null;
private WaveOutEvent playRecord;
private AudioFileReader audioFile;
public Form1()
{
InitializeComponent();
buttonReplay.Enabled = false;
buttonStopRec.Enabled = false;
buttonNext.Enabled = false;
}
private void buttonLoadTranscript_Click(object sender, EventArgs e)
{
dialogFileTranscriptPath.Filter = "Text File|*.txt";
if(dialogFileTranscriptPath.ShowDialog() == DialogResult.OK)
{
textBoxTranscriptPath.Text = dialogFileTranscriptPath.FileName;
using (StreamReader file = new StreamReader(textBoxTranscriptPath.Text))
{
for (int i = 0; i < 1; i++)
{
richTextBoxTranscript.Text = file.ReadLine();
}
}
}
}
public void buttonSavePath_Click(object sender, EventArgs e)
{
if(dialogFolderSavePath.ShowDialog() == DialogResult.OK)
{
textBoxSavePath.Text = dialogFolderSavePath.SelectedPath;
if (!File.Exists(@textBoxSavePath.Text + @"\checkpoint.txt"))
{
using (TextWriter newCheck = new StreamWriter(@textBoxSavePath.Text + @"\checkpoint.txt"))
{
newCheck.WriteLine("0");
newCheck.Close();
}
}
using (StreamReader checkpoint = new StreamReader(@textBoxSavePath.Text + @"\checkpoint.txt"))
{
int.TryParse(checkpoint.ReadLine(), out lineNum);
}
using (StreamReader transcript = new StreamReader(@textBoxTranscriptPath.Text))
{
for (int i = 0; i <= lineNum; i++)
{
richTextBoxTranscript.Text = transcript.ReadLine();
}
}
Directory.CreateDirectory(@textBoxSavePath.Text + @"\clips");
}
}
private void buttonRec_Click(object sender, EventArgs e)
{
buttonRec.Enabled = false;
buttonReplay.Enabled = false;
buttonNext.Enabled = false;
buttonStopRec.Enabled = true;
//Arigathanks gozaimuch Corey (https://stackoverflow.com/a/17983876/15597379)
waveSource = new WaveIn();
waveSource.WaveFormat = new WaveFormat(16000, 1);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
waveFile = new WaveFileWriter(@textBoxSavePath.Text + @"\clips\Test.wav", waveSource.WaveFormat);
waveSource.StartRecording();
}
private void buttonStopRec_Click(object sender, EventArgs e)
{
buttonRec.Enabled = true;
buttonReplay.Enabled = true;
buttonNext.Enabled = true;
buttonStopRec.Enabled = false;
buttonRec.Text = "Retry";
waveSource.StopRecording();
//stop
}
void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveFile != null)
{
waveFile.Write(e.Buffer, 0, e.BytesRecorded);
waveFile.Flush();
}
}
void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
{
if (waveSource != null)
{
waveSource.Dispose();
waveSource = null;
}
if (waveFile != null)
{
waveFile.Dispose();
waveFile = null;
}
buttonRec.Enabled = true;
}
private void buttonNext_Click(object sender, EventArgs e)
{
buttonRec.Enabled = true;
buttonReplay.Enabled = false;
buttonNext.Enabled = false;
buttonStopRec.Enabled = false;
buttonRec.Text = "REC";
if(!File.Exists(@textBoxSavePath.Text + @"\main.csv"))
{
using (TextWriter CSV = new StreamWriter(@textBoxSavePath.Text + @"\main.csv"))
{
CSV.WriteLine("wav_filename,wav_size,transcript");
CSV.Close();
}
}
File.Move(@textBoxSavePath.Text + @"\clips\Test.wav", String.Format(@"{0}\clips\{1}.wav", @textBoxSavePath.Text, lineNum));
using (TextWriter CSV = new StreamWriter(@textBoxSavePath.Text + @"\main.csv", true))
{
long fileSize = new System.IO.FileInfo(String.Format(@"{0}\clips\{1}.wav", @textBoxSavePath.Text, lineNum)).Length;
CSV.WriteLine(String.Format("{0}.wav,{1},{2}", lineNum, fileSize, richTextBoxTranscript.Text));
CSV.Close();
}
lineNum++;
using (TextWriter checkpoint = new StreamWriter(@textBoxSavePath.Text + @"\checkpoint.txt"))
{
checkpoint.WriteLine(lineNum);
checkpoint.Close();
}
using (StreamReader transcript = new StreamReader(@textBoxTranscriptPath.Text))
{
for (int i = 0; i <= lineNum; i++)
{
richTextBoxTranscript.Text = transcript.ReadLine();
}
}
}
private void buttonReplay_Click(object sender, EventArgs e)
{
buttonRec.Enabled = false;
buttonReplay.Enabled = false;
buttonNext.Enabled = false;
buttonStopRec.Enabled = false;
buttonRec.Text = "Retry";
if (playRecord == null)
{
playRecord = new WaveOutEvent();
playRecord.PlaybackStopped += OnPlaybackStopped;
}
if (audioFile == null)
{
audioFile = new AudioFileReader(@textBoxSavePath.Text + @"\clips\Test.wav");
playRecord.Init(audioFile);
}
playRecord.Play();
}
private void OnPlaybackStopped(object sender, StoppedEventArgs args)
{
playRecord.Dispose();
playRecord = null;
audioFile.Dispose();
audioFile = null;
buttonRec.Enabled = true;
buttonReplay.Enabled = true;
buttonNext.Enabled = true;
buttonStopRec.Enabled = true;
}
}
}