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

时间:2013-11-16 15:04:08

标签: .net vb.net audio naudio cscore

我想使用库NReplayGain计算MP3文件的重放次数,然后使用TagLibSharp库(使用非官方开源重播支持修改)来编写{{1} } replaygain标签到文件中。

嗯,这应该是使用NReplayGain lib计算样本集重放次数的伪代码,因为他们的网站指出:https://github.com/karamanolev/NReplayGain

ID3v2

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

在尝试计算示例集的ReplayGain之前,我需要获取我需要传递给上面代码的必要数据,因此我需要获取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() samplerate,{{1}和MP3文件的SampleSet

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

我之所以要求提供完整的代码,是因为我知道自己无法做到这一点,我已经在NAudio图书馆之前尝试了一些其他事情并且对我来说非常困难,看起来这个libray只是为Audio Master程序员和Audio guru编写的,没有任何简单。

2 个答案:

答案 0 :(得分:4)

从未听说过“样本集”。但是到目前为止我可以看到,样本集只包含左右声道的样本。 您可以使用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

答案 1 :(得分:0)

这有效:

  Sub Main()
    Dim originalWavFile As IWaveSource
    originalWavFile = CodecFactory.Instance.GetCodec("1.mp3")
    Dim bufferSize As Integer = 64000
    Dim bytesBuffer = New Byte(bufferSize) {}
    Dim read As Integer = bufferSize
    While read = bufferSize
        Dim nby = originalWavFile.Length - originalWavFile.Position
        If nby > bufferSize Then
            nby = bufferSize
        End If
        read = originalWavFile.Read(bytesBuffer, 0, nby)
        ' do something with the samples
    End While
    originalWavFile.Dispose()
End Sub