Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일

C# / NAudio - (AI 학습을 위해) 무음 구간을 반영한 오디오 파일 분할

Whisper 모델의 경우,

C# - Whisper.NET Library를 이용해 음성을 텍스트로 변환 및 번역하는 예제
; https://www.sysnet.pe.kr/2/0/14013

로컬에서 실행하는 경우에는 문제가 없지만, Azure OpenAI 서비스의 Whisper 모델을 이용하는 경우에는 25MB 파일 크기 제한이 있습니다. 그렇다면 일정 크기로 나눠야 할 텐데요, 하지만 음성 데이터의 특성상 단순히 파일 크기로 나누기보다는 무음 구간을 인지해 나누는 것이 음성 인식의 정확도를 높이는 데 도움이 됩니다.

자, 그래서 ^^ 이번에는 무음 구간을 반영한 오디오 청크 분할 방법에 대해 알아보겠습니다.




이를 위해 우선 적절한 동영상 파일이 있어야 하는데요, 마침 Youtube로부터 다운로드하는 것도 만들었으니,

C# - Youtube 동영상 다운로드 (YoutubeExplode 패키지)
; https://www.sysnet.pe.kr/2/0/14021

먼저 이것을 시간에 따라 청크로 나눠보겠습니다. NAudio 라이브러리의 경우 특정 시간만을 잘라내는 기능을 이미 제공하고 있으므로,

Using OffsetSampleProvider
; https://github.com/naudio/NAudio/blob/master/Docs/OffsetSampleProvider.md

using var reader = new AudioFileReader("test.wav");

var offset = new OffsetSampleProvider(reader);
offset.SkipOver = TimeSpan.FromSeconds(10); // 5초 후,
offset.Take = TimeSpan.FromSeconds(20); // 20초 분량만큼.

// 이 구간만을 잘라내서 out.wav로 저장.
WaveFileWriter.CreateWaveFile16(@"out.wav", offset);

특정 오디오 파일을 (예를 들어) 2분마다 끊어서 파일에 저장하는 것을 다음과 같이 구현할 수 있습니다.

using var reader = new AudioFileReader(inputPath);

var chunkDuration = TimeSpan.FromMinutes(2);
var numOfChunks = (int)Math.Ceiling(reader.TotalTime.TotalSeconds / chunkDuration.TotalSeconds);

for (int i = 0; i < numOfChunks; i++)
{
    reader.Position = 0;
    var offset = new OffsetSampleProvider(reader);
    offset.SkipOver = TimeSpan.FromSeconds(i * chunkDuration.TotalSeconds);
    offset.Take = TimeSpan.FromSeconds(chunkDuration.TotalSeconds);

    WaveFileWriter.CreateWaveFile16($"out_{i}min.wav", offset);
}




하지만 우리가 원하는 것은, 단순한 파일 크기만이 아니라 거기에 무음 구간을 반영하는 것입니다. 이를 위해서는 어쨌든 음성 데이터에 접근해야 하는데요, NAudio의 경우 샘플링된 데이터를 다음과 같은 방식으로 열거할 수 있습니다.

AudioFileReader source = new("...[오디오 파일 경로]...");

source.Position = 0;

float[] buffer = new float[1600];
long totalSamplesRead = 0;
int read;

while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
    for (int i = 0; i < read; i++)
    {
        float value = buffer[i]; // -1.0 ~ +1.0 사이의 값
    }

    totalSamplesRead += read;
}

엄밀히 WAV 데이터는 bitsPerSample의 값 범위를 갖는데, 예를 들어 16비트라면 (-32768도 표현은 되지만) -32767 ~ +32767 사이의 값이 됩니다. 반면 NAudio가 열거한 값은 -1.0 ~ +1.0 사이의 정규화된 값이므로 이상적인 무음 값은 0.0이 연속으로 나와야 합니다.

하지만, 아날로그 성격상 오디오 데이터는 잡음이 섞일 수 있기 때문에, 가령 Abs(0.01) 이하의 진동만 있다면 무음이라는 식으로 판단해야 합니다. 그리고 한 가지 더 고려해야 할 것이, "무음의 연속"에 대한 판단입니다. 0.01 이하의 값은 샘플링된 순간에 따라 나오는 것도 가능하기 때문에 어느 정도 지속 구간을 함께 기준으로 추가해야 합니다.

이를 바탕으로 코딩을 하려고 했는데... 요즘이 어떤 세상입니까? ^^ AI가 코드 생성을 이렇게 해주는군요.

