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)
13773정성태10/18/20249075Linux: 90. pid 네임스페이스 구성으로 본 WSL 2 + docker-desktop
13772정성태10/17/20249943Linux: 89. pid 네임스페이스 구성으로 본 WSL 2 배포본의 계층 관계
13771정성태10/17/20249792Linux: 88. WSL 2 리눅스 배포본 내에서의 pid 네임스페이스 구성
13770정성태10/17/202410580Linux: 87. ps + grep 조합에서 grep 명령어를 사용한 프로세스를 출력에서 제거하는 방법
13769정성태10/15/202411808Linux: 86. Golang + bpf2go를 사용한 eBPF 기본 예제 [1]파일 다운로드1
13768정성태10/15/202410357C/C++: 179. C++ - _O_WTEXT, _O_U16TEXT, _O_U8TEXT의 Unicode stream 모드파일 다운로드2
13767정성태10/14/20248567오류 유형: 929. bpftrace 수행 시 "ERROR: Could not resolve symbol: /proc/self/exe:BEGIN_trigger"
13766정성태10/14/20247728C/C++: 178. C++ - 파일에 대한 Text 모드의 "translated" 동작파일 다운로드1
13765정성태10/12/202410387오류 유형: 928. go build 시 "package maps is not in GOROOT" 오류
13764정성태10/11/202411704Linux: 85. Ubuntu - 원하는 golang 버전 설치
13763정성태10/11/20249102Linux: 84. WSL / Ubuntu 20.04 - bpftool 설치
13762정성태10/11/20249483Linux: 83. WSL / Ubuntu 22.04 - bpftool 설치
13761정성태10/11/20248786오류 유형: 927. WSL / Ubuntu - /usr/include/linux/types.h:5:10: fatal error: 'asm/types.h' file not found
13760정성태10/11/202410648Linux: 82. Ubuntu - clang 최신(stable) 버전 설치
13759정성태10/10/202411525C/C++: 177. C++ - 자유 함수(free function) 및 주소 지정 가능한 함수(addressable function) [6]
13758정성태10/8/202410311오류 유형: 926. dotnet tools를 sudo로 실행하는 경우 command not found
13757정성태10/8/202410648닷넷: 2306. Linux - dotnet tool의 설치 디렉터리가 PATH 환경변수에 자동 등록이 되는 이유
13756정성태10/8/202410729오류 유형: 925. ssh로 docker 접근을 할 때 "... malformed HTTP status code ..." 오류 발생
13755정성태10/7/202411277닷넷: 2305. C# 13 - (9) 메서드 바인딩의 우선순위를 지정하는 OverloadResolutionPriority 특성 도입 (Overload resolution priority)파일 다운로드1
13754정성태10/4/202410178닷넷: 2304. C# 13 - (8) 부분 메서드 정의를 속성 및 인덱서에도 확대파일 다운로드1
13753정성태10/4/20249554Linux: 81. Linux - PATH 환경변수의 적용 규칙
13752정성태10/2/202411110닷넷: 2303. C# 13 - (7) ref struct의 interface 상속 및 제네릭 제약으로 사용 가능 [6]파일 다운로드1
13751정성태10/2/20248793C/C++: 176. C/C++ - ARM64로 포팅할 때 유의할 점
13750정성태10/1/20248565C/C++: 175. C++ - WinMain/wWinMain 호출 전의 CRT 초기화 단계
13749정성태9/30/20249557닷넷: 2302. C# - ssh-keygen으로 생성한 Private Key와 Public Key 연동파일 다운로드1
13748정성태9/29/20249563닷넷: 2301. C# - BigInteger 타입이 byte 배열로 직렬화하는 방식
1  2  3  4  5  6  7  8  9  10  [11]  12  13  14  15  ...