Tôi đang sử dụng NAudio, và tôi muốn để phát hiện sự im lặng trong tập tin âm thanh để tôi có thể một trong hai báo cáo hoặc cắt ngắn.
Sau nhiều nghiên cứu, tôi đã đưa ra triển khai cơ bản này. Vì vậy, tôi đã viết một phương pháp mở rộng cho lớp AudioFileReader
trả về khoảng thời gian im lặng ở đầu/cuối của tệp hoặc bắt đầu từ một vị trí cụ thể.
đây:
static class AudioFileReaderExt
{
public enum SilenceLocation { Start, End }
private static bool IsSilence(float amplitude, sbyte threshold)
{
double dB = 20 * Math.Log10(Math.Abs(amplitude));
return dB < threshold;
}
public static TimeSpan GetSilenceDuration(this AudioFileReader reader,
SilenceLocation location,
sbyte silenceThreshold = -40)
{
int counter = 0;
bool volumeFound = false;
bool eof = false;
long oldPosition = reader.Position;
var buffer = new float[reader.WaveFormat.SampleRate * 4];
while (!volumeFound && !eof)
{
int samplesRead = reader.Read(buffer, 0, buffer.Length);
if (samplesRead == 0)
eof = true;
for (int n = 0; n < samplesRead; n++)
{
if (IsSilence(buffer[n], silenceThreshold))
{
counter++;
}
else
{
if (location == SilenceLocation.Start)
{
volumeFound = true;
break;
}
else if (location == SilenceLocation.End)
{
counter = 0;
}
}
}
}
// reset position
reader.Position = oldPosition;
double silenceSamples = (double)counter/reader.WaveFormat.Channels;
double silenceDuration = (silenceSamples/reader.WaveFormat.SampleRate) * 1000;
return TimeSpan.FromMilliseconds(silenceDuration);
}
}
này sẽ chấp nhận hầu như bất kỳ định dạng tập tin âm thanh không chỉ WAV.
Cách sử dụng:
using (AudioFileReader reader = new AudioFileReader(filePath))
{
TimeSpan duration = reader.GetSilenceDuration(AudioFileReaderExt.SilenceLocation.Start);
Console.WriteLine(duration.TotalMilliseconds);
}
Tài liệu tham khảo:
Nguồn
2017-09-03 14:23:18
Liên kết đã chết .Đây là 6 năm sau đó – yazanpro
Nó không phải, nó còn sống và đá! Không chắc chắn liệu dự án có hoạt động tốt hay không. – Saleem