static List<TimeSpan> DetectSilencePoints(
        AudioFileReader source,
        double silenceThresholdDb = -35,
        int minSilenceDurationMs = 300,
        int analysisWindowMs = 50)
{
    // Reset reader to start
    source.Position = 0;

    int sampleRate = source.WaveFormat.SampleRate;
    int channels = source.WaveFormat.Channels;
    int samplesPerWindow = Math.Max(1, sampleRate * analysisWindowMs / 1000) * channels;

    float[] buffer = new float[samplesPerWindow];
    var silenceRuns = new List<(TimeSpan start, TimeSpan end)>();

    bool inSilence = false;
    TimeSpan runStart = TimeSpan.Zero;

    long totalSamplesRead = 0;
    int read;

    while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
    {
        int frames = read / channels;
        if (frames <= 0) continue;

        // Peak of this window
        float peak = 0f;
        for (int i = 0; i < read; i++)
        {
            peak = Math.Max(peak, Math.Abs(buffer[i]));
        }

        // Convert to dBFS; clamp to avoid log(0)
        double db = (peak <= 1e-9) ? -120.0 : 20.0 * Math.Log10(peak);

        TimeSpan windowStart = SamplesToTime(totalSamplesRead, sampleRate, channels);
        TimeSpan windowEnd = SamplesToTime(totalSamplesRead + read, sampleRate, channels);

        bool isSilent = db <= silenceThresholdDb;

        if (isSilent && !inSilence)
        {
            inSilence = true;
            runStart = windowStart;
        }
        else if (!isSilent && inSilence)
        {
            inSilence = false;
            var dur = windowStart - runStart;
            if (dur.TotalMilliseconds >= minSilenceDurationMs)
                silenceRuns.Add((runStart, windowStart));
        }

        totalSamplesRead += read;
    }

    // End boundary
    if (inSilence)
    {
        var end = SamplesToTime(totalSamplesRead, sampleRate, channels);
        var dur = end - runStart;
        if (dur.TotalMilliseconds >= minSilenceDurationMs)
            silenceRuns.Add((runStart, end));
    }

    // Use midpoints of runs as candidate "clean" cut points.
    var points = silenceRuns.Select(run => run.start + TimeSpan.FromTicks((run.end - run.start).Ticks / 2)).ToList();

    Console.WriteLine($"Detected {points.Count} silence candidates (≥ {minSilenceDurationMs} ms @ {silenceThresholdDb} dBFS).");
    return points;
}

그러니까, 총 300ms 구간 동안 -35dBFS 이하의 값이 연속으로 나오면 그것을 무음으로 판단하고 있습니다. 개인적으로 "dBFS"라는 단위를 처음 봤는데요, 코드에서처럼 정규화된 PCM 값에 아래의 공식을 적용해 계산할 수 있다고 합니다.

dBFS = 20 * log10(정규화된 값)

그런 의미에서 봤을 때, -35dBFS는 대략 0.0177 정도의 진폭에 해당하기 때문에 저 계산을 그냥 빼고 진폭 값을 직접 비교해도 무방합니다.

static List<TimeSpan> DetectSilencePoints(
        AudioFileReader source,
        double silenceThreshold = 0.0177f,
        int minSilenceDurationMs = 300,
        int analysisWindowMs = 50)
{
    // ...[생략]...

    while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
    {
        // ...[생략]...

        // Peak of this window
        float peak = 0f;
        for (int i = 0; i < read; i++)
        {
            peak = Math.Max(peak, Math.Abs(buffer[i]));
        }

        // 0.1 ==> 약 -40dBFS
        // 0.0177 ==> 약 -35.04dBFS

        // 주석 처리
        // double db = peak; // (peak <= 1e-9) ? -120.0 : 20.0 * Math.Log10(peak);

        TimeSpan windowStart = SamplesToTime(totalSamplesRead, sampleRate, channels);
        TimeSpan windowEnd = SamplesToTime(totalSamplesRead + read, sampleRate, channels);

        bool isSilent = peak <= silenceThreshold;

        // ...[생략]...
    }

    // ...[생략]...

    return points;
}




자, 이제 마지막으로 무음 구간을 반영해 청크로 나눠 볼 텐데요, 가령 2분 단위로 끊되 앞/뒤로 무음 구간이 있다면 그걸 반영해 끊는 것입니다. 이것도 그냥 AI한테 만들어 달라고 하니 다음과 같이 ^^ 잘 만들어줍니다.

