使用 CSCore lib 获取 MP3 文件样本数据和信息

Ele*_*ios 0 .net vb.net audio naudio cscore

我想使用库NReplayGain来计算 MP3 文件的重播,然后使用TagLibSharp库(带有非官方开源重播支持修改)将ID3v2重播标签写入文件。

好吧,这应该是使用 NReplayGain 库计算样本集重放增益的伪代码,正如他们的网站所示:https : //github.com/karamanolev/NReplayGain

Dim trackGain As New TrackGain(samplerate, samplesize)

For Each sampleSet As SampleSet In track
    trackGain.AnalyzeSamples(sampleSet.leftSamples, sampleSet.rightSamples)
Next

Dim gain As Double = trackGain.GetGain()
Dim peak As Double = trackGain.GetPeak()
Run Code Online (Sandbox Code Playgroud)

(...但如果我需要说实话,我不确切知道什么是 SampleSet(所有帧都加入了?))

试图计算sampleset的回放增益之前,我需要获得必要的数据,我需要传递给上面的代码,所以我需要得到的samplerateSampleSetleftSamplesrightSamples一个MP3文件。

我需要一个完整的代码示例,说明如何使用NAudiolib 或任何其他类型的 lib来检索这些数据。

我要求完整代码的原因是因为我知道我自己不能做到这一点,我在 NAudio 库之前接触了一些其他东西,对我来说非常困难,似乎libray 是专为音频大师程序员和音频大师编写的,没有任何简单的东西。

Flo*_*ian 5

从未听说过“样本集”。但正如我目前所见,一个样本集只包含左右声道的样本。您可以使用CSCore以一种非常简单的方式访问曲目的所有样本:

Option Strict On

Imports CSCore
Imports CSCore.Codecs

Module Test

    Sub Main()
        Dim source As IWaveSource = CodecFactory.Instance.GetCodec("C:\Temp\test.mp3")
        Dim sampleSource As ISampleSource = source.ToSampleSource()

        Dim sampleBuffer(source.WaveFormat.SampleRate * source.WaveFormat.Channels) As Single
        Dim sampleRate As Integer = source.WaveFormat.SampleRate
        Dim channelCount As Short = source.WaveFormat.Channels
        Dim read As Integer

        Dim leftSamples As New List(Of Single)
        Dim rightSamples As New List(Of Single)

        Do
            'now iterate through the sampleBuffer
            For i = 0 To read Step channelCount
                If channelCount = 1 Then 'mono
                    leftSamples.Add(sampleBuffer(i))
                ElseIf channelCount = 2 Then
                    leftSamples.Add(sampleBuffer(i))
                    rightSamples.Add(sampleBuffer(i + 1))
                Else
                    Throw New NotSupportedException("3 or more channels are not supported.")
                End If
            Next
        Loop While read > 0

        'now you've got all samples in a range of -1 to 1
        'do what ever you need to do with them
    End Sub

End Module
Run Code Online (Sandbox Code Playgroud)