/// <summary>
/// Compute final cut points: start(0), snapped cuts near each target interval, end(total).
/// </summary>
static List<TimeSpan> ComputeCutPoints(
    TimeSpan total,
    TimeSpan targetInterval,
    List<TimeSpan> silenceCandidates,
    TimeSpan searchWindow,
    TimeSpan minGapAfterCut)
{
    var cuts = new List<TimeSpan> { TimeSpan.Zero };

    // Generate target times: 2min, 4min, ... < total
    var targets = new List<TimeSpan>();
    for (var t = targetInterval; t < total; t += targetInterval)
        targets.Add(t);

    TimeSpan lastCut = TimeSpan.Zero;

    foreach (var t in targets)
    {
        var min = t - searchWindow;
        var max = t + searchWindow;

        // Find nearest silence within window that keeps reasonable spacing
        var candidate = silenceCandidates
            .Where(s => s >= min && s <= max && (s - lastCut) >= minGapAfterCut)
            .OrderBy(s => Math.Abs((s - t).Ticks))
            .FirstOrDefault();

        TimeSpan chosen;
        if (candidate == default)
        {
            chosen = (t - lastCut) >= minGapAfterCut ? t : lastCut; // fallback to exact t if spacing ok
        }
        else
        {
            chosen = candidate;
        }

        if (chosen > lastCut && chosen < total)
        {
            cuts.Add(chosen);
            lastCut = chosen;
        }
    }

    if (cuts.Last() != total)
        cuts.Add(total);

    // Deduplicate / sort just in case
    cuts = cuts.Distinct().OrderBy(ts => ts).ToList();

    Console.WriteLine("Cut map:");
    for (int i = 0; i < cuts.Count; i++)
        Console.WriteLine($"  [{i}] {cuts[i]}");

    return cuts;
}

딱히 어려운 메서드는 아니므로 설명은 생략하겠습니다. ^^ 여기까지의 코드를 종합하면,

string inputPath = @"C:\media_sample\test.wav";

using var reader = new AudioFileReader(inputPath);

// 1) Detect silence points.
List<TimeSpan> silencePoints = DetectSilencePoints(reader, silenceThresholdDb: 0.0177, minSilenceDurationMs: 300, analysisWindowMs: 50);

double segmentSec = 120;
double searchWindowSec = 15;

// 2) Build cut list: 0, cuts near 2min multiples (snapped to silence), end
List<TimeSpan> cutPoints = ComputeCutPoints(
    total: reader.TotalTime,
    targetInterval: TimeSpan.FromSeconds(segmentSec),
    silenceCandidates: silencePoints,
    searchWindow: TimeSpan.FromSeconds(searchWindowSec),
    minGapAfterCut: TimeSpan.FromSeconds(5) // avoid super-nearby duplicate cuts
);

우리가 원하는 오디오 구간을 반영한 TimeSpan 목록을 얻게 됩니다.




끝이군요, ^^ TimeSpan 목록을 얻었으니 이제 그에 맞게 오디오 청크를 (처음에 설명했던 OffsetSampleProvider를 이용해) 잘라내는 것으로 마무리할 수 있습니다.

string outputDir = Path.Combine(Environment.CurrentDirectory, "output");
Directory.CreateDirectory(outputDir);
ExportSegments(inputPath, outputDir, cutPoints, reader);

static void ExportSegments(string inputPath, string outputDir, List<TimeSpan> cuts, AudioFileReader reader)
{
    string baseName = Path.GetFileNameWithoutExtension(inputPath);

    for (int i = 0; i + 1 < cuts.Count; i++)
    {
        var start = cuts[i];
        var end = cuts[i + 1];
        var len = end - start;
        if (len <= TimeSpan.Zero) continue;

        reader.Position = 0;
        var offset = new OffsetSampleProvider(reader)
        {
            SkipOver = start,
            Take = len,
        };

        string outPath = Path.Combine(outputDir, $"{baseName}_part_{i + 1:00}_{FormatTime(start)}~{FormatTime(end)}.wav");
        WaveFileWriter.CreateWaveFile16(outPath, offset); // writes 16-bit WAV
        Console.WriteLine($"Wrote: {outPath} ({len})");
    }
}

제가 테스트한 "https://youtu.be/90HFIm2Reqk" 40여 분 정도의 동영상은 다음과 같이 21개의 청크로 나누어졌고,

Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_01_00-00-00~00-01-58.wav (00:01:58.7000000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_02_00-01-58~00-03-56.wav (00:01:57.7000000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_03_00-03-56~00-06-06.wav (00:02:09.7750000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_04_00-06-06~00-07-57.wav (00:01:51.6500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_05_00-07-57~00-10-00.wav (00:02:02.8250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_06_00-10-00~00-12-12.wav (00:02:11.5500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_07_00-12-12~00-14-01.wav (00:01:48.8500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_08_00-14-01~00-15-59.wav (00:01:57.9500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_09_00-15-59~00-18-09.wav (00:02:10.5250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_10_00-18-09~00-19-54.wav (00:01:45.2250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_11_00-19-54~00-22-00.wav (00:02:05.6500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_12_00-22-00~00-24-06.wav (00:02:06.2250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_13_00-24-06~00-25-57.wav (00:01:50.5750000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_14_00-25-57~00-28-00.wav (00:02:03.0250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_15_00-28-00~00-30-07.wav (00:02:07.0500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_16_00-30-07~00-32-05.wav (00:01:57.8500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_17_00-32-05~00-34-02.wav (00:01:57.8500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_18_00-34-02~00-36-08.wav (00:02:05.1750000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_19_00-36-08~00-38-00.wav (00:01:51.8500000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_20_00-38-00~00-39-55.wav (00:01:55.5250000)
Wrote: c:\temp\ConsoleApp2\AnyCPU\Debug\output\test_part_21_00-39-55~00-40-59.wav (00:01:03.9950000)

몇몇 음성 파일의 끝과 그다음 시작 부분을 들어보니 무음 구간을 잘 반영해 끊어진 것을 알 수 있었습니다. ^^

(첨부 파일은 이 글의 소스 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 10/10/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  9  10  11  [12]  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13748정성태9/29/20249563닷넷: 2301. C# - BigInteger 타입이 byte 배열로 직렬화하는 방식
13747정성태9/28/202411048닷넷: 2300. C# - OpenSSH의 공개키 파일에 대한 "BEGIN OPENSSH PUBLIC KEY" / "END OPENSSH PUBLIC KEY" PEM 포맷파일 다운로드1
13746정성태9/28/20249249오류 유형: 924. Python - LocalProtocolError("Illegal header value ...")
13745정성태9/28/20249017Linux: 80. 리눅스 - 실행 중인 프로세스 내부의 환경변수 설정을 구하는 방법 (lldb)
13744정성태9/27/202410501닷넷: 2299. C# - Windows Hello 사용자 인증 다이얼로그 표시하기파일 다운로드1
13743정성태9/26/202410844닷넷: 2298. C# - Console 프로젝트에서의 await 대상으로 Main 스레드 활용하는 방법 [1]
13742정성태9/26/202410839닷넷: 2297. C# - ssh-keygen으로 생성한 ecdsa 유형의 Public Key 파일 해석 [1]파일 다운로드1
13741정성태9/25/202410557디버깅 기술: 202. windbg - ASP.NET MVC Web Application (.NET Framework) 응용 프로그램의 덤프 분석 시 요령
13740정성태9/24/20249726기타: 86. RSA 공개키 등의 modulus 값에 0x00 선행 바이트가 있는 이유(ASN.1 인코딩)
13739정성태9/24/202410148닷넷: 2297. C# - ssh-keygen으로 생성한 Public Key 파일 해석과 fingerprint 값(md5, sha256) 생성 [1]파일 다운로드1
13738정성태9/22/202410062C/C++: 174. C/C++ - 윈도우 운영체제에서의 file descriptor, FILE*파일 다운로드1
13737정성태9/21/202410631개발 환경 구성: 727. Visual C++ - 리눅스 프로젝트를 위한 빌드 서버의 msbuild 구성
13736정성태9/20/202410511오류 유형: 923. Visual Studio Code - Could not establish connection to "...": Port forwarding is disabled.
13735정성태9/20/20249725개발 환경 구성: 726. ARM 플랫폼용 Visual C++ 리눅스 프로젝트 빌드
13734정성태9/19/20249493개발 환경 구성: 725. ssh를 이용한 원격 docker 서비스 사용
13733정성태9/19/20249837VS.NET IDE: 194. Visual Studio - Cross Platform / "Authentication Type: Private Key"로 접속하는 방법
13732정성태9/17/202410369개발 환경 구성: 724. ARM + docker 환경에서 .NET 8 설치
13731정성태9/15/202411532개발 환경 구성: 723. C# / Visual C++ - Control Flow Guard (CFG) 활성화 [1]파일 다운로드2
13730정성태9/10/202412166오류 유형: 922. docker - RULE_APPEND failed (No such file or directory): rule in chain DOCKER
13729정성태9/9/202412902C/C++: 173. Windows / C++ - AllocConsole로 할당한 콘솔과 CRT 함수 연동 [1]파일 다운로드1
13728정성태9/7/202412486C/C++: 172. Windows - C 런타임에서 STARTUPINFO의 cbReserved2, lpReserved2 멤버를 사용하는 이유파일 다운로드1
13727정성태9/6/202413088개발 환경 구성: 722. ARM 플랫폼 빌드를 위한 미니 PC(?) - Khadas VIM4 [1]
13726정성태9/5/202411067C/C++: 171. C/C++ - 윈도우 운영체제에서의 file descriptor와 HANDLE파일 다운로드1
13725정성태9/4/20249819디버깅 기술: 201. WinDbg - sos threads 명령어 실행 시 "Failed to request ThreadStore"
13724정성태9/3/202413395닷넷: 2296. Win32/C# - 자식 프로세스로 HANDLE 상속파일 다운로드1
13723정성태9/2/202412158C/C++: 170. Windows - STARTUPINFO의 cbReserved2, lpReserved2 멤버 사용자 정의파일 다운로드2
1  2  3  4  5  6  7  8  9  10  11  [12]  13  14  15  